Add basic GUI and getting coordinates by world
This commit is contained in:
@@ -6,6 +6,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
|
|
||||||
import nl.interestingcorner.commands.CoordinatesCommand;
|
import nl.interestingcorner.commands.CoordinatesCommand;
|
||||||
import nl.interestingcorner.db.DatabaseManager;
|
import nl.interestingcorner.db.DatabaseManager;
|
||||||
|
import nl.interestingcorner.gui.CoordinatesGUIListener;
|
||||||
|
|
||||||
public class App extends JavaPlugin {
|
public class App extends JavaPlugin {
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ public class App extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
getLogger().info("Successfully initialized database");
|
getLogger().info("Successfully initialized database");
|
||||||
|
|
||||||
|
getServer().getPluginManager().registerEvents(new CoordinatesGUIListener(), this);
|
||||||
registerCommands();
|
registerCommands();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,23 @@ import org.bukkit.entity.Player;
|
|||||||
|
|
||||||
import nl.interestingcorner.db.Coordinate;
|
import nl.interestingcorner.db.Coordinate;
|
||||||
import nl.interestingcorner.db.DatabaseManager;
|
import nl.interestingcorner.db.DatabaseManager;
|
||||||
|
import nl.interestingcorner.gui.CoordinatesGUI;
|
||||||
|
|
||||||
public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handleCommand(CommandSender sender, String[] args) {
|
public boolean handleCommand(CommandSender sender, String[] args) {
|
||||||
if (sender instanceof Player player) {
|
if (sender instanceof Player player) {
|
||||||
List<Coordinate> coords = DatabaseManager.INSTANCE.getAllCoordinates();
|
List<Coordinate> coords;
|
||||||
|
if (args.length < 2) {
|
||||||
|
coords = DatabaseManager.INSTANCE.getAllCoordinates();
|
||||||
|
} else if (args[1].equalsIgnoreCase("world")) {
|
||||||
|
String world = player.getWorld().getName();
|
||||||
|
coords = DatabaseManager.INSTANCE.getAllCoordinates(world);
|
||||||
|
} else {
|
||||||
|
sender.sendMessage("Invalid argument: " + args[1]);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
StringBuilder res = new StringBuilder("Coordinates: ");
|
StringBuilder res = new StringBuilder("Coordinates: ");
|
||||||
|
|
||||||
for (Coordinate coordinate : coords) {
|
for (Coordinate coordinate : coords) {
|
||||||
@@ -21,6 +31,8 @@ public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
player.sendMessage(res.toString());
|
player.sendMessage(res.toString());
|
||||||
|
|
||||||
|
CoordinatesGUI.open(player, coords);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,41 +73,65 @@ public enum DatabaseManager {
|
|||||||
try {
|
try {
|
||||||
PreparedStatement getAllCoordinatesStatement = this.connection
|
PreparedStatement getAllCoordinatesStatement = this.connection
|
||||||
.prepareStatement("SELECT * FROM coordinates");
|
.prepareStatement("SELECT * FROM coordinates");
|
||||||
ResultSet allCoordinates = getAllCoordinatesStatement.executeQuery();
|
|
||||||
|
|
||||||
while (allCoordinates.next()) {
|
result = convertResultsToCoordinates(getAllCoordinatesStatement.executeQuery());
|
||||||
|
|
||||||
MinecraftColor color = MinecraftColor.fromString(allCoordinates.getString("color"));
|
} catch (SQLException e) {
|
||||||
|
this.app.getLogger().log(Level.SEVERE, "Could not get coordinates! {0}", e.getMessage());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Coordinate> getAllCoordinates(String world) {
|
||||||
|
List<Coordinate> result = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
PreparedStatement getAllCoordinatesStatement = this.connection
|
||||||
|
.prepareStatement("SELECT * FROM coordinates WHERE world = ?");
|
||||||
|
getAllCoordinatesStatement.setString(1, world);
|
||||||
|
|
||||||
|
result = convertResultsToCoordinates(getAllCoordinatesStatement.executeQuery());
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
this.app.getLogger().log(Level.SEVERE, "Could not get coordinates for world {0}! {1}",
|
||||||
|
new Object[] { world, e.getMessage() });
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Coordinate> convertResultsToCoordinates(ResultSet results) throws SQLException {
|
||||||
|
List<Coordinate> coordinates = new ArrayList<>();
|
||||||
|
while (results.next()) {
|
||||||
|
|
||||||
|
MinecraftColor color = MinecraftColor.fromString(results.getString("color"));
|
||||||
if (color == null) {
|
if (color == null) {
|
||||||
color = MinecraftColor.WHITE;
|
color = MinecraftColor.WHITE;
|
||||||
}
|
}
|
||||||
|
|
||||||
Coordinate c = new Coordinate(
|
Coordinate c = new Coordinate(
|
||||||
allCoordinates.getInt("id"),
|
results.getInt("id"),
|
||||||
allCoordinates.getString("name"),
|
results.getString("name"),
|
||||||
allCoordinates.getString("description"),
|
results.getString("description"),
|
||||||
new Coordinate.Position(
|
new Coordinate.Position(
|
||||||
allCoordinates.getInt("x"),
|
results.getInt("x"),
|
||||||
allCoordinates.getInt("y"),
|
results.getInt("y"),
|
||||||
allCoordinates.getInt("z")),
|
results.getInt("z")),
|
||||||
allCoordinates.getBoolean("nether"),
|
results.getBoolean("nether"),
|
||||||
allCoordinates.getString("world"),
|
results.getString("world"),
|
||||||
color);
|
color);
|
||||||
|
|
||||||
result.add(c);
|
coordinates.add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException e) {
|
return coordinates;
|
||||||
this.app.getLogger().log(Level.SEVERE, "Could not get commands! {0}", e.getMessage());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new coordinate to the database with the given parameters.
|
* Adds a new coordinate to the database with the given parameters.
|
||||||
|
*
|
||||||
* @param name the name of the coordinate
|
* @param name the name of the coordinate
|
||||||
* @param description a short description of the coordinate
|
* @param description a short description of the coordinate
|
||||||
* @param position the position of the coordinate where the player will spwan when they teleport to the coordinate
|
* @param position the position of the coordinate where the player will
|
||||||
|
* spwan when they teleport to the coordinate
|
||||||
* @param nether if the coordinate is in the nether
|
* @param nether if the coordinate is in the nether
|
||||||
* @param world the Multiverse world the coordinate belongs to
|
* @param world the Multiverse world the coordinate belongs to
|
||||||
* @param color the color to display for the coordinate name
|
* @param color the color to display for the coordinate name
|
||||||
@@ -136,8 +160,8 @@ public enum DatabaseManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the connectino to the database. Creates a new .db file if it doesn't
|
* Sets up the connectino to the database. Creates a new .db file if it
|
||||||
* exist yet.
|
* doesn't exist yet.
|
||||||
*
|
*
|
||||||
* @return true if the connection was set up successfully. False if not.
|
* @return true if the connection was set up successfully. False if not.
|
||||||
*/
|
*/
|
||||||
@@ -173,7 +197,7 @@ public enum DatabaseManager {
|
|||||||
try {
|
try {
|
||||||
PreparedStatement createTableStatement = this.connection.prepareStatement(Coordinate.createTableStatement);
|
PreparedStatement createTableStatement = this.connection.prepareStatement(Coordinate.createTableStatement);
|
||||||
createTableStatement.executeUpdate();
|
createTableStatement.executeUpdate();
|
||||||
this.app.getLogger().info("Executed create table statement");
|
this.app.getLogger().fine("Executed create table statement");
|
||||||
|
|
||||||
PreparedStatement getTablesStatement = this.connection.prepareStatement(
|
PreparedStatement getTablesStatement = this.connection.prepareStatement(
|
||||||
"SELECT name FROM sqlite_master WHERE type='table'");
|
"SELECT name FROM sqlite_master WHERE type='table'");
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package nl.interestingcorner.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.db.Coordinate;
|
||||||
|
|
||||||
|
public class CoordinatesGUI {
|
||||||
|
public static void open(Player player, List<Coordinate> coordinates) {
|
||||||
|
// Create an inventory with 9 slots and a title
|
||||||
|
Inventory gui = Bukkit.createInventory(null, 9, "Coordinates Menu");
|
||||||
|
|
||||||
|
// Create an example item
|
||||||
|
ItemStack addItem = new ItemStack(Material.GREEN_WOOL);
|
||||||
|
ItemMeta meta = addItem.getItemMeta();
|
||||||
|
meta.setDisplayName(coordinates.get(0).name);
|
||||||
|
addItem.setItemMeta(meta);
|
||||||
|
|
||||||
|
// Place it in slot 0
|
||||||
|
gui.setItem(0, addItem);
|
||||||
|
|
||||||
|
// Open the GUI for the player
|
||||||
|
player.openInventory(gui);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package nl.interestingcorner.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