Add adding coordinate
This commit is contained in:
@@ -4,7 +4,7 @@ import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import nl.interestingcorner.commands.GetCoordinatesCommand;
|
||||
import nl.interestingcorner.commands.CoordinatesCommand;
|
||||
import nl.interestingcorner.db.DatabaseManager;
|
||||
|
||||
public class App extends JavaPlugin {
|
||||
@@ -18,6 +18,7 @@ public class App extends JavaPlugin {
|
||||
setEnabled(false);
|
||||
return;
|
||||
}
|
||||
getLogger().info("Successfully initialized database");
|
||||
|
||||
registerCommands();
|
||||
|
||||
@@ -33,7 +34,7 @@ public class App extends JavaPlugin {
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
registerSimgleCommand("get_coords", new GetCoordinatesCommand());
|
||||
registerSimgleCommand("ic-coords", new CoordinatesCommand());
|
||||
}
|
||||
|
||||
private void registerSimgleCommand(String name, CommandExecutor executor)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package nl.interestingcorner.commands;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import nl.interestingcorner.db.Coordinate;
|
||||
import nl.interestingcorner.db.DatabaseManager;
|
||||
import nl.interestingcorner.db.MinecraftColor;
|
||||
|
||||
public class AddCoordinateCommandHandler implements CoordinatesCommandHandler {
|
||||
|
||||
/**
|
||||
* Minimum arguments needed for adding a command: name, description, color
|
||||
*/
|
||||
private final int MIN_ARGS = 3;
|
||||
|
||||
@Override
|
||||
public boolean handleCommand(CommandSender sender, String[] args) {
|
||||
if (args.length < MIN_ARGS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sender instanceof Player player) {
|
||||
Location playerLocation = player.getLocation();
|
||||
if (playerLocation == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
World playerWorld = playerLocation.getWorld();
|
||||
if (playerWorld == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return DatabaseManager.INSTANCE
|
||||
.addCoordinate(args[0], // name
|
||||
args[1], // description
|
||||
new Coordinate.Position(playerLocation.getBlockX(), playerLocation.getBlockY(),
|
||||
playerLocation.getBlockZ()), // position
|
||||
playerWorld.getEnvironment().equals(World.Environment.NETHER), // nether
|
||||
playerWorld.getName(), // world
|
||||
MinecraftColor.fromString(args[2]) // color
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package nl.interestingcorner.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class CoordinatesCommand implements CommandExecutor{
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
return switch (label) {
|
||||
case "get" -> new GetCoordinatesCommandHandler().handleCommand(sender, args);
|
||||
case "add" -> new AddCoordinateCommandHandler().handleCommand(sender, args);
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package nl.interestingcorner.commands;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public interface CoordinatesCommandHandler {
|
||||
boolean handleCommand(CommandSender sender, String[] args);
|
||||
}
|
||||
@@ -1,22 +1,18 @@
|
||||
package nl.interestingcorner.commands;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import nl.interestingcorner.db.Coordinate;
|
||||
import nl.interestingcorner.db.DatabaseManager;
|
||||
|
||||
public class GetCoordinatesCommand implements CommandExecutor{
|
||||
public class GetCoordinatesCommandHandler implements CoordinatesCommandHandler {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (sender instanceof Player player)
|
||||
{
|
||||
public boolean handleCommand(CommandSender sender, String[] args) {
|
||||
if (sender instanceof Player player) {
|
||||
List<Coordinate> coords = DatabaseManager.INSTANCE.getAllCoordinates();
|
||||
StringBuilder res = new StringBuilder("Coordinates: ");
|
||||
|
||||
@@ -27,8 +23,6 @@ public class GetCoordinatesCommand implements CommandExecutor{
|
||||
player.sendMessage(res.toString());
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,10 @@ package nl.interestingcorner.db;
|
||||
* +-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Class to represent a coordinate to teleport to
|
||||
*/
|
||||
public class Coordinate {
|
||||
/**
|
||||
* auto-generated ID
|
||||
@@ -54,6 +58,22 @@ public class Coordinate {
|
||||
*/
|
||||
public String world;
|
||||
|
||||
public Coordinate(int id, String name, String description, Position position, boolean nether, String world) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.position = position;
|
||||
this.nether = nether;
|
||||
this.world = world;
|
||||
this.color = MinecraftColor.WHITE;
|
||||
}
|
||||
|
||||
public Coordinate(int id, String name, String description, Position position, boolean nether, String world,
|
||||
MinecraftColor color) {
|
||||
this(id, name, description, position, nether, world);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public record Position(int x, int y, int z) {
|
||||
}
|
||||
|
||||
|
||||
@@ -76,24 +76,23 @@ public enum DatabaseManager {
|
||||
ResultSet allCoordinates = getAllCoordinatesStatement.executeQuery();
|
||||
|
||||
while (allCoordinates.next()) {
|
||||
Coordinate c = new Coordinate();
|
||||
|
||||
c.id = allCoordinates.getInt("id");
|
||||
c.name = allCoordinates.getString("name");
|
||||
c.description = allCoordinates.getString("description");
|
||||
c.position = new Coordinate.Position(
|
||||
allCoordinates.getInt("x"),
|
||||
allCoordinates.getInt("y"),
|
||||
allCoordinates.getInt("z"));
|
||||
c.nether = allCoordinates.getBoolean("nether");
|
||||
|
||||
MinecraftColor color = MinecraftColor.fromString(allCoordinates.getString("color"));
|
||||
if (color == null) {
|
||||
color = MinecraftColor.WHITE;
|
||||
}
|
||||
c.color = color;
|
||||
|
||||
c.world = allCoordinates.getString("world");
|
||||
Coordinate c = new Coordinate(
|
||||
allCoordinates.getInt("id"),
|
||||
allCoordinates.getString("name"),
|
||||
allCoordinates.getString("description"),
|
||||
new Coordinate.Position(
|
||||
allCoordinates.getInt("x"),
|
||||
allCoordinates.getInt("y"),
|
||||
allCoordinates.getInt("z")),
|
||||
allCoordinates.getBoolean("nether"),
|
||||
allCoordinates.getString("world"),
|
||||
color);
|
||||
|
||||
result.add(c);
|
||||
}
|
||||
@@ -104,6 +103,38 @@ public enum DatabaseManager {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new coordinate to the database with the given parameters.
|
||||
* @param name the name 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 nether if the coordinate is in the nether
|
||||
* @param world the Multiverse world the coordinate belongs to
|
||||
* @param color the color to display for the coordinate name
|
||||
* @return true if the command was added successfully, false otherwise
|
||||
*/
|
||||
public boolean addCoordinate(String name, String description, Coordinate.Position position, boolean nether,
|
||||
String world, MinecraftColor color) {
|
||||
String sql = "INSERT INTO coordinates (name, description, x, y, z, nether, color, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
try {
|
||||
PreparedStatement addCoordinateStatement = this.connection.prepareStatement(sql);
|
||||
addCoordinateStatement.setString(1, name);
|
||||
addCoordinateStatement.setString(2, description);
|
||||
addCoordinateStatement.setInt(3, position.x());
|
||||
addCoordinateStatement.setInt(4, position.y());
|
||||
addCoordinateStatement.setInt(5, position.z());
|
||||
addCoordinateStatement.setBoolean(6, nether);
|
||||
addCoordinateStatement.setString(7, color.name());
|
||||
addCoordinateStatement.setString(8, world);
|
||||
|
||||
addCoordinateStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
this.app.getLogger().log(Level.SEVERE, "Error adding coordinate to database: {0}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the connectino to the database. Creates a new .db file if it doesn't
|
||||
* exist yet.
|
||||
@@ -142,12 +173,15 @@ public enum DatabaseManager {
|
||||
try {
|
||||
PreparedStatement createTableStatement = this.connection.prepareStatement(Coordinate.createTableStatement);
|
||||
createTableStatement.executeUpdate();
|
||||
this.app.getLogger().fine("Executed create table statement");
|
||||
this.app.getLogger().info("Executed create table statement");
|
||||
|
||||
PreparedStatement getTablesStatement = this.connection.prepareStatement("show tables");
|
||||
PreparedStatement getTablesStatement = this.connection.prepareStatement(
|
||||
"SELECT name FROM sqlite_master WHERE type='table'");
|
||||
ResultSet results = getTablesStatement.executeQuery();
|
||||
|
||||
this.app.getLogger().info(results.getString(1));
|
||||
while (results.next()) {
|
||||
this.app.getLogger().log(Level.INFO, "Found table: {0}", results.getString("name"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
this.app.getLogger().log(Level.SEVERE, "Could not create table: {0}", e.getMessage());
|
||||
return false;
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
main: nl.interestingcorner.App
|
||||
name: InterestingCornerPlugin
|
||||
version: 0.1
|
||||
version: 0.1
|
||||
|
||||
commands:
|
||||
ic-coords:
|
||||
description: Main command for the coordinates
|
||||
usage: /ic-coords get|add
|
||||
Reference in New Issue
Block a user