From 7d5060d09b2912eee311b3a3c07ff69700107532 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 22 Jun 2023 21:52:02 +0200 Subject: [PATCH] Improved ItemStackBuilder + removed GUIInventory unused methods --- .../java/fr/pandacube/lib/chat/ChatUtil.java | 2 +- .../pandacube/lib/paper/gui/GUIInventory.java | 87 +------------------ .../lib/paper/util/ItemStackBuilder.java | 51 ++++++++--- 3 files changed, 42 insertions(+), 98 deletions(-) diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java index 8f33dbb..ebad4d1 100644 --- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java +++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java @@ -173,7 +173,7 @@ public class ChatUtil { /** * Do like {@link String#join(CharSequence, Iterable)}, but for components, and the last separator is different from * the others. It is useful when enumerating things in a sentence, for instance : - * "a thing, a thingand a thing" + * "a thing, a thing and a thing" * (the coma being the usual separator, and {@code " and "} being the final separator). * @param regularSeparator the separator used everywhere except between the two last components to join. * @param finalSeparator the separator used between the two last components to join. diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java index a453a6f..8d10d6e 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java @@ -1,17 +1,8 @@ package fr.pandacube.lib.paper.gui; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.function.Consumer; - -import com.google.common.collect.ImmutableMap; +import fr.pandacube.lib.chat.Chat; import fr.pandacube.lib.paper.PandaLibPaper; -import net.kyori.adventure.text.ComponentLike; import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; @@ -21,20 +12,15 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import fr.pandacube.lib.chat.Chat; -import fr.pandacube.lib.paper.util.ItemStackBuilder; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; /** * An inventory based GUI. */ public class GUIInventory implements Listener { - /** - * Used as parameter of {@link #buildButton(ItemStack, Integer, ComponentLike, List, Map)} to indicate that a button should - * shine like an enchanted object, without showing enchant information in the hover text. - */ - public static final Map FAKE_ENCHANT = ImmutableMap.of(Enchantment.DURABILITY, 1); - private final Player player; private final Inventory inv; private Consumer onCloseEvent; @@ -204,73 +190,8 @@ public class GUIInventory implements Listener { } - - - - - - - public static ItemStack buildButton(ItemStack base, Integer amount, ComponentLike displayName, - List lores, Map enchantments) { - - ItemStackBuilder iStackBuilder = ItemStackBuilder.of(base); - - if (amount != null) - iStackBuilder.amount(amount); - if (displayName != null) - iStackBuilder.displayName(displayName); - if (lores != null) - iStackBuilder.lore(lores); - if (enchantments != null) { - if (enchantments == FAKE_ENCHANT) - iStackBuilder.fakeEnchant(); - else { - for (Entry e : enchantments.entrySet()) { - iStackBuilder.enchant(e.getKey(), e.getValue()); - } - } - } - - return iStackBuilder.build(); - } - public static ItemStack buildButton(ItemStack base, ComponentLike displayName, List lores, Map enchantments) { - return buildButton(base, null, displayName, lores, enchantments); - } - public static ItemStack buildButton(ItemStack base, ComponentLike displayName, Map enchantments) { - return buildButton(base, null, displayName, null, enchantments); - } - public static ItemStack buildButton(ItemStack base, Integer amount, ComponentLike displayName, List lores) { - return buildButton(base, amount, displayName, lores, null); - } - public static ItemStack buildButton(ItemStack base, ComponentLike displayName, List lores) { - return buildButton(base, null, displayName, lores, null); - } - public static ItemStack buildButton(ItemStack base, ComponentLike displayName) { - return buildButton(base, null, displayName, null, null); - } - - - public static ItemStack buildButton(Material m, ComponentLike displayName, List lores, Map enchantments) { - return buildButton(new ItemStack(m), null, displayName, lores, enchantments); - } - public static ItemStack buildButton(Material m, ComponentLike displayName, Map enchantments) { - return buildButton(new ItemStack(m), null, displayName, null, enchantments); - } - public static ItemStack buildButton(Material m, ComponentLike displayName, List lores) { - return buildButton(new ItemStack(m), null, displayName, lores, null); - } - public static ItemStack buildButton(Material m, ComponentLike displayName) { - return buildButton(new ItemStack(m), null, displayName, null, null); - } - - - - - - - diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java index 0f785c2..e7f9fc3 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java @@ -79,10 +79,6 @@ public class ItemStackBuilder { private ItemMeta getOrInitMeta() { return (cachedMeta != null) ? cachedMeta : (cachedMeta = stack.getItemMeta()); } - - private void updateMeta() { - stack.setItemMeta(cachedMeta); - } public ItemStackBuilder meta(Consumer metaUpdater) { return meta(metaUpdater, ItemMeta.class); @@ -148,30 +144,57 @@ public class ItemStackBuilder { return this; return addLoreAfter(Arrays.asList(lores)); } - + + /** + * Supports unsafe enchants. + */ public ItemStackBuilder enchant(Enchantment enchantment, int level) { return meta(m -> m.addEnchant(enchantment, level, true)); } - + public ItemStackBuilder flags(ItemFlag... flags) { - return meta(m -> m.addItemFlags(flags)); + return flags(true, flags); } - + + public ItemStackBuilder flags(boolean add, ItemFlag... flags) { + return add ? meta(m -> m.addItemFlags(flags)) + : meta(m -> m.removeItemFlags(flags)); + } + public ItemStackBuilder hideEnchants() { - return flags(ItemFlag.HIDE_ENCHANTS); + return hideEnchants(true); } - + + public ItemStackBuilder hideEnchants(boolean hide) { + return flags(hide, ItemFlag.HIDE_ENCHANTS); + } + public ItemStackBuilder hideAttributes() { - return flags(ItemFlag.HIDE_ATTRIBUTES); + return hideAttributes(true); + } + + public ItemStackBuilder hideAttributes(boolean hide) { + return flags(hide, ItemFlag.HIDE_ATTRIBUTES); } public ItemStackBuilder fakeEnchant() { - enchant(Enchantment.DURABILITY, 1); - return hideEnchants(); + return fakeEnchant(true); + } + + public ItemStackBuilder fakeEnchant(boolean apply) { + if (apply) { + enchant(Enchantment.DURABILITY, 1); + return hideEnchants(); + } + return this; } public ItemStackBuilder unbreakable() { - return meta(m -> m.setUnbreakable(true)); + return unbreakable(true); + } + + public ItemStackBuilder unbreakable(boolean unbreakable) { + return meta(m -> m.setUnbreakable(unbreakable)); } public ItemStackBuilder canDestroy(Set destroyable) {