diff --git a/Core/pom.xml b/Core/pom.xml index 5b45a5f1..2c07eeb6 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -97,6 +97,7 @@ 1.14.4 + ${project.groupId} SongodaCore-NMS-API diff --git a/Core/src/main/java/com/songoda/core/gui/AnvilGui.java b/Core/src/main/java/com/songoda/core/gui/AnvilGui.java new file mode 100644 index 00000000..86ddb53f --- /dev/null +++ b/Core/src/main/java/com/songoda/core/gui/AnvilGui.java @@ -0,0 +1,87 @@ +package com.songoda.core.gui; + +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.nms.CoreNMS; +import com.songoda.core.nms.CustomAnvil; +import com.songoda.core.nms.NmsManager; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +/** + * Anvil GUI for text prompts + * + * @since 2019-09-15 + * @author jascotty2 + */ +public class AnvilGui extends Gui { + + final Player player; + CustomAnvil anvil; + + public AnvilGui(Player player) { + this.player = player; + } + + public AnvilGui(Player player, Gui parent) { + super(parent); + this.player = player; + } + + public void open() { + anvil.open(); + } + + public AnvilGui setInput(ItemStack item) { + return (AnvilGui) this.setItem(0, item); + } + + public ItemStack getInput() { + return this.getItem(0); + } + + public AnvilGui setOutput(ItemStack item) { + return (AnvilGui) this.setItem(2, item); + } + + public ItemStack getOutput() { + return this.getItem(2); + } + + public String getInputText() { + return anvil != null ? anvil.getRenameText() : null; + } + + @NotNull + @Override + protected Inventory generateInventory(@NotNull GuiManager manager) { + this.guiManager = manager; + + createInventory(); + ItemStack item; + if ((item = cellItems.get(0)) != null) { + inventory.setItem(0, item); + } else if ((item = cellItems.get(1)) != null) { + inventory.setItem(1, item); + } else if (!acceptsItems) { + inventory.setItem(0, GuiUtils.createButtonItem(CompatibleMaterial.PAPER, " ", " ")); + } + if ((item = cellItems.get(2)) != null) { + inventory.setItem(2, item); + } + + return inventory; + } + + @Override + protected void createInventory() { + CoreNMS nms = NmsManager.getNMS(); + if (nms != null) { + anvil = nms.createAnvil(player, new GuiHolder(guiManager, this)); + anvil.setCustomTitle(title); + anvil.setLevelCost(0); + inventory = anvil.getInventory(); + } + } +} diff --git a/Core/src/main/java/com/songoda/core/gui/GuiManager.java b/Core/src/main/java/com/songoda/core/gui/GuiManager.java index e8e0ffca..9bea4035 100644 --- a/Core/src/main/java/com/songoda/core/gui/GuiManager.java +++ b/Core/src/main/java/com/songoda/core/gui/GuiManager.java @@ -78,20 +78,34 @@ public class GuiManager { } else if (!initialized) { init(); } - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + if (gui instanceof AnvilGui) { + // bukkit throws a fit now if you try to set anvil stuff asyncronously Gui openInv = openInventories.get(player); if (openInv != null) { openInv.open = false; } - Inventory inv = gui.getOrCreateInventory(this); - Bukkit.getScheduler().runTask(plugin, () -> { - player.openInventory(inv); - gui.onOpen(this, player); - synchronized(lock) { - openInventories.put(player, gui); + gui.getOrCreateInventory(this); + ((AnvilGui) gui).open(); + gui.onOpen(this, player); + synchronized (lock) { + openInventories.put(player, gui); + } + } else { + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + Gui openInv = openInventories.get(player); + if (openInv != null) { + openInv.open = false; } + Inventory inv = gui.getOrCreateInventory(this); + Bukkit.getScheduler().runTask(plugin, () -> { + player.openInventory(inv); + gui.onOpen(this, player); + synchronized(lock) { + openInventories.put(player, gui); + } + }); }); - }); + } } public void showPopup(Player player, String message) { @@ -195,7 +209,11 @@ public class GuiManager { if (openInv.getHolder() != null && openInv.getHolder() instanceof GuiHolder && ((GuiHolder) openInv.getHolder()).manager.uuid.equals(manager.uuid)) { Gui gui = ((GuiHolder) openInv.getHolder()).getGUI(); - if(!gui.open) { + if (gui instanceof AnvilGui) { + gui.inventory.clear(); + gui.inventory = null; + } + if (!gui.open) { return; } final Player player = (Player) event.getPlayer(); diff --git a/Core/src/main/java/com/songoda/core/gui/PopupMessage.java b/Core/src/main/java/com/songoda/core/gui/PopupMessage.java index e9d904e9..cc30a8d8 100644 --- a/Core/src/main/java/com/songoda/core/gui/PopupMessage.java +++ b/Core/src/main/java/com/songoda/core/gui/PopupMessage.java @@ -31,9 +31,8 @@ class PopupMessage { private final NamespacedKey key; private final TextComponent title; CompatibleMaterial icon; - int iconAmount = 1; // experimental, untested TriggerType trigger = TriggerType.IMPOSSIBLE; - FrameType frame = FrameType.GOAL; + FrameType frame = FrameType.GOAL; // TASK is the default BackgroundType background = BackgroundType.ADVENTURE; PopupMessage(Plugin source, CompatibleMaterial icon, String title) { @@ -58,9 +57,6 @@ class PopupMessage { if (this.icon.usesData()) { displayIcon.addProperty("data", this.icon.getData()); } - if (this.iconAmount > 1) { - displayIcon.addProperty("amount", this.iconAmount); // not entirely sure if this works - } advDisplay.add("icon", displayIcon); } advDisplay.add("title", gson.fromJson(ComponentSerializer.toString(this.title), JsonElement.class)); diff --git a/Core/src/main/java/com/songoda/core/nms/NmsManager.java b/Core/src/main/java/com/songoda/core/nms/NmsManager.java index dbb3d85c..e0fec81b 100644 --- a/Core/src/main/java/com/songoda/core/nms/NmsManager.java +++ b/Core/src/main/java/com/songoda/core/nms/NmsManager.java @@ -11,13 +11,38 @@ public class NmsManager { private final static CoreNMS nms = _getNMS(); private static CoreNMS _getNMS() { - CoreNMS result = null; - try { - result = (CoreNMS) Class.forName("com.songoda.core.nms." + serverPackageVersion + ".NMS").newInstance(); - } catch (Exception ex) { - Logger.getLogger(NmsManager.class.getName()).log(Level.SEVERE, "Failed to load NMS for this server version", ex); +// try { +// return (CoreNMS) Class.forName("com.songoda.core.nms." + serverPackageVersion + ".NMS").newInstance(); +// } catch (Exception ex) { +// Logger.getLogger(NmsManager.class.getName()).log(Level.SEVERE, "Failed to load NMS for this server version", ex); +// } + // this block was only added to keep minimizeJar happy + switch (serverPackageVersion) { + case "v1_8_R1": + return new com.songoda.core.nms.v1_8_R1.NMS(); + case "v1_8_R2": + return new com.songoda.core.nms.v1_8_R2.NMS(); + case "v1_8_R3": + return new com.songoda.core.nms.v1_8_R3.NMS(); + case "v1_9_R1": + return new com.songoda.core.nms.v1_9_R1.NMS(); + case "v1_9_R2": + return new com.songoda.core.nms.v1_9_R2.NMS(); + case "v1_10_R1": + return new com.songoda.core.nms.v1_10_R1.NMS(); + case "v1_11_R1": + return new com.songoda.core.nms.v1_11_R1.NMS(); + case "v1_12_R1": + return new com.songoda.core.nms.v1_12_R1.NMS(); + case "v1_13_R1": + return new com.songoda.core.nms.v1_13_R1.NMS(); + case "v1_13_R2": + return new com.songoda.core.nms.v1_13_R2.NMS(); + case "v1_14_R1": + return new com.songoda.core.nms.v1_14_R1.NMS(); } - return result; + Logger.getLogger(NmsManager.class.getName()).log(Level.SEVERE, "Failed to load NMS for this server version: version {0} not found", serverPackageVersion); + return null; } public static CoreNMS getNMS() {