Add basics of GUI
This commit is contained in:
@@ -6,7 +6,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import nl.interestingcorner.coordinates.commands.CoordinatesCommand;
|
||||
import nl.interestingcorner.coordinates.db.CoordinatesDatabaseManager;
|
||||
import nl.interestingcorner.coordinates.gui.CoordinatesGUIListener;
|
||||
// import nl.interestingcorner.coordinates.gui.CoordinatesGUIListener;
|
||||
|
||||
public class App extends JavaPlugin {
|
||||
|
||||
@@ -16,7 +16,7 @@ public class App extends JavaPlugin {
|
||||
CoordinatesDatabaseManager.INSTANCE.initialize(this);
|
||||
getLogger().info("Successfully initialized database");
|
||||
|
||||
getServer().getPluginManager().registerEvents(new CoordinatesGUIListener(), this);
|
||||
// getServer().getPluginManager().registerEvents(new CoordinatesGUIListener(), this);
|
||||
registerCommands();
|
||||
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import java.util.List;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import nl.interestingcorner.coordinates.db.CoordinatesDatabaseManager;
|
||||
import nl.interestingcorner.coordinates.db.Coordinate;
|
||||
import nl.interestingcorner.coordinates.db.CoordinatesDatabaseManager;
|
||||
import nl.interestingcorner.coordinates.gui.CoordinatesGUI;
|
||||
|
||||
public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
||||
|
||||
@@ -2,67 +2,24 @@ package nl.interestingcorner.coordinates.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.coordinates.db.Coordinate;
|
||||
import nl.interestingcorner.core.MinecraftColor;
|
||||
import nl.interestingcorner.core.gui.GUI;
|
||||
|
||||
public class CoordinatesGUI {
|
||||
public static void open(Player player, List<Coordinate> coordinates) {
|
||||
public static void open(Player player, List<Coordinate> coords) {
|
||||
// Convert coordinates to ItemStacks for GUI display
|
||||
List<ItemStack> items = coords.stream()
|
||||
.map(coord -> {
|
||||
ItemStack item = coord.toItem();
|
||||
return item;
|
||||
})
|
||||
.toList();
|
||||
|
||||
int coordinatesAmount = coordinates.size();
|
||||
int size;
|
||||
if (coordinatesAmount <= 9) {
|
||||
size = 9;
|
||||
} else {
|
||||
// TODO handle if size is more than 54 - 9, add pagination
|
||||
/**
|
||||
* if more than 45, add pages. Split by 45 items per page.
|
||||
* use bottom row leftmost and rightmost item for navigation.
|
||||
* use paper item for page number display in middle.
|
||||
* use lore of navigation items clicked to handle showing next/previous page.
|
||||
* use player.getOpeninventory() to get current inventory and update or close it.
|
||||
*/
|
||||
|
||||
// round up to nearest multiple of 9
|
||||
size = ((coordinatesAmount / 9) + 1) * 9;
|
||||
player.sendMessage("Size: " + size);
|
||||
}
|
||||
|
||||
// Create an inventory with 9 slots and a title
|
||||
Inventory gui = Bukkit.createInventory(null, size, "Coordinates Menu");
|
||||
|
||||
for (Coordinate coordinate : coordinates) {
|
||||
ItemStack item = createCoordinateItem(coordinate);
|
||||
gui.addItem(item);
|
||||
}
|
||||
|
||||
// Open the GUI for the player
|
||||
player.openInventory(gui);
|
||||
}
|
||||
|
||||
private static ItemStack createCoordinateItem(Coordinate coordinate) {
|
||||
ItemStack itemStack = new ItemStack(coordinate.color.toMaterial());
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
if (meta == null) {
|
||||
return itemStack;
|
||||
}
|
||||
meta.setDisplayName(coordinate.color.toColorCode() + coordinate.name);
|
||||
meta.setLore(List.of(
|
||||
MinecraftColor.WHITE.toColorCode() + coordinate.description,
|
||||
coordinate.nether ? MinecraftColor.RED.toColorCode() + "Nether Coordinate" : "Overworld Coordinate",
|
||||
"X: " + coordinate.position.x(),
|
||||
"Y: " + coordinate.position.y(),
|
||||
"Z: " + coordinate.position.z(),
|
||||
"World: " + coordinate.world
|
||||
));
|
||||
itemStack.setItemMeta(meta);
|
||||
|
||||
return itemStack;
|
||||
GUI gui = new GUI("Coordinates", GUI.DEFAULT_PAGE_SIZE);
|
||||
gui.setItems(items);
|
||||
gui.show(player);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package nl.interestingcorner.coordinates.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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user