Add teleporting player
This commit is contained in:
@@ -32,18 +32,16 @@ public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
||||
}
|
||||
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
sender.sendMessage("An error occurred while getting coordinates: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -13,8 +13,6 @@ 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 = new ArrayList<>();
|
||||
|
||||
for (Coordinate coord : coords) {
|
||||
@@ -25,12 +23,17 @@ public class CoordinatesGUI {
|
||||
for (int i = 0; i < 90; i++) {
|
||||
ItemStack item = new ItemStack(Material.AMETHYST_BLOCK);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
meta.setDisplayName("Test Item " + (i + 1));
|
||||
item.setItemMeta(meta);
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
GUI gui = new GUI("Coordinates", GUI.DEFAULT_PAGE_SIZE);
|
||||
gui.setItemClickListener(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.closeInventory();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -5,11 +5,12 @@ import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
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;
|
||||
|
||||
public class GUI implements InventoryHolder {
|
||||
public 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)
|
||||
|
||||
@@ -60,12 +61,17 @@ public void onInventoryClick(InventoryClickEvent event) {
|
||||
/**
|
||||
* Title of the GUI
|
||||
*/
|
||||
private String title;
|
||||
private final String title;
|
||||
|
||||
/**
|
||||
* Number of items per page
|
||||
*/
|
||||
private int pageSize;
|
||||
private final int pageSize;
|
||||
|
||||
/**
|
||||
* Listener for item click events
|
||||
*/
|
||||
private GUIItemClickListener itemClickListener;
|
||||
|
||||
public GUI(String title, int pageSize) {
|
||||
this.title = title;
|
||||
@@ -81,6 +87,14 @@ public void onInventoryClick(InventoryClickEvent event) {
|
||||
updateInventory();
|
||||
}
|
||||
|
||||
public void setItemClickListener(GUIItemClickListener listener) {
|
||||
this.itemClickListener = listener;
|
||||
}
|
||||
|
||||
public GUIItemClickListener getItemClickListener() {
|
||||
return this.itemClickListener;
|
||||
}
|
||||
|
||||
public void addItem(ItemStack item) {
|
||||
this.items.add(item);
|
||||
updateInventory();
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
|
||||
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");
|
||||
GUIItemClickListener itemClickListener = gui.getItemClickListener();
|
||||
if (itemClickListener!= null) {
|
||||
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,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