From 78102819765762d769080e62860860c7cda45ab2 Mon Sep 17 00:00:00 2001 From: SemvdH Date: Wed, 22 Oct 2025 23:42:30 +0200 Subject: [PATCH] Add basic GUI and getting coordinates by world --- .../main/java/nl/interestingcorner/App.java | 4 +- .../GetCoordinatesCommandHandler.java | 14 ++- .../interestingcorner/db/DatabaseManager.java | 96 ++++++++++++------- .../interestingcorner/gui/CoordinatesGUI.java | 31 ++++++ .../gui/CoordinatesGUIListener.java | 33 +++++++ 5 files changed, 140 insertions(+), 38 deletions(-) create mode 100644 ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUI.java create mode 100644 ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUIListener.java diff --git a/ic_plugin/src/main/java/nl/interestingcorner/App.java b/ic_plugin/src/main/java/nl/interestingcorner/App.java index ed68423..a9607af 100644 --- a/ic_plugin/src/main/java/nl/interestingcorner/App.java +++ b/ic_plugin/src/main/java/nl/interestingcorner/App.java @@ -6,6 +6,7 @@ import org.bukkit.plugin.java.JavaPlugin; import nl.interestingcorner.commands.CoordinatesCommand; import nl.interestingcorner.db.DatabaseManager; +import nl.interestingcorner.gui.CoordinatesGUIListener; public class App extends JavaPlugin { @@ -19,7 +20,8 @@ public class App extends JavaPlugin { return; } getLogger().info("Successfully initialized database"); - + + getServer().getPluginManager().registerEvents(new CoordinatesGUIListener(), this); registerCommands(); } diff --git a/ic_plugin/src/main/java/nl/interestingcorner/commands/GetCoordinatesCommandHandler.java b/ic_plugin/src/main/java/nl/interestingcorner/commands/GetCoordinatesCommandHandler.java index 1aac1a8..0ad90dd 100644 --- a/ic_plugin/src/main/java/nl/interestingcorner/commands/GetCoordinatesCommandHandler.java +++ b/ic_plugin/src/main/java/nl/interestingcorner/commands/GetCoordinatesCommandHandler.java @@ -7,13 +7,23 @@ import org.bukkit.entity.Player; import nl.interestingcorner.db.Coordinate; import nl.interestingcorner.db.DatabaseManager; +import nl.interestingcorner.gui.CoordinatesGUI; public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler { @Override public boolean handleCommand(CommandSender sender, String[] args) { if (sender instanceof Player player) { - List coords = DatabaseManager.INSTANCE.getAllCoordinates(); + List coords; + if (args.length < 2) { + coords = DatabaseManager.INSTANCE.getAllCoordinates(); + } else if (args[1].equalsIgnoreCase("world")) { + String world = player.getWorld().getName(); + coords = DatabaseManager.INSTANCE.getAllCoordinates(world); + } else { + sender.sendMessage("Invalid argument: " + args[1]); + return false; + } StringBuilder res = new StringBuilder("Coordinates: "); for (Coordinate coordinate : coords) { @@ -21,6 +31,8 @@ public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler { } player.sendMessage(res.toString()); + + CoordinatesGUI.open(player, coords); } return true; } diff --git a/ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java b/ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java index da571f7..53ebf1a 100644 --- a/ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java +++ b/ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java @@ -26,7 +26,7 @@ public enum DatabaseManager { /** * Initializes the database. Creates the file and sets up the db structure. - * + * * @param app the app to use for logging * @return true if the database was initialized successfully. False if not. */ @@ -46,7 +46,7 @@ public enum DatabaseManager { /** * Closes the connectino to the database - * + * * @return true if the connection closed succesfully, false if not. */ public boolean close() { @@ -64,7 +64,7 @@ public enum DatabaseManager { /** * Gets all coordinates from the database - * + * * @return a list with all coordinates from the database. Empty if an error * occurred. */ @@ -73,44 +73,68 @@ public enum DatabaseManager { try { PreparedStatement getAllCoordinatesStatement = this.connection .prepareStatement("SELECT * FROM coordinates"); - ResultSet allCoordinates = getAllCoordinatesStatement.executeQuery(); - while (allCoordinates.next()) { - - MinecraftColor color = MinecraftColor.fromString(allCoordinates.getString("color")); - if (color == null) { - color = MinecraftColor.WHITE; - } - - Coordinate c = new Coordinate( - allCoordinates.getInt("id"), - allCoordinates.getString("name"), - allCoordinates.getString("description"), - new Coordinate.Position( - allCoordinates.getInt("x"), - allCoordinates.getInt("y"), - allCoordinates.getInt("z")), - allCoordinates.getBoolean("nether"), - allCoordinates.getString("world"), - color); - - result.add(c); - } + result = convertResultsToCoordinates(getAllCoordinatesStatement.executeQuery()); } catch (SQLException e) { - this.app.getLogger().log(Level.SEVERE, "Could not get commands! {0}", e.getMessage()); + this.app.getLogger().log(Level.SEVERE, "Could not get coordinates! {0}", e.getMessage()); } return result; } + public List getAllCoordinates(String world) { + List result = new ArrayList<>(); + try { + PreparedStatement getAllCoordinatesStatement = this.connection + .prepareStatement("SELECT * FROM coordinates WHERE world = ?"); + getAllCoordinatesStatement.setString(1, world); + + result = convertResultsToCoordinates(getAllCoordinatesStatement.executeQuery()); + + } catch (SQLException e) { + this.app.getLogger().log(Level.SEVERE, "Could not get coordinates for world {0}! {1}", + new Object[] { world, e.getMessage() }); + } + return result; + } + + public List convertResultsToCoordinates(ResultSet results) throws SQLException { + List coordinates = new ArrayList<>(); + while (results.next()) { + + MinecraftColor color = MinecraftColor.fromString(results.getString("color")); + if (color == null) { + color = MinecraftColor.WHITE; + } + + Coordinate c = new Coordinate( + results.getInt("id"), + results.getString("name"), + results.getString("description"), + new Coordinate.Position( + results.getInt("x"), + results.getInt("y"), + results.getInt("z")), + results.getBoolean("nether"), + results.getString("world"), + color); + + coordinates.add(c); + } + + return coordinates; + } + /** * Adds a new coordinate to the database with the given parameters. - * @param name the name of the coordinate + * + * @param name the name of the coordinate * @param description a short description of the coordinate - * @param position the position of the coordinate where the player will spwan when they teleport to the coordinate - * @param nether if the coordinate is in the nether - * @param world the Multiverse world the coordinate belongs to - * @param color the color to display for the coordinate name + * @param position the position of the coordinate where the player will + * spwan when they teleport to the coordinate + * @param nether if the coordinate is in the nether + * @param world the Multiverse world the coordinate belongs to + * @param color the color to display for the coordinate name * @return true if the command was added successfully, false otherwise */ public boolean addCoordinate(String name, String description, Coordinate.Position position, boolean nether, @@ -136,9 +160,9 @@ public enum DatabaseManager { } /** - * Sets up the connectino to the database. Creates a new .db file if it doesn't - * exist yet. - * + * Sets up the connectino to the database. Creates a new .db file if it + * doesn't exist yet. + * * @return true if the connection was set up successfully. False if not. */ private boolean setupDatabase() { @@ -166,14 +190,14 @@ public enum DatabaseManager { /** * initializes the tables for the database. - * + * * @return true if the tables were initialized successfully. False if not. */ private boolean initializeTables() { try { PreparedStatement createTableStatement = this.connection.prepareStatement(Coordinate.createTableStatement); createTableStatement.executeUpdate(); - this.app.getLogger().info("Executed create table statement"); + this.app.getLogger().fine("Executed create table statement"); PreparedStatement getTablesStatement = this.connection.prepareStatement( "SELECT name FROM sqlite_master WHERE type='table'"); diff --git a/ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUI.java b/ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUI.java new file mode 100644 index 0000000..6022944 --- /dev/null +++ b/ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUI.java @@ -0,0 +1,31 @@ +package nl.interestingcorner.gui; + +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import nl.interestingcorner.db.Coordinate; + +public class CoordinatesGUI { + public static void open(Player player, List coordinates) { + // Create an inventory with 9 slots and a title + Inventory gui = Bukkit.createInventory(null, 9, "Coordinates Menu"); + + // Create an example item + ItemStack addItem = new ItemStack(Material.GREEN_WOOL); + ItemMeta meta = addItem.getItemMeta(); + meta.setDisplayName(coordinates.get(0).name); + addItem.setItemMeta(meta); + + // Place it in slot 0 + gui.setItem(0, addItem); + + // Open the GUI for the player + player.openInventory(gui); + } +} diff --git a/ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUIListener.java b/ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUIListener.java new file mode 100644 index 0000000..532db73 --- /dev/null +++ b/ic_plugin/src/main/java/nl/interestingcorner/gui/CoordinatesGUIListener.java @@ -0,0 +1,33 @@ +package nl.interestingcorner.gui; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.entity.Player; +import org.bukkit.ChatColor; + +public class CoordinatesGUIListener implements Listener{ + + @EventHandler + public void onInventoryClick(InventoryClickEvent event) { + + // Check if this is our custom GUI + if (event.getView().getTitle().equals("Coordinates Menu")) { + event.setCancelled(true); // Prevent taking the item + + if (event.getCurrentItem() == null) return; + + Player player = (Player) event.getWhoClicked(); + + switch (event.getSlot()) { + case 0 -> { + player.sendMessage(ChatColor.GREEN + "You clicked Add Coordinate!"); + player.closeInventory(); + // Here you could open another GUI or run a command + } + // Add other cases for other slots if needed + } + } + } + +}