From ffa4f86167083e85befed26505b02b7375ac21c8 Mon Sep 17 00:00:00 2001 From: Brianna Date: Fri, 3 May 2019 03:49:50 -0400 Subject: [PATCH] Redid GUI system which fixes flickering. --- .../epicanchors/EpicAnchorsPlugin.java | 10 -- .../songoda/epicanchors/anchor/EAnchor.java | 68 +--------- .../songoda/epicanchors/gui/GUIOverview.java | 117 ++++++++++++++++++ .../epicanchors/handlers/AnchorHandler.java | 3 - .../epicanchors/handlers/MenuHandler.java | 56 --------- .../listeners/InventoryListeners.java | 47 ------- 6 files changed, 119 insertions(+), 182 deletions(-) create mode 100644 EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java delete mode 100644 EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/MenuHandler.java delete mode 100644 EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/listeners/InventoryListeners.java diff --git a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/EpicAnchorsPlugin.java b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/EpicAnchorsPlugin.java index 0000a84..0855b63 100644 --- a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/EpicAnchorsPlugin.java +++ b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/EpicAnchorsPlugin.java @@ -11,9 +11,7 @@ import com.songoda.epicanchors.api.utils.ProtectionPluginHook; import com.songoda.epicanchors.command.CommandManager; import com.songoda.epicanchors.listeners.BlockListeners; import com.songoda.epicanchors.listeners.InteractListeners; -import com.songoda.epicanchors.listeners.InventoryListeners; import com.songoda.epicanchors.handlers.AnchorHandler; -import com.songoda.epicanchors.handlers.MenuHandler; import com.songoda.epicanchors.hooks.*; import com.songoda.epicanchors.utils.ConfigWrapper; import com.songoda.epicanchors.utils.Methods; @@ -25,7 +23,6 @@ import com.songoda.update.SongodaUpdate; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.command.CommandSender; @@ -54,7 +51,6 @@ public class EpicAnchorsPlugin extends JavaPlugin implements EpicAnchors { private SettingsManager settingsManager; private EAnchorManager anchorManager; - private MenuHandler menuHandler; private CommandManager commandManager; @@ -92,7 +88,6 @@ public class EpicAnchorsPlugin extends JavaPlugin implements EpicAnchors { dataFile.createNewFile("Loading Data File", "EpicAnchors Data File"); this.references = new References(); - this.menuHandler = new MenuHandler(this); this.anchorManager = new EAnchorManager(); this.commandManager = new CommandManager(this); @@ -108,7 +103,6 @@ public class EpicAnchorsPlugin extends JavaPlugin implements EpicAnchors { // Event registration pluginManager.registerEvents(new BlockListeners(this), this); pluginManager.registerEvents(new InteractListeners(this), this); - pluginManager.registerEvents(new InventoryListeners(this), this); // Register default hooks if (pluginManager.isPluginEnabled("ASkyBlock")) this.register(HookASkyBlock::new); @@ -265,10 +259,6 @@ public class EpicAnchorsPlugin extends JavaPlugin implements EpicAnchors { return settingsManager; } - public MenuHandler getMenuHandler() { - return menuHandler; - } - public Locale getLocale() { return locale; } diff --git a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/anchor/EAnchor.java b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/anchor/EAnchor.java index 5ab50ba..447dd93 100644 --- a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/anchor/EAnchor.java +++ b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/anchor/EAnchor.java @@ -2,6 +2,7 @@ package com.songoda.epicanchors.anchor; import com.songoda.epicanchors.EpicAnchorsPlugin; import com.songoda.epicanchors.api.anchor.Anchor; +import com.songoda.epicanchors.gui.GUIOverview; import com.songoda.epicanchors.utils.Methods; import net.milkbowl.vault.economy.Economy; import org.bukkit.*; @@ -26,72 +27,7 @@ public class EAnchor implements Anchor { } public void overview(Player player) { - EpicAnchorsPlugin instance = EpicAnchorsPlugin.getInstance(); - - String timeRemaining = Methods.makeReadable((long) (ticksLeft / 20) * 1000) + " remaining."; - - Inventory inventory = Bukkit.createInventory(null, 27, Methods.formatText(instance.getLocale().getMessage("interface.anchor.title"))); - - int nu = 0; - while (nu != 27) { - inventory.setItem(nu, Methods.getGlass()); - nu++; - } - inventory.setItem(0, Methods.getBackgroundGlass(true)); - inventory.setItem(1, Methods.getBackgroundGlass(true)); - inventory.setItem(2, Methods.getBackgroundGlass(false)); - inventory.setItem(6, Methods.getBackgroundGlass(false)); - inventory.setItem(7, Methods.getBackgroundGlass(true)); - inventory.setItem(8, Methods.getBackgroundGlass(true)); - inventory.setItem(9, Methods.getBackgroundGlass(true)); - inventory.setItem(10, Methods.getBackgroundGlass(false)); - inventory.setItem(16, Methods.getBackgroundGlass(false)); - inventory.setItem(17, Methods.getBackgroundGlass(true)); - inventory.setItem(18, Methods.getBackgroundGlass(true)); - inventory.setItem(19, Methods.getBackgroundGlass(true)); - inventory.setItem(20, Methods.getBackgroundGlass(false)); - inventory.setItem(24, Methods.getBackgroundGlass(false)); - inventory.setItem(25, Methods.getBackgroundGlass(true)); - inventory.setItem(26, Methods.getBackgroundGlass(true)); - - ItemStack itemXP = new ItemStack(Material.valueOf(instance.getConfig().getString("Interfaces.XP Icon")), 1); - ItemMeta itemmetaXP = itemXP.getItemMeta(); - itemmetaXP.setDisplayName(instance.getLocale().getMessage("interface.button.addtimewithxp")); - ArrayList loreXP = new ArrayList<>(); - loreXP.add(instance.getLocale().getMessage("interface.button.addtimewithxplore", Integer.toString(instance.getConfig().getInt("Main.XP Cost")))); - itemmetaXP.setLore(loreXP); - itemXP.setItemMeta(itemmetaXP); - - ItemStack itemECO = new ItemStack(Material.valueOf(instance.getConfig().getString("Interfaces.Economy Icon")), 1); - ItemMeta itemmetaECO = itemECO.getItemMeta(); - itemmetaECO.setDisplayName(instance.getLocale().getMessage("interface.button.addtimewitheconomy")); - ArrayList loreECO = new ArrayList<>(); - loreECO.add(instance.getLocale().getMessage("interface.button.addtimewitheconomylore", Methods.formatEconomy(instance.getConfig().getInt("Main.Economy Cost")))); - itemmetaECO.setLore(loreECO); - itemECO.setItemMeta(itemmetaECO); - - ItemStack item = instance.makeAnchorItem(ticksLeft); - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Methods.formatText(instance.getLocale().getMessage("interface.anchor.smalltitle"))); - List lore = new ArrayList<>(); - - lore.add(Methods.formatText("&7" + timeRemaining)); - - meta.setLore(lore); - item.setItemMeta(meta); - inventory.setItem(13, item); - - - if (instance.getConfig().getBoolean("Main.Add Time With Economy")) { - inventory.setItem(11, itemECO); - } - - if (instance.getConfig().getBoolean("Main.Add Time With XP")) { - inventory.setItem(15, itemXP); - } - - player.openInventory(inventory); - EpicAnchorsPlugin.getInstance().getMenuHandler().addPlayer(player, location); + new GUIOverview(EpicAnchorsPlugin.getInstance(), this, player); } public void addTime(String type, Player player) { diff --git a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java new file mode 100644 index 0000000..c9414f8 --- /dev/null +++ b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java @@ -0,0 +1,117 @@ +package com.songoda.epicanchors.gui; + +import com.songoda.epicanchors.EpicAnchorsPlugin; +import com.songoda.epicanchors.anchor.EAnchor; +import com.songoda.epicanchors.utils.Methods; +import com.songoda.epicspawners.utils.gui.AbstractGUI; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; + +public class GUIOverview extends AbstractGUI { + + private final EpicAnchorsPlugin plugin; + private final EAnchor anchor; + + private int task; + + public GUIOverview(EpicAnchorsPlugin plugin, EAnchor anchor, Player player) { + super(player); + this.plugin = plugin; + this.anchor = anchor; + + + init(Methods.formatText(plugin.getLocale().getMessage("interface.anchor.title")), 27); + runTask(); + } + + @Override + protected void constructGUI() { + String timeRemaining = Methods.makeReadable((long) (anchor.getTicksLeft() / 20) * 1000) + " remaining."; + + int nu = 0; + while (nu != 27) { + inventory.setItem(nu, Methods.getGlass()); + nu++; + } + inventory.setItem(0, Methods.getBackgroundGlass(true)); + inventory.setItem(1, Methods.getBackgroundGlass(true)); + inventory.setItem(2, Methods.getBackgroundGlass(false)); + inventory.setItem(6, Methods.getBackgroundGlass(false)); + inventory.setItem(7, Methods.getBackgroundGlass(true)); + inventory.setItem(8, Methods.getBackgroundGlass(true)); + inventory.setItem(9, Methods.getBackgroundGlass(true)); + inventory.setItem(10, Methods.getBackgroundGlass(false)); + inventory.setItem(16, Methods.getBackgroundGlass(false)); + inventory.setItem(17, Methods.getBackgroundGlass(true)); + inventory.setItem(18, Methods.getBackgroundGlass(true)); + inventory.setItem(19, Methods.getBackgroundGlass(true)); + inventory.setItem(20, Methods.getBackgroundGlass(false)); + inventory.setItem(24, Methods.getBackgroundGlass(false)); + inventory.setItem(25, Methods.getBackgroundGlass(true)); + inventory.setItem(26, Methods.getBackgroundGlass(true)); + + ItemStack itemXP = new ItemStack(Material.valueOf(plugin.getConfig().getString("Interfaces.XP Icon")), 1); + ItemMeta itemmetaXP = itemXP.getItemMeta(); + itemmetaXP.setDisplayName(plugin.getLocale().getMessage("interface.button.addtimewithxp")); + ArrayList loreXP = new ArrayList<>(); + loreXP.add(plugin.getLocale().getMessage("interface.button.addtimewithxplore", Integer.toString(plugin.getConfig().getInt("Main.XP Cost")))); + itemmetaXP.setLore(loreXP); + itemXP.setItemMeta(itemmetaXP); + + ItemStack itemECO = new ItemStack(Material.valueOf(plugin.getConfig().getString("Interfaces.Economy Icon")), 1); + ItemMeta itemmetaECO = itemECO.getItemMeta(); + itemmetaECO.setDisplayName(plugin.getLocale().getMessage("interface.button.addtimewitheconomy")); + ArrayList loreECO = new ArrayList<>(); + loreECO.add(plugin.getLocale().getMessage("interface.button.addtimewitheconomylore", Methods.formatEconomy(plugin.getConfig().getInt("Main.Economy Cost")))); + itemmetaECO.setLore(loreECO); + itemECO.setItemMeta(itemmetaECO); + + ItemStack item = plugin.makeAnchorItem(anchor.getTicksLeft()); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(Methods.formatText(plugin.getLocale().getMessage("interface.anchor.smalltitle"))); + List lore = new ArrayList<>(); + + lore.add(Methods.formatText("&7" + timeRemaining)); + + meta.setLore(lore); + item.setItemMeta(meta); + inventory.setItem(13, item); + + + if (plugin.getConfig().getBoolean("Main.Add Time With Economy")) { + inventory.setItem(11, itemECO); + } + + if (plugin.getConfig().getBoolean("Main.Add Time With XP")) { + inventory.setItem(15, itemXP); + } + } + + private void runTask() { + task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::constructGUI, 5L, 5L); + } + + @Override + protected void registerClickables() { + registerClickable(11, ((player, inventory, cursor, slot, type) -> { + if (plugin.getConfig().getBoolean("Main.Add Time With Economy")) + anchor.addTime("ECO", player); + })); + + registerClickable(15, ((player, inventory, cursor, slot, type) -> { + if (plugin.getConfig().getBoolean("Main.Add Time With XP")) + anchor.addTime("XP", player); + })); + } + + @Override + protected void registerOnCloses() { + registerOnClose(((player1, inventory1) -> Bukkit.getScheduler().cancelTask(task))); + } +} diff --git a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/AnchorHandler.java b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/AnchorHandler.java index 5b5a3b9..f503c7e 100644 --- a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/AnchorHandler.java +++ b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/AnchorHandler.java @@ -95,8 +95,6 @@ public class AnchorHandler { if (anchor.getLocation().getBlock().getType() != Material.valueOf(instance.getConfig().getString("Main.Anchor Block Material"))) continue; - instance.getMenuHandler().updateMenu(); - Chunk chunk = location.getChunk(); chunk.load(); @@ -129,7 +127,6 @@ public class AnchorHandler { location.getWorld().playSound(location, instance.isServerVersionAtLeast(ServerVersion.V1_13) ? Sound.ENTITY_GENERIC_EXPLODE : Sound.valueOf("EXLODE"), 10, 10); location.getBlock().setType(Material.AIR); - instance.getMenuHandler().removeAnchor(location); chunk.unload(); } diff --git a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/MenuHandler.java b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/MenuHandler.java deleted file mode 100644 index 48f124a..0000000 --- a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/handlers/MenuHandler.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.songoda.epicanchors.handlers; - -import com.songoda.epicanchors.EpicAnchorsPlugin; -import com.songoda.epicanchors.anchor.EAnchor; -import com.songoda.epicanchors.api.anchor.Anchor; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.entity.Player; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -public class MenuHandler { - - private Map playersInMenu = new HashMap<>(); - - private EpicAnchorsPlugin instance; - - public MenuHandler(EpicAnchorsPlugin instance) { - this.instance = instance; - } - - public void updateMenu() { - if (playersInMenu.size() == 0) return; - for (Map.Entry entry : playersInMenu.entrySet()) { - Player player = Bukkit.getPlayer(entry.getKey()); - ((EAnchor)instance.getAnchorManager().getAnchor(entry.getValue())).overview(player); - } - } - - public Anchor getAnchor(Player player) { - return instance.getAnchorManager().getAnchor(playersInMenu.get(player.getUniqueId())); - } - - public void addPlayer(Player player, Location location) { - playersInMenu.put(player.getUniqueId(), location); - } - - public void removePlayer(Player player) { - playersInMenu.remove(player.getUniqueId()); - } - - public boolean isPlayerInMenu(Player player) { - return playersInMenu.containsKey(player.getUniqueId()); - } - - public void removeAnchor(Location location) { - for (Map.Entry entry : playersInMenu.entrySet()) { - if (entry.getValue() != location) return; - Player player = Bukkit.getPlayer(entry.getKey()); - playersInMenu.remove(player.getUniqueId()); - player.closeInventory(); - } - } -} diff --git a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/listeners/InventoryListeners.java b/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/listeners/InventoryListeners.java deleted file mode 100644 index 71ec515..0000000 --- a/EpicAnchors-Plugin/src/main/java/com/songoda/epicanchors/listeners/InventoryListeners.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.songoda.epicanchors.listeners; - -import com.songoda.epicanchors.EpicAnchorsPlugin; -import com.songoda.epicanchors.anchor.EAnchor; -import com.songoda.epicanchors.api.anchor.Anchor; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; - -public class InventoryListeners implements Listener { - - EpicAnchorsPlugin instance; - - public InventoryListeners(EpicAnchorsPlugin instance) { - this.instance = instance; - } - - @EventHandler() - public void onInventoryClick(InventoryClickEvent event) { - if (event.getRawSlot() >= event.getView().getTopInventory().getSize()) return; - - Player player = (Player) event.getWhoClicked(); - if (!instance.getMenuHandler().isPlayerInMenu(player)) return; - - Anchor anchor = instance.getMenuHandler().getAnchor(player); - - event.setCancelled(true); - - if (event.getSlot() == 15 && instance.getConfig().getBoolean("Main.Add Time With XP")) { - if (!event.getCurrentItem().getItemMeta().getDisplayName().equals("§l")) { - ((EAnchor) anchor).addTime("XP", player); - } - } else if (event.getSlot() == 11 && instance.getConfig().getBoolean("Main.Add Time With Economy")) { - if (!event.getCurrentItem().getItemMeta().getDisplayName().equals("§l")) { - ((EAnchor) anchor).addTime("ECO", player); - } - } - } - - @EventHandler() - public void onInventoryClose(InventoryCloseEvent event) { - instance.getMenuHandler().removePlayer((Player) event.getPlayer()); - } - -}