From da7924881ab14e4580ae6c56dbdf8346dbf2a267 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 12 Nov 2022 13:39:07 -0500 Subject: first commit for new version --- src/main/java/net/benjidial/nswp/Database.java | 157 +++++++++++++++++++++ src/main/java/net/benjidial/nswp/Plugin.java | 34 +++++ src/main/java/net/benjidial/nswp/Waypoint.java | 13 ++ .../benjidial/nswp/commands/DeleteWaypoint.java | 20 +++ .../net/benjidial/nswp/commands/ListWaypoints.java | 39 +++++ .../net/benjidial/nswp/commands/SaveWaypoint.java | 22 +++ .../nswp/commands/TeleportToWaypoint.java | 27 ++++ .../net/benjidial/nswp/commands/WaypointBack.java | 27 ++++ .../benjidial/nswp/commands/WaypointCommand.java | 71 ++++++++++ 9 files changed, 410 insertions(+) create mode 100644 src/main/java/net/benjidial/nswp/Database.java create mode 100644 src/main/java/net/benjidial/nswp/Plugin.java create mode 100644 src/main/java/net/benjidial/nswp/Waypoint.java create mode 100644 src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java create mode 100644 src/main/java/net/benjidial/nswp/commands/ListWaypoints.java create mode 100644 src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java create mode 100644 src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java create mode 100644 src/main/java/net/benjidial/nswp/commands/WaypointBack.java create mode 100644 src/main/java/net/benjidial/nswp/commands/WaypointCommand.java (limited to 'src/main/java') diff --git a/src/main/java/net/benjidial/nswp/Database.java b/src/main/java/net/benjidial/nswp/Database.java new file mode 100644 index 0000000..21f52f5 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/Database.java @@ -0,0 +1,157 @@ +package net.benjidial.nswp; + +import org.bukkit.entity.Player; +import org.bukkit.Location; +import org.bukkit.Bukkit; + +import java.sql.PreparedStatement; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.ArrayList; +import java.sql.Connection; +import java.sql.ResultSet; + +public class Database { + static Connection connection; + + public static void connect(String path) throws SQLException { + connection = DriverManager.getConnection("jdbc:sqlite:" + path); + } + + static String playerID(Player player) { + return player.getUniqueId().toString().replaceAll("-", ""); + } + + static String waypointTableName(Player player) { + return "waypoints_" + playerID(player); + } + + public static void createWaypointTable(Player player) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "CREATE TABLE IF NOT EXISTS " + waypointTableName(player) + " (" + + "name TEXT PRIMARY KEY, " + + "world TEXT NOT NULL, " + + "x REAL NOT NULL, " + + "y REAL NOT NULL, " + + "z REAL NOT NULL, " + + "pitch REAL NOT NULL, " + + "yaw REAL NOT NULL" + + ")" + ); + statement.executeUpdate(); + } + + static void createWBacks() throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "CREATE TABLE IF NOT EXISTS wbacks (" + + "player_uuid TEXT PRIMARY KEY, " + + "world TEXT NOT NULL, " + + "x REAL NOT NULL, " + + "y REAL NOT NULL, " + + "z REAL NOT NULL, " + + "pitch REAL NOT NULL, " + + "yaw REAL NOT NULL" + + ")" + ); + statement.executeUpdate(); + } + + public static Location getWBack(Player player) throws SQLException { + createWBacks(); + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM wbacks WHERE player_uuid = ?" + ); + statement.setString(1, playerID(player)); + ResultSet results = statement.executeQuery(); + if (!results.next()) + return null; + return cursorToLocation(results); + } + + public static void setWBack(Player player, Location location) throws SQLException { + createWBacks(); + PreparedStatement statement = connection.prepareStatement( + "REPLACE INTO wbacks(player_uuid, world, x, y, z, pitch, yaw) VALUES(?, ?, ?, ?, ?, ?, ?)" + ); + statement.setString(1, playerID(player)); + statement.setString(2, location.getWorld().getName()); + statement.setDouble(3, location.getX()); + statement.setDouble(4, location.getY()); + statement.setDouble(5, location.getZ()); + statement.setDouble(6, location.getPitch()); + statement.setDouble(7, location.getYaw()); + statement.executeUpdate(); + } + + static Location cursorToLocation(ResultSet results) throws SQLException { + return new Location( + Bukkit.getWorld(results.getString("world")), + results.getDouble("x"), + results.getDouble("y"), + results.getDouble("z"), + (float)results.getDouble("yaw"), + (float)results.getDouble("pitch") + ); + } + + static Waypoint cursorToWaypoint(ResultSet results) throws SQLException { + return new Waypoint( + results.getString("name"), + cursorToLocation(results) + ); + } + + public static ArrayList searchWaypoints(Player player, String start) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + waypointTableName(player) + " WHERE name LIKE ? ESCAPE '!' ORDER BY world, name ASC" + ); + statement.setString(1, "%" + start.replace("!", "!!").replace("_", "!_") + "%"); + ResultSet results = statement.executeQuery(); + ArrayList list = new ArrayList(); + while (results.next()) + list.add(cursorToWaypoint(results)); + return list; + } + + public static Waypoint lookupWaypoint(Player player, String name) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM " + waypointTableName(player) + " WHERE name = ?" + ); + statement.setString(1, name); + ResultSet results = statement.executeQuery(); + if (!results.next()) + return null; + return cursorToWaypoint(results); + } + + public static void addWaypoint(Player player, Waypoint waypoint) throws SQLException { + if (lookupWaypoint(player, waypoint.name) != null) + player.sendMessage("There is already a waypoint with that name."); + else { + PreparedStatement statement = connection.prepareStatement( + "INSERT INTO " + waypointTableName(player) + "(name, world, x, y, z, pitch, yaw) " + + "VALUES(?, ?, ?, ?, ?, ?, ?)" + ); + statement.setString(1, waypoint.name); + statement.setString(2, waypoint.location.getWorld().getName()); + statement.setDouble(3, waypoint.location.getX()); + statement.setDouble(4, waypoint.location.getY()); + statement.setDouble(5, waypoint.location.getZ()); + statement.setDouble(6, waypoint.location.getPitch()); + statement.setDouble(7, waypoint.location.getYaw()); + statement.executeUpdate(); + player.sendMessage("Waypoint added."); + } + } + + public static void deleteWaypoint(Player player, String name) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "DELETE FROM " + waypointTableName(player) + " WHERE name = ?" + ); + statement.setString(1, name); + if (statement.executeUpdate() == 0) + player.sendMessage("No waypoint with that name."); + else + player.sendMessage("Waypoint deleted."); + } +} diff --git a/src/main/java/net/benjidial/nswp/Plugin.java b/src/main/java/net/benjidial/nswp/Plugin.java new file mode 100644 index 0000000..9a46cb6 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/Plugin.java @@ -0,0 +1,34 @@ +package net.benjidial.nswp; + +import net.benjidial.nswp.commands.*; + +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.logging.Logger; +import java.util.logging.Level; +import java.sql.SQLException; +import java.io.File; + +public class Plugin extends JavaPlugin { + public static Logger logger; + + public void onEnable() { + logger = getLogger(); + getDataFolder().mkdirs(); + + try { + Database.connect(new File(getDataFolder(), "database.db").getPath()); + } + catch (SQLException ex) { + logger.log(Level.SEVERE, "Database error: (not enabling plugin)"); + ex.printStackTrace(); + return; + } + + getCommand("wlist").setExecutor(new ListWaypoints()); + getCommand("wsave").setExecutor(new SaveWaypoint()); + getCommand("wtp" ).setExecutor(new TeleportToWaypoint()); + getCommand("wdel" ).setExecutor(new DeleteWaypoint()); + getCommand("wback").setExecutor(new WaypointBack()); + } +} diff --git a/src/main/java/net/benjidial/nswp/Waypoint.java b/src/main/java/net/benjidial/nswp/Waypoint.java new file mode 100644 index 0000000..ec3daf6 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/Waypoint.java @@ -0,0 +1,13 @@ +package net.benjidial.nswp; + +import org.bukkit.Location; + +public class Waypoint { + public Waypoint(String name, Location location) { + this.name = name; + this.location = location; + } + + public String name; + public Location location; +} diff --git a/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java new file mode 100644 index 0000000..d24d133 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/DeleteWaypoint.java @@ -0,0 +1,20 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; + +import org.bukkit.entity.Player; + +import java.sql.SQLException; + +public class DeleteWaypoint extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.Waypoint; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 1) + return false; + Database.deleteWaypoint(player, args[0]); + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java new file mode 100644 index 0000000..b6b61a3 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/ListWaypoints.java @@ -0,0 +1,39 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; + +import org.bukkit.entity.Player; +import org.bukkit.World; + +import java.sql.SQLException; +import java.util.ArrayList; + +public class ListWaypoints extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.Waypoint; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length > 1) + return false; + ArrayList waypoints = Database.searchWaypoints(player, args.length == 0 ? "" : args[0]); + if (waypoints.size() == 0) { + player.sendMessage("No waypoints found."); + return true; + } + if (waypoints.size() == 1) + player.sendMessage("1 waypoint found:"); + else + player.sendMessage(waypoints.size() + " waypoints found:"); + World lastWorld = null; + for (Waypoint waypoint : waypoints) { + if (waypoint.location.getWorld() != lastWorld) { + lastWorld = waypoint.location.getWorld(); + player.sendMessage(" " + lastWorld.getName() + ": "); + } + player.sendMessage(" " + waypoint.name); + } + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java new file mode 100644 index 0000000..f0b9dcd --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/SaveWaypoint.java @@ -0,0 +1,22 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; + +import org.bukkit.entity.Player; + +import java.sql.SQLException; + +public class SaveWaypoint extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.None; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 1) + return false; + Waypoint waypoint = new Waypoint(args[0], player.getLocation()); + Database.addWaypoint(player, waypoint); + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java new file mode 100644 index 0000000..4e84e3c --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/TeleportToWaypoint.java @@ -0,0 +1,27 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; + +import org.bukkit.entity.Player; + +import java.sql.SQLException; + +public class TeleportToWaypoint extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.Waypoint; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 1) + return false; + Waypoint waypoint = Database.lookupWaypoint(player, args[0]); + if (waypoint == null) + player.sendMessage("No waypoint with that name."); + else { + Database.setWBack(player, player.getLocation()); + player.teleport(waypoint.location); + } + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointBack.java b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java new file mode 100644 index 0000000..3fa0de3 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/WaypointBack.java @@ -0,0 +1,27 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; + +import org.bukkit.entity.Player; +import org.bukkit.Location; + +import java.sql.SQLException; + +public class WaypointBack extends WaypointCommand { + public CompletionType getCompletionType() { + return CompletionType.None; + } + + public boolean body(Player player, String[] args) throws SQLException { + if (args.length != 0) + return false; + Location wback = Database.getWBack(player); + if (wback == null) + player.sendMessage("You have not teleported to any waypoints."); + else { + Database.setWBack(player, player.getLocation()); + player.teleport(wback); + } + return true; + } +} diff --git a/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java b/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java new file mode 100644 index 0000000..618ab74 --- /dev/null +++ b/src/main/java/net/benjidial/nswp/commands/WaypointCommand.java @@ -0,0 +1,71 @@ +package net.benjidial.nswp.commands; + +import net.benjidial.nswp.Database; +import net.benjidial.nswp.Waypoint; +import net.benjidial.nswp.Plugin; + +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +import java.util.logging.Level; +import java.sql.SQLException; +import java.util.ArrayList; + +public abstract class WaypointCommand implements TabExecutor { + enum CompletionType { + None, Waypoint + } + + public abstract CompletionType getCompletionType(); + + public ArrayList onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("Only players may use waypoints."); + return new ArrayList<>(); + } + Player player = (Player)sender; + + switch (getCompletionType()) { + case Waypoint: + if (args.length > 1) + return new ArrayList<>(); + try { + Database.createWaypointTable(player); + ArrayList results = new ArrayList<>(); + for (Waypoint waypoint : Database.searchWaypoints(player, args.length == 0 ? "" : args[0])) + results.add(waypoint.name); + return results; + } + catch (SQLException ex) { + Plugin.logger.log(Level.SEVERE, "Database error:"); + ex.printStackTrace(); + sender.sendMessage("Database error."); + return new ArrayList<>(); + } + default: + return new ArrayList<>(); + } + } + + public abstract boolean body(Player player, String[] args) throws SQLException; + + public boolean onCommand(CommandSender sender, Command command, String name, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("Only players may use waypoints."); + return true; + } + try { + Player player = (Player)sender; + Database.createWaypointTable(player); + return body(player, args); + } + catch (SQLException ex) { + Plugin.logger.log(Level.SEVERE, "Database error:"); + ex.printStackTrace(); + sender.sendMessage("Database error."); + return true; + } + } +} -- cgit v1.2.3