made some fixes after testing

This commit is contained in:
Benji Dial 2023-12-31 15:05:28 -05:00
parent e06ee55259
commit 34d3a59182

View file

@ -2,6 +2,7 @@ package net.benjidial.nswp;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -23,6 +24,14 @@ public class Database {
return player.getUniqueId().toString().replaceAll("-", ""); 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) { static String waypointTableName(Player player) {
return "waypoints_" + playerID(player); return "waypoints_" + playerID(player);
} }
@ -124,23 +133,18 @@ public class Database {
} }
public static void addWaypoint(Player player, Waypoint waypoint) throws SQLException { public static void addWaypoint(Player player, Waypoint waypoint) throws SQLException {
if (lookupWaypoint(player, waypoint.name) != null) PreparedStatement statement = connection.prepareStatement(
player.sendMessage("There is already a waypoint with that name."); "INSERT INTO " + waypointTableName(player) + "(name, world, x, y, z, pitch, yaw) " +
else { "VALUES(?, ?, ?, ?, ?, ?, ?)"
PreparedStatement statement = connection.prepareStatement( );
"INSERT INTO " + waypointTableName(player) + "(name, world, x, y, z, pitch, yaw) " + statement.setString(1, waypoint.name);
"VALUES(?, ?, ?, ?, ?, ?, ?)" statement.setString(2, waypoint.location.getWorld().getName());
); statement.setDouble(3, waypoint.location.getX());
statement.setString(1, waypoint.name); statement.setDouble(4, waypoint.location.getY());
statement.setString(2, waypoint.location.getWorld().getName()); statement.setDouble(5, waypoint.location.getZ());
statement.setDouble(3, waypoint.location.getX()); statement.setDouble(6, waypoint.location.getPitch());
statement.setDouble(4, waypoint.location.getY()); statement.setDouble(7, waypoint.location.getYaw());
statement.setDouble(5, waypoint.location.getZ()); statement.executeUpdate();
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 { public static void deleteWaypoint(Player player, String name) throws SQLException {
@ -148,17 +152,14 @@ public class Database {
"DELETE FROM " + waypointTableName(player) + " WHERE name = ?" "DELETE FROM " + waypointTableName(player) + " WHERE name = ?"
); );
statement.setString(1, name); statement.setString(1, name);
if (statement.executeUpdate() == 0) statement.executeUpdate();
player.sendMessage("No waypoint with that name.");
else
player.sendMessage("Waypoint deleted.");
} }
public static void createTPATable() throws SQLException { public static void createTPATable() throws SQLException {
PreparedStatement statement = connection.prepareStatement( PreparedStatement statement = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS tpa_allowed (" + "CREATE TABLE IF NOT EXISTS tpa_allowed (" +
"from TEXT NOT NULL, " + "from_id TEXT NOT NULL, " +
"to TEXT NOT NULL" + "to_id TEXT NOT NULL" +
")" ")"
); );
statement.executeUpdate(); statement.executeUpdate();
@ -166,7 +167,7 @@ public class Database {
public static boolean isTPAAllowed(Player from, Player to) throws SQLException { public static boolean isTPAAllowed(Player from, Player to) throws SQLException {
PreparedStatement statement = connection.prepareStatement( 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(1, playerID(from));
statement.setString(2, playerID(to)); statement.setString(2, playerID(to));
@ -176,7 +177,7 @@ public class Database {
public static void allowTPA(Player from, Player to) throws SQLException { public static void allowTPA(Player from, Player to) throws SQLException {
PreparedStatement statement = connection.prepareStatement( 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(1, playerID(from));
statement.setString(2, playerID(to)); statement.setString(2, playerID(to));
@ -187,7 +188,7 @@ public class Database {
//this is kinda scrungled but i think it should work. //this is kinda scrungled but i think it should work.
PreparedStatement statement = connection.prepareStatement( PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM tpa_allowed WHERE to = ?" "SELECT * FROM tpa_allowed WHERE to_id = ?"
); );
statement.setString(1, playerID(to)); statement.setString(1, playerID(to));
ResultSet table = statement.executeQuery(); ResultSet table = statement.executeQuery();
@ -195,11 +196,11 @@ public class Database {
int removed = 0; int removed = 0;
while (table.next()) { 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( 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(1, fromID);
deleteStatement.setString(2, playerID(to)); deleteStatement.setString(2, playerID(to));
@ -225,14 +226,14 @@ public class Database {
public static ArrayList<String> getTPAAllowedTo(Player to) throws SQLException { public static ArrayList<String> getTPAAllowedTo(Player to) throws SQLException {
PreparedStatement statement = connection.prepareStatement( PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM tpa_allowed WHERE to = ?" "SELECT * FROM tpa_allowed WHERE to_id = ?"
); );
statement.setString(1, playerID(to)); statement.setString(1, playerID(to));
ResultSet table = statement.executeQuery(); ResultSet table = statement.executeQuery();
ArrayList<String> results = new ArrayList<>(); ArrayList<String> results = new ArrayList<>();
while (table.next()) { 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, //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. //they have to be online, and so their name should be known.
if (name != null) if (name != null)