Compare commits
4 Commits
e1527dad4c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69dc87b718 | ||
|
|
de87781ee1 | ||
|
|
a07108e141 | ||
|
|
98fe1a696a |
@@ -5,8 +5,10 @@ import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import nl.interestingcorner.coordinates.commands.CoordinatesCommand;
|
||||
import nl.interestingcorner.coordinates.db.Coordinate;
|
||||
import nl.interestingcorner.coordinates.db.CoordinatesDatabaseManager;
|
||||
// import nl.interestingcorner.coordinates.gui.CoordinatesGUIListener;
|
||||
import nl.interestingcorner.coordinates.listeners.MapUseListener;
|
||||
import nl.interestingcorner.core.MinecraftColor;
|
||||
|
||||
public class App extends JavaPlugin {
|
||||
|
||||
@@ -15,10 +17,9 @@ public class App extends JavaPlugin {
|
||||
getLogger().info("Eyooo we boutta get lit!");
|
||||
CoordinatesDatabaseManager.INSTANCE.initialize(this);
|
||||
getLogger().info("Successfully initialized database");
|
||||
|
||||
// getServer().getPluginManager().registerEvents(new CoordinatesGUIListener(), this);
|
||||
registerCommands();
|
||||
|
||||
getServer().getPluginManager().registerEvents(new MapUseListener(), this);
|
||||
registerCommands();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -27,15 +28,76 @@ public class App extends JavaPlugin {
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
registerSimgleCommand("ic-coords", new CoordinatesCommand());
|
||||
registerSingleCommand("ic-coords", new CoordinatesCommand());
|
||||
}
|
||||
|
||||
private void registerSimgleCommand(String name, CommandExecutor executor)
|
||||
{
|
||||
|
||||
private void registerSingleCommand(String name, CommandExecutor executor) {
|
||||
PluginCommand pc = this.getCommand(name);
|
||||
if (pc != null)
|
||||
{
|
||||
if (pc != null) {
|
||||
pc.setExecutor(executor);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private void addOldCoordinates() {
|
||||
CoordinatesDatabaseManager db = CoordinatesDatabaseManager.INSTANCE;
|
||||
db.addCoordinate(new Coordinate(1, "The Tower", "Teleport to the Castle of Illusion",
|
||||
new Coordinate.Position(2700, 65, 450), false, "the-old-world", MinecraftColor.GOLD));
|
||||
db.addCoordinate(new Coordinate(12, "Temple of Tears", "Teleport to the temple in the sky",
|
||||
new Coordinate.Position(2445, 218, -1902), false, "the-old-world", MinecraftColor.AQUA));
|
||||
db.addCoordinate(new Coordinate(13, "Big village", "Teleport to a big ass village",
|
||||
new Coordinate.Position(1828, 63, -845), false, "the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(14, "Other village", "Teleport to yet another village",
|
||||
new Coordinate.Position(1771, 64, -1626), false, "the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(15, "Acacia village", "Teleport to acacia village",
|
||||
new Coordinate.Position(2287, 63, -1492), false, "the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(16, "Illager castle", "Teleport to an illager castle",
|
||||
new Coordinate.Position(2694, 93, -1240), false, "the-old-world", MinecraftColor.GRAY));
|
||||
db.addCoordinate(new Coordinate(17, "Huge honeycomb", "Hmmm honey", new Coordinate.Position(1650, 70, 637),
|
||||
false, "the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(18, "Mansion", "The big mansion next to The Tower",
|
||||
new Coordinate.Position(2833, 66, 377), false, "the-old-world", MinecraftColor.DARK_AQUA));
|
||||
db.addCoordinate(new Coordinate(19, "Village with labyrinth", "Big village with a labyrinth",
|
||||
new Coordinate.Position(871, 66, 884), false, "the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(20, "Ravine cave entrance", "Teleport to big cave entrance",
|
||||
new Coordinate.Position(2080, 105, 1135), false, "the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(21, "Skeleton spawner", "Get some XP made by Sienna",
|
||||
new Coordinate.Position(2278, -9, 1484), false, "the-old-world", MinecraftColor.LIGHT_PURPLE));
|
||||
db.addCoordinate(new Coordinate(22, "Nether portal", "Nether portal IN THE NETHER",
|
||||
new Coordinate.Position(316, 81, 50), true, "the-old-world", MinecraftColor.RED));
|
||||
db.addCoordinate(new Coordinate(25, "Home village", "The first home village",
|
||||
new Coordinate.Position(2212, 69, 926), false, "the-old-world", MinecraftColor.GREEN));
|
||||
db.addCoordinate(new Coordinate(26, "deep dark wool eiland", "ready to get your asshole ripped",
|
||||
new Coordinate.Position(2626, -36, 1714), false, "the-old-world", MinecraftColor.DARK_BLUE));
|
||||
db.addCoordinate(new Coordinate(27, "2nd Nether portal", "2nd nether portal IN THE NETHER",
|
||||
new Coordinate.Position(264, 43, 106), true, "the-old-world", MinecraftColor.RED));
|
||||
db.addCoordinate(new Coordinate(28, "Nether fortress", "Nether fortress IN THE NETHER",
|
||||
new Coordinate.Position(85, 60, 423), true, "the-old-world", MinecraftColor.DARK_RED));
|
||||
db.addCoordinate(new Coordinate(29, "Village hidden in the sand", "Also known as Sunagakure",
|
||||
new Coordinate.Position(223, 75, 688), false, "the-old-world", MinecraftColor.YELLOW));
|
||||
db.addCoordinate(new Coordinate(30, "deep dark", "deep down", new Coordinate.Position(2590, -17, -1690), false,
|
||||
"the-old-world", MinecraftColor.DARK_BLUE));
|
||||
db.addCoordinate(new Coordinate(31, "Mob farm", "mob farm maken voor xp en andere stuff enzo",
|
||||
new Coordinate.Position(2471, 79, -440), false, "the-old-world", MinecraftColor.RED));
|
||||
db.addCoordinate(new Coordinate(33, "Green cave", "NEW Green cave", new Coordinate.Position(10098, -10, 10076),
|
||||
false, "the-old-world", MinecraftColor.GREEN));
|
||||
db.addCoordinate(new Coordinate(34, "Castle", "Beautiful castle in plains biome",
|
||||
new Coordinate.Position(9678, 90, 11222), false, "the-old-world", MinecraftColor.GOLD));
|
||||
db.addCoordinate(new Coordinate(35, "RPG style city", "large village with a castle",
|
||||
new Coordinate.Position(8904, 68, 11200), false, "the-old-world", MinecraftColor.AQUA));
|
||||
db.addCoordinate(new Coordinate(36, "Abandoned castle", "Abandoned castle which we conquered",
|
||||
new Coordinate.Position(9071, 74, 11022), false, "the-old-world", MinecraftColor.LIGHT_PURPLE));
|
||||
db.addCoordinate(new Coordinate(37, "Portal", "New portal IN THE NETHER",
|
||||
new Coordinate.Position(1136, 32, 1397), true, "the-old-world", MinecraftColor.RED));
|
||||
db.addCoordinate(new Coordinate(38, "bee boss", "ga dood lol", new Coordinate.Position(3470, 64, 227), false,
|
||||
"the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(39, "bee boss 2.0", "ga dood neef", new Coordinate.Position(4045, 63, 130),
|
||||
false, "the-old-world", MinecraftColor.BLACK));
|
||||
db.addCoordinate(new Coordinate(40, "Ice biome", "RIP my boi titanic", new Coordinate.Position(4587, 63, 476),
|
||||
false, "the-old-world", MinecraftColor.AQUA));
|
||||
db.addCoordinate(new Coordinate(41, "Onderwater gang trap ofzo", "Neem water breathing of hoedje mee ofzo",
|
||||
new Coordinate.Position(6156, 63, 2228), false, "the-old-world", MinecraftColor.GRAY));
|
||||
db.addCoordinate(new Coordinate(42, "The hood", "da hood", new Coordinate.Position(8202, 70, 999580), false,
|
||||
"the-old-world", MinecraftColor.GREEN));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,11 +41,6 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
sender.sendMessage("Args after parsing:");
|
||||
sender.sendMessage("0:" + parsedArgs.get(0));
|
||||
sender.sendMessage("1:" + parsedArgs.get(1));
|
||||
sender.sendMessage("2:" + parsedArgs.get(2));
|
||||
|
||||
String name = parsedArgs.get(0);
|
||||
String description = parsedArgs.get(1);
|
||||
String color = parsedArgs.get(2);
|
||||
@@ -66,6 +61,7 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
player.sendMessage(MinecraftColor.BLUE.toColorCode() + "Adding coordinate '" + name + "' at your current location...");
|
||||
//TODO check if item doesnt already exist, coordinate with same values, create isEquals method for coordinate
|
||||
return CoordinatesDatabaseManager.INSTANCE
|
||||
.addCoordinate(name, // name
|
||||
@@ -84,12 +80,10 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
private List<String> removeFirstArg(String[] args) {
|
||||
List<String> onlyArgs = new ArrayList<>();
|
||||
|
||||
sender.sendMessage("removeFirstArg: size of args is " + String.valueOf(args.length));
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
onlyArgs.add(args[i]);
|
||||
}
|
||||
|
||||
sender.sendMessage("removeFirstArg: size of onlyArgs is " + String.valueOf(onlyArgs.size()));
|
||||
return onlyArgs;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,14 +14,12 @@ public class CoordinatesCommand implements CommandExecutor{
|
||||
sb.append(string);
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
sender.sendMessage("Args length: " + String.valueOf(args.length));
|
||||
sender.sendMessage(sb.toString());
|
||||
|
||||
// strategy design pattern
|
||||
return switch (args[0]) {
|
||||
case "get" -> new GetCoordinatesCommandHandler().handleCommand(sender, args);
|
||||
case "add" -> new AddCoordinateCommandHandler().handleCommand(sender, args);
|
||||
case "map" -> new GetMapCommandHandler().handleCommand(sender, args);
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,34 +13,31 @@ public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
||||
|
||||
@Override
|
||||
public boolean handleCommand(CommandSender sender, String[] args) {
|
||||
if (sender instanceof Player player) {
|
||||
List<Coordinate> coords;
|
||||
if (args.length < 2) {
|
||||
coords = CoordinatesDatabaseManager.INSTANCE.getAllCoordinates();
|
||||
} else if (args[1].equalsIgnoreCase("world")) {
|
||||
String world = player.getWorld().getName();
|
||||
coords = CoordinatesDatabaseManager.INSTANCE.getAllCoordinates(world);
|
||||
} else {
|
||||
sender.sendMessage("Invalid argument: " + args[1]);
|
||||
return false;
|
||||
try {
|
||||
if (sender instanceof Player player) {
|
||||
List<Coordinate> coords;
|
||||
if (args.length < 2) {
|
||||
coords = CoordinatesDatabaseManager.INSTANCE.getAllCoordinates();
|
||||
} else if (args[1].equalsIgnoreCase("world")) {
|
||||
String world = player.getWorld().getName();
|
||||
coords = CoordinatesDatabaseManager.INSTANCE.getAllCoordinates(world);
|
||||
} else {
|
||||
sender.sendMessage("Invalid argument: " + args[1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (coords.isEmpty()) {
|
||||
player.sendMessage("No coordinates found! Add some with the §3/ic-coords §5add §fcommand.");
|
||||
return true;
|
||||
}
|
||||
|
||||
CoordinatesGUI.open(player, coords);
|
||||
}
|
||||
|
||||
if (coords.isEmpty()) {
|
||||
player.sendMessage("No coordinates found! Add some with the §3/ic-coords §5add §fcommand.");
|
||||
return true;
|
||||
}
|
||||
|
||||
StringBuilder res = new StringBuilder("Coordinates: ");
|
||||
|
||||
for (Coordinate coordinate : coords) {
|
||||
res.append(coordinate.toString());
|
||||
}
|
||||
|
||||
player.sendMessage(res.toString());
|
||||
|
||||
CoordinatesGUI.open(player, coords);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
sender.sendMessage("An error occurred while getting coordinates: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package nl.interestingcorner.coordinates.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import nl.interestingcorner.core.MinecraftColor;
|
||||
|
||||
public class GetMapCommandHandler implements CoordinatesCommandHandler {
|
||||
public static final String MAP_ITEM_NAME = MinecraftColor.LIGHT_PURPLE + "Map of Coordinates";
|
||||
public static final String MAP_ITEM_LORE = MinecraftColor.AQUA.toColorCode() + "This will take you anywhere!";
|
||||
|
||||
@Override
|
||||
public boolean handleCommand(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage("This command can only be used by a player.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Create a custom map item
|
||||
ItemStack map = new ItemStack(Material.FILLED_MAP);
|
||||
ItemMeta meta = map.getItemMeta();
|
||||
if (meta != null) {
|
||||
meta.setDisplayName(MAP_ITEM_NAME);
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add(MAP_ITEM_LORE);
|
||||
meta.setLore(lore);
|
||||
map.setItemMeta(meta);
|
||||
}
|
||||
|
||||
// Give the item to the player
|
||||
player.getInventory().addItem(map);
|
||||
player.sendMessage(MinecraftColor.GREEN.toColorCode() + "You received the Coordinates map! Right-click to use it.");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given item is the special map item.
|
||||
* @param item The item to check.
|
||||
* @return true if it is the special map, false otherwise.
|
||||
*/
|
||||
public static boolean isSpecialMap(ItemStack item) {
|
||||
if (item == null || item.getType() != Material.FILLED_MAP) return false;
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return false;
|
||||
return meta.hasDisplayName() && meta.getDisplayName().equals(MAP_ITEM_NAME) &&
|
||||
meta.hasLore() && meta.getLore() != null && meta.getLore().contains(MAP_ITEM_LORE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package nl.interestingcorner.coordinates.db;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
@@ -136,6 +137,56 @@ public class Coordinate {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this Coordinate to a Bukkit Location object
|
||||
* @return the Location object
|
||||
*/
|
||||
public Location toLocation() {
|
||||
return new Location(
|
||||
org.bukkit.Bukkit.getWorld(this.world),
|
||||
this.position.x(),
|
||||
this.position.y(),
|
||||
this.position.z()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Coordinate object from an ItemStack
|
||||
* @param item the ItemStack to convert
|
||||
* @return the Coordinate object, or null if the ItemStack is not a valid Coordinate
|
||||
*/
|
||||
public static Coordinate fromItem(ItemStack item)
|
||||
{
|
||||
if (item == null || item.getItemMeta() == null) {
|
||||
return null;
|
||||
}
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) {
|
||||
return null;
|
||||
}
|
||||
String name = meta.getDisplayName().replaceAll("§[0-9a-fk-or]", "");
|
||||
|
||||
List<String> lore = meta.getLore();
|
||||
if (lore == null || lore.size() < 6) {
|
||||
return null;
|
||||
}
|
||||
String description = lore.get(0).replaceAll("§[0-9a-fk-or]", "");
|
||||
boolean nether = lore.get(1).contains("Nether Coordinate");
|
||||
String xLine = lore.get(2);
|
||||
String yLine = lore.get(3);
|
||||
String zLine = lore.get(4);
|
||||
String worldLine = lore.get(5);
|
||||
|
||||
int x = Integer.parseInt(xLine.substring(xLine.indexOf(":") + 2));
|
||||
int y = Integer.parseInt(yLine.substring(yLine.indexOf(":") + 2));
|
||||
int z = Integer.parseInt(zLine.substring(zLine.indexOf(":") + 2));
|
||||
String world = worldLine.substring(worldLine.indexOf(":") + 2);
|
||||
|
||||
MinecraftColor color = MinecraftColor.fromMaterial(item.getType());
|
||||
|
||||
return new Coordinate(-1, name, description, new Position(x, y, z), nether, world, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import nl.interestingcorner.core.MinecraftColor;
|
||||
import nl.interestingcorner.core.db.DatabaseManager;
|
||||
import nl.interestingcorner.core.logging.Logger;
|
||||
|
||||
public enum CoordinatesDatabaseManager implements nl.interestingcorner.core.db.DatabaseInitializeListener{
|
||||
|
||||
@@ -20,6 +21,8 @@ public enum CoordinatesDatabaseManager implements nl.interestingcorner.core.db.D
|
||||
private Connection connection;
|
||||
private JavaPlugin app;
|
||||
|
||||
private final String TAG = getClass().getSimpleName();
|
||||
|
||||
public void initialize(JavaPlugin app) {
|
||||
this.app = app;
|
||||
DatabaseManager.INSTANCE.registerDatabaseInitializeListener(this);
|
||||
@@ -119,6 +122,8 @@ public enum CoordinatesDatabaseManager implements nl.interestingcorner.core.db.D
|
||||
this.app.getLogger().log(Level.SEVERE, "Error adding coordinate to database: {0}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger.INSTANCE.info(TAG, "Added coordinate '" + name + "' to database.");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package nl.interestingcorner.coordinates.gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -10,15 +11,15 @@ import nl.interestingcorner.core.gui.GUI;
|
||||
|
||||
public class CoordinatesGUI {
|
||||
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();
|
||||
List<ItemStack> items = new ArrayList<>();
|
||||
|
||||
for (Coordinate coord : coords) {
|
||||
ItemStack item = coord.toItem();
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
GUI gui = new GUI("Coordinates", GUI.DEFAULT_PAGE_SIZE);
|
||||
gui.addItemClickListener(new TeleportItemClickListener());
|
||||
gui.setItems(items);
|
||||
gui.show(player);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package nl.interestingcorner.coordinates.gui;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import nl.interestingcorner.coordinates.db.Coordinate;
|
||||
import nl.interestingcorner.core.gui.GUI;
|
||||
import nl.interestingcorner.core.gui.GUIItemClickListener;
|
||||
import nl.interestingcorner.core.logging.Logger;
|
||||
|
||||
public class TeleportItemClickListener implements GUIItemClickListener {
|
||||
|
||||
private final String TAG = this.getClass().getSimpleName();
|
||||
|
||||
@Override
|
||||
public void onItemClick(Player player, GUI gui, ItemStack item, int slot) {
|
||||
|
||||
Logger.INSTANCE.info(TAG, "Item clicked in slot " + slot + " by player " + player.getName());
|
||||
Coordinate coordinate = Coordinate.fromItem(item);
|
||||
|
||||
if (coordinate != null) {
|
||||
Location loc = coordinate.toLocation();
|
||||
Logger.INSTANCE.info(TAG, "Teleporting player " + player.getName() + " to coordinate " + coordinate.name);
|
||||
player.sendMessage("Teleporting you to " + coordinate.name);
|
||||
player.teleport(loc);
|
||||
player.sendTitle(coordinate.name, coordinate.description, 10, 70, 20);
|
||||
player.closeInventory();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package nl.interestingcorner.coordinates.listeners;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import nl.interestingcorner.coordinates.commands.GetMapCommandHandler;
|
||||
|
||||
public class MapUseListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerUseMap(PlayerInteractEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
ItemStack item = event.getItem();
|
||||
|
||||
if (GetMapCommandHandler.isSpecialMap(item)) {
|
||||
event.setCancelled(true);
|
||||
|
||||
// execute command to show coordinates of this world
|
||||
player.performCommand("ic-coords get world");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,4 @@ depend: [IC-core]
|
||||
commands:
|
||||
ic-coords:
|
||||
description: Main command for the coordinates
|
||||
usage: /ic-coords get | add
|
||||
usage: /ic-coords get | add | map
|
||||
@@ -3,6 +3,8 @@ package nl.interestingcorner.core;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import nl.interestingcorner.core.db.DatabaseManager;
|
||||
import nl.interestingcorner.core.gui.GUIInventoryClickListener;
|
||||
import nl.interestingcorner.core.logging.Logger;
|
||||
|
||||
/**
|
||||
* Core plugin class for the Interesting Corner plugin
|
||||
@@ -11,6 +13,8 @@ public class App extends JavaPlugin {
|
||||
@Override
|
||||
public void onEnable() {
|
||||
DatabaseManager.INSTANCE.initialize(this);
|
||||
Logger.INSTANCE.initialize(this);
|
||||
getServer().getPluginManager().registerEvents(GUIInventoryClickListener.INSTANCE, this);
|
||||
getLogger().info("Core plugin loaded!");
|
||||
}
|
||||
|
||||
|
||||
@@ -124,6 +124,20 @@ public enum MinecraftColor {
|
||||
return materialsMap.get(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a MinecraftColor from a Material.
|
||||
* @param material the material to find the color from
|
||||
* @return the MinecraftColor corresponding to the material. White if not found.
|
||||
*/
|
||||
public static MinecraftColor fromMaterial(Material material) {
|
||||
for (MinecraftColor color : MinecraftColor.values()) {
|
||||
if (materialsMap.get(color) == material) {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
return WHITE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this MinecraftColor to a corresponding Minecraft color code.
|
||||
* @return
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package nl.interestingcorner.core.gui;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
* Generalized chest GUI for use in plugins.
|
||||
*/
|
||||
public class ChestGUI {
|
||||
public static void open(Player player, String title, List<ItemStack> items) {
|
||||
int itemsAmount = items.size();
|
||||
int size;
|
||||
if (itemsAmount <= 9) {
|
||||
size = 9;
|
||||
} else {
|
||||
// TODO: handle pagination for >54 items
|
||||
size = ((itemsAmount / 9) + 1) * 9;
|
||||
}
|
||||
Inventory gui = Bukkit.createInventory(null, size, title);
|
||||
for (ItemStack item : items) {
|
||||
gui.addItem(item);
|
||||
}
|
||||
player.openInventory(gui);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package nl.interestingcorner.core.gui;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
|
||||
/**
|
||||
* Generalized listener for chest GUIs. Extend or register with custom logic.
|
||||
*/
|
||||
public class ChestGUIListener implements Listener {
|
||||
private final String guiTitle;
|
||||
|
||||
public ChestGUIListener(String guiTitle) {
|
||||
this.guiTitle = guiTitle;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
if (event.getView().getTitle().equals(guiTitle)) {
|
||||
event.setCancelled(true);
|
||||
if (event.getCurrentItem() == null) return;
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
// Extend this class and override this method for custom slot handling
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,23 +4,49 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
public class GUI implements InventoryHolder {
|
||||
public final class GUI implements InventoryHolder, Listener {
|
||||
public static final int MAX_INVENTORY_SIZE = 54; // max size of inventory before pages are needed
|
||||
public static final int DEFAULT_PAGE_SIZE = 45; // default items per page (excluding navigation row)
|
||||
public static final int PREVIOUS_PAGE_SLOT = 45; // slot for previous page button
|
||||
public static final int NEXT_PAGE_SLOT = 53; // slot for next page button
|
||||
|
||||
/**
|
||||
* Inventory that will be displayed
|
||||
*/
|
||||
private Inventory inventory;
|
||||
|
||||
/**
|
||||
* Full list of items that the GUI holds
|
||||
*/
|
||||
private List<ItemStack> items;
|
||||
|
||||
/**
|
||||
* Current page being displayed
|
||||
*/
|
||||
private int currentPage;
|
||||
private String title;
|
||||
private int pageSize;
|
||||
|
||||
/**
|
||||
* Title of the GUI
|
||||
*/
|
||||
private final String title;
|
||||
|
||||
/**
|
||||
* Number of items per page
|
||||
*/
|
||||
private final int pageSize;
|
||||
|
||||
/**
|
||||
* Listener for item click events
|
||||
*/
|
||||
private final List<GUIItemClickListener> itemClickListeners;
|
||||
|
||||
public GUI(String title, int pageSize) {
|
||||
this.title = title;
|
||||
@@ -28,6 +54,8 @@ public class GUI implements InventoryHolder {
|
||||
this.items = new ArrayList<>();
|
||||
this.currentPage = 0;
|
||||
this.inventory = null;
|
||||
this.itemClickListeners = new ArrayList<>();
|
||||
addItemClickListener(new NavigationItemClickListener());
|
||||
}
|
||||
|
||||
public void setItems(List<ItemStack> items) {
|
||||
@@ -36,6 +64,14 @@ public class GUI implements InventoryHolder {
|
||||
updateInventory();
|
||||
}
|
||||
|
||||
public void addItemClickListener(GUIItemClickListener listener) {
|
||||
this.itemClickListeners.add(listener);
|
||||
}
|
||||
|
||||
public List<GUIItemClickListener> getItemClickListeners() {
|
||||
return this.itemClickListeners;
|
||||
}
|
||||
|
||||
public void addItem(ItemStack item) {
|
||||
this.items.add(item);
|
||||
updateInventory();
|
||||
@@ -65,16 +101,55 @@ public class GUI implements InventoryHolder {
|
||||
}
|
||||
|
||||
private void updateInventory() {
|
||||
// Create inventory for the current page
|
||||
int size = Math.min(pageSize, MAX_INVENTORY_SIZE);
|
||||
this.inventory = Bukkit.createInventory(this, size, title);
|
||||
int start = currentPage * pageSize;
|
||||
int end = Math.min(start + pageSize, items.size());
|
||||
boolean needsPagination = items.size() > DEFAULT_PAGE_SIZE;
|
||||
int inventorySize = needsPagination ? MAX_INVENTORY_SIZE : pageSize;
|
||||
this.inventory = Bukkit.createInventory(this, inventorySize, title);
|
||||
|
||||
int itemsPerPage = needsPagination ? DEFAULT_PAGE_SIZE : pageSize;
|
||||
int start = currentPage * itemsPerPage;
|
||||
int end = Math.min(start + itemsPerPage, items.size());
|
||||
List<ItemStack> pageItems = items.subList(start, end);
|
||||
|
||||
for (int i = 0; i < pageItems.size(); i++) {
|
||||
if (needsPagination && i >= DEFAULT_PAGE_SIZE) {
|
||||
// Fill only the top rows (0-44) with items if paginating
|
||||
break;
|
||||
}
|
||||
this.inventory.setItem(i, pageItems.get(i));
|
||||
}
|
||||
// TODO: Add navigation items if needed
|
||||
|
||||
// Add navigation buttons if needed
|
||||
if (needsPagination) {
|
||||
if (currentPage > 0) {
|
||||
// Slot 45: Previous Page
|
||||
// use currentPage to show 1-based page number
|
||||
ItemStack prevButton = createNavigationItem("Previous Page", (currentPage) + " / " + getPagesAmount());
|
||||
this.inventory.setItem(PREVIOUS_PAGE_SLOT, prevButton);
|
||||
}
|
||||
|
||||
if ((currentPage + 1) * pageSize < items.size()) {
|
||||
// Slot 53: Next Page
|
||||
// use currentPage + 2 to show 1-based page number
|
||||
ItemStack nextButton = createNavigationItem("Next Page", (currentPage + 2) + " / " + getPagesAmount());
|
||||
this.inventory.setItem(NEXT_PAGE_SLOT, nextButton);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ItemStack createNavigationItem(String name, String description) {
|
||||
ItemStack compass = new ItemStack(Material.COMPASS);
|
||||
ItemMeta meta = compass.getItemMeta();
|
||||
if (meta == null)
|
||||
{
|
||||
return compass;
|
||||
}
|
||||
|
||||
meta.setDisplayName(name);
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add(description);
|
||||
meta.setLore(lore);
|
||||
compass.setItemMeta(meta);
|
||||
return compass;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package nl.interestingcorner.core.gui;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class GUIBuilder {
|
||||
|
||||
public Inventory buildInventory(List<ItemStack> items) {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
int itemsAmount = items.size();
|
||||
int pagesAmount = getPagesAmount(itemsAmount);
|
||||
|
||||
Inventory gui = Bukkit.createInventory(null, GUI.MAX_INVENTORY_SIZE, "Coordinates Menu");
|
||||
|
||||
for (ItemStack item : items) {
|
||||
gui.addItem(item);
|
||||
}
|
||||
|
||||
return gui;
|
||||
}
|
||||
|
||||
private static int getPagesAmount(int itemsAmount) {
|
||||
if (itemsAmount > GUI.MAX_INVENTORY_SIZE) {
|
||||
return ((itemsAmount / GUI.DEFAULT_PAGE_SIZE) + 1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package nl.interestingcorner.core.gui;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
|
||||
import nl.interestingcorner.core.logging.Logger;
|
||||
|
||||
public enum GUIInventoryClickListener implements Listener{
|
||||
INSTANCE;
|
||||
|
||||
private final String TAG = this.getClass().getSimpleName();
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
if (event.getInventory().getHolder() instanceof GUI gui) {
|
||||
Logger.INSTANCE.info(TAG, "Inventory click event detected");
|
||||
|
||||
List<GUIItemClickListener> itemClickListeners = gui.getItemClickListeners();
|
||||
for (GUIItemClickListener itemClickListener : itemClickListeners)
|
||||
{
|
||||
event.setCancelled(true);
|
||||
Logger.INSTANCE.info(TAG, "Sending click event to listener");
|
||||
itemClickListener.onItemClick((Player) event.getWhoClicked(), gui, event.getCurrentItem(), event.getSlot());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package nl.interestingcorner.core.gui;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public interface GUIItemClickListener {
|
||||
void onItemClick(Player player, GUI gui, ItemStack item, int slot);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package nl.interestingcorner.core.gui;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class NavigationItemClickListener implements GUIItemClickListener {
|
||||
|
||||
@Override
|
||||
public void onItemClick(Player player, GUI gui, ItemStack item, int slot) {
|
||||
if (slot == GUI.PREVIOUS_PAGE_SLOT) {
|
||||
gui.previousPage();
|
||||
player.closeInventory();
|
||||
gui.show(player);
|
||||
} else if (slot == GUI.NEXT_PAGE_SLOT) {
|
||||
gui.nextPage();
|
||||
player.closeInventory();
|
||||
gui.show(player);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package nl.interestingcorner.core.logging;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public enum Logger {
|
||||
INSTANCE;
|
||||
|
||||
private JavaPlugin plugin;
|
||||
|
||||
public void initialize(JavaPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void info(String tag, String message) {
|
||||
if (this.plugin != null) {
|
||||
this.plugin.getLogger().info("[" + tag + "] " + message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user