diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/net/benjidial/nswp/Database.java | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/src/main/java/net/benjidial/nswp/Database.java b/src/main/java/net/benjidial/nswp/Database.java index f123bdb..84d64f5 100644 --- a/src/main/java/net/benjidial/nswp/Database.java +++ b/src/main/java/net/benjidial/nswp/Database.java @@ -2,6 +2,7 @@ package net.benjidial.nswp; import org.bukkit.entity.Player; import org.bukkit.Location; +import org.bukkit.OfflinePlayer; import org.bukkit.Bukkit; import java.sql.PreparedStatement; @@ -23,6 +24,14 @@ public class Database { return player.getUniqueId().toString().replaceAll("-", ""); } + static OfflinePlayer idToPlayer(String id) { + return Bukkit.getOfflinePlayer(UUID.fromString( + id.substring( 0, 8) + "-" + id.substring( 8, 12) + "-" + + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + + id.substring(20, 32) + )); + } + static String waypointTableName(Player player) { return "waypoints_" + playerID(player); } @@ -124,23 +133,18 @@ public class Database { } 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."); - } + 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(); } public static void deleteWaypoint(Player player, String name) throws SQLException { @@ -148,17 +152,14 @@ public class Database { "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."); + statement.executeUpdate(); } public static void createTPATable() throws SQLException { PreparedStatement statement = connection.prepareStatement( "CREATE TABLE IF NOT EXISTS tpa_allowed (" + - "from TEXT NOT NULL, " + - "to TEXT NOT NULL" + + "from_id TEXT NOT NULL, " + + "to_id TEXT NOT NULL" + ")" ); statement.executeUpdate(); @@ -166,7 +167,7 @@ public class Database { public static boolean isTPAAllowed(Player from, Player to) throws SQLException { PreparedStatement statement = connection.prepareStatement( - "SELECT * FROM tpa_allowed WHERE from = ? AND to = ?" + "SELECT * FROM tpa_allowed WHERE from_id = ? AND to_id = ?" ); statement.setString(1, playerID(from)); statement.setString(2, playerID(to)); @@ -176,7 +177,7 @@ public class Database { public static void allowTPA(Player from, Player to) throws SQLException { PreparedStatement statement = connection.prepareStatement( - "INSERT INTO tpa_allowed(from, to) VALUES(?, ?)" + "INSERT INTO tpa_allowed(from_id, to_id) VALUES(?, ?)" ); statement.setString(1, playerID(from)); statement.setString(2, playerID(to)); @@ -187,7 +188,7 @@ public class Database { //this is kinda scrungled but i think it should work. PreparedStatement statement = connection.prepareStatement( - "SELECT * FROM tpa_allowed WHERE to = ?" + "SELECT * FROM tpa_allowed WHERE to_id = ?" ); statement.setString(1, playerID(to)); ResultSet table = statement.executeQuery(); @@ -195,11 +196,11 @@ public class Database { int removed = 0; while (table.next()) { - String fromID = table.getString("from"); + String fromID = table.getString("from_id"); - if (Bukkit.getOfflinePlayer(UUID.fromString(fromID)).getName() == fromName) { + if (idToPlayer(fromID).getName().equals(fromName)) { PreparedStatement deleteStatement = connection.prepareStatement( - "DELETE FROM tpa_allowed WHERE from = ? AND to = ?" + "DELETE FROM tpa_allowed WHERE from_id = ? AND to_id = ?" ); deleteStatement.setString(1, fromID); deleteStatement.setString(2, playerID(to)); @@ -225,14 +226,14 @@ public class Database { public static ArrayList<String> getTPAAllowedTo(Player to) throws SQLException { PreparedStatement statement = connection.prepareStatement( - "SELECT * FROM tpa_allowed WHERE to = ?" + "SELECT * FROM tpa_allowed WHERE to_id = ?" ); statement.setString(1, playerID(to)); ResultSet table = statement.executeQuery(); ArrayList<String> results = new ArrayList<>(); while (table.next()) { - String name = Bukkit.getOfflinePlayer(UUID.fromString(table.getString("from"))).getName(); + String name = idToPlayer(table.getString("from_id")).getName(); //there is no way to disallow a player whose name is not know. but, to allow a player, //they have to be online, and so their name should be known. if (name != null) |