From f4b513f1533671aece42e76a47aa0f9d5910e345 Mon Sep 17 00:00:00 2001 From: Georg Date: Mon, 23 Mar 2015 19:22:06 +0100 Subject: [PATCH] Add colorcode translation and MySQL field config --- pom.xml | 2 +- .../georgh/MinePacks/Database/Config.java | 21 +++++++- .../georgh/MinePacks/Database/MySQL.java | 50 +++++++++++++------ .../georgh/MinePacks/EventListener.java | 5 +- .../georgh/MinePacks/MinePacks.java | 2 +- .../georgh/MinePacks/OnCommand.java | 8 +-- 6 files changed, 64 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index ae5d5ee..84a9a6f 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 at.pcgamingfreaks MinePacks - 1.4 + 1.4.1 in-project diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/Config.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Config.java index bd3993c..c1a80ef 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/Config.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/Config.java @@ -31,7 +31,7 @@ public class Config { private MinePacks MP; private FileConfiguration config; - private static final int CONFIG_VERSION = 1; + private static final int CONFIG_VERSION = 2; public Config(MinePacks mp) { @@ -95,6 +95,12 @@ private void NewConfig(File file) config.set("Database.MySQL.Password", "minecraft"); config.set("Database.Tables.User", "backpack_players"); config.set("Database.Tables.Backpack", "backpacks"); + config.set("Database.Tables.Fields.User.Player_ID", "player_id"); + config.set("Database.Tables.Fields.User.Name", "name"); + config.set("Database.Tables.Fields.User.UUID", "uuid"); + config.set("Database.Tables.Fields.Backpack.Owner_ID", "owner"); + config.set("Database.Tables.Fields.Backpack.ItemStacks", "itemstacks"); + config.set("Database.Tables.Fields.Backpack.Version", "version"); config.set("Version",CONFIG_VERSION); try @@ -112,7 +118,13 @@ private boolean UpdateConfig(File file) { switch(config.getInt("Version")) { - case 0: + case 1: + config.set("Database.Tables.Fields.User.Player_ID", "player_id"); + config.set("Database.Tables.Fields.User.Name", "name"); + config.set("Database.Tables.Fields.User.UUID", "uuid"); + config.set("Database.Tables.Fields.Backpack.Owner_ID", "owner"); + config.set("Database.Tables.Fields.Backpack.ItemStacks", "itemstacks"); + config.set("Database.Tables.Fields.Backpack.Version", "version"); break; case CONFIG_VERSION: return false; default: MP.log.info("Config File Version newer than expected!"); return false; @@ -176,6 +188,11 @@ public String getBackpackTable() return config.getString("Database.Tables.Backpack", "backpacks"); } + public String getDBFields(String sub) + { + return config.getString("Database.Tables.Fields." + sub); + } + public boolean getUpdatePlayer() { return config.getBoolean("Database.UpdatePlayer", true); diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java b/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java index 82ce795..a7c4e2b 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/Database/MySQL.java @@ -38,7 +38,7 @@ public class MySQL extends Database private Connection conn = null; private String Table_Players, Table_Backpacks; // Table Names - + private String Query_UpdatePlayerGet, Query_UpdatePlayerUUID, Query_UpdatePlayerAdd, Query_GetPlayerID, Query_InsertBP, Query_UpdateBP, Query_GetBP; private boolean UpdatePlayer; public MySQL(MinePacks mp) @@ -49,6 +49,7 @@ public MySQL(MinePacks mp) Table_Backpacks = plugin.config.getBackpackTable(); UpdatePlayer = plugin.config.getUpdatePlayer(); + BuildQuerys(); CheckDB(); // Check Database if(plugin.config.UseUUIDs()) { @@ -56,20 +57,41 @@ public MySQL(MinePacks mp) } } + private void BuildQuerys() + { + if(plugin.UseUUIDs) + { + Query_UpdatePlayerGet = "SELECT `" + plugin.config.getDBFields("User.Player_ID") + "` FROM `" + Table_Players + "` WHERE `" + plugin.config.getDBFields("User.UUID") + "`=?;"; + Query_UpdatePlayerUUID = "UPDATE `" + Table_Players + "` SET `" + plugin.config.getDBFields("User.Name") + "`=? WHERE `" + plugin.config.getDBFields("User.UUID") + "`=?;"; + Query_UpdatePlayerAdd = "INSERT INTO `" + Table_Players + "` (`" + plugin.config.getDBFields("User.Name") + "`,`" + plugin.config.getDBFields("User.UUID") + "`) VALUES (?,?);"; + Query_GetPlayerID = "SELECT `" + plugin.config.getDBFields("User.Player_ID") + "` FROM `" + Table_Players + "` WHERE `" + plugin.config.getDBFields("User.UUID") + "`=?;"; + Query_GetBP = "SELECT `" + plugin.config.getDBFields("Backpack.Owner_ID") + "`,`" + plugin.config.getDBFields("Backpack.ItemStacks") + "`,`" + plugin.config.getDBFields("Backpack.Version") + "` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `" + plugin.config.getDBFields("Backpack.Owner_ID") + "`=`" + plugin.config.getDBFields("User.Player_ID") + "` WHERE `" + plugin.config.getDBFields("User.UUID") + "`=?;"; + } + else + { + Query_UpdatePlayerGet = "SELECT `" + plugin.config.getDBFields("User.Player_ID") + "` FROM `" + Table_Players + "` WHERE `" + plugin.config.getDBFields("User.Name") + "`=?;"; + Query_UpdatePlayerAdd = "INSERT INTO `" + Table_Players + "` (`" + plugin.config.getDBFields("User.Name") + "`) VALUES (?);"; + Query_GetPlayerID = "SELECT `" + plugin.config.getDBFields("User.Player_ID") + "` FROM `" + Table_Players + "` WHERE `" + plugin.config.getDBFields("User.Name") + "`=?;"; + Query_GetBP = "SELECT `" + plugin.config.getDBFields("Backpack.Owner_ID") + "`,`" + plugin.config.getDBFields("Backpack.ItemStacks") + "`,`" + plugin.config.getDBFields("Backpack.Version") + "` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `" + plugin.config.getDBFields("Backpack.Owner_ID") + "`=`" + plugin.config.getDBFields("User.Player_ID") + "` WHERE `" + plugin.config.getDBFields("User.Name") + "`=?;"; + } + Query_InsertBP = "INSERT INTO `" + Table_Backpacks + "` (`" + plugin.config.getDBFields("Backpack.Owner_ID") + "`, `" + plugin.config.getDBFields("Backpack.ItemStacks") + "`, `" + plugin.config.getDBFields("Backpack.Version") + "`) VALUES (?,?,?);"; + Query_UpdateBP = "UPDATE `" + Table_Backpacks + "` SET `" + plugin.config.getDBFields("Backpack.ItemStacks") + "`=?,`" + plugin.config.getDBFields("Backpack.Version") + "`=? WHERE `" + plugin.config.getDBFields("Backpack.Owner_ID") + "`=?;"; + } + private void CheckUUIDs() { try { List converter = new ArrayList(); Statement stmt = GetConnection().createStatement(); - ResultSet res = stmt.executeQuery("SELECT `name` FROM `" + Table_Players + "` WHERE `uuid` IS NULL"); + ResultSet res = stmt.executeQuery("SELECT `" + plugin.config.getDBFields("User.Name") + "` FROM `" + Table_Players + "` WHERE `" + plugin.config.getDBFields("User.UUID") + "` IS NULL"); while(res.next()) { if(res.isFirst()) { plugin.log.info(plugin.lang.Get("Console.UpdateUUIDs")); } - converter.add("UPDATE `" + Table_Players + "` SET `uuid`='" + UUIDConverter.getUUIDFromName(res.getString(1), true) + "' WHERE `name`='" + res.getString(1).replace("\\", "\\\\").replace("'", "\\'") + "'"); + converter.add("UPDATE `" + Table_Players + "` SET `" + plugin.config.getDBFields("User.UUID") + "`='" + UUIDConverter.getUUIDFromName(res.getString(1), true) + "' WHERE `" + plugin.config.getDBFields("User.Name") + "`='" + res.getString(1).replace("\\", "\\\\").replace("'", "\\'") + "'"); } if(converter.size() > 0) { @@ -107,19 +129,19 @@ private void CheckDB() try { Statement stmt = GetConnection().createStatement(); - stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Players + "` (`player_id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `name` CHAR(16) NOT NULL UNIQUE, PRIMARY KEY (`player_id`));"); + stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Players + "` (`" + plugin.config.getDBFields("User.Player_ID") + "` INT UNSIGNED NOT NULL AUTO_INCREMENT, `" + plugin.config.getDBFields("User.Name") + "` CHAR(16) NOT NULL UNIQUE, PRIMARY KEY (`player_id`));"); if(plugin.UseUUIDs) { try { - stmt.execute("ALTER TABLE `" + Table_Players + "` ADD COLUMN `uuid` CHAR(32) UNIQUE;"); + stmt.execute("ALTER TABLE `" + Table_Players + "` ADD COLUMN `" + plugin.config.getDBFields("User.UUID") + "` CHAR(32) UNIQUE;"); } catch(SQLException e) { } } - stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Backpacks + "` (`owner` INT UNSIGNED NOT NULL, `itemstacks` BLOB, PRIMARY KEY (`owner`));"); + stmt.execute("CREATE TABLE IF NOT EXISTS `" + Table_Backpacks + "` (`" + plugin.config.getDBFields("Backpack.Owner_ID") + "` INT UNSIGNED NOT NULL, `" + plugin.config.getDBFields("Backpack.ItemStacks") + "` BLOB, PRIMARY KEY (`" + plugin.config.getDBFields("Backpack.Owner_ID") + "`));"); try { - stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `version` INT DEFAULT 0;"); + stmt.execute("ALTER TABLE `" + Table_Backpacks + "` ADD COLUMN `" + plugin.config.getDBFields("Backpack.Version") + "` INT DEFAULT 0;"); } catch(SQLException e) { } stmt.close(); @@ -146,7 +168,7 @@ public void run() { PreparedStatement ps; Connection con = DriverManager.getConnection("jdbc:mysql://" + plugin.config.GetMySQLHost() + "/" + plugin.config.GetMySQLDatabase(), plugin.config.GetMySQLUser(), plugin.config.GetMySQLPassword());; - ps = con.prepareStatement("SELECT `player_id` FROM `" + Table_Players + "` WHERE " + ((plugin.UseUUIDs) ? "`uuid`" : "`name`") + "=?;"); + ps = con.prepareStatement(Query_UpdatePlayerGet); if(plugin.UseUUIDs) { ps.setString(1, player.getUniqueId().toString().replace("-", "")); @@ -165,7 +187,7 @@ public void run() con.close(); return; } - ps = con.prepareStatement("UPDATE `" + Table_Players + "` SET `name`=? WHERE `uuid`=?;"); + ps = con.prepareStatement(Query_UpdatePlayerUUID); ps.setString(1, player.getName()); ps.setString(2, player.getUniqueId().toString().replace("-", "")); } @@ -173,7 +195,7 @@ public void run() { rs.close(); ps.close(); - ps = con.prepareStatement("INSERT INTO `" + Table_Players + "` (`name`" + ((plugin.UseUUIDs) ? ",`uuid`" : "") + ") VALUES (?" + ((plugin.UseUUIDs) ? ",?" : "") + ");"); + ps = con.prepareStatement(Query_UpdatePlayerAdd); ps.setString(1, player.getName()); if(plugin.UseUUIDs) { @@ -200,7 +222,7 @@ public void SaveBackpack(Backpack backpack) // Building the mysql statement if(backpack.getID() <= 0) { - ps = GetConnection().prepareStatement("SELECT `player_id` FROM `" + Table_Players + "` WHERE " + ((plugin.UseUUIDs) ? "`uuid`" : "`name`")+ "=?;"); + ps = GetConnection().prepareStatement(Query_GetPlayerID); if(plugin.UseUUIDs) { ps.setString(1, backpack.getOwner().getUniqueId().toString().replace("-", "")); @@ -221,7 +243,7 @@ public void SaveBackpack(Backpack backpack) } rs.close(); ps.close(); - ps = GetConnection().prepareStatement("INSERT INTO `" + Table_Backpacks + "` (`owner`, `itemstacks`, `version`) VALUES (?,?,?);", Statement.RETURN_GENERATED_KEYS); + ps = GetConnection().prepareStatement(Query_InsertBP, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, backpack.getID()); ps.setBytes(2, itsSerializer.Serialize(backpack.getBackpack())); ps.setInt(3, itsSerializer.getUsedVersion()); @@ -234,7 +256,7 @@ public void SaveBackpack(Backpack backpack) } else { - ps = GetConnection().prepareStatement("UPDATE `" + Table_Backpacks + "` SET `itemstacks`=?,`version`=? WHERE `owner`=?"); + ps = GetConnection().prepareStatement(Query_UpdateBP); ps.setBytes(1, itsSerializer.Serialize(backpack.getBackpack())); ps.setInt(2, itsSerializer.getUsedVersion()); ps.setInt(3, backpack.getID()); @@ -253,7 +275,7 @@ public Backpack LoadBackpack(OfflinePlayer player) try { PreparedStatement ps = null; // Statement Variable - ps = GetConnection().prepareStatement("SELECT `owner`,`itemstacks`,`version` FROM `" + Table_Backpacks + "` INNER JOIN `" + Table_Players + "` ON `owner`=`player_id` WHERE " + ((plugin.UseUUIDs) ? "`uuid`" : "`name`")+ "=?;"); + ps = GetConnection().prepareStatement(Query_GetBP); if(plugin.UseUUIDs) { ps.setString(1, player.getUniqueId().toString().replace("-", "")); diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/EventListener.java b/src/at/pcgamingfreaks/georgh/MinePacks/EventListener.java index b6138dc..afd53c0 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/EventListener.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/EventListener.java @@ -17,6 +17,7 @@ package at.pcgamingfreaks.georgh.MinePacks; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -40,8 +41,8 @@ public EventListener(MinePacks mp) plugin = mp; drop_on_death = plugin.config.getDropOnDeath(); - Message_OwnBPClose = plugin.lang.Get("Ingame.OwnBackPackClose"); - Message_PlayerBPClose = plugin.lang.Get("Ingame.PlayerBackPackClose"); + Message_OwnBPClose = ChatColor.translateAlternateColorCodes('&', plugin.lang.Get("Ingame.OwnBackPackClose")); + Message_PlayerBPClose = ChatColor.translateAlternateColorCodes('&', plugin.lang.Get("Ingame.PlayerBackPackClose")); } @EventHandler diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/MinePacks.java b/src/at/pcgamingfreaks/georgh/MinePacks/MinePacks.java index 194c89a..3433651 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/MinePacks.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/MinePacks.java @@ -56,7 +56,7 @@ public void onEnable() getServer().getPluginManager().registerEvents(new EventListener(this), this); BackpackTitle = config.getBPTitle(); - Message_IvalidBackpack = ChatColor.RED + lang.Get("Ingame.IvalidBackpack"); + Message_IvalidBackpack = ChatColor.translateAlternateColorCodes('&', ChatColor.RED + lang.Get("Ingame.IvalidBackpack")); getServer().getServicesManager().register(MinePacks.class, this, this, ServicePriority.Normal); log.info(lang.Get("Console.Enabled")); } diff --git a/src/at/pcgamingfreaks/georgh/MinePacks/OnCommand.java b/src/at/pcgamingfreaks/georgh/MinePacks/OnCommand.java index b5075fc..c63598c 100644 --- a/src/at/pcgamingfreaks/georgh/MinePacks/OnCommand.java +++ b/src/at/pcgamingfreaks/georgh/MinePacks/OnCommand.java @@ -37,10 +37,10 @@ public class OnCommand implements CommandExecutor public OnCommand(MinePacks mp) { plugin = mp; - Message_NotFromConsole = plugin.lang.Get("Console.NotFromConsole"); - Message_NoPermission = ChatColor.RED + plugin.lang.Get("Ingame.NoPermission"); - Message_IvalidBackpack = ChatColor.RED + plugin.lang.Get("Ingame.IvalidBackpack"); - Message_BackpackCleaned = ChatColor.DARK_GREEN + plugin.lang.Get("Ingame.BackpackCleaned"); + Message_NotFromConsole = ChatColor.translateAlternateColorCodes('&', plugin.lang.Get("Console.NotFromConsole")); + Message_NoPermission = ChatColor.translateAlternateColorCodes('&', ChatColor.RED + plugin.lang.Get("Ingame.NoPermission")); + Message_IvalidBackpack = ChatColor.translateAlternateColorCodes('&', ChatColor.RED + plugin.lang.Get("Ingame.IvalidBackpack")); + Message_BackpackCleaned = ChatColor.translateAlternateColorCodes('&', ChatColor.DARK_GREEN + plugin.lang.Get("Ingame.BackpackCleaned")); } @SuppressWarnings("deprecation")