Skip to content

Commit

Permalink
Merge pull request #373 from BentoBoxWorld/better_icons_for_entities
Browse files Browse the repository at this point in the history
Better icons for entities
  • Loading branch information
tastybento authored Feb 10, 2025
2 parents 0bd1c79 + 23ed293 commit 5f8aeba
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import java.util.Map;

import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

import lv.id.bonne.panelutils.PanelUtils;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.challenges.panel.CommonPanel;
import world.bentobox.challenges.panel.util.MultiEntitySelector;
import world.bentobox.challenges.panel.util.SingleEntitySelector;
import world.bentobox.challenges.utils.Constants;
import world.bentobox.challenges.utils.Utils;

Expand All @@ -32,17 +33,12 @@
public class ManageEntitiesPanel extends AbstractManageEnumPanel<EntityType> {

/**
* Flag to indicate whether entities should be displayed as eggs (true) or mob heads (false).
*/
private boolean asEggs = true;

/**
* Private constructor that initializes the ManageEntitiesPanel with the provided
* entities map.
*
* @param parentGUI The parent panel that spawns this panel.
* @param requiredEntities A map of EntityType objects to their required counts.
*/
* Private constructor that initializes the ManageEntitiesPanel with the provided
* entities map.
*
* @param parentGUI The parent panel that spawns this panel.
* @param requiredEntities A map of EntityType objects to their required counts.
*/
private ManageEntitiesPanel(CommonPanel parentGUI, Map<EntityType, Integer> requiredEntities) {
super(parentGUI, requiredEntities);
}
Expand All @@ -69,7 +65,12 @@ public static void open(CommonPanel parentGUI, Map<EntityType, Integer> required
*/
@Override
protected ItemStack getElementIcon(EntityType entity, int count) {
return asEggs ? PanelUtils.getEntityEgg(entity, count) : PanelUtils.getEntityHead(entity, count);
if (Tag.ENTITY_TYPES_CAN_TURN_IN_BOATS.isTagged(entity) && count > 1) {
return new ItemStack(Material.OAK_PLANKS, count); // Boats cannot be stacked
}
ItemStack icon = SingleEntitySelector.getIcon(entity);
icon.setAmount(count);
return icon;
}

/**
Expand Down Expand Up @@ -113,8 +114,6 @@ protected void addFunctionalButtons(PanelBuilder panelBuilder) {
panelBuilder.item(3, createButton(Button.ADD_ENTITY));
// Position 5: Button for removing selected entities.
panelBuilder.item(5, createButton(Button.REMOVE_ENTITY));
// Position 8: Button to switch between displaying entity eggs and mob heads.
panelBuilder.item(8, createButton(Button.SWITCH_ENTITY));
}

/**
Expand All @@ -138,7 +137,7 @@ private PanelItem createButton(Button button) {
icon = new ItemStack(Material.BUCKET);
clickHandler = (panel, user, clickType, slot) -> {
// Open a multi-selection tool to add new entities.
MultiEntitySelector.open(this.user, this.asEggs, MultiEntitySelector.Mode.ALIVE, this.itemsMap.keySet(),
MultiEntitySelector.open(this.user, MultiEntitySelector.Mode.ALIVE, this.itemsMap.keySet(),
(status, entities) -> {
if (status) {
// For each selected entity, add it to the map with a default count.
Expand Down Expand Up @@ -179,19 +178,6 @@ private PanelItem createButton(Button button) {
description.add("");
description.add(this.user.getTranslation(Constants.TIPS + "click-to-remove"));
}
case SWITCH_ENTITY -> {
// Button to toggle the display mode between entity eggs and mob heads.
icon = new ItemStack(asEggs ? Material.EGG : Material.PLAYER_HEAD);
clickHandler = (panel, user, clickType, slot) -> {
// Toggle the display mode flag and rebuild the panel.
this.asEggs = !this.asEggs;
this.build();
return true;
};
glow = false;
description.add("");
description.add(this.user.getTranslation(Constants.TIPS + "click-to-toggle"));
}
default -> {
icon = new ItemStack(Material.PAPER);
clickHandler = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import world.bentobox.challenges.panel.CommonPanel;
import world.bentobox.challenges.panel.ConversationUtils;
import world.bentobox.challenges.panel.util.MultiEntityTypeTagsSelector;
import world.bentobox.challenges.panel.util.SingleEntitySelector;
import world.bentobox.challenges.panel.util.UnifiedMultiSelector.Mode;
import world.bentobox.challenges.utils.Constants;
import world.bentobox.challenges.utils.Utils;
Expand Down Expand Up @@ -316,18 +317,12 @@ protected PanelItem createElementButton(Tag<EntityType> tag)
if (entityTag.getKey().getKey().contains("boat")) {
return new ItemStack(Material.OAK_PLANKS, quantity); // Boats cannot be stacked
}
EntityType entType = Registry.ENTITY_TYPE.stream().filter(entityTag::isTagged).findAny().orElse(null);
EntityType entType = Registry.ENTITY_TYPE.stream()
.filter(entityTag::isTagged).findAny().orElse(null);
if (entType == null) {
return new ItemStack(Material.PAPER, quantity);
}
String eggName = entType.getKey().getKey().toUpperCase(Locale.ENGLISH) + "_SPAWN_EGG";
Material result;
try {
result = Material.valueOf(eggName);
} catch (Exception e) {
result = Material.PAPER;
}
return new ItemStack(result, quantity);
return SingleEntitySelector.getIcon(entType);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

import lv.id.bonne.panelutils.PanelUtils;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
Expand Down Expand Up @@ -584,7 +582,7 @@ private PanelItem createStatisticRequirementButton(RequirementButton button, Sta
icon = req.entity() == null ? new ItemStack(Material.BARRIER)
: new ItemStack(PanelUtils.getEntityEgg(req.entity()));
clickHandler = (panel, user, clickType, slot) -> {
SingleEntitySelector.open(this.user, true, (status, entity) -> {
SingleEntitySelector.open(this.user, (status, entity) -> {
if (status) {
// Replace the old with the new
statisticsList.removeIf(sr -> sr.equals(req));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

import lv.id.bonne.panelutils.PanelUtils;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.utils.Utils;

Expand All @@ -22,49 +21,44 @@
*/
public class MultiEntitySelector extends UnifiedMultiSelector<EntityType> {

private final boolean asEgg;
private final Set<EntityType> excluded;

/**
* Private constructor.
*
* @param user the user opening the selector
* @param asEgg if true, display entities using their spawn egg icon; otherwise, use the entity head
* @param mode determines whether to show only living entities (ALIVE) or all (ANY)
* @param excluded a set of EntityType values to exclude
* @param consumer the callback to be invoked when the user confirms or cancels
*/
private MultiEntitySelector(User user, boolean asEgg, Mode mode, Set<EntityType> excluded,
private MultiEntitySelector(User user, Mode mode, Set<EntityType> excluded,
java.util.function.BiConsumer<Boolean, Collection<EntityType>> consumer) {
super(user, mode, consumer);
this.asEgg = asEgg;
this.excluded = excluded;
}

/**
* Opens the MultiEntitySelector GUI with the specified parameters.
*
* @param user the user who opens the GUI
* @param asEgg if true, show the entity spawn egg icon; otherwise, show the entity head
* @param mode the filtering mode (ALIVE or ANY)
* @param excluded a set of EntityType values to exclude from the list
* @param consumer a callback to receive the result
*/
public static void open(User user, boolean asEgg, Mode mode, Set<EntityType> excluded,
public static void open(User user, Mode mode, Set<EntityType> excluded,
java.util.function.BiConsumer<Boolean, Collection<EntityType>> consumer) {
new MultiEntitySelector(user, asEgg, mode, excluded, consumer).build();
new MultiEntitySelector(user, mode, excluded, consumer).build();
}

/**
* Opens the MultiEntitySelector GUI with default parameters (mode ANY and no exclusions).
*
* @param user the user who opens the GUI
* @param asEgg if true, show the entity spawn egg icon; otherwise, show the entity head
* @param consumer a callback to receive the result
*/
public static void open(User user, boolean asEgg,
public static void open(User user,
java.util.function.BiConsumer<Boolean, Collection<EntityType>> consumer) {
new MultiEntitySelector(user, asEgg, Mode.ANY, new HashSet<>(), consumer).build();
new MultiEntitySelector(user, Mode.ANY, new HashSet<>(), consumer).build();
}

/**
Expand All @@ -73,7 +67,7 @@ public static void open(User user, boolean asEgg,
@Override
protected List<EntityType> getElements() {
return Arrays.stream(EntityType.values()).filter(entity -> excluded == null || !excluded.contains(entity))
.filter(entity -> mode == Mode.ALIVE ? entity.isAlive() : true)
.filter(entity -> !SingleEntitySelector.NON_ENTITIES.contains(entity))
.sorted(Comparator.comparing(EntityType::name)).collect(Collectors.toList());
}

Expand All @@ -99,7 +93,7 @@ protected String getElementKeyPrefix() {
*/
@Override
protected ItemStack getIcon(EntityType element) {
return asEgg ? PanelUtils.getEntityEgg(element) : PanelUtils.getEntityHead(element);
return SingleEntitySelector.getIcon(element);
}

/**
Expand Down
Loading

0 comments on commit 5f8aeba

Please sign in to comment.