diff --git a/src/main/java/de/fanta/challenges/Challenges.java b/src/main/java/de/fanta/challenges/Challenges.java index f2e2834..c2db8fd 100644 --- a/src/main/java/de/fanta/challenges/Challenges.java +++ b/src/main/java/de/fanta/challenges/Challenges.java @@ -305,20 +305,6 @@ return this.sbManager; } - public ItemStack addGUIItem(Material mat, String name, String... lore) { - ItemStack item = new ItemStack(mat, 1); - ItemMeta meta = item.getItemMeta(); - if (mat != Material.AIR) { - meta.setDisplayName(name); - ArrayList metalore = new ArrayList<>(); - if (lore != null) { - metalore.addAll(Arrays.asList(lore)); - } - meta.setLore(metalore); - } - return item; - } - public Player getCurrentEditor() { return currentEditor; } diff --git a/src/main/java/de/fanta/challenges/commands/CommandRegistration.java b/src/main/java/de/fanta/challenges/commands/CommandRegistration.java index ea99c48..78a60a8 100644 --- a/src/main/java/de/fanta/challenges/commands/CommandRegistration.java +++ b/src/main/java/de/fanta/challenges/commands/CommandRegistration.java @@ -64,11 +64,11 @@ backpackRouter.addCommandMapping(new BackpackCommand(plugin)); CommandRouter coordsRouter = new CommandRouter(plugin.getCommand("coords")); - coordsRouter.addCommandMapping(new CoordsCommand(plugin)); + coordsRouter.addCommandMapping(new CoordsCommand()); coordsRouter.addCommandMapping(new CoordsShareCommand(plugin), "share"); coordsRouter.addCommandMapping(new CoordsGetCommand(plugin), "get"); coordsRouter.addCommandMapping(new CoordsSaveCommand(plugin), "save"); - coordsRouter.addCommandMapping(new CoordsDeleteCommand(plugin), "delete"); + coordsRouter.addCommandMapping(new CoordsDeleteCommand(), "delete"); CommandRouter hpRouter = new CommandRouter(plugin.getCommand("hp")); hpRouter.addCommandMapping(new HPAddCommand(plugin), "add"); diff --git a/src/main/java/de/fanta/challenges/commands/coords/CoordsCommand.java b/src/main/java/de/fanta/challenges/commands/coords/CoordsCommand.java index d8c8583..f8212d8 100644 --- a/src/main/java/de/fanta/challenges/commands/coords/CoordsCommand.java +++ b/src/main/java/de/fanta/challenges/commands/coords/CoordsCommand.java @@ -1,71 +1,26 @@ package de.fanta.challenges.commands.coords; -import de.fanta.challenges.Challenges; +import de.fanta.challenges.guis.coordsgui.CoordsGUI; import de.fanta.challenges.utils.ChatUtil; import de.iani.cubesideutils.bukkit.commands.SubCommand; import de.iani.cubesideutils.commands.ArgsParser; - -import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; public class CoordsCommand extends SubCommand { - private Challenges plugin; - - public static final Inventory CoordsGUI = Bukkit.createInventory(null, 45, "§6Koordinaten"); - - public static final String CGUIN = "§6Koordinaten"; - - public CoordsCommand(Challenges plugin) { - this.plugin = plugin; + public CoordsCommand() { } @Override public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { - if (!(sender instanceof Player)) { + if (!(sender instanceof Player player)) { ChatUtil.sendErrorMessage(sender, "You are not a Player :>"); return true; } - createMainGUI((Player) sender); + CoordsGUI.buildCoordsInventory(player); return true; -} - - public void createMainGUI(Player p) { - int i = 0; - String w; - String x; - String y; - String z; - for (String key : plugin.getConfig().getConfigurationSection("Saved_Locations").getKeys(false)) { - if (i >= 45) { - break; - } - w = plugin.getConfig().getString("Saved_Locations." + key + ".World"); - x = plugin.getConfig().getString("Saved_Locations." + key + ".BlockX"); - y = plugin.getConfig().getString("Saved_Locations." + key + ".BlockY"); - z = plugin.getConfig().getString("Saved_Locations." + key + ".BlockZ"); - CoordsGUI.setItem(i, plugin.addGUIItem(getBlock(w), key, "§bX: §c" + x, "§bY: §c" + y, "§bZ: §c" + z)); - i++; - } - for (int ii = i; ii < 45; ii++) { - CoordsGUI.setItem(ii, plugin.addGUIItem(Material.AIR, "")); - } - p.openInventory(CoordsGUI); - } - - private Material getBlock(String wname) { - ItemStack mat = new ItemStack(Material.GRASS_BLOCK); - if (wname.equals(Bukkit.getWorlds().get(1).getName())) { - mat.setType(Material.NETHERRACK); - } else if (wname.equals(Bukkit.getWorlds().get(2).getName())) { - mat.setType(Material.END_STONE); - } - return mat.getType(); } } \ No newline at end of file diff --git a/src/main/java/de/fanta/challenges/commands/coords/CoordsDeleteCommand.java b/src/main/java/de/fanta/challenges/commands/coords/CoordsDeleteCommand.java index 1e9ef94..f5e7acf 100644 --- a/src/main/java/de/fanta/challenges/commands/coords/CoordsDeleteCommand.java +++ b/src/main/java/de/fanta/challenges/commands/coords/CoordsDeleteCommand.java @@ -1,26 +1,20 @@ package de.fanta.challenges.commands.coords; import de.fanta.challenges.Challenges; +import de.fanta.challenges.guis.coordsgui.CoordsDeleteGUI; import de.fanta.challenges.utils.ChatUtil; import de.iani.cubesideutils.bukkit.commands.SubCommand; import de.iani.cubesideutils.commands.ArgsParser; -import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.inventory.Inventory; import java.util.Collection; public class CoordsDeleteCommand extends SubCommand { + private static final Challenges plugin = Challenges.getPlugin(); - public static final Inventory CoordsDeleteGUI = Bukkit.createInventory(null, 9, "§6Entferne Koordinate"); - public static final String CDGUIN = "§6Entferne Koordinate"; - private final Challenges plugin; - - public CoordsDeleteCommand(Challenges plugin) { - this.plugin = plugin; + public CoordsDeleteCommand() { } @Override @@ -31,10 +25,15 @@ } if (args.hasNext()) { - String next = args.getNext(); + String next = args.getAll(null); + + if (next == null) { + ChatUtil.sendErrorMessage(player, "Du musst einen Namen angeben."); + return true; + } + if (plugin.getConfig().contains("Saved_Locations." + next)) { - createDeleteGUI(player, next); - player.openInventory(CoordsDeleteGUI); + CoordsDeleteGUI.createCoordsDeleteGUI(player, next, null); } else { ChatUtil.sendWarningMessage(player, "Diese Position existiert nicht!"); } @@ -43,18 +42,6 @@ return false; } - public void createDeleteGUI(Player p, String dname) { - CoordsDeleteGUI.setItem(0, plugin.addGUIItem(Material.LIME_TERRACOTTA, "§aLöschen bestätigen", "§bWegpunkt: §c" + dname)); - CoordsDeleteGUI.setItem(4, plugin.addGUIItem(Material.ORANGE_BANNER, dname)); - for (int i = 1; i < 8; i++) { - if (CoordsDeleteGUI.getItem(i) == null) { - CoordsDeleteGUI.setItem(i, plugin.addGUIItem(Material.ORANGE_STAINED_GLASS_PANE, "")); - } - } - CoordsDeleteGUI.setItem(8, plugin.addGUIItem(Material.RED_TERRACOTTA, "§cLöschen abbrechen", "§bWegpunkt: §c" + dname)); - p.openInventory(CoordsDeleteGUI); - } - @Override public Collection onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) { return plugin.getConfig().getConfigurationSection("Saved_Locations").getKeys(false); diff --git a/src/main/java/de/fanta/challenges/commands/coords/CoordsGetCommand.java b/src/main/java/de/fanta/challenges/commands/coords/CoordsGetCommand.java index f970e60..d8e5352 100644 --- a/src/main/java/de/fanta/challenges/commands/coords/CoordsGetCommand.java +++ b/src/main/java/de/fanta/challenges/commands/coords/CoordsGetCommand.java @@ -31,7 +31,13 @@ ChatUtil.sendNormalMessage(player, key); } } else { - String next = args.getNext(); + String next = args.getAll(null); + + if (next == null) { + ChatUtil.sendErrorMessage(player, "Du musst einen Namen angeben."); + return true; + } + if (plugin.getConfig().contains("Saved_Locations." + next)) { String w = plugin.getConfig().getString("Saved_Locations." + next + ".World"); String x = plugin.getConfig().getString("Saved_Locations." + next + ".BlockX"); diff --git a/src/main/java/de/fanta/challenges/commands/coords/CoordsSaveCommand.java b/src/main/java/de/fanta/challenges/commands/coords/CoordsSaveCommand.java index 557711b..465e0e9 100644 --- a/src/main/java/de/fanta/challenges/commands/coords/CoordsSaveCommand.java +++ b/src/main/java/de/fanta/challenges/commands/coords/CoordsSaveCommand.java @@ -24,7 +24,13 @@ } if (args.hasNext()) { - String next = args.getNext(); + String next = args.getAll(null); + + if (next == null) { + ChatUtil.sendErrorMessage(player, "Du musst einen Namen angeben."); + return true; + } + if (!plugin.getConfig().contains("Saved_Locations." + next)) { String w = player.getLocation().getWorld().getName().toLowerCase(); int x = player.getLocation().getBlockX(); diff --git a/src/main/java/de/fanta/challenges/guis/coordsgui/CoordsDeleteGUI.java b/src/main/java/de/fanta/challenges/guis/coordsgui/CoordsDeleteGUI.java new file mode 100644 index 0000000..a469a6c --- /dev/null +++ b/src/main/java/de/fanta/challenges/guis/coordsgui/CoordsDeleteGUI.java @@ -0,0 +1,70 @@ +package de.fanta.challenges.guis.coordsgui; + +import de.fanta.challenges.Challenges; +import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.guiutils.GUIUtils; +import org.bukkit.Bukkit; +import org.bukkit.Material; +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.InventoryType; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class CoordsDeleteGUI implements Listener { + private static final Challenges plugin = Challenges.getPlugin(); + + private static final int ACCEPT_INDEX = 0; + private static final int DENY_INDEX = 4; + private static final int ITEM_INDEX = 2; + + public static final int INVENTORY_SIZE = 5; + public static final Inventory COORDS_DELETE_GUI = Bukkit.createInventory(null, InventoryType.HOPPER, plugin.getGUIPREFIX() + " >> Waypoint Löschen"); + + public static void createCoordsDeleteGUI(Player p, String waypointName, ItemStack stack) { + COORDS_DELETE_GUI.setItem(ACCEPT_INDEX, GUIUtils.createGuiItem(Material.LIME_CONCRETE, ChatUtil.GREEN + "Löschen bestätigen", ChatUtil.GREEN + "Wegpunkt: " + ChatUtil.BLUE + waypointName)); + + if (stack != null) { + COORDS_DELETE_GUI.setItem(ITEM_INDEX, GUIUtils.createGuiItem(stack.getType(), ChatUtil.BLUE + waypointName)); + } else { + COORDS_DELETE_GUI.setItem(ITEM_INDEX, GUIUtils.createGuiItem(Material.GRAY_BANNER, ChatUtil.BLUE + waypointName)); + } + + COORDS_DELETE_GUI.setItem(DENY_INDEX, GUIUtils.createGuiItem(Material.RED_CONCRETE, ChatUtil.RED + "Löschen abbrechen", ChatUtil.GREEN + "Wegpunkt: " + ChatUtil.BLUE + waypointName)); + + for (int i = 0; i < INVENTORY_SIZE; i++) { + ItemStack item = COORDS_DELETE_GUI.getItem(i); + if (item == null || item.getType() == Material.AIR) { + COORDS_DELETE_GUI.setItem(i, GUIUtils.EMPTY_ICON); + } + } + + p.openInventory(COORDS_DELETE_GUI); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + int slot = e.getRawSlot(); + Player player = (Player) e.getWhoClicked(); + if (COORDS_DELETE_GUI.equals(e.getClickedInventory())) { + ItemStack stack = e.getClickedInventory().getItem(ITEM_INDEX); + + if (stack == null) { + return; + } + + String waypointName = stack.getItemMeta().getDisplayName().substring(14); + + if (slot == ACCEPT_INDEX) { + plugin.getConfig().set("Saved_Locations." + waypointName, null); + ChatUtil.sendNormalMessage(player, ChatUtil.BLUE + waypointName + ChatUtil.GREEN + " wurde gelöscht!"); + player.closeInventory(); + } else if (slot == DENY_INDEX) { + CoordsGUI.buildCoordsInventory(player); + } + e.setCancelled(true); + } + } +} diff --git a/src/main/java/de/fanta/challenges/guis/coordsgui/CoordsGUI.java b/src/main/java/de/fanta/challenges/guis/coordsgui/CoordsGUI.java new file mode 100644 index 0000000..606090f --- /dev/null +++ b/src/main/java/de/fanta/challenges/guis/coordsgui/CoordsGUI.java @@ -0,0 +1,133 @@ +package de.fanta.challenges.guis.coordsgui; + +import de.fanta.challenges.Challenges; +import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.guiutils.GUIUtils; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +public class CoordsGUI implements Listener { + private static final Challenges plugin = Challenges.getPlugin(); + private static final Map wayPointPerSlot = new HashMap<>(); + private static final Map InventoryperPlayer = new HashMap<>(); + private static int CLOSE_IDEX = 0; + + public static void buildCoordsInventory(Player p) { + Set waypoints = plugin.getConfig().getConfigurationSection("Saved_Locations").getKeys(false); + + if (waypoints.isEmpty()) { + ChatUtil.sendErrorMessage(p, "Keine WayPoints vorhanden"); + return; + } + + Inventory COORDS_UI = Bukkit.createInventory(p, 9 * (waypoints.size() / 7 + ((waypoints.size() % 7 > 0) ? 1 : 0)) + 18, plugin.getGUIPREFIX() + " >> Waypoints"); + InventoryperPlayer.put(p, COORDS_UI); + CLOSE_IDEX = COORDS_UI.getSize() - 9; + int row = 0; + int cat = 0; + + for (String waypoint : waypoints) { + int slot = row * 9 + cat + 10; + + World world = Bukkit.getWorld(Objects.requireNonNull(plugin.getConfig().getString("Saved_Locations." + waypoint + ".World"))); + double x = Double.parseDouble(Objects.requireNonNull(plugin.getConfig().getString("Saved_Locations." + waypoint + ".BlockX"))); + double y = Double.parseDouble(Objects.requireNonNull(plugin.getConfig().getString("Saved_Locations." + waypoint + ".BlockY"))); + double z = Double.parseDouble(Objects.requireNonNull(plugin.getConfig().getString("Saved_Locations." + waypoint + ".BlockZ"))); + + Location waypointlocation = new Location(world, x, y, z); + + ArrayList metalore = new ArrayList<>(); + + if (world != null) { + ItemStack stack = new ItemStack(getBlock(world.getName())); + ItemMeta itemMeta = stack.getItemMeta(); + itemMeta.setDisplayName(ChatUtil.BLUE + waypoint); + metalore.add(ChatUtil.BLUE + "X: " + ChatUtil.GREEN + x + ChatUtil.BLUE + " Y: " + ChatUtil.GREEN + y + ChatUtil.BLUE + " Z: " + ChatUtil.GREEN + z); + metalore.add(ChatUtil.GREEN + "Linksklick: Zeigt Coords im Chat."); + metalore.add(ChatUtil.RED + "Rechtsklick: Waypoint Löschen."); + itemMeta.setLore(metalore); + stack.setItemMeta(itemMeta); + COORDS_UI.setItem(slot, stack); + Bukkit.getLogger().info("" + waypoint); + wayPointPerSlot.put(waypoint, waypointlocation); + if (cat < 6) { + cat++; + continue; + } + row++; + cat = 0; + } + + } + + COORDS_UI.setItem(CLOSE_IDEX, GUIUtils.createGuiItem(Material.BARRIER, ChatUtil.RED + "Menü verlassen!")); + for (int i = 0; i < COORDS_UI.getSize(); i++) { + if (COORDS_UI.getItem(i) == null || COORDS_UI.getItem(i).getType() == Material.AIR) { + COORDS_UI.setItem(i, GUIUtils.EMPTY_ICON); + } + } + p.openInventory(COORDS_UI); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + Player p = (Player) e.getWhoClicked(); + + if (Objects.equals(e.getClickedInventory(), InventoryperPlayer.get(p))) { + int slot = e.getRawSlot(); + ItemStack stack = e.getClickedInventory().getItem(slot); + + if (stack == null) { + return; + } + + String waypointName = stack.getItemMeta().getDisplayName(); + + if (waypointName.length() > 14) { + waypointName = waypointName.substring(14); + } + + Location location = wayPointPerSlot.get(waypointName); + + if (location != null) { + if (e.isLeftClick()) { + ChatUtil.sendNormalMessage(p, ChatUtil.GREEN + "Position " + ChatUtil.ORANGE + waypointName + ChatUtil.GREEN + ": " + ChatUtil.ORANGE + "[" + location.getX() + "/" + location.getY() + "/" + location.getZ() + " " + location.getWorld().getName() + "]"); + p.closeInventory(); + } else if (e.isRightClick()) { + CoordsDeleteGUI.createCoordsDeleteGUI(p, waypointName, stack); + } + } + + if (slot == CLOSE_IDEX) { + p.closeInventory(); + } + e.setCancelled(true); + } + } + + private static Material getBlock(String wname) { + ItemStack mat = new ItemStack(Material.GRASS_BLOCK); + if (wname.equals(Bukkit.getWorlds().get(1).getName())) { + mat.setType(Material.NETHERRACK); + } else if (wname.equals(Bukkit.getWorlds().get(2).getName())) { + mat.setType(Material.END_STONE); + } + return mat.getType(); + } + +} diff --git a/src/main/java/de/fanta/challenges/listeners/EventRegistration.java b/src/main/java/de/fanta/challenges/listeners/EventRegistration.java index b6e5b4f..c4ac625 100644 --- a/src/main/java/de/fanta/challenges/listeners/EventRegistration.java +++ b/src/main/java/de/fanta/challenges/listeners/EventRegistration.java @@ -3,6 +3,8 @@ import de.fanta.challenges.Challenges; import de.fanta.challenges.challenges.*; import de.fanta.challenges.gravestones.GravestoneListener; +import de.fanta.challenges.guis.coordsgui.CoordsDeleteGUI; +import de.fanta.challenges.guis.coordsgui.CoordsGUI; import de.fanta.challenges.guis.ResetGui; import de.fanta.challenges.guis.TeleportGUI; import de.fanta.challenges.guis.TimerGui; @@ -22,7 +24,6 @@ public record EventRegistration(Challenges plugin) { public static final PluginManager pM = Bukkit.getPluginManager(); public void registerEvents() { - pM.registerEvents(new InventoryClickListener(), plugin); pM.registerEvents(new QuitJoinListener(), plugin); pM.registerEvents(new BlockDestroyListener(), plugin); pM.registerEvents(new BlockCreateListener(), plugin); @@ -47,6 +48,8 @@ pM.registerEvents(new TeleportGUI(), plugin); pM.registerEvents(new TeamSelectGUI(), plugin); pM.registerEvents(new TeamSettingsGUI(), plugin); + pM.registerEvents(new CoordsGUI(), plugin); + pM.registerEvents(new CoordsDeleteGUI(), plugin); pM.registerEvents(new GravestoneListener(), plugin); diff --git a/src/main/java/de/fanta/challenges/listeners/InventoryClickListener.java b/src/main/java/de/fanta/challenges/listeners/InventoryClickListener.java deleted file mode 100644 index 27f6c72..0000000 --- a/src/main/java/de/fanta/challenges/listeners/InventoryClickListener.java +++ /dev/null @@ -1,44 +0,0 @@ -package de.fanta.challenges.listeners; - -import de.fanta.challenges.Challenges; -import de.fanta.challenges.utils.ChatUtil; -import de.fanta.challenges.commands.coords.CoordsCommand; -import de.fanta.challenges.commands.coords.CoordsDeleteCommand; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; - -public class InventoryClickListener implements Listener { - - private final Challenges plugin = Challenges.getPlugin(); - - @EventHandler - public void InvClick(InventoryClickEvent e) { - String invname = e.getView().getTitle(); - Player p = (Player) e.getWhoClicked(); - int slot = e.getRawSlot(); - if (invname.equals(CoordsCommand.CGUIN)) { - e.setCancelled(true); - if (slot >= 0 && slot <= 44) { - if (CoordsCommand.CoordsGUI.getItem(slot) != null && p.hasPermission("challenges.coords.delete")) { - p.performCommand("coords delete " + e.getCurrentItem().getItemMeta().getDisplayName()); - } - } - } - if (invname.equals(CoordsDeleteCommand.CDGUIN)) { - e.setCancelled(true); - if (slot == 0) { - String cname = CoordsDeleteCommand.CoordsDeleteGUI.getItem(4).getItemMeta().getDisplayName(); - plugin.getConfig().set("Saved_Locations." + cname, null); - plugin.saveConfig(); - ChatUtil.sendNormalMessage(p, "Position " + cname + " wurde gelöscht!"); - p.closeInventory(); - } - if (slot == 8) { - p.performCommand("coords"); - } - } - } -} -