diff --git a/src/main/java/me/william278/huskhomes2/Commands/editHomeCommand.java b/src/main/java/me/william278/huskhomes2/Commands/editHomeCommand.java index 5c4d38a8..6eff3214 100644 --- a/src/main/java/me/william278/huskhomes2/Commands/editHomeCommand.java +++ b/src/main/java/me/william278/huskhomes2/Commands/editHomeCommand.java @@ -38,7 +38,7 @@ public class editHomeCommand implements CommandExecutor { String newDescriptionString = newDescription.toString(); // Check the description is valid - if (!newDescriptionString.matches("[a-zA-Z0-9\\d\\-_\\s]+")) { + if (!newDescriptionString.matches("[a-zA-Z0-9\\d\\-_\\s]+") && newDescriptionString.length() > 255) { messageManager.sendMessage(p, "error_edit_home_invalid_description"); return true; } diff --git a/src/main/java/me/william278/huskhomes2/Commands/editWarpCommand.java b/src/main/java/me/william278/huskhomes2/Commands/editWarpCommand.java index 1b17d7c1..744543a7 100644 --- a/src/main/java/me/william278/huskhomes2/Commands/editWarpCommand.java +++ b/src/main/java/me/william278/huskhomes2/Commands/editWarpCommand.java @@ -39,7 +39,7 @@ public class editWarpCommand implements CommandExecutor { String newDescriptionString = newDescription.toString(); // Check the description is valid - if (!newDescriptionString.matches("[a-zA-Z0-9\\d\\-_\\s]+")) { + if (!newDescriptionString.matches("[a-zA-Z0-9\\d\\-_\\s]+") && newDescriptionString.length() > 255) { messageManager.sendMessage(p, "error_edit_warp_invalid_description"); return true; } diff --git a/src/main/java/me/william278/huskhomes2/Commands/huskHomesCommand.java b/src/main/java/me/william278/huskhomes2/Commands/huskHomesCommand.java index 2da1c2f2..988faeeb 100644 --- a/src/main/java/me/william278/huskhomes2/Commands/huskHomesCommand.java +++ b/src/main/java/me/william278/huskhomes2/Commands/huskHomesCommand.java @@ -31,16 +31,6 @@ public class huskHomesCommand implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (args.length >= 1) { switch (args[0]) { - case "about": - if (sender instanceof Player) { - Player p = (Player) sender; - if (!p.hasPermission("huskhomes.about")) { - messageManager.sendMessage(p, "error_no_permission"); - return true; - } - } - showAboutMenu(sender); - return true; case "update": if (sender instanceof Player) { Player p = (Player) sender; @@ -84,6 +74,16 @@ public class huskHomesCommand implements CommandExecutor { plugin.getLogger().info("Reloaded config and message files."); } return true; + default: + if (sender instanceof Player) { + Player p = (Player) sender; + if (!p.hasPermission("huskhomes.about")) { + messageManager.sendMessage(p, "error_no_permission"); + return true; + } + } + showAboutMenu(sender); + return true; } } else { if (sender instanceof Player) { diff --git a/src/main/java/me/william278/huskhomes2/Commands/tpCommand.java b/src/main/java/me/william278/huskhomes2/Commands/tpCommand.java index 57171b63..f456e8be 100644 --- a/src/main/java/me/william278/huskhomes2/Commands/tpCommand.java +++ b/src/main/java/me/william278/huskhomes2/Commands/tpCommand.java @@ -30,7 +30,7 @@ public class tpCommand implements CommandExecutor { TeleportationPoint teleportationPoint = new TeleportationPoint(p.getWorld().getName(), x, y, z, 0F, 0F, Main.settings.getServerID()); teleportManager.teleportPlayer(p, teleportationPoint); } catch (Exception e) { - messageManager.sendMessage(p, "error_invalid_syntax", "/tp "); + messageManager.sendMessage(p, "error_invalid_syntax", "/tp [world] [server]"); } return true; case 4: @@ -40,13 +40,13 @@ public class tpCommand implements CommandExecutor { double z = Double.parseDouble(args[2]); String worldName = args[3]; if (Bukkit.getWorld(worldName) == null) { - messageManager.sendMessage(p, "error_invalid_syntax", "/tp "); + messageManager.sendMessage(p, "error_invalid_syntax", "/tp [world] [server]"); return true; } TeleportationPoint teleportationPoint = new TeleportationPoint(worldName, x, y, z, 0F, 0F, Main.settings.getServerID()); teleportManager.teleportPlayer(p, teleportationPoint); } catch (Exception e) { - messageManager.sendMessage(p, "error_invalid_syntax", "/tp "); + messageManager.sendMessage(p, "error_invalid_syntax", "/tp [world] [server]"); } return true; case 5: diff --git a/src/main/java/me/william278/huskhomes2/Commands/tpHereCommand.java b/src/main/java/me/william278/huskhomes2/Commands/tpHereCommand.java index d08daf7f..70aca2b1 100644 --- a/src/main/java/me/william278/huskhomes2/Commands/tpHereCommand.java +++ b/src/main/java/me/william278/huskhomes2/Commands/tpHereCommand.java @@ -15,7 +15,7 @@ public class tpHereCommand implements CommandExecutor { Player p = (Player) sender; if (args.length == 1) { String targetPlayer = args[0]; - teleportManager.teleportPlayer(p, targetPlayer); + teleportManager.teleportHere(p, targetPlayer); } else { messageManager.sendMessage(p, "error_invalid_syntax", command.getUsage()); } diff --git a/src/main/java/me/william278/huskhomes2/Data/Database.java b/src/main/java/me/william278/huskhomes2/Data/Database.java index 5d4d6a53..6f075cf4 100644 --- a/src/main/java/me/william278/huskhomes2/Data/Database.java +++ b/src/main/java/me/william278/huskhomes2/Data/Database.java @@ -8,10 +8,7 @@ import me.william278.huskhomes2.Objects.TeleportationPoint; import me.william278.huskhomes2.Objects.Warp; import org.bukkit.entity.Player; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.logging.Level; public abstract class Database { @@ -29,7 +26,7 @@ public abstract class Database { public void initialize() { connection = getSQLConnection(); try { - PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerDataTable() + " WHERE `user_uuid`=?"); + PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerDataTable() + ";"); ResultSet rs = ps.executeQuery(); close(ps, rs); @@ -38,10 +35,10 @@ public abstract class Database { } } - // Insert a teleportation point - public void addTeleportationPoint(TeleportationPoint point, Connection conn) { + // Insert a teleportation point, returns generated id + public Integer addTeleportationPoint(TeleportationPoint point, Connection conn) { try { - PreparedStatement ps = conn.prepareStatement("INSERT INTO " + Main.settings.getLocationsDataTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);"); + PreparedStatement ps = conn.prepareStatement("INSERT INTO " + Main.settings.getLocationsDataTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);", Statement.RETURN_GENERATED_KEYS); ps.setString(1, point.getWorldName()); ps.setString(2, point.getServer()); @@ -51,9 +48,16 @@ public abstract class Database { ps.setFloat(6, point.getYaw()); ps.setFloat(7, point.getPitch()); ps.executeUpdate(); + + ResultSet rs = ps.getGeneratedKeys(); + if (rs.next()) { + return rs.getInt(1); + } + } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); } + return null; } // Insert a home into the database @@ -63,13 +67,14 @@ public abstract class Database { try { conn = getSQLConnection(); - addTeleportationPoint(home, conn); + Integer locationID = addTeleportationPoint(home, conn); - ps = conn.prepareStatement("INSERT INTO " + Main.settings.getHomesDataTable() + " (player_id,location_id,name,description,public) VALUES(?,(SELECT last_insert_rowid()),?,?,?);"); + ps = conn.prepareStatement("INSERT INTO " + Main.settings.getHomesDataTable() + " (player_id,location_id,name,description,public) VALUES(?,?,?,?,?);"); ps.setInt(1, playerID); - ps.setString(2, home.getName()); - ps.setString(3, home.getDescription()); - ps.setBoolean(4, home.isPublic()); + ps.setInt(2, locationID); + ps.setString(3, home.getName()); + ps.setString(4, home.getDescription()); + ps.setBoolean(5, home.isPublic()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); @@ -94,12 +99,13 @@ public abstract class Database { conn = getSQLConnection(); // Add the teleportation point - addTeleportationPoint(warp, conn); + Integer locationID = addTeleportationPoint(warp, conn); // Insert the warp with the location_id of the last inserted teleport point - ps = conn.prepareStatement("INSERT INTO " + Main.settings.getWarpsDataTable() + " (location_id,name,description) VALUES((SELECT last_insert_rowid()),?,?);"); - ps.setString(1, warp.getName()); - ps.setString(2, warp.getDescription()); + ps = conn.prepareStatement("INSERT INTO " + Main.settings.getWarpsDataTable() + " (location_id,name,description) VALUES(?,?,?);"); + ps.setInt(1, locationID); + ps.setString(2, warp.getName()); + ps.setString(3, warp.getDescription()); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); @@ -335,12 +341,13 @@ public abstract class Database { conn = getSQLConnection(); // Add the teleportation point - addTeleportationPoint(point, conn); + Integer locationID = addTeleportationPoint(point, conn); // Set the home location ID to the new teleport point for the given home - ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesDataTable() + " SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=? AND `player_id`=?;"); - ps.setString(1, homeName); - ps.setInt(2, ownerID); + ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesDataTable() + " SET `location_id`=? WHERE `name`=? AND `player_id`=?;"); + ps.setInt(1, locationID); + ps.setString(2, homeName); + ps.setInt(3, ownerID); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); @@ -365,11 +372,12 @@ public abstract class Database { conn = getSQLConnection(); // Add the teleportation point - addTeleportationPoint(point, conn); + Integer locationID = addTeleportationPoint(point, conn); // Set the warp location ID to the new teleport point - ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsDataTable() + " SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=?;"); - ps.setString(1, warpName); + ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsDataTable() + " SET `location_id`=? WHERE `name`=?;"); + ps.setInt(1, locationID); + ps.setString(2, warpName); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); @@ -496,8 +504,8 @@ public abstract class Database { } } - // Update a player's destination teleport point - public void setTeleportationLastOrDest(int playerID, TeleportationPoint point, String type) { + // Update a player's last position teleport point + public void setTeleportationLastPosition(int playerID, TeleportationPoint point) { Connection conn = null; PreparedStatement ps = null; @@ -505,11 +513,12 @@ public abstract class Database { conn = getSQLConnection(); // Add the teleportation point - addTeleportationPoint(point, conn); + Integer locationID = addTeleportationPoint(point, conn); // Set the destination location with the location_id of the last inserted teleport point - ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `" + type + "_location_id`=(SELECT last_insert_rowid()) WHERE `player_id`=?;"); - ps.setInt(1, playerID); + ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `last_location_id`=? WHERE `player_id`=?;"); + ps.setInt(1, locationID); + ps.setInt(2, playerID); ps.executeUpdate(); } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); @@ -525,6 +534,37 @@ public abstract class Database { } } + // Update a player's destination teleport point + public void setTeleportationDestination(int playerID, TeleportationPoint point) { + Connection conn = null; + PreparedStatement ps = null; + + try { + conn = getSQLConnection(); + + // Add the teleportation point + Integer locationID = addTeleportationPoint(point, conn); + + // Set the destination location with the location_id of the last inserted teleport point + ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `dest_location_id`=? WHERE `player_id`=?;"); + ps.setInt(1, locationID); + ps.setInt(2, playerID); + ps.executeUpdate(); + + } catch (SQLException ex) { + plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); + } finally { + try { + if (ps != null) + ps.close(); + if (conn != null) + conn.close(); + } catch (SQLException ex) { + plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); + } + } + } + // Clear a player's destination public void clearPlayerDestination(int playerID) { Connection conn = null; @@ -549,6 +589,30 @@ public abstract class Database { } } + // Clear a player's last position + public void clearPlayerLastPosition(int playerID) { + Connection conn = null; + PreparedStatement ps = null; + + try { + conn = getSQLConnection(); + ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerDataTable() + " SET `last_location_id`=NULL WHERE `player_id`=?;"); + ps.setInt(1, playerID); + ps.executeUpdate(); + } catch (SQLException ex) { + plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); + } finally { + try { + if (ps != null) + ps.close(); + if (conn != null) + conn.close(); + } catch (SQLException ex) { + plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); + } + } + } + public void close(PreparedStatement ps, ResultSet rs) { try { if (ps != null) diff --git a/src/main/java/me/william278/huskhomes2/Data/SQL.java b/src/main/java/me/william278/huskhomes2/Data/SQL.java index 9f3b171a..77bd68bc 100644 --- a/src/main/java/me/william278/huskhomes2/Data/SQL.java +++ b/src/main/java/me/william278/huskhomes2/Data/SQL.java @@ -31,12 +31,12 @@ public class SQL extends Database { try { synchronized (Main.getInstance()) { Class.forName("com.mysql.jdbc.Driver"); - return DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password); + return DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&useSSL=false", username, password); } } catch (SQLException e) { - Main.disablePlugin("Could not connect to the mySQL Database with the credentials provided!"); + Main.disablePlugin("[!] Could not connect to the mySQL Database with the credentials provided!\n[!] Check that your host IP address and port are valid and that your username and password are valid and that the user has the correct access permissions on the database."); } catch (ClassNotFoundException e) { - Main.disablePlugin("A critical exception occurred when attempting to establish a mySQL connection."); + Main.disablePlugin("[!] A critical exception occurred when attempting to establish a connection to the mySQL database!"); e.printStackTrace(); } return null; @@ -87,8 +87,8 @@ public class SQL extends Database { try { Statement s = connection.createStatement(); - s.execute(dataManager.createPlayerTable); s.execute(dataManager.createLocationsTable); + s.execute(dataManager.createPlayerTable); s.execute(dataManager.createHomesTable); s.execute(dataManager.createWarpsTable); s.close(); diff --git a/src/main/java/me/william278/huskhomes2/Main.java b/src/main/java/me/william278/huskhomes2/Main.java index e6e816c5..e7cb4ed8 100644 --- a/src/main/java/me/william278/huskhomes2/Main.java +++ b/src/main/java/me/william278/huskhomes2/Main.java @@ -20,7 +20,7 @@ public final class Main extends JavaPlugin { // Disable the plugin for the given reason public static void disablePlugin(String reason) { - getInstance().getLogger().severe("Disabling HuskHomes plugin because: " + reason); + getInstance().getLogger().severe("Disabling HuskHomes plugin because:\n" + reason); Bukkit.getPluginManager().disablePlugin(getInstance()); } @@ -79,7 +79,12 @@ public final class Main extends JavaPlugin { messageManager.loadMessages(Main.settings.getLanguage()); // Set up data storage - dataManager.setupStorage(settings.getStorageType()); + dataManager.setupStorage(); + + // Return if the plugin is disabled + if (!Main.getInstance().isEnabled()) { + return; + } // Set up bungee channels if bungee mode is enabled if (settings.doBungee()) { diff --git a/src/main/java/me/william278/huskhomes2/Objects/Settings.java b/src/main/java/me/william278/huskhomes2/Objects/Settings.java index 370e2952..eefe2866 100644 --- a/src/main/java/me/william278/huskhomes2/Objects/Settings.java +++ b/src/main/java/me/william278/huskhomes2/Objects/Settings.java @@ -43,15 +43,15 @@ public class Settings { if (storageType.equalsIgnoreCase("sqlite") && doBungee) { Bukkit.getLogger().warning("Bungee mode was set in config to be enabled but storage type was set to SQLite!"); Bukkit.getLogger().warning("A mySQL Database is required to utilise Bungee mode, so bungee mode has been disabled."); - Bukkit.getLogger().warning("To use Bungee mode and cross-server teleportation, please update your data storage settings to use \"mysql\" and update the connection details accordingly."); + Bukkit.getLogger().warning("To use Bungee mode and cross-server teleportation, please update your data storage settings to use \"mysql\" and update the connection credentials accordingly."); this.doBungee = false; } - this.mySQLhost = configFile.getString("data_storage_options.mysql_database.host"); - this.mySQLdatabase = configFile.getString("data_storage_options.mysql_database.database"); - this.mySQLusername = configFile.getString("data_storage_options.mysql_database.username"); - this.mySQLpassword = configFile.getString("data_storage_options.mysql_database.password"); - this.mySQLport = configFile.getInt("data_storage_options.mysql_database.port"); + this.mySQLhost = configFile.getString("data_storage_options.mysql_credentials.host"); + this.mySQLdatabase = configFile.getString("data_storage_options.mysql_credentials.database"); + this.mySQLusername = configFile.getString("data_storage_options.mysql_credentials.username"); + this.mySQLpassword = configFile.getString("data_storage_options.mysql_credentials.password"); + this.mySQLport = configFile.getInt("data_storage_options.mysql_credentials.port"); this.playerDataTable = configFile.getString("data_storage_options.table_names.player_data"); this.locationsDataTable = configFile.getString("data_storage_options.table_names.locations_data"); diff --git a/src/main/java/me/william278/huskhomes2/configManager.java b/src/main/java/me/william278/huskhomes2/configManager.java index cdf9b67c..ab1b22c4 100644 --- a/src/main/java/me/william278/huskhomes2/configManager.java +++ b/src/main/java/me/william278/huskhomes2/configManager.java @@ -13,13 +13,9 @@ public class configManager { "| HuskHomes Config |\n" + "| Developed by William278 |\n" + " ------------------------------ \n" + - "Configuration guide: [tbd] \n" + - "To quickly setup the plugin:\n" + - "1. Fill in the details of your mySQL Database below\n" + - "2. (Optional) Change the appropriate details in the configuration\n" + - "3. Restart the server"); - plugin.getConfig().options().copyHeader(true).copyDefaults(true); - plugin.saveConfig(); + "Configuration guide: [tbd] \n"); + plugin.getConfig().options().copyHeader(true); + plugin.saveDefaultConfig(); Main.settings = new Settings(plugin.getConfig()); } } diff --git a/src/main/java/me/william278/huskhomes2/dataManager.java b/src/main/java/me/william278/huskhomes2/dataManager.java index 817d8db7..326f845f 100644 --- a/src/main/java/me/william278/huskhomes2/dataManager.java +++ b/src/main/java/me/william278/huskhomes2/dataManager.java @@ -19,50 +19,29 @@ import java.util.ArrayList; import java.util.UUID; import java.util.logging.Level; -// This class handles the saving of data; whether that be through SQLite or SQL +// This class handles the saving of data; whether that be through SQLite or mySQL public class dataManager { - public static String createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerDataTable() + " (" + - "`player_id` integer NOT NULL," + - "`user_uuid` char(36) NOT NULL UNIQUE," + - "`username` varchar(16) NOT NULL," + - "`home_slots` integer NOT NULL," + - "`rtp_cooldown` integer NOT NULL," + - "`is_teleporting` boolean NOT NULL," + - "`dest_location_id` integer NULL," + - "`last_location_id` integer NULL," + - "PRIMARY KEY (`player_id`)," + - "FOREIGN KEY (`dest_location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + - "FOREIGN KEY (`last_location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" + - ");"; + // Syntax for creating tables; changes based on storage medium + public static String createPlayerTable = ""; - public static String createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsDataTable() + " (" + - "`location_id` integer PRIMARY KEY," + - "`server` text NOT NULL," + - "`world` text NOT NULL," + - "`x` double NOT NULL," + - "`y` double NOT NULL," + - "`z` double NOT NULL," + - "`yaw` float NOT NULL," + - "`pitch` float NOT NULL" + - ");"; + public static String createLocationsTable = ""; public static String createHomesTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getHomesDataTable() + " (" + "`player_id` integer NOT NULL," + "`location_id` integer NOT NULL," + - "`name` double NOT NULL," + - "`description` varchar NOT NULL," + + "`name` varchar(16) NOT NULL," + + "`description` varchar(255) NOT NULL," + "`public` boolean NOT NULL," + - "PRIMARY KEY (`player_id`, `location_id`)," + + "PRIMARY KEY (`player_id`, `name`)," + "FOREIGN KEY (`player_id`) REFERENCES " + Main.settings.getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + "FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" + ");"; public static String createWarpsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getWarpsDataTable() + " (" + "`location_id` integer NOT NULL," + - "`name` varchar NOT NULL UNIQUE," + - "`description` varchar NOT NULL," + - + "`name` varchar(16) NOT NULL UNIQUE," + + "`description` varchar(255) NOT NULL," + "PRIMARY KEY (`location_id`)," + "FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" + ");"; @@ -447,15 +426,17 @@ public class dataManager { ps.setString(1, name); rs = ps.executeQuery(); if (rs != null) { - int locationID = rs.getInt("location_id"); - TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); - if (teleportationPoint != null) { - return new Warp(teleportationPoint, - rs.getString("name"), - rs.getString("description")); - } else { - Bukkit.getLogger().severe("An error occurred returning a warp from the table!"); - return null; + if (rs.next()) { + int locationID = rs.getInt("location_id"); + TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); + if (teleportationPoint != null) { + return new Warp(teleportationPoint, + rs.getString("name"), + rs.getString("description")); + } else { + Bukkit.getLogger().severe("An error occurred returning a warp from the table!"); + return null; + } } } else { Bukkit.getLogger().severe("Warp returned null!"); @@ -488,8 +469,10 @@ public class dataManager { ps.setString(2, homeName); rs = ps.executeQuery(); if (rs != null) { - int locationID = rs.getInt("location_id"); - database.deleteTeleportationPoint(locationID); + if (rs.next()) { + int locationID = rs.getInt("location_id"); + database.deleteTeleportationPoint(locationID); + } } else { Bukkit.getLogger().severe("Failed to delete home teleportation location"); } @@ -518,8 +501,10 @@ public class dataManager { ps.setString(1, warpName); rs = ps.executeQuery(); if (rs != null) { - int locationID = rs.getInt("location_id"); - database.deleteTeleportationPoint(locationID); + if (rs.next()) { + int locationID = rs.getInt("location_id"); + database.deleteTeleportationPoint(locationID); + } } else { Bukkit.getLogger().severe("Failed to delete warp teleportation location"); } @@ -585,18 +570,20 @@ public class dataManager { ps.setString(2, homeName); rs = ps.executeQuery(); if (rs != null) { - int locationID = rs.getInt("location_id"); - TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); - if (teleportationPoint != null) { - return new Home(teleportationPoint, - ownerUsername, - getPlayerUUID(playerID), - rs.getString("name"), - rs.getString("description"), - rs.getBoolean("public")); - } else { - Bukkit.getLogger().severe("An error occurred retrieving a home from the table!"); - return null; + if (rs.next()) { + int locationID = rs.getInt("location_id"); + TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); + if (teleportationPoint != null) { + return new Home(teleportationPoint, + ownerUsername, + getPlayerUUID(playerID), + rs.getString("name"), + rs.getString("description"), + rs.getBoolean("public")); + } else { + Bukkit.getLogger().severe("An error occurred retrieving a home from the table!"); + return null; + } } } else { Bukkit.getLogger().severe("Home returned null; perhaps player ID was null?"); @@ -739,6 +726,8 @@ public class dataManager { rs.getFloat("pitch"), rs.getString("server")); } else { Bukkit.getLogger().severe("An SQL exception occurred in retrieving a teleportation location."); + Bukkit.getLogger().severe("location ID: " + locationID.toString()); + Bukkit.getLogger().severe("SELECT * FROM " + Main.settings.getLocationsDataTable() + " WHERE `location_id`=" + locationID.toString() + ";"); return null; } } catch (SQLException ex) { @@ -779,7 +768,7 @@ public class dataManager { Integer playerID = getPlayerId(p.getName()); if (playerID != null) { deletePlayerLastPosition(p); - database.setTeleportationLastOrDest(playerID, point, "last"); + database.setTeleportationLastPosition(playerID, point); } else { Bukkit.getLogger().warning("Failed to update player last position records for " + p.getName()); } @@ -790,7 +779,7 @@ public class dataManager { Integer playerID = getPlayerId(playerName); if (playerID != null) { deletePlayerDestination(playerName); - database.setTeleportationLastOrDest(playerID, point, "dest"); + database.setTeleportationDestination(playerID, point); } else { Bukkit.getLogger().warning("Failed to update player destination records for " + playerName); } @@ -798,22 +787,15 @@ public class dataManager { // Update a player's destination location on SQL public static void setPlayerDestinationLocation(Player p, TeleportationPoint point) { - Integer playerID = getPlayerId(p.getName()); - if (playerID != null) { - deletePlayerDestination(p); - database.setTeleportationLastOrDest(playerID, point, "dest"); - } else { - Bukkit.getLogger().warning("Failed to update player destination records for " + p.getName()); - } + setPlayerDestinationLocation(p.getName(), point); } public static void setPlayerTeleporting(Player p, boolean value) { Integer playerID = getPlayerId(p.getName()); if (playerID != null) { - deletePlayerDestination(p); database.setPlayerTeleporting(playerID, value); } else { - Bukkit.getLogger().warning("Failed to update player destination records for " + p.getName()); + Bukkit.getLogger().warning("Failed to set a player as teleporting (" + p.getName() + ")"); } } @@ -838,20 +820,19 @@ public class dataManager { Integer destinationID = getPlayerInteger(playerName, "dest_location_id"); if (destinationID != null) { database.deleteTeleportationPoint(destinationID); + database.clearPlayerDestination(getPlayerId(playerName)); } } public static void deletePlayerDestination(Player p) { - Integer destinationID = getPlayerInteger(p, "dest_location_id"); - if (destinationID != null) { - database.deleteTeleportationPoint(destinationID); - } + deletePlayerDestination(p.getName()); } public static void deletePlayerLastPosition(Player p) { Integer lastPositionID = getPlayerInteger(p, "last_location_id"); if (lastPositionID != null) { database.deleteTeleportationPoint(lastPositionID); + database.clearPlayerLastPosition(getPlayerId(p.getUniqueId())); } } @@ -883,7 +864,61 @@ public class dataManager { database.deleteWarp(warpName); } - public static void setupStorage(String storageType) { + public static void setupStorage() { + if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) { + createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerDataTable() + " (" + + "`player_id` integer AUTO_INCREMENT," + + "`user_uuid` char(36) NOT NULL UNIQUE," + + "`username` varchar(16) NOT NULL," + + "`home_slots` integer NOT NULL," + + "`rtp_cooldown` integer NOT NULL," + + "`is_teleporting` boolean NOT NULL," + + "`dest_location_id` integer NULL," + + "`last_location_id` integer NULL," + + + "PRIMARY KEY (`player_id`)," + + "FOREIGN KEY (`dest_location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," + + "FOREIGN KEY (`last_location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION" + + ");"; + createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsDataTable() + " (" + + "`location_id` integer AUTO_INCREMENT," + + "`server` text NOT NULL," + + "`world` text NOT NULL," + + "`x` double NOT NULL," + + "`y` double NOT NULL," + + "`z` double NOT NULL," + + "`yaw` float NOT NULL," + + "`pitch` float NOT NULL," + + + "PRIMARY KEY (`location_id`)" + + ");"; + } else { + createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerDataTable() + " (" + + "`player_id` integer PRIMARY KEY," + + "`user_uuid` char(36) NOT NULL UNIQUE," + + "`username` varchar(16) NOT NULL," + + "`home_slots` integer NOT NULL," + + "`rtp_cooldown` integer NOT NULL," + + "`is_teleporting` boolean NOT NULL," + + "`dest_location_id` integer NULL," + + "`last_location_id` integer NULL," + + + "FOREIGN KEY (`dest_location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," + + "FOREIGN KEY (`last_location_id`) REFERENCES " + Main.settings.getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION" + + ");"; + createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsDataTable() + " (" + + "`location_id` integer PRIMARY KEY," + + "`server` text NOT NULL," + + "`world` text NOT NULL," + + "`x` double NOT NULL," + + "`y` double NOT NULL," + + "`z` double NOT NULL," + + "`yaw` float NOT NULL," + + "`pitch` float NOT NULL," + + + "PRIMARY KEY (`location_id`)" + + ");"; + } initializeDatabase(); } diff --git a/src/main/java/me/william278/huskhomes2/editingHandler.java b/src/main/java/me/william278/huskhomes2/editingHandler.java index 47c919b3..551f7400 100644 --- a/src/main/java/me/william278/huskhomes2/editingHandler.java +++ b/src/main/java/me/william278/huskhomes2/editingHandler.java @@ -43,7 +43,7 @@ public class editingHandler { options.append(optionButton("[Edit Description] ", net.md_5.bungee.api.ChatColor.GOLD, ClickEvent.Action.SUGGEST_COMMAND, "/edithome " + home.getName() + " description ", "Update the description of your home")); if (p.hasPermission("huskhomes.edithome.public")) { - if (home.isPublic()) { + if (!home.isPublic()) { options.append(optionButton("[Make Public] ", net.md_5.bungee.api.ChatColor.DARK_GREEN, ClickEvent.Action.RUN_COMMAND, "/edithome " + home.getName() + " public", "Open your home to the public")); } else { options.append(optionButton("[Make Private] ", net.md_5.bungee.api.ChatColor.DARK_RED, ClickEvent.Action.RUN_COMMAND, "/edithome " + home.getName() + " private", "Make your home private")); @@ -64,10 +64,10 @@ public class editingHandler { if (Main.settings.doBungee()) { messageManager.sendMessage(p, "edit_server", home.getServer()); } - if (home.isPublic()) { - messageManager.sendMessage(p, "edit_home_privacy_public"); - } else { + if (!home.isPublic()) { messageManager.sendMessage(p, "edit_home_privacy_private"); + } else { + messageManager.sendMessage(p, "edit_home_privacy_public"); } p.sendMessage(""); p.spigot().sendMessage(editHomeOptions(p, home).create()); diff --git a/src/main/java/me/william278/huskhomes2/pluginMessageHandler.java b/src/main/java/me/william278/huskhomes2/pluginMessageHandler.java index 410d9fed..cc286fe8 100644 --- a/src/main/java/me/william278/huskhomes2/pluginMessageHandler.java +++ b/src/main/java/me/william278/huskhomes2/pluginMessageHandler.java @@ -89,9 +89,16 @@ public class pluginMessageHandler implements PluginMessageListener { String replierName; boolean accepted; + // Respond to different cross-server requests switch (messageType) { - case "set_teleportation_destination": + case "set_tp_destination": setTeleportDestination(messageData, recipient); + pluginMessageHandler.sendPluginMessage(recipient, messageData, "confirm_destination_set", "confirmed"); + break; + case "confirm_destination_set": + if (messageData.equals("confirmed")) { + teleportManager.teleportPlayer(recipient); + } break; case "tpa_request": teleportRequestHandler.teleportRequests.put(recipient, new TeleportRequest(messageData, "tpa")); @@ -120,6 +127,9 @@ public class pluginMessageHandler implements PluginMessageListener { messageManager.sendMessage(recipient, "tpa_has_declined", replierName); } break; + case "teleport_to_me": + teleportManager.teleportPlayer(recipient, messageData); + break; } } diff --git a/src/main/java/me/william278/huskhomes2/teleportManager.java b/src/main/java/me/william278/huskhomes2/teleportManager.java index bd849212..19569d45 100644 --- a/src/main/java/me/william278/huskhomes2/teleportManager.java +++ b/src/main/java/me/william278/huskhomes2/teleportManager.java @@ -16,7 +16,6 @@ public class teleportManager { public static void teleportPlayer(Player player, String targetPlayer) { dataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), Main.settings.getServerID())); setPlayerDestinationFromTargetPlayer(player, targetPlayer); - teleportPlayer(player); } public static void teleportPlayer(Player player, TeleportationPoint point) { @@ -32,9 +31,11 @@ public class teleportManager { if (!Main.settings.doBungee() || server.equals(Main.settings.getServerID())) { p.teleport(teleportationPoint.getLocation()); messageManager.sendMessage(p, "teleporting_complete"); + dataManager.setPlayerTeleporting(p, false); dataManager.clearPlayerDestination(p.getName()); } else if (Main.settings.doBungee()) { dataManager.setPlayerDestinationLocation(p, teleportationPoint); + dataManager.setPlayerTeleporting(p, true); pluginMessageHandler.sendPlayer(p, server); } } @@ -78,8 +79,25 @@ public class teleportManager { queuedTeleports.add(new TimedTeleport(player)); } + public static void teleportHere(Player requester, String targetPlayerName) { + Player targetPlayer = Bukkit.getPlayer(targetPlayerName); + if (targetPlayer != null) { + teleportPlayer(targetPlayer, requester.getName()); + } else { + if (Main.settings.doBungee()) { + teleportHereCrossServer(requester, targetPlayerName); + } else { + messageManager.sendMessage(requester, "error_player_not_found", targetPlayerName); + } + } + } + + private static void teleportHereCrossServer(Player requester, String targetPlayerName) { + pluginMessageHandler.sendPluginMessage(requester, targetPlayerName, "teleport_to_me", requester.getName()); + } + private static void setTeleportationDestinationCrossServer(Player requester, String targetPlayerName) { - pluginMessageHandler.sendPluginMessage(requester, targetPlayerName, "set_teleportation_destination", requester.getName()); + pluginMessageHandler.sendPluginMessage(requester, targetPlayerName, "set_tp_destination", requester.getName()); } public static void setPlayerDestinationFromTargetPlayer(Player requester, String targetPlayerName) { @@ -87,6 +105,7 @@ public class teleportManager { if (targetPlayer != null) { dataManager.setPlayerDestinationLocation(requester, new TeleportationPoint(targetPlayer.getLocation(), Main.settings.getServerID())); + teleportPlayer(requester); } else { if (Main.settings.doBungee()) { setTeleportationDestinationCrossServer(requester, targetPlayerName); diff --git a/src/main/java/me/william278/huskhomes2/teleportRequestHandler.java b/src/main/java/me/william278/huskhomes2/teleportRequestHandler.java index 764b0688..8a3e40aa 100644 --- a/src/main/java/me/william278/huskhomes2/teleportRequestHandler.java +++ b/src/main/java/me/william278/huskhomes2/teleportRequestHandler.java @@ -26,7 +26,7 @@ public class teleportRequestHandler { if (targetPlayer != null) { teleportRequests.put(targetPlayer, new TeleportRequest(requester.getName(), "tpa")); messageManager.sendMessage(requester, "tpa_request_sent", targetPlayerName); - messageManager.sendMessage(targetPlayer, "tpa_request_ask", targetPlayerName); + messageManager.sendMessage(targetPlayer, "tpa_request_ask", requester.getName()); } else { if (Main.settings.doBungee()) { sendTeleportRequestCrossServer(requester, targetPlayerName, "tpa"); @@ -42,7 +42,7 @@ public class teleportRequestHandler { if (targetPlayer != null) { teleportRequests.put(targetPlayer, new TeleportRequest(requester.getName(), "tpahere")); messageManager.sendMessage(requester, "tpahere_request_sent", targetPlayerName); - messageManager.sendMessage(targetPlayer, "tpahere_request_ask", targetPlayerName); + messageManager.sendMessage(targetPlayer, "tpahere_request_ask", requester.getName()); } else { if (Main.settings.doBungee()) { sendTeleportRequestCrossServer(requester, targetPlayerName, "tpahere"); diff --git a/src/main/resources/Languages/en-gb.yml b/src/main/resources/Languages/en-gb.yml index 2639a9e5..b17c5555 100644 --- a/src/main/resources/Languages/en-gb.yml +++ b/src/main/resources/Languages/en-gb.yml @@ -26,7 +26,7 @@ edit_home_privacy_private_success: '§aSuccessfully made your home §l%1%§r§a error_edit_home_privacy_already_public: '§4Error: §cYour home is already set to public.' error_edit_home_privacy_already_private: '§4Error: §cYour home is already set to private.' error_edit_home_privacy_no_permission: '§4Error: §cYou do not have permission to make homes public.' -error_edit_home_invalid_description: '§4Error: §cHome descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_)' +error_edit_home_invalid_description: '§4Error: §cHome descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_) and must be less than 255 characters in length' edit_home_update_location: '§aRe-located §l%1% §r§ato your current position.' edit_home_update_description: '§aChanged the description for §l%1%§r§a to "%2%".' edit_home_update_name: '§aSuccessfully changed your home''s name: %1% → §l%2%' @@ -54,7 +54,7 @@ error_set_warp_name_taken: '§4Error: §cThere is already a warp set with that n set_spawn_success: '§aUpdated /spawn command location for this server to your current position.' error_spawn_undefined: '§4Error: §cThere is no spawn location defined on this server.' error_tpa_no_pending_request: '§4Error: §cYou do not have a pending teleport request.' -error_player_not_found: '§4Error: §cCould not find %1%.' +error_player_not_found: '§4Error: §cCould not find the player %1%.' return_by_death: '§7Return to where you died?:' tpa_you_accepted: '§aYou accepted %1%''s teleport request' tpa_you_declined: '§cYou declined %1%''s teleport request' @@ -73,7 +73,7 @@ error_command_disabled: '§4Error: §cThat command is disabled!' error_console_only: '§4Error: §cThat command can only be run from the server console.' error_tp_invalid_world: '§4Error: §cPlease specify a valid server world.' edit_warp_title: '§aEdit details for the warp §l%1%§r§a:' -error_edit_warp_invalid_description: '§4Error: §cWarp descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_)' +error_edit_warp_invalid_description: '§4Error: §cWarp descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_) and must be less than 255 characters in length' edit_warp_update_location: '§aRe-located the warp §l%1% §r§ato your current position.' edit_warp_update_description: '§aChanged the description for the warp §l%1%§r§a to "%2%".' edit_warp_update_name: '§aSuccessfully changed a warp''s name: %1% → §l%2%' \ No newline at end of file diff --git a/src/main/resources/Languages/zh-tw.yml b/src/main/resources/Languages/zh-tw.yml index a547b62e..3c848ff6 100644 --- a/src/main/resources/Languages/zh-tw.yml +++ b/src/main/resources/Languages/zh-tw.yml @@ -24,7 +24,7 @@ edit_home_privacy_private_success: '§a已將名爲§l%1%§r§a設置成私人' error_edit_home_privacy_already_public: '§4錯誤: §c你的家已設置成公開.' error_edit_home_privacy_already_private: '§4錯誤: §c你的家已設置私人.' error_edit_home_privacy_no_permission: '§4錯誤: §c你并沒有權限設置成公開.' -error_edit_home_invalid_description: '§4錯誤: §c介紹只能使用(A-Z, 0-9)字符, 符號(- , _)' +error_edit_home_invalid_description: '§4Error: §cHome descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_) and must be less than 255 characters in length' edit_home_update_location: '§a將§l%1% §r§a設置成你所在的位置.' edit_home_update_description: '§a已將§l%1%§r§a舊的介紹轉換成"%2%".' edit_home_update_name: '§a成功幫你的家改名: %1% → §l%2%' @@ -71,7 +71,9 @@ error_set_warp_invalid_characters: '§4Error: §cWarp names must be alphanumeric error_set_warp_invalid_length: '§4Error: §cWarp names can only contain up to 16 characters!' error_no_warps_set: '§4Error: §cThere aren''t any warps set!' edit_warp_title: '§aEdit details for the warp §l%1%§r§a:' -error_edit_warp_invalid_description: '§4Error: §cWarp descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_)' +error_edit_warp_invalid_description: '§4Error: §cWarp descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_) and must be less than 255 characters in length' edit_warp_update_location: '§aRe-located the warp §l%1% §r§ato your current position.' edit_warp_update_description: '§aChanged the description for the warp §l%1%§r§a to "%2%".' -edit_warp_update_name: '§aSuccessfully changed a warp''s name: %1% → §l%2%' \ No newline at end of file +edit_warp_update_name: '§aSuccessfully changed a warp''s name: %1% → §l%2%' +warp_list_page_top: '§aWarps: (§l%1%-%2%§r§a of §l%3%§r§a)' +warp_list_page_empty: '§7(There are no warps to list on this page)' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8c976d53..0987168c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -92,6 +92,11 @@ commands: usage: /tpdeny permission: huskhomes.tprequest aliases: [tpno] + rtp: + description: Teleport randomly into the wild + usage: /rtp + permission: huskhomes.rtp + aliases: [ randomtp, randomteleport, wild ] huskhomes: description: Plugin information & reload configs. usage: /huskhomes