From 7d479773f9158cc6e7eb296f56a39aa06665b797 Mon Sep 17 00:00:00 2001 From: Jules Date: Mon, 21 Oct 2024 20:09:15 +0200 Subject: [PATCH] Added "Can Break" item option for 1.20.6+ --- .../java/net/Indyuce/mmoitems/ItemStats.java | 1 + .../api/item/build/ItemStackBuilder.java | 22 +- .../net/Indyuce/mmoitems/stat/CanBreak.java | 166 +++++++++++++++ .../Indyuce/mmoitems/stat/CompatibleIds.java | 26 ++- .../mmoitems/stat/CompatibleMaterials.java | 40 ++-- .../mmoitems/stat/CompatibleTypes.java | 26 ++- .../Indyuce/mmoitems/stat/RequiredClass.java | 201 +++++++++--------- 7 files changed, 328 insertions(+), 154 deletions(-) create mode 100644 MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CanBreak.java diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/ItemStats.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/ItemStats.java index f1c4186f..327024ad 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/ItemStats.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/ItemStats.java @@ -27,6 +27,7 @@ public class ItemStats { LORE = new Lore(), NBT_TAGS = new NBTTags(), MAX_STACK_SIZE = new MaxStackSize(), + CAN_BREAK = new CanBreak(), LORE_FORMAT = new LoreFormat(), TOOLTIP = new TooltipStat(), diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java index 55e239e8..c5a39e65 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/build/ItemStackBuilder.java @@ -33,8 +33,10 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; import java.util.logging.Level; +// TODO getItemMeta, asNMSCopy = two clones. could be done with NO clone, simply initializing item with proper interfacing public class ItemStackBuilder { @NotNull private final MMOItem mmoitem; @@ -44,6 +46,12 @@ public class ItemStackBuilder { private final LoreBuilder lore; private final List tags = new ArrayList<>(); + /** + * @deprecated Temp fix before MI7 + */ + @Deprecated + private List> futureActions; + private static final AttributeModifier FAKE_MODIFIER = VersionUtils.attrMod(new NamespacedKey(MMOItems.plugin, "decoy"), 0, Operation.ADD_NUMBER); /** @@ -97,6 +105,15 @@ public class ItemStackBuilder { return meta; } + /** + * @deprecated Temp fix before MI7 + */ + @Deprecated + public void addFutureAction(Consumer action) { + if (futureActions == null) futureActions = new ArrayList<>(); + futureActions.add(action); + } + public void addItemTag(List newTags) { tags.addAll(newTags); } @@ -241,7 +258,10 @@ public class ItemStackBuilder { item.setItemMeta(meta); - return NBTItem.get(item).addTag(tags); + NBTItem nbt = NBTItem.get(item).addTag(tags); + if (futureActions != null) futureActions.forEach(a -> a.accept(nbt)); + + return nbt; } /** diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CanBreak.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CanBreak.java new file mode 100644 index 00000000..7563ba1e --- /dev/null +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CanBreak.java @@ -0,0 +1,166 @@ +package net.Indyuce.mmoitems.stat; + +import io.lumine.mythic.lib.UtilityMethods; +import io.lumine.mythic.lib.api.item.ItemTag; +import io.lumine.mythic.lib.api.item.SupportedNBTTagValues; +import io.lumine.mythic.lib.api.util.AltChar; +import io.lumine.mythic.lib.gson.JsonArray; +import io.lumine.mythic.lib.gson.JsonParser; +import io.lumine.mythic.lib.gson.JsonSyntaxException; +import net.Indyuce.mmoitems.MMOItems; +import net.Indyuce.mmoitems.api.edition.StatEdition; +import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; +import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem; +import net.Indyuce.mmoitems.gui.edition.EditionInventory; +import net.Indyuce.mmoitems.stat.annotation.VersionDependant; +import net.Indyuce.mmoitems.stat.data.StringListData; +import net.Indyuce.mmoitems.stat.data.type.StatData; +import net.Indyuce.mmoitems.stat.type.ItemStat; +import org.apache.commons.lang.Validate; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@VersionDependant(version = {1, 20, 6}) +public class CanBreak extends ItemStat { + public CanBreak() { + super("CAN_BREAK", + Material.IRON_PICKAXE, + "Can Break?", + new String[]{"The list of materials that this item can break.", "This restriction only applies to adventure mode."}, + new String[]{"tool"}); + } + + @Override + @SuppressWarnings("unchecked") + public StringListData whenInitialized(Object object) { + Validate.isTrue(object instanceof List, "Must specify a string list"); + return new StringListData((List) object); + } + + @Override + public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { + if (event.getAction() == InventoryAction.PICKUP_ALL) + new StatEdition(inv, this).enable("Write in the chat the name of the material you want to add."); + + if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath())) + return; + + List list = inv.getEditedSection().getStringList(getPath()); + if (list.isEmpty()) return; + + String last = list.removeLast(); + inv.getEditedSection().set(getPath(), list); + inv.registerTemplateEdition(); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'."); + } + + @Override + public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { + List list = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath()) : new ArrayList<>(); + + // Validate material + final Material material = Material.valueOf(UtilityMethods.enumName(message)); + + list.add(material.name()); + inv.getEditedSection().set(getPath(), list); + inv.registerTemplateEdition(); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Material " + material.name() + " successfully added."); + } + + @Override + public void whenDisplayed(List lore, Optional statData) { + if (statData.isPresent()) { + lore.add(ChatColor.GRAY + "Current Value:"); + statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str)); + } else + lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Cannot mine any item (adventure mode)."); + + lore.add(""); + lore.add(ChatColor.YELLOW + AltChar.listDash + " Click to add a new material."); + lore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove the last material."); + } + + @NotNull + @Override + public StringListData getClearStatData() { + return new StringListData(); + } + + @Override + public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) { + List mats = data.getList().stream().map(Material::valueOf).collect(Collectors.toList()); + item.addFutureAction(nbt -> { + nbt.setCanMine(mats); + }); + + item.addItemTag(getAppliedNBT(data)); + } + + @NotNull + @Override + public ArrayList getAppliedNBT(@NotNull StringListData data) { + // Build Json Array + JsonArray array = new JsonArray(); + + // For each string in the ids of the data + for (String sts : data.getList()) { + array.add(sts); + } + + // Make returning array + ArrayList tags = new ArrayList<>(); + + // Add Json Array + tags.add(new ItemTag(getNBTPath(), array.toString())); + + return tags; + } + + @Override + public void whenLoaded(@NotNull ReadMMOItem mmoitem) { + // FInd relevant tags + ArrayList relevantTags = new ArrayList<>(); + if (mmoitem.getNBT().hasTag(getNBTPath())) + relevantTags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING)); + + // Generate data + StatData data = getLoadedNBT(relevantTags); + + if (data != null) + mmoitem.setData(this, data); + } + + @Nullable + @Override + public StringListData getLoadedNBT(@NotNull ArrayList storedTags) { + // Find relevant tag + ItemTag rTag = ItemTag.getTagAtPath(getNBTPath(), storedTags); + + // Found? + if (rTag == null) + // Nope + return null; + + try { + // Parse onto Json Array + JsonArray array = new JsonParser().parse((String) rTag.getValue()).getAsJsonArray(); + + // Make and return list + return new StringListData(array); + } catch (JsonSyntaxException | IllegalStateException exception) { + /* + * OLD ITEM WHICH MUST BE UPDATED. + */ + } + return null; + } +} diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleIds.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleIds.java index 0c8c9aab..492f9948 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleIds.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleIds.java @@ -6,7 +6,6 @@ import io.lumine.mythic.lib.api.util.AltChar; import io.lumine.mythic.lib.gson.JsonArray; import io.lumine.mythic.lib.gson.JsonParser; import io.lumine.mythic.lib.gson.JsonSyntaxException; -import net.Indyuce.mmoitems.ItemStats; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.edition.StatEdition; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; @@ -43,36 +42,35 @@ public class CompatibleIds extends ItemStat { @Override public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { if (event.getAction() == InventoryAction.PICKUP_ALL) - new StatEdition(inv, ItemStats.COMPATIBLE_IDS).enable("Write in the chat the item id you want to add."); + new StatEdition(inv, this).enable("Write in the chat the item id you want to add."); - if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains("compatible-ids")) - return; - List lore = inv.getEditedSection().getStringList("compatible-ids"); - if (lore.size() < 1) + if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath())) return; - String last = lore.get(lore.size() - 1); - lore.remove(last); - inv.getEditedSection().set("compatible-ids", lore); + List lore = inv.getEditedSection().getStringList(getPath()); + if (lore.isEmpty()) return; + + String last = lore.removeLast(); + inv.getEditedSection().set(getPath(), lore); inv.registerTemplateEdition(); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'."); } @Override public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { - List lore = inv.getEditedSection().contains("compatible-ids") ? inv.getEditedSection().getStringList("compatible-ids") + List lore = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath()) : new ArrayList<>(); lore.add(message.toUpperCase()); - inv.getEditedSection().set("compatible-ids", lore); + inv.getEditedSection().set(getPath(), lore); inv.registerTemplateEdition(); - inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Compatible IDs successfully added."); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "ID '" + message.toUpperCase() + "' successfully added."); } @Override public void whenDisplayed(List lore, Optional statData) { if (statData.isPresent()) { lore.add(ChatColor.GRAY + "Current Value:"); - statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + str)); + statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str)); } else lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any item."); @@ -91,7 +89,7 @@ public class CompatibleIds extends ItemStat { public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) { // Copy Array, for lore List compatibleIds = new ArrayList<>(data.getList()); - item.getLore().insert("compatible-ids", compatibleIds); + item.getLore().insert(getPath(), compatibleIds); // Add data item.addItemTag(getAppliedNBT(data)); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleMaterials.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleMaterials.java index 991f89cf..5d6fb894 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleMaterials.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleMaterials.java @@ -1,12 +1,12 @@ package net.Indyuce.mmoitems.stat; +import io.lumine.mythic.lib.UtilityMethods; import io.lumine.mythic.lib.api.item.ItemTag; import io.lumine.mythic.lib.api.item.SupportedNBTTagValues; import io.lumine.mythic.lib.api.util.AltChar; import io.lumine.mythic.lib.gson.JsonArray; import io.lumine.mythic.lib.gson.JsonParser; import io.lumine.mythic.lib.gson.JsonSyntaxException; -import net.Indyuce.mmoitems.ItemStats; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.edition.StatEdition; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; @@ -18,14 +18,12 @@ import net.Indyuce.mmoitems.stat.type.ItemStat; import org.apache.commons.lang.Validate; import org.bukkit.ChatColor; import org.bukkit.Material; -import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -46,43 +44,37 @@ public class CompatibleMaterials extends ItemStat lore = inv.getEditedSection().getStringList("compatible-materials"); - if (lore.size() < 1) + if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath())) return; - String last = lore.get(lore.size() - 1); - lore.remove(last); - inv.getEditedSection().set("compatible-materials", lore); + final List lore = inv.getEditedSection().getStringList(getPath()); + if (lore.isEmpty()) return; + + final String last = lore.removeLast(); + inv.getEditedSection().set(getPath(), lore); inv.registerTemplateEdition(); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'."); } @Override public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { - final Player player = inv.getPlayer(); - // Check if material exists - if (Arrays.stream(Material.values()).noneMatch(versionMaterial -> versionMaterial.name().equalsIgnoreCase(message))) { - player.sendMessage(MMOItems.plugin.getPrefix() + "Invalid material name."); - return; - } - List lore = inv.getEditedSection().contains("compatible-materials") ? inv.getEditedSection().getStringList("compatible-materials") - : new ArrayList<>(); - lore.add(message.toUpperCase()); - inv.getEditedSection().set("compatible-materials", lore); + Material mat = Material.valueOf(UtilityMethods.enumName(message)); + + List lore = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath()) : new ArrayList<>(); + lore.add(mat.name()); + inv.getEditedSection().set(getPath(), lore); inv.registerTemplateEdition(); - player.sendMessage(MMOItems.plugin.getPrefix() + "Compatible Materials successfully added."); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Material " + mat.name() + " successfully added."); } @Override public void whenDisplayed(List lore, Optional statData) { if (statData.isPresent()) { lore.add(ChatColor.GRAY + "Current Value:"); - statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + str)); + statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str)); } else lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any material."); @@ -101,7 +93,7 @@ public class CompatibleMaterials extends ItemStat compatibleMaterials = new ArrayList<>(data.getList()); - item.getLore().insert("compatible-materials", compatibleMaterials); + item.getLore().insert(getPath(), compatibleMaterials); // Add data item.addItemTag(getAppliedNBT(data)); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleTypes.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleTypes.java index ce430251..38d23ad6 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleTypes.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/CompatibleTypes.java @@ -6,7 +6,6 @@ import io.lumine.mythic.lib.api.util.AltChar; import io.lumine.mythic.lib.gson.JsonArray; import io.lumine.mythic.lib.gson.JsonParser; import io.lumine.mythic.lib.gson.JsonSyntaxException; -import net.Indyuce.mmoitems.ItemStats; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.edition.StatEdition; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; @@ -43,36 +42,35 @@ public class CompatibleTypes extends ItemStat { @Override public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { if (event.getAction() == InventoryAction.PICKUP_ALL) - new StatEdition(inv, ItemStats.COMPATIBLE_TYPES).enable("Write in the chat the name of the type you want to add."); + new StatEdition(inv, this).enable("Write in the chat the name of the type you want to add."); - if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains("compatible-types")) - return; - List lore = inv.getEditedSection().getStringList("compatible-types"); - if (lore.size() < 1) + if (event.getAction() != InventoryAction.PICKUP_HALF || !inv.getEditedSection().contains(getPath())) return; - String last = lore.get(lore.size() - 1); - lore.remove(last); - inv.getEditedSection().set("compatible-types", lore); + List lore = inv.getEditedSection().getStringList(getPath()); + if (lore.isEmpty()) return; + + String last = lore.removeLast(); + inv.getEditedSection().set(getPath(), lore); inv.registerTemplateEdition(); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + last + "'."); } @Override public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { - List lore = inv.getEditedSection().contains("compatible-types") ? inv.getEditedSection().getStringList("compatible-types") + List lore = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath()) : new ArrayList<>(); lore.add(message.toUpperCase()); - inv.getEditedSection().set("compatible-types", lore); + inv.getEditedSection().set(getPath(), lore); inv.registerTemplateEdition(); - inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Compatible Types successfully added."); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Type " + message.toUpperCase() + " successfully added."); } @Override public void whenDisplayed(List lore, Optional statData) { if (statData.isPresent()) { lore.add(ChatColor.GRAY + "Current Value:"); - statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + str)); + statData.get().getList().forEach(str -> lore.add(ChatColor.GRAY + "* " + str)); } else lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "Compatible with any item."); @@ -91,7 +89,7 @@ public class CompatibleTypes extends ItemStat { public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringListData data) { // Copy Array, for lore List compatibleTypes = new ArrayList<>(data.getList()); - item.getLore().insert("compatible-types", compatibleTypes); + item.getLore().insert(getPath(), compatibleTypes); // Add data item.addItemTag(getAppliedNBT(data)); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RequiredClass.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RequiredClass.java index 7ebd7e54..12c120ca 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RequiredClass.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RequiredClass.java @@ -31,131 +31,130 @@ import java.util.Optional; import java.util.regex.Pattern; public class RequiredClass extends StringListStat implements ItemRestriction, GemStoneStat { - public RequiredClass() { - super("REQUIRED_CLASS", Material.WRITABLE_BOOK, "Required Class", - new String[] { "The class you need to", "profess to use your item." }, new String[] { "!block", "all" }); - } + public RequiredClass() { + super("REQUIRED_CLASS", Material.WRITABLE_BOOK, "Required Class", + new String[]{"The class you need to", "profess to use your item."}, new String[]{"!block", "all"}); + } - @Override - @SuppressWarnings("unchecked") - public StringListData whenInitialized(Object object) { - Validate.isTrue(object instanceof List, "Must specify a string list"); - return new StringListData((List) object); - } + @Override + @SuppressWarnings("unchecked") + public StringListData whenInitialized(Object object) { + Validate.isTrue(object instanceof List, "Must specify a string list"); + return new StringListData((List) object); + } - @Override - public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { - if (event.getAction() == InventoryAction.PICKUP_ALL) - new StatEdition(inv, this).enable("Write in the chat the class you want your item to support."); + @Override + public void whenClicked(@NotNull EditionInventory inv, @NotNull InventoryClickEvent event) { + if (event.getAction() == InventoryAction.PICKUP_ALL) + new StatEdition(inv, this).enable("Write in the chat the class you want your item to support."); - if (event.getAction() == InventoryAction.PICKUP_HALF) { - if (inv.getEditedSection().getKeys(false).contains("required-class")) { - List supportedClasses = inv.getEditedSection().getStringList("required-class"); - if (supportedClasses.size() < 1) - return; + if (event.getAction() == InventoryAction.PICKUP_HALF && inv.getEditedSection().getKeys(false).contains("required-class")) { + List supportedClasses = inv.getEditedSection().getStringList("required-class"); + if (supportedClasses.size() < 1) + return; - String last = supportedClasses.get(supportedClasses.size() - 1); - supportedClasses.remove(last); - inv.getEditedSection().set(getPath(), supportedClasses.size() == 0 ? null : supportedClasses); - inv.registerTemplateEdition(); - inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed " + last + "."); - } - } - } + String last = supportedClasses.remove(supportedClasses.size() - 1); + inv.getEditedSection().set(getPath(), supportedClasses.size() == 0 ? null : supportedClasses); + inv.registerTemplateEdition(); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed " + last + "."); + } + } - @Override - public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { - List lore = (inv.getEditedSection().getKeys(false).contains("required-class") ? inv.getEditedSection().getStringList("required-class") - : new ArrayList<>()); - lore.add(message); - inv.getEditedSection().set(getPath(), lore); - inv.registerTemplateEdition(); - inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Required Class successfully added."); - } + @Override + public void whenInput(@NotNull EditionInventory inv, @NotNull String message, Object... info) { + List lore = (inv.getEditedSection().getKeys(false).contains("required-class") ? inv.getEditedSection().getStringList("required-class") + : new ArrayList<>()); + lore.add(message); + inv.getEditedSection().set(getPath(), lore); + inv.registerTemplateEdition(); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Required Class successfully added."); + } - @Override - public void whenLoaded(@NotNull ReadMMOItem mmoitem) { + @Override + public void whenLoaded(@NotNull ReadMMOItem mmoitem) { - // Find tag - ArrayList rtags = new ArrayList<>(); - if (mmoitem.getNBT().hasTag(getNBTPath())) - rtags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING)); + // Find tag + ArrayList rtags = new ArrayList<>(); + if (mmoitem.getNBT().hasTag(getNBTPath())) + rtags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING)); - // Build - StatData data = getLoadedNBT(rtags); + // Build + StatData data = getLoadedNBT(rtags); - // Success? - if (data != null) { mmoitem.setData(this, data);} - } + // Success? + if (data != null) { + mmoitem.setData(this, data); + } + } - @Nullable - @Override - public StringListData getLoadedNBT(@NotNull ArrayList storedTags) { + @Nullable + @Override + public StringListData getLoadedNBT(@NotNull ArrayList storedTags) { - // Get it - ItemTag listTag = ItemTag.getTagAtPath(getNBTPath(), storedTags); + // Get it + ItemTag listTag = ItemTag.getTagAtPath(getNBTPath(), storedTags); - // Found? - if (listTag != null) { + // Found? + if (listTag != null) { - // Create String List Data - return new StringListData(((String) listTag.getValue()).split(Pattern.quote(", "))); - } + // Create String List Data + return new StringListData(((String) listTag.getValue()).split(Pattern.quote(", "))); + } - // No correct tags - return null; - } + // No correct tags + return null; + } - @Override - public void whenDisplayed(List lore, Optional statData) { + @Override + public void whenDisplayed(List lore, Optional statData) { - if (statData.isPresent()) { - lore.add(ChatColor.GRAY + "Current Value:"); - StringListData data = (StringListData) statData.get(); - data.getList().forEach(el -> lore.add(ChatColor.GRAY + "* " + ChatColor.GREEN + el)); + if (statData.isPresent()) { + lore.add(ChatColor.GRAY + "Current Value:"); + StringListData data = (StringListData) statData.get(); + data.getList().forEach(el -> lore.add(ChatColor.GRAY + "* " + ChatColor.GREEN + el)); - } else - lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "None"); + } else + lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "None"); - lore.add(""); - lore.add(ChatColor.YELLOW + AltChar.listDash + " Click to add a class."); - lore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove the last class."); - } + lore.add(""); + lore.add(ChatColor.YELLOW + AltChar.listDash + " Click to add a class."); + lore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove the last class."); + } - @NotNull - @Override - public ArrayList getAppliedNBT(@NotNull StringListData data) { + @NotNull + @Override + public ArrayList getAppliedNBT(@NotNull StringListData data) { - // Make the result list - ArrayList ret = new ArrayList<>(); + // Make the result list + ArrayList ret = new ArrayList<>(); - // Add the Json Array - ret.add(new ItemTag(getNBTPath(), String.join(", ", ((StringListData) data).getList()))); + // Add the Json Array + ret.add(new ItemTag(getNBTPath(), String.join(", ", ((StringListData) data).getList()))); - // Ready. - return ret; - } + // Ready. + return ret; + } - @Override - public boolean canUse(RPGPlayer player, NBTItem item, boolean message) { - String requiredClass = item.getString(ItemStats.REQUIRED_CLASS.getNBTPath()); - if (!requiredClass.equals("") && !hasRightClass(player, requiredClass) && !player.getPlayer().hasPermission("mmoitems.bypass.class")) { - if (message) { - Message.WRONG_CLASS.format(ChatColor.RED).send(player.getPlayer()); - player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f); - } - return false; - } - return true; - } + @Override + public boolean canUse(RPGPlayer player, NBTItem item, boolean message) { + String requiredClass = item.getString(ItemStats.REQUIRED_CLASS.getNBTPath()); + if (!requiredClass.equals("") && !hasRightClass(player, requiredClass) && !player.getPlayer().hasPermission("mmoitems.bypass.class")) { + if (message) { + Message.WRONG_CLASS.format(ChatColor.RED).send(player.getPlayer()); + player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f); + } + return false; + } + return true; + } - private boolean hasRightClass(RPGPlayer player, String requiredClass) { - String name = ChatColor.stripColor(player.getClassName()); + private boolean hasRightClass(RPGPlayer player, String requiredClass) { + String name = ChatColor.stripColor(player.getClassName()); - for (String found : requiredClass.split(Pattern.quote(", "))) - if (found.equalsIgnoreCase(name)) - return true; + for (String found : requiredClass.split(Pattern.quote(", "))) + if (found.equalsIgnoreCase(name)) + return true; - return false; - } + return false; + } }