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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user