Add displaying coordinates in inventory upon get request
This commit is contained in:
@@ -55,10 +55,9 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
|||||||
String description = parsedArgs.get(1);
|
String description = parsedArgs.get(1);
|
||||||
String color = parsedArgs.get(2);
|
String color = parsedArgs.get(2);
|
||||||
|
|
||||||
// TODO check if color is added, if not, use white
|
|
||||||
if (!MinecraftColor.isValidColor(color)) {
|
if (!MinecraftColor.isValidColor(color)) {
|
||||||
sendInvalidColorMessage(color);
|
sendInvalidColorMessage(color);
|
||||||
color = "WHITE";
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sender instanceof Player player) {
|
if (sender instanceof Player player) {
|
||||||
@@ -72,6 +71,7 @@ public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO check if item doesnt already exist, coordinate with same values, create isEquals method for coordinate
|
||||||
return DatabaseManager.INSTANCE
|
return DatabaseManager.INSTANCE
|
||||||
.addCoordinate(name, // name
|
.addCoordinate(name, // name
|
||||||
description, // description
|
description, // description
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public class CoordinatesCommand implements CommandExecutor{
|
|||||||
sender.sendMessage("Args length: " + String.valueOf(args.length));
|
sender.sendMessage("Args length: " + String.valueOf(args.length));
|
||||||
sender.sendMessage(sb.toString());
|
sender.sendMessage(sb.toString());
|
||||||
|
|
||||||
|
// strategy design pattern
|
||||||
return switch (args[0]) {
|
return switch (args[0]) {
|
||||||
case "get" -> new GetCoordinatesCommandHandler().handleCommand(sender, args);
|
case "get" -> new GetCoordinatesCommandHandler().handleCommand(sender, args);
|
||||||
case "add" -> new AddCoordinateCommandHandler().handleCommand(sender, args);
|
case "add" -> new AddCoordinateCommandHandler().handleCommand(sender, args);
|
||||||
|
|||||||
@@ -24,6 +24,12 @@ public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
|||||||
sender.sendMessage("Invalid argument: " + args[1]);
|
sender.sendMessage("Invalid argument: " + args[1]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (coords.isEmpty()) {
|
||||||
|
player.sendMessage("No coordinates found! Add some with the §3/ic-coords §5add §fcommand.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
StringBuilder res = new StringBuilder("Coordinates: ");
|
StringBuilder res = new StringBuilder("Coordinates: ");
|
||||||
|
|
||||||
for (Coordinate coordinate : coords) {
|
for (Coordinate coordinate : coords) {
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ public class Coordinate {
|
|||||||
|
|
||||||
public static String createTableStatement = """
|
public static String createTableStatement = """
|
||||||
CREATE TABLE IF NOT EXISTS coordinates (
|
CREATE TABLE IF NOT EXISTS coordinates (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
description VARCHAR(255),
|
description VARCHAR(255),
|
||||||
x INT NOT NULL,
|
x INT NOT NULL,
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package nl.interestingcorner.db;
|
package nl.interestingcorner.db;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.bukkit.Material;
|
||||||
|
|
||||||
public enum MinecraftColor {
|
public enum MinecraftColor {
|
||||||
DARK_RED("dark_red"),
|
DARK_RED("dark_red"),
|
||||||
RED("red"),
|
RED("red"),
|
||||||
@@ -20,6 +24,44 @@ public enum MinecraftColor {
|
|||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
private static final HashMap<MinecraftColor, Material> materialsMap = new HashMap<>() {{
|
||||||
|
put(DARK_RED, Material.RED_WOOL);
|
||||||
|
put(RED, Material.RED_WOOL);
|
||||||
|
put(BLACK, Material.BLACK_WOOL);
|
||||||
|
put(BLUE, Material.BLUE_WOOL);
|
||||||
|
put(AQUA, Material.LIGHT_BLUE_WOOL);
|
||||||
|
put(DARK_AQUA, Material.CYAN_WOOL);
|
||||||
|
put(GREEN, Material.GREEN_WOOL);
|
||||||
|
put(GOLD, Material.YELLOW_WOOL);
|
||||||
|
put(DARK_PURPLE, Material.PURPLE_WOOL);
|
||||||
|
put(LIGHT_PURPLE, Material.MAGENTA_WOOL);
|
||||||
|
put(YELLOW, Material.YELLOW_WOOL);
|
||||||
|
put(DARK_GREEN, Material.LIME_WOOL);
|
||||||
|
put(GRAY, Material.LIGHT_GRAY_WOOL);
|
||||||
|
put(DARK_GRAY, Material.GRAY_WOOL);
|
||||||
|
put(WHITE, Material.WHITE_WOOL);
|
||||||
|
put(DARK_BLUE, Material.BLUE_WOOL);
|
||||||
|
}};
|
||||||
|
|
||||||
|
private static final HashMap<MinecraftColor, String> colorCodesMap = new HashMap<>() {{
|
||||||
|
put(DARK_RED, "§4");
|
||||||
|
put(RED, "§c");
|
||||||
|
put(BLACK, "§0");
|
||||||
|
put(BLUE, "§9");
|
||||||
|
put(AQUA, "§b");
|
||||||
|
put(DARK_AQUA, "§3");
|
||||||
|
put(GREEN, "§a");
|
||||||
|
put(GOLD, "§6");
|
||||||
|
put(DARK_PURPLE, "§5");
|
||||||
|
put(LIGHT_PURPLE, "§d");
|
||||||
|
put(YELLOW, "§e");
|
||||||
|
put(DARK_GREEN, "§2");
|
||||||
|
put(GRAY, "§7");
|
||||||
|
put(DARK_GRAY, "§8");
|
||||||
|
put(WHITE, "§f");
|
||||||
|
put(DARK_BLUE, "§1");
|
||||||
|
}};
|
||||||
|
|
||||||
MinecraftColor(String name) {
|
MinecraftColor(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
@@ -72,4 +114,22 @@ public enum MinecraftColor {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts this MinecraftColor to a corresponding Material.
|
||||||
|
*
|
||||||
|
* @return the Material corresponding to this MinecraftColor
|
||||||
|
*/
|
||||||
|
public Material toMaterial() {
|
||||||
|
return materialsMap.get(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts this MinecraftColor to a corresponding Minecraft color code.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String toColorCode() {
|
||||||
|
return colorCodesMap.get(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,22 +10,59 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
import nl.interestingcorner.db.Coordinate;
|
import nl.interestingcorner.db.Coordinate;
|
||||||
|
import nl.interestingcorner.db.MinecraftColor;
|
||||||
|
|
||||||
public class CoordinatesGUI {
|
public class CoordinatesGUI {
|
||||||
public static void open(Player player, List<Coordinate> coordinates) {
|
public static void open(Player player, List<Coordinate> coordinates) {
|
||||||
|
|
||||||
|
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
|
// Create an inventory with 9 slots and a title
|
||||||
Inventory gui = Bukkit.createInventory(null, 9, "Coordinates Menu");
|
Inventory gui = Bukkit.createInventory(null, size, "Coordinates Menu");
|
||||||
|
|
||||||
// Create an example item
|
for (Coordinate coordinate : coordinates) {
|
||||||
ItemStack addItem = new ItemStack(Material.GREEN_WOOL);
|
ItemStack item = createCoordinateItem(coordinate);
|
||||||
ItemMeta meta = addItem.getItemMeta();
|
gui.addItem(item);
|
||||||
meta.setDisplayName(coordinates.get(0).name);
|
}
|
||||||
addItem.setItemMeta(meta);
|
|
||||||
|
|
||||||
// Place it in slot 0
|
|
||||||
gui.setItem(0, addItem);
|
|
||||||
|
|
||||||
// Open the GUI for the player
|
// Open the GUI for the player
|
||||||
player.openInventory(gui);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
main: nl.interestingcorner.App
|
main: nl.interestingcorner.App
|
||||||
name: InterestingCornerPlugin
|
name: InterestingCornerPlugin
|
||||||
version: 0.1
|
version: 0.1
|
||||||
|
api-version: 1.21
|
||||||
|
|
||||||
commands:
|
commands:
|
||||||
ic-coords:
|
ic-coords:
|
||||||
|
|||||||
Reference in New Issue
Block a user