From 354a5f9336f11d7f89e49710735a06b9536bccec Mon Sep 17 00:00:00 2001 From: Jules Date: Thu, 16 May 2024 18:19:24 -0700 Subject: [PATCH] New option to hide item types from item browser --- .../java/net/Indyuce/mmoitems/api/Type.java | 105 ++++++++++-------- .../net/Indyuce/mmoitems/gui/ItemBrowser.java | 12 +- .../Indyuce/mmoitems/manager/TypeManager.java | 2 +- .../src/main/resources/default/item-types.yml | 3 + 4 files changed, 68 insertions(+), 54 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java index 4b4ff9ce..4a8f9b9e 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java @@ -97,7 +97,7 @@ public class Type implements CooldownObject, PreloadedObject { public Script ent; - private boolean meleeAttacks; + private boolean meleeAttacks, hideInGame; /** * List of stats which can be applied onto an item which has this type. This @@ -112,14 +112,6 @@ public class Type implements CooldownObject, PreloadedObject { onEntityInteract = config.contains("on-entity-interact") ? MythicLib.plugin.getSkills().loadSkillHandler(config.get("on-entity-interact")) : null; }); - /** - * @deprecated 'weapon' boolean is now automatically inferred from the modifierSource - */ - @Deprecated - public Type(@NotNull String id, boolean weapon, @NotNull ModifierSource modSource) { - this(id, modSource); - } - /** * Hard-coded type with given parameters. Can be used by other plugins * to create types using MMOItems API. @@ -147,12 +139,7 @@ public class Type implements CooldownObject, PreloadedObject { loreFormat = config.getString("LoreFormat", (parent != null ? parent.loreFormat : null)); attackCooldownKey = config.getString("attack-cooldown-key", "default"); meleeAttacks = !config.getBoolean("disable-melee-attacks"); - } - - @Deprecated - public void postload(ConfigurationSection config) { - postLoadAction.cacheConfig(config); - postLoadAction.performAction(); + hideInGame = config.getBoolean("hide-in-game"); } @NotNull @@ -161,13 +148,8 @@ public class Type implements CooldownObject, PreloadedObject { return postLoadAction; } - /** - * @deprecated Type is no longer an enum so that external plugins - * can register their own types. Use getId() instead - */ - @Deprecated - public String name() { - return getId(); + public boolean isDisplayed() { + return !hideInGame; } /** @@ -225,22 +207,6 @@ public class Type implements CooldownObject, PreloadedObject { return onEntityInteract; } - /** - * @deprecated Use {@link #getSupertype()} - */ - @Deprecated - public boolean isSubtype() { - return parent != null; - } - - /** - * @deprecated Use {@link #getSupertype()} - */ - @Deprecated - public Type getParent() { - return parent; - } - /** * @return Does it display as four rows in /mmoitems browse? */ @@ -296,16 +262,6 @@ public class Type implements CooldownObject, PreloadedObject { return unidentifiedTemplate; } - /** - * @param stat The stat to check - * @return If the stat can be handled by this type of item - * @deprecated Use ItemStat.isCompatible(Type) instead - */ - @Deprecated - public boolean canHave(ItemStat stat) { - return stat.isCompatible(this); - } - private ItemStack read(String str) { Validate.notNull(str, "Input must not be null"); @@ -373,4 +329,57 @@ public class Type implements CooldownObject, PreloadedObject { public static boolean isValid(@Nullable String id) { return id != null && MMOItems.plugin.getTypes().has(id.toUpperCase().replace("-", "_").replace(" ", "_")); } + + //region Deprecated API + + /** + * @param stat The stat to check + * @return If the stat can be handled by this type of item + * @deprecated Use ItemStat.isCompatible(Type) instead + */ + @Deprecated + public boolean canHave(ItemStat stat) { + return stat.isCompatible(this); + } + + /** + * @deprecated Use {@link #getSupertype()} + */ + @Deprecated + public boolean isSubtype() { + return parent != null; + } + + /** + * @deprecated Use {@link #getSupertype()} + */ + @Deprecated + public Type getParent() { + return parent; + } + + /** + * @deprecated 'weapon' boolean is now automatically inferred from the modifierSource + */ + @Deprecated + public Type(@NotNull String id, boolean weapon, @NotNull ModifierSource modSource) { + this(id, modSource); + } + + @Deprecated + public void postload(ConfigurationSection config) { + postLoadAction.cacheConfig(config); + postLoadAction.performAction(); + } + + /** + * @deprecated Type is no longer an enum so that external plugins + * can register their own types. Use getId() instead + */ + @Deprecated + public String name() { + return getId(); + } + + //endregion } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java index ce35fda4..32b98438 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java @@ -28,9 +28,11 @@ import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.stream.Collectors; public class ItemBrowser extends PluginInventory { private final Map cached = new LinkedHashMap<>(); + private final List itemTypes; private final Type type; private boolean deleteMode; @@ -49,8 +51,9 @@ public class ItemBrowser extends PluginInventory { super(player); this.type = type; - } + this.itemTypes = MMOItems.plugin.getTypes().getAll().stream().filter(Type::isDisplayed).collect(Collectors.toList()); + } @NotNull @Override @@ -61,7 +64,7 @@ public class ItemBrowser extends PluginInventory { * TYPE BROWSER * * Displays all possible item types if no type was previously selected by the player. - * ------------------------------ + * ------------------------------ */ if (type == null) { @@ -74,11 +77,10 @@ public class ItemBrowser extends PluginInventory { Inventory inv = Bukkit.createInventory(this, 54, "Item Explorer"); // Fetch the list of types - List types = new ArrayList<>(MMOItems.plugin.getTypes().getAll()); - for (int j = min; j < Math.min(max, types.size()); j++) { + for (int j = min; j < Math.min(max, itemTypes.size()); j++) { // Current type to display into the GUI - Type currentType = types.get(j); + Type currentType = itemTypes.get(j); // Get number of items int items = MMOItems.plugin.getTemplates().getTemplates(currentType).size(); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TypeManager.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TypeManager.java index f2c704ee..5a036d8d 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TypeManager.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TypeManager.java @@ -50,7 +50,7 @@ public class TypeManager { try { ConfigurationSection section = config.getConfigurationSection(type.getId()); - Validate.notNull(section, "Could not find config section for type '" + type.getId() + "'"); + Validate.notNull(section, "Could not find config section"); type.load(section); if (clearBefore) type.getPostLoadAction().performAction(); } catch (RuntimeException exception) { diff --git a/MMOItems-Dist/src/main/resources/default/item-types.yml b/MMOItems-Dist/src/main/resources/default/item-types.yml index 09d623cf..634e3fbd 100644 --- a/MMOItems-Dist/src/main/resources/default/item-types.yml +++ b/MMOItems-Dist/src/main/resources/default/item-types.yml @@ -19,6 +19,9 @@ SWORD: # Name displayed in the item lore. name: 'Sword' + + # Should this type be displayed inside the item browser? + hide-in-game: false # Template of an unidentified item. unident-item: