From e06ee55259b3549a59358a13c095a274bacf6a05 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 30 Dec 2023 14:24:46 -0500 Subject: testing new version --- src/main/java/net/benjidial/nswp/Database.java | 107 ++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 4 deletions(-) (limited to 'src/main/java/net/benjidial/nswp/Database.java') diff --git a/src/main/java/net/benjidial/nswp/Database.java b/src/main/java/net/benjidial/nswp/Database.java index 21f52f5..f123bdb 100644 --- a/src/main/java/net/benjidial/nswp/Database.java +++ b/src/main/java/net/benjidial/nswp/Database.java @@ -10,6 +10,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.sql.Connection; import java.sql.ResultSet; +import java.util.UUID; public class Database { static Connection connection; @@ -57,7 +58,6 @@ public class Database { } public static Location getWBack(Player player) throws SQLException { - createWBacks(); PreparedStatement statement = connection.prepareStatement( "SELECT * FROM wbacks WHERE player_uuid = ?" ); @@ -69,7 +69,6 @@ public class Database { } 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(?, ?, ?, ?, ?, ?, ?)" ); @@ -101,11 +100,11 @@ public class Database { ); } - public static ArrayList searchWaypoints(Player player, String start) throws SQLException { + public static ArrayList searchWaypoints(Player player, String searchString) 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("_", "!_") + "%"); + statement.setString(1, "%" + searchString.replace("!", "!!").replace("_", "!_") + "%"); ResultSet results = statement.executeQuery(); ArrayList list = new ArrayList(); while (results.next()) @@ -154,4 +153,104 @@ public class Database { else player.sendMessage("Waypoint deleted."); } + + 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" + + ")" + ); + statement.executeUpdate(); + } + + public static boolean isTPAAllowed(Player from, Player to) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM tpa_allowed WHERE from = ? AND to = ?" + ); + statement.setString(1, playerID(from)); + statement.setString(2, playerID(to)); + ResultSet results = statement.executeQuery(); + return results.next(); + } + + public static void allowTPA(Player from, Player to) throws SQLException { + PreparedStatement statement = connection.prepareStatement( + "INSERT INTO tpa_allowed(from, to) VALUES(?, ?)" + ); + statement.setString(1, playerID(from)); + statement.setString(2, playerID(to)); + statement.executeUpdate(); + } + + public static int disallowTPA(String fromName, Player to) throws SQLException { + //this is kinda scrungled but i think it should work. + + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM tpa_allowed WHERE to = ?" + ); + statement.setString(1, playerID(to)); + ResultSet table = statement.executeQuery(); + + int removed = 0; + + while (table.next()) { + String fromID = table.getString("from"); + + if (Bukkit.getOfflinePlayer(UUID.fromString(fromID)).getName() == fromName) { + PreparedStatement deleteStatement = connection.prepareStatement( + "DELETE FROM tpa_allowed WHERE from = ? AND to = ?" + ); + deleteStatement.setString(1, fromID); + deleteStatement.setString(2, playerID(to)); + deleteStatement.executeUpdate(); + ++removed; + } + } + + return removed; + } + + public static ArrayList getOnlineTPADisallowedTo(Player to) throws SQLException { + + ArrayList results = new ArrayList<>(); + for (Player player : Bukkit.getOnlinePlayers()) + if (!isTPAAllowed(player, to)) + results.add(player.getName()); + + return results; + + } + + public static ArrayList getTPAAllowedTo(Player to) throws SQLException { + + PreparedStatement statement = connection.prepareStatement( + "SELECT * FROM tpa_allowed WHERE to = ?" + ); + statement.setString(1, playerID(to)); + ResultSet table = statement.executeQuery(); + + ArrayList results = new ArrayList<>(); + while (table.next()) { + String name = Bukkit.getOfflinePlayer(UUID.fromString(table.getString("from"))).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) + results.add(name); + } + + return results; + + } + + public static ArrayList getOnlineTPAAllowedFrom(Player from) throws SQLException { + + ArrayList results = new ArrayList<>(); + for (Player player : Bukkit.getOnlinePlayers()) + if (isTPAAllowed(from, player)) + results.add(player.getName()); + + return results; + + } } -- cgit v1.2.3