From 2bb9b64090e4fa36c66973c09fb809363f94a838 Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 10 Mar 2021 10:23:48 -0600 Subject: [PATCH] Added the ability to use whole items as display items. --- .../songoda/ultimatekits/UltimateKits.java | 41 +++++------- .../conversion/hooks/DefaultHook.java | 3 +- .../ultimatekits/gui/ConfirmBuyGui.java | 2 +- .../ultimatekits/gui/KitDecorOptionsGui.java | 2 +- .../ultimatekits/gui/KitEditorGui.java | 5 +- .../gui/KitGeneralOptionsGui.java | 3 + .../ultimatekits/gui/KitGuiOptionsGui.java | 5 +- .../ultimatekits/gui/KitSelectorGui.java | 2 +- .../handlers/DisplayItemHandler.java | 2 +- .../com/songoda/ultimatekits/kit/Kit.java | 36 +++++----- .../com/songoda/ultimatekits/kit/KitItem.java | 3 +- .../ultimatekits/kit/type/KitContentItem.java | 3 +- .../ultimatekits/utils/ItemSerializer.java | 66 +++++++++++-------- 13 files changed, 89 insertions(+), 84 deletions(-) diff --git a/src/main/java/com/songoda/ultimatekits/UltimateKits.java b/src/main/java/com/songoda/ultimatekits/UltimateKits.java index 13fa41e..81542f1 100644 --- a/src/main/java/com/songoda/ultimatekits/UltimateKits.java +++ b/src/main/java/com/songoda/ultimatekits/UltimateKits.java @@ -46,6 +46,7 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.PluginManager; import java.util.ArrayList; @@ -75,8 +76,6 @@ public class UltimateKits extends SongodaPlugin { private CrateManager crateManager; private CategoryManager categoryManager; - private ItemSerializer itemSerializer; - private DatabaseConnector databaseConnector; private DataMigrationManager dataMigrationManager; private DataManager dataManager; @@ -95,12 +94,6 @@ public class UltimateKits extends SongodaPlugin { @Override public void onPluginLoad() { INSTANCE = this; - try { - this.itemSerializer = new ItemSerializer(); - } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) { - console.sendMessage(ChatColor.RED + "Could not load the serialization class! Please report this error."); - e.printStackTrace(); - } } @Override @@ -227,13 +220,22 @@ public class UltimateKits extends SongodaPlugin { ConfigurationSection section = kitConfig.getConfigurationSection("Kits." + kitName); if (section == null) continue; + String itemString = section.getString("displayItem"); + + ItemStack item = null; + + if (itemString != null) { + if (itemString.contains("{")) + item = ItemSerializer.deserializeItemStackFromJson(itemString); + else + item = CompatibleMaterial.getMaterial(itemString).getItem(); + } + kitManager.addKit(new Kit(kitName) .setTitle(section.getString("title")) .setDelay(section.getLong("delay")) .setLink(section.getString("link")) - .setDisplayItem(section.contains("displayItem") - ? CompatibleMaterial.getMaterial(section.getString("displayItem"), CompatibleMaterial.DIAMOND_HELMET) - : null) + .setDisplayItem(item) .setCategory(categoryManager.getCategory(section.getString("category"))) .setHidden(section.getBoolean("hidden")) .setPrice(section.getDouble("price")) @@ -314,7 +316,7 @@ public class UltimateKits extends SongodaPlugin { public void onPluginDisable() { saveKits(false); dataFile.save(); - this.dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations()); + dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations()); kitManager.clearKits(); HologramManager.removeAllHolograms(); } @@ -326,9 +328,9 @@ public class UltimateKits extends SongodaPlugin { @Override public void onConfigReload() { - this.setLocale(Settings.LANGUGE_MODE.getString(), true); + setLocale(Settings.LANGUGE_MODE.getString(), true); - this.dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations()); + dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations()); kitConfig.load(); categoryConfig.load(); keyFile.load(); @@ -488,7 +490,7 @@ public class UltimateKits extends SongodaPlugin { if (kit.getCategory() != null) kitConfig.set("Kits." + kit.getKey() + ".category", kit.getCategory().getKey()); if (kit.getDisplayItem() != null) - kitConfig.set("Kits." + kit.getKey() + ".displayItem", kit.getDisplayItem().toString()); + kitConfig.set("Kits." + kit.getKey() + ".displayItem", ItemSerializer.serializeItemStackToJson(kit.getDisplayItem())); else kitConfig.set("Kits." + kit.getKey() + ".displayItem", null); @@ -587,15 +589,6 @@ public class UltimateKits extends SongodaPlugin { return guiManager; } - /** - * Grab instance of the item serializer - * - * @return instance of ItemSerializer - */ - public ItemSerializer getItemSerializer() { - return this.itemSerializer; - } - public DisplayItemHandler getDisplayItemHandler() { return displayItemHandler; } diff --git a/src/main/java/com/songoda/ultimatekits/conversion/hooks/DefaultHook.java b/src/main/java/com/songoda/ultimatekits/conversion/hooks/DefaultHook.java index 1483dc5..ca3ca34 100644 --- a/src/main/java/com/songoda/ultimatekits/conversion/hooks/DefaultHook.java +++ b/src/main/java/com/songoda/ultimatekits/conversion/hooks/DefaultHook.java @@ -3,6 +3,7 @@ package com.songoda.ultimatekits.conversion.hooks; import com.songoda.core.compatibility.ServerVersion; import com.songoda.ultimatekits.UltimateKits; import com.songoda.ultimatekits.conversion.Hook; +import com.songoda.ultimatekits.utils.ItemSerializer; import org.bukkit.inventory.ItemStack; import java.util.HashSet; @@ -16,7 +17,7 @@ public class DefaultHook implements Hook { for (Kits kit : Kits.values()) { if (!kit.name().equalsIgnoreCase(kitName)) continue; for (String string : kit.items) { - items.add(UltimateKits.getInstance().getItemSerializer().deserializeItemStackFromJson(string)); + items.add(ItemSerializer.deserializeItemStackFromJson(string)); } } diff --git a/src/main/java/com/songoda/ultimatekits/gui/ConfirmBuyGui.java b/src/main/java/com/songoda/ultimatekits/gui/ConfirmBuyGui.java index db7e1c0..3a679ec 100644 --- a/src/main/java/com/songoda/ultimatekits/gui/ConfirmBuyGui.java +++ b/src/main/java/com/songoda/ultimatekits/gui/ConfirmBuyGui.java @@ -29,7 +29,7 @@ public class ConfirmBuyGui extends Gui { Methods.fillGlass(this); // Kit information - setItem(0, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.DIAMOND_HELMET, + setItem(0, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.DIAMOND_HELMET.getItem(), ChatColor.RED + TextUtils.formatText(kit.getKey().toLowerCase(), true), ChatColor.GREEN + Settings.CURRENCY_SYMBOL.getString() + Methods.formatEconomy(cost))); diff --git a/src/main/java/com/songoda/ultimatekits/gui/KitDecorOptionsGui.java b/src/main/java/com/songoda/ultimatekits/gui/KitDecorOptionsGui.java index c97ab26..d0b622e 100644 --- a/src/main/java/com/songoda/ultimatekits/gui/KitDecorOptionsGui.java +++ b/src/main/java/com/songoda/ultimatekits/gui/KitDecorOptionsGui.java @@ -71,7 +71,7 @@ public class KitDecorOptionsGui extends Gui { }); // Item Display Override - setButton(1, 7, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON, + setButton(1, 7, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON.getItem(), plugin.getLocale().getMessage("interface.kitdecor.displayone").getMessage(), plugin.getLocale().getMessage("interface.kitdecor.displayonelore") .processPlaceholder("enabled", kitBlockData.isItemOverride() ? enableLore : disableLore) diff --git a/src/main/java/com/songoda/ultimatekits/gui/KitEditorGui.java b/src/main/java/com/songoda/ultimatekits/gui/KitEditorGui.java index dc77cc1..127f8c2 100644 --- a/src/main/java/com/songoda/ultimatekits/gui/KitEditorGui.java +++ b/src/main/java/com/songoda/ultimatekits/gui/KitEditorGui.java @@ -1,6 +1,5 @@ package com.songoda.ultimatekits.gui; -import com.songoda.core.commands.AbstractCommand; import com.songoda.core.compatibility.CompatibleMaterial; import com.songoda.core.compatibility.CompatibleSound; import com.songoda.core.gui.AnvilGui; @@ -54,7 +53,7 @@ public class KitEditorGui extends DoubleGui { setOnClose((event) -> { restoreItemsInstance(); - this.saveKit(player, inventory, false); + saveKit(player, inventory, false); CompatibleSound.ENTITY_VILLAGER_YES.play(player); }); @@ -262,7 +261,7 @@ public class KitEditorGui extends DoubleGui { .processPlaceholder("command", msg).getMessage()) .sendPrefixedMessage(player); - this.inventory.addItem(parseStack); + inventory.addItem(parseStack); Bukkit.getScheduler().runTask(plugin, event.player::closeInventory); }).setOnClose(() -> { event.manager.showGUI(event.player, this); diff --git a/src/main/java/com/songoda/ultimatekits/gui/KitGeneralOptionsGui.java b/src/main/java/com/songoda/ultimatekits/gui/KitGeneralOptionsGui.java index 1bfeb3b..158b76f 100644 --- a/src/main/java/com/songoda/ultimatekits/gui/KitGeneralOptionsGui.java +++ b/src/main/java/com/songoda/ultimatekits/gui/KitGeneralOptionsGui.java @@ -55,6 +55,7 @@ public class KitGeneralOptionsGui extends Gui { } catch (NumberFormatException e) { } plugin.getLocale().getMessage("interface.kitoptions.delaynonumber").processPlaceholder("input", msg).sendPrefixedMessage(player); + plugin.saveKits(false); }); guiManager.showGUI(event.player, gui); }); @@ -79,6 +80,7 @@ public class KitGeneralOptionsGui extends Gui { return; } plugin.getLocale().getMessage("interface.kitoptions.notacategory").processPlaceholder("input", msg).sendPrefixedMessage(player); + plugin.saveKits(false); }); guiManager.showGUI(event.player, gui); } else if (event.clickType == ClickType.RIGHT) { @@ -105,6 +107,7 @@ public class KitGeneralOptionsGui extends Gui { plugin.getLocale().getMessage("interface.kitoptions.destroycancel").sendPrefixedMessage(player); } aevent.player.closeInventory(); + plugin.saveKits(false); }); guiManager.showGUI(event.player, gui); }); diff --git a/src/main/java/com/songoda/ultimatekits/gui/KitGuiOptionsGui.java b/src/main/java/com/songoda/ultimatekits/gui/KitGuiOptionsGui.java index 3dc5dc4..174bcb1 100644 --- a/src/main/java/com/songoda/ultimatekits/gui/KitGuiOptionsGui.java +++ b/src/main/java/com/songoda/ultimatekits/gui/KitGuiOptionsGui.java @@ -82,11 +82,12 @@ public class KitGuiOptionsGui extends Gui { plugin.saveKits(false); }); - setButton(1, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON, + setButton(1, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON.getItem(), plugin.getLocale().getMessage("interface.kitguioptions.item").getMessage(), plugin.getLocale().getMessage("interface.kitguioptions.itemlore") .processPlaceholder("onoff", - kit.getDisplayItem() != null ? plugin.getLocale().getMessage("interface.kitguioptions.itemon").processPlaceholder("item", kit.getDisplayItem().toString()).getMessage() + kit.getDisplayItem() != null ? plugin.getLocale().getMessage("interface.kitguioptions.itemon") + .processPlaceholder("item", kit.getDisplayItem().toString()).getMessage() : plugin.getLocale().getMessage("interface.kitguioptions.itemoff").getMessage() ).getMessage().split("\\|")), ClickType.LEFT, diff --git a/src/main/java/com/songoda/ultimatekits/gui/KitSelectorGui.java b/src/main/java/com/songoda/ultimatekits/gui/KitSelectorGui.java index c62ae67..3c5add0 100644 --- a/src/main/java/com/songoda/ultimatekits/gui/KitSelectorGui.java +++ b/src/main/java/com/songoda/ultimatekits/gui/KitSelectorGui.java @@ -144,7 +144,7 @@ public class KitSelectorGui extends Gui { .processPlaceholder("kit", TextUtils.formatText(kitItem, true)).getMessage(); setButton(row, col, GuiUtils.createButtonItem( - kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.ENCHANTED_BOOK, kitTitle, + kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.ENCHANTED_BOOK.getItem(), kitTitle, getKitLore(kit)), event -> { if (event.clickType == ClickType.MIDDLE && player.hasPermission("ultimatekits.admin")) { diff --git a/src/main/java/com/songoda/ultimatekits/handlers/DisplayItemHandler.java b/src/main/java/com/songoda/ultimatekits/handlers/DisplayItemHandler.java index 62953fb..be74743 100644 --- a/src/main/java/com/songoda/ultimatekits/handlers/DisplayItemHandler.java +++ b/src/main/java/com/songoda/ultimatekits/handlers/DisplayItemHandler.java @@ -74,7 +74,7 @@ public class DisplayItemHandler { ItemStack is = list.get(inum - 1); if (kitBlockData.isItemOverride()) { if (kit.getDisplayItem() != null) - is = kit.getDisplayItem().getItem(); + is = kit.getDisplayItem(); } is.setAmount(1); ItemMeta meta = is.getItemMeta(); diff --git a/src/main/java/com/songoda/ultimatekits/kit/Kit.java b/src/main/java/com/songoda/ultimatekits/kit/Kit.java index b00f572..a9a70f6 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/Kit.java +++ b/src/main/java/com/songoda/ultimatekits/kit/Kit.java @@ -42,7 +42,7 @@ import java.util.Objects; public class Kit implements Cloneable { private String key; - private String name; + private String name; private Category category = null; private static UltimateKits plugin; @@ -50,7 +50,7 @@ public class Kit implements Cloneable { private String link, title = null; private long delay = 0L; private boolean hidden = false; - private CompatibleMaterial displayItem = null; + private ItemStack displayItem = null; private List contents = new ArrayList<>(); private KitAnimation kitAnimation = KitAnimation.NONE; @@ -281,7 +281,7 @@ public class Kit implements Cloneable { } public boolean giveKit(Player player) { - return giveKit(player, getContents().size(), -1); + return giveKit(player, contents.size(), -1); } private boolean giveKit(Player player, Key key) { @@ -294,13 +294,13 @@ public class Kit implements Cloneable { private boolean giveKit(Player player, int itemAmount, int kitAmount) { List innerContents = new ArrayList<>(getContents()); - int kitSize = innerContents.size(); // Amount of items from the kit to give to the player. if (kitAnimation == KitAnimation.ROULETTE) itemAmount = 1; //TODO how about kitAmount > 1? generateRandomItem() will only give 1 random item instead of kitAmount - int itemGiveAmount = itemAmount > 0 ? itemAmount : kitSize; - if (kitAmount > 0) itemGiveAmount = itemGiveAmount * kitAmount; + System.out.println("itemAmount" + ": " + itemAmount); + int itemGiveAmount = kitAmount > 0 ? itemAmount * kitAmount : kitAmount; + System.out.println(itemGiveAmount + " : " + kitAmount + " : " + itemAmount); if (Settings.NO_REDEEM_WHEN_FULL.getBoolean() && !hasRoom(player, itemGiveAmount)) { plugin.getLocale().getMessage("event.claim.full").sendPrefixedMessage(player); @@ -310,20 +310,21 @@ public class Kit implements Cloneable { if (Settings.SOUNDS_ENABLED.getBoolean() && kitAnimation == KitAnimation.NONE) CompatibleSound.ENTITY_PLAYER_LEVELUP.play(player, 0.6F, 15.0F); - return generateRandomItem(innerContents, itemGiveAmount, player); + return generateRandomItem(innerContents, itemGiveAmount, 0, player); } - private boolean generateRandomItem(List innerContents, int itemGiveAmount, Player player) { + private boolean generateRandomItem(List innerContents, int itemGiveAmount, int itemGivenAmount, Player player) { if (innerContents.size() != itemGiveAmount || kitAnimation != KitAnimation.NONE) Collections.shuffle(innerContents); for (KitItem item : new ArrayList<>(innerContents)) { - if (itemGiveAmount == 0) break; + if (itemGiveAmount <= 0 && itemGivenAmount != 0) break; double ch = item.getChance() == 0 ? 100 : item.getChance(); double rand = Math.random() * 100; + System.out.println("We tryin here [" + ch + ":" + rand + "]"); itemGiveAmount--; if (rand < ch || ch == 100) { - + itemGivenAmount++; if (kitAnimation != KitAnimation.NONE) { // TODO: this is a very bad way to solve this problem. // Giving the player kit rewards really should be done outside of the Kit class. @@ -347,9 +348,8 @@ public class Kit implements Cloneable { } } - if (itemGiveAmount > 0 && !innerContents.isEmpty()) { - return generateRandomItem(innerContents, itemGiveAmount, player); - } + if ((itemGiveAmount > 0 || itemGivenAmount == 0) && !innerContents.isEmpty()) + return generateRandomItem(innerContents, itemGiveAmount, itemGivenAmount, player); player.updateInventory(); return true; @@ -447,16 +447,12 @@ public class Kit implements Cloneable { return name; } - public CompatibleMaterial getDisplayItem() { + public ItemStack getDisplayItem() { return displayItem; } - public void setDisplayItem(ItemStack item) { - this.displayItem = item != null ? CompatibleMaterial.getMaterial(item) : null; - } - - public Kit setDisplayItem(CompatibleMaterial material) { - this.displayItem = material; + public Kit setDisplayItem(ItemStack item) { + this.displayItem = item; return this; } diff --git a/src/main/java/com/songoda/ultimatekits/kit/KitItem.java b/src/main/java/com/songoda/ultimatekits/kit/KitItem.java index 099a755..942f9cd 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/KitItem.java +++ b/src/main/java/com/songoda/ultimatekits/kit/KitItem.java @@ -9,6 +9,7 @@ import com.songoda.ultimatekits.kit.type.KitContentCommand; import com.songoda.ultimatekits.kit.type.KitContentEconomy; import com.songoda.ultimatekits.kit.type.KitContentItem; import com.songoda.ultimatekits.settings.Settings; +import com.songoda.ultimatekits.utils.ItemSerializer; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; @@ -52,7 +53,7 @@ public class KitItem implements Cloneable { this.content = new KitContentCommand(line.substring(1)); this.type = KitItemType.COMMAND; } else { - ItemStack itemStack = item == null ? UltimateKits.getInstance().getItemSerializer().deserializeItemStackFromJson(line) : item; + ItemStack itemStack = item == null ? ItemSerializer.deserializeItemStackFromJson(line) : item; this.content = itemStack != null ? new KitContentItem(itemStack) : null; this.type = KitItemType.ITEM; } diff --git a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java index cea5b62..62b547c 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java +++ b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java @@ -1,6 +1,7 @@ package com.songoda.ultimatekits.kit.type; import com.songoda.ultimatekits.UltimateKits; +import com.songoda.ultimatekits.utils.ItemSerializer; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -25,7 +26,7 @@ public class KitContentItem implements KitContent { @Override public String getSerialized() { if (serialized != null) return serialized; - serialized = UltimateKits.getInstance().getItemSerializer().serializeItemStackToJson(itemStack); + serialized = ItemSerializer.serializeItemStackToJson(itemStack); return serialized; } diff --git a/src/main/java/com/songoda/ultimatekits/utils/ItemSerializer.java b/src/main/java/com/songoda/ultimatekits/utils/ItemSerializer.java index eb6aaa3..22aefd2 100644 --- a/src/main/java/com/songoda/ultimatekits/utils/ItemSerializer.java +++ b/src/main/java/com/songoda/ultimatekits/utils/ItemSerializer.java @@ -11,23 +11,23 @@ public class ItemSerializer { // classes needed for reflections - private Class classMojangsonParser = Class.forName(formatNMS("net.minecraft.server.NMS.MojangsonParser")); - private Class classItemStack = Class.forName(formatNMS("net.minecraft.server.NMS.ItemStack")); - private Class classCraftItemStack = Class.forName(formatNMS("org.bukkit.craftbukkit.NMS.inventory.CraftItemStack")); - private Class classNBTTagCompound = Class.forName(formatNMS("net.minecraft.server.NMS.NBTTagCompound")); - private Class classBukkitItemStack = Class.forName("org.bukkit.inventory.ItemStack"); + private static Class classMojangsonParser; + private static Class classItemStack; + private static Class classCraftItemStack; + private static Class classNBTTagCompound; + private static Class classBukkitItemStack; - private Constructor constructorItemStack; + private static Constructor constructorItemStack; // reflected methods - private Method methodParseString; - private Method methodCreateStack; - private Method methodToItemStack; - private Method methodTobItemStack; - private Method methodTocItemStack; - private Method methodSaveTagToStack; - private Method methodToString; + private static Method methodParseString; + private static Method methodCreateStack; + private static Method methodToItemStack; + private static Method methodTobItemStack; + private static Method methodTocItemStack; + private static Method methodSaveTagToStack; + private static Method methodToString; /** * Initializes all reflection methods @@ -36,19 +36,29 @@ public class ItemSerializer { * @throws SecurityException * @throws ClassNotFoundException */ - public ItemSerializer() throws NoSuchMethodException, SecurityException, ClassNotFoundException { - methodParseString = classMojangsonParser.getMethod("parse", String.class); - if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) - methodToItemStack = classItemStack.getMethod("a", classNBTTagCompound); - else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) - constructorItemStack = classItemStack.getConstructor(classNBTTagCompound); - else - methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound); - methodTobItemStack = classCraftItemStack.getMethod("asBukkitCopy", classItemStack); + static { + try { + classMojangsonParser = Class.forName(formatNMS("net.minecraft.server.NMS.MojangsonParser")); + classItemStack = Class.forName(formatNMS("net.minecraft.server.NMS.ItemStack")); + classCraftItemStack = Class.forName(formatNMS("org.bukkit.craftbukkit.NMS.inventory.CraftItemStack")); + classNBTTagCompound = Class.forName(formatNMS("net.minecraft.server.NMS.NBTTagCompound")); + classBukkitItemStack = Class.forName("org.bukkit.inventory.ItemStack"); + methodParseString = classMojangsonParser.getMethod("parse", String.class); - methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack); - methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound); - methodToString = classNBTTagCompound.getMethod("toString"); + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) + methodToItemStack = classItemStack.getMethod("a", classNBTTagCompound); + else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) + constructorItemStack = classItemStack.getConstructor(classNBTTagCompound); + else + methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound); + methodTobItemStack = classCraftItemStack.getMethod("asBukkitCopy", classItemStack); + + methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack); + methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound); + methodToString = classNBTTagCompound.getMethod("toString"); + } catch (NoSuchMethodException | ClassNotFoundException e) { + e.getStackTrace(); + } } /** @@ -57,7 +67,7 @@ public class ItemSerializer { * @param s the string to format, must contain NMS. * @return formatted string */ - private String formatNMS(String s) { + private static String formatNMS(String s) { String packageName = Bukkit.getServer().getClass().getPackage().getName(); String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1); return s.replace("NMS", nmsVersion); @@ -69,7 +79,7 @@ public class ItemSerializer { * @param jsonString the JSON String to parse * @return the deserialized ItemStack */ - public ItemStack deserializeItemStackFromJson(String jsonString) { + public static ItemStack deserializeItemStackFromJson(String jsonString) { try { Object nbtTagCompound = methodParseString.invoke(null, jsonString); Object citemStack; @@ -94,7 +104,7 @@ public class ItemSerializer { * @param itemStack the ItemStack to parse * @return condensed JSON String */ - public String serializeItemStackToJson(ItemStack itemStack) { + public static String serializeItemStackToJson(ItemStack itemStack) { try { Object citemStack = methodTocItemStack.invoke(null, itemStack); Object nbtTagCompoundObject = classNBTTagCompound.newInstance();