From c98878ed4e0ccd309199e72487706fd25ba735a8 Mon Sep 17 00:00:00 2001 From: ASangarin Date: Thu, 29 Aug 2019 16:13:16 +0100 Subject: [PATCH] CreateItem button and ChatEdition changes --- .../mmoitems/api/edition/AnvilGUI.java | 8 +-- .../mmoitems/api/edition/ChatEdition.java | 8 +-- .../mmoitems/api/edition/ChatEditionBase.java | 35 ++++++++++ .../mmoitems/api/edition/NewItemEdition.java | 64 +++++++++++++++++++ .../mmoitems/api/edition/StatEdition.java | 29 ++------- .../net/Indyuce/mmoitems/gui/ItemBrowser.java | 12 +++- 6 files changed, 123 insertions(+), 33 deletions(-) create mode 100644 src/main/java/net/Indyuce/mmoitems/api/edition/ChatEditionBase.java create mode 100644 src/main/java/net/Indyuce/mmoitems/api/edition/NewItemEdition.java diff --git a/src/main/java/net/Indyuce/mmoitems/api/edition/AnvilGUI.java b/src/main/java/net/Indyuce/mmoitems/api/edition/AnvilGUI.java index 187dfbc7..81982041 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/edition/AnvilGUI.java +++ b/src/main/java/net/Indyuce/mmoitems/api/edition/AnvilGUI.java @@ -12,10 +12,10 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.api.edition.StatEdition.StatEditionProcess; +import net.Indyuce.mmoitems.api.edition.ChatEditionBase.ChatEditionProcess; -public class AnvilGUI implements StatEditionProcess, Listener { - private StatEdition edition; +public class AnvilGUI implements ChatEditionProcess, Listener { + private ChatEditionBase edition; private int containerId; private Inventory inventory; @@ -26,7 +26,7 @@ public class AnvilGUI implements StatEditionProcess, Listener { } @Override - public void open(StatEdition edition) { + public void open(ChatEditionBase edition) { this.edition = edition; ItemStack paper = new ItemStack(Material.PAPER); diff --git a/src/main/java/net/Indyuce/mmoitems/api/edition/ChatEdition.java b/src/main/java/net/Indyuce/mmoitems/api/edition/ChatEdition.java index b0321429..aff5db74 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/edition/ChatEdition.java +++ b/src/main/java/net/Indyuce/mmoitems/api/edition/ChatEdition.java @@ -9,13 +9,13 @@ import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.api.edition.StatEdition.StatEditionProcess; +import net.Indyuce.mmoitems.api.edition.ChatEditionBase.ChatEditionProcess; -public class ChatEdition implements StatEditionProcess, Listener { - private StatEdition edition; +public class ChatEdition implements ChatEditionProcess, Listener { + private ChatEditionBase edition; @Override - public void open(StatEdition edition) { + public void open(ChatEditionBase edition) { this.edition = edition; Bukkit.getPluginManager().registerEvents(this, MMOItems.plugin); diff --git a/src/main/java/net/Indyuce/mmoitems/api/edition/ChatEditionBase.java b/src/main/java/net/Indyuce/mmoitems/api/edition/ChatEditionBase.java new file mode 100644 index 00000000..f451194c --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/edition/ChatEditionBase.java @@ -0,0 +1,35 @@ +package net.Indyuce.mmoitems.api.edition; + +import org.bukkit.entity.Player; +import net.Indyuce.mmoitems.gui.PluginInventory; + +public abstract class ChatEditionBase { + + /* + * saves the last inventory opened. it saves the item data, and the last + * opened page. allows for a much easier access to this data + */ + protected PluginInventory inv; + + public ChatEditionBase(PluginInventory inv) { + this.inv = inv; + } + + public PluginInventory getInventory() { + return inv; + } + + public Player getPlayer() { + return inv.getPlayer(); + } + + public abstract void enable(String... messages); + + public abstract void output(String output); + + public interface ChatEditionProcess { + void open(ChatEditionBase edition); + + void close(); + } +} diff --git a/src/main/java/net/Indyuce/mmoitems/api/edition/NewItemEdition.java b/src/main/java/net/Indyuce/mmoitems/api/edition/NewItemEdition.java new file mode 100644 index 00000000..f9bfa343 --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/edition/NewItemEdition.java @@ -0,0 +1,64 @@ +package net.Indyuce.mmoitems.api.edition; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.scheduler.BukkitRunnable; + +import net.Indyuce.mmoitems.MMOItems; +import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.gui.PluginInventory; + +public class NewItemEdition extends ChatEditionBase { + + /* + * saves the data about the edited data so the plugin can edit the + * corresponding stat. some stats have complex chat formats, so the object + * array allow to save more complex edition info + */ + private Type type; + + public NewItemEdition(PluginInventory inv, Type type) { + super(inv); + this.type = type; + } + + public Type getType() { + return type; + } + + @Override + public void enable(String... messages) { + getPlayer().closeInventory(); + + getPlayer().sendMessage(ChatColor.YELLOW + "" + ChatColor.STRIKETHROUGH + "-----------------------------------------------------"); + getPlayer().sendMessage(MMOItems.plugin.getPrefix() + ChatColor.translateAlternateColorCodes('&', "Write in the chat, the id of the new item.")); + getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Type 'cancel' to abort editing."); + + /* + * anvil text input feature. enables players to use an anvil to input + * text if they are having conflicts with their chat management plugins. + */ + if (MMOItems.plugin.getConfig().getBoolean("anvil-text-input") && MMOItems.plugin.getVersion().isBelowOrEqual(1, 13)) { + new AnvilGUI().open(this); + return; + } + + /* + * default chat edition feature + */ + new ChatEdition().open(this); + MMOItems.plugin.getNMS().sendTitle(getPlayer(), ChatColor.GOLD + "" + ChatColor.BOLD + "Item Creation", "See chat.", 10, 40, 10); + } + + @Override + public void output(String output) { + if (output.equals("cancel")) + inv.open(); + else new BukkitRunnable() { + @Override + public void run() { + Bukkit.dispatchCommand(getPlayer(), "mi create " + type.getId() + " " + output.toUpperCase().replace(" ", "_").replace("-", "_")); + } + }.runTask(MMOItems.plugin); + } +} diff --git a/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java b/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java index 0e76a361..53cdf4c0 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java +++ b/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java @@ -1,19 +1,12 @@ package net.Indyuce.mmoitems.api.edition; import org.bukkit.ChatColor; -import org.bukkit.entity.Player; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.gui.edition.EditionInventory; import net.Indyuce.mmoitems.stat.type.ItemStat; -public class StatEdition { - - /* - * saves the last inventory opened. it saves the item data, and the last - * opened page. allows for a much easier access to this data - */ - private EditionInventory inv; +public class StatEdition extends ChatEditionBase { /* * saves the data about the edited data so the plugin can edit the @@ -24,15 +17,11 @@ public class StatEdition { private Object[] info; public StatEdition(EditionInventory inv, ItemStat stat, Object... info) { - this.inv = inv; + super(inv); this.stat = stat; this.info = info; } - public EditionInventory getEditionInventory() { - return inv; - } - public ItemStat getStat() { return stat; } @@ -41,10 +30,7 @@ public class StatEdition { return info; } - public Player getPlayer() { - return inv.getPlayer(); - } - + @Override public void enable(String... messages) { getPlayer().closeInventory(); @@ -69,16 +55,11 @@ public class StatEdition { MMOItems.plugin.getNMS().sendTitle(getPlayer(), ChatColor.GOLD + "" + ChatColor.BOLD + "Item Edition", "See chat.", 10, 40, 10); } + @Override public void output(String output) { if (output.equals("cancel")) inv.open(); else - stat.whenInput(inv, inv.getItemType().getConfigFile(), output, info); - } - - public interface StatEditionProcess { - void open(StatEdition edition); - - void close(); + stat.whenInput((EditionInventory)inv, ((EditionInventory) inv).getItemType().getConfigFile(), output, info); } } diff --git a/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java b/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java index 4a295d81..3e7500ad 100644 --- a/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java +++ b/src/main/java/net/Indyuce/mmoitems/gui/ItemBrowser.java @@ -20,6 +20,7 @@ import org.bukkit.inventory.meta.ItemMeta; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.api.edition.NewItemEdition; import net.Indyuce.mmoitems.api.item.NBTItem; import net.Indyuce.mmoitems.api.util.AltChar; import net.Indyuce.mmoitems.gui.edition.ItemEdition; @@ -68,7 +69,7 @@ public class ItemBrowser extends PluginInventory { meta.setDisplayName(ChatColor.GREEN + type.getName() + ChatColor.DARK_GRAY + " (Click to browse)"); meta.addItemFlags(ItemFlag.values()); List lore = new ArrayList<>(); - lore.add(ChatColor.GRAY + "" + ChatColor.ITALIC + "There " + (items > 1 || items < 1 ? "are" : "is") + " " + (items < 1 ? "" + ChatColor.RED + ChatColor.ITALIC + "no" : "" + ChatColor.GOLD + ChatColor.ITALIC + items) + ChatColor.GRAY + ChatColor.ITALIC + " item" + (items > 1 ? "s" : "") + " in that type."); + lore.add(ChatColor.GRAY + "" + ChatColor.ITALIC + "There " + (items != 1 ? "are" : "is") + " " + (items < 1 ? "" + ChatColor.RED + ChatColor.ITALIC + "no" : "" + ChatColor.GOLD + ChatColor.ITALIC + items) + ChatColor.GRAY + ChatColor.ITALIC + " item" + (items != 1 ? "s" : "") + " in that type."); meta.setLore(lore); item.setItemMeta(meta); @@ -151,6 +152,11 @@ public class ItemBrowser extends PluginInventory { backMeta.setDisplayName(ChatColor.GREEN + AltChar.rightArrow + " Back"); back.setItemMeta(backMeta); + ItemStack create = new ItemStack(Material.GREEN_STAINED_GLASS_PANE); + ItemMeta createMeta = create.getItemMeta(); + createMeta.setDisplayName(ChatColor.GREEN + "Create New"); + create.setItemMeta(createMeta); + ItemStack previous = new ItemStack(Material.ARROW); ItemMeta previousMeta = previous.getItemMeta(); previousMeta.setDisplayName(ChatColor.GREEN + "Previous Page"); @@ -159,6 +165,7 @@ public class ItemBrowser extends PluginInventory { while (n < slots.length) inv.setItem(slots[n++], noItem); inv.setItem(49, back); + inv.setItem(51, create); inv.setItem(18, page > 1 ? previous : null); inv.setItem(26, max >= itemIDs.size() ? null : next); return inv; @@ -187,6 +194,9 @@ public class ItemBrowser extends PluginInventory { if (item.getItemMeta().getDisplayName().equals(ChatColor.GREEN + AltChar.rightArrow + " Back")) new ItemBrowser(player).open(); + if (item.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Create New")) + new NewItemEdition(this, this.type).enable("Write in the chat the text you want."); + if (type == null && !item.getItemMeta().getDisplayName().equals(ChatColor.RED + "- No type -")) { Type type = MMOItems.plugin.getTypes().get(NBTItem.get(item).getString("typeId")); new ItemBrowser(player, type).open();