Add colorcode translation and MySQL field config

This commit is contained in:
Georg 2015-03-23 19:22:06 +01:00
parent c813804707
commit f4b513f153
6 changed files with 64 additions and 24 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId> <groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId> <artifactId>MinePacks</artifactId>
<version>1.4</version> <version>1.4.1</version>
<repositories> <repositories>
<repository> <repository>
<id>in-project</id> <id>in-project</id>

View File

@ -31,7 +31,7 @@ public class Config
{ {
private MinePacks MP; private MinePacks MP;
private FileConfiguration config; private FileConfiguration config;
private static final int CONFIG_VERSION = 1; private static final int CONFIG_VERSION = 2;
public Config(MinePacks mp) public Config(MinePacks mp)
{ {
@ -95,6 +95,12 @@ private void NewConfig(File file)
config.set("Database.MySQL.Password", "minecraft"); config.set("Database.MySQL.Password", "minecraft");
config.set("Database.Tables.User", "backpack_players"); config.set("Database.Tables.User", "backpack_players");
config.set("Database.Tables.Backpack", "backpacks"); 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); config.set("Version",CONFIG_VERSION);
try try
@ -112,7 +118,13 @@ private boolean UpdateConfig(File file)
{ {
switch(config.getInt("Version")) 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; break;
case CONFIG_VERSION: return false; case CONFIG_VERSION: return false;
default: MP.log.info("Config File Version newer than expected!"); 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"); return config.getString("Database.Tables.Backpack", "backpacks");
} }
public String getDBFields(String sub)
{
return config.getString("Database.Tables.Fields." + sub);
}
public boolean getUpdatePlayer() public boolean getUpdatePlayer()
{ {
return config.getBoolean("Database.UpdatePlayer", true); return config.getBoolean("Database.UpdatePlayer", true);

View File

@ -38,7 +38,7 @@ public class MySQL extends Database
private Connection conn = null; private Connection conn = null;
private String Table_Players, Table_Backpacks; // Table Names 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; private boolean UpdatePlayer;
public MySQL(MinePacks mp) public MySQL(MinePacks mp)
@ -49,6 +49,7 @@ public MySQL(MinePacks mp)
Table_Backpacks = plugin.config.getBackpackTable(); Table_Backpacks = plugin.config.getBackpackTable();
UpdatePlayer = plugin.config.getUpdatePlayer(); UpdatePlayer = plugin.config.getUpdatePlayer();
BuildQuerys();
CheckDB(); // Check Database CheckDB(); // Check Database
if(plugin.config.UseUUIDs()) 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() private void CheckUUIDs()
{ {
try try
{ {
List<String> converter = new ArrayList<String>(); List<String> converter = new ArrayList<String>();
Statement stmt = GetConnection().createStatement(); 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()) while(res.next())
{ {
if(res.isFirst()) if(res.isFirst())
{ {
plugin.log.info(plugin.lang.Get("Console.UpdateUUIDs")); 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) if(converter.size() > 0)
{ {
@ -107,19 +129,19 @@ private void CheckDB()
try try
{ {
Statement stmt = GetConnection().createStatement(); 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) if(plugin.UseUUIDs)
{ {
try 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) { } 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 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) { } catch(SQLException e) { }
stmt.close(); stmt.close();
@ -146,7 +168,7 @@ public void run()
{ {
PreparedStatement ps; PreparedStatement ps;
Connection con = DriverManager.getConnection("jdbc:mysql://" + plugin.config.GetMySQLHost() + "/" + plugin.config.GetMySQLDatabase(), plugin.config.GetMySQLUser(), plugin.config.GetMySQLPassword());; 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) if(plugin.UseUUIDs)
{ {
ps.setString(1, player.getUniqueId().toString().replace("-", "")); ps.setString(1, player.getUniqueId().toString().replace("-", ""));
@ -165,7 +187,7 @@ public void run()
con.close(); con.close();
return; return;
} }
ps = con.prepareStatement("UPDATE `" + Table_Players + "` SET `name`=? WHERE `uuid`=?;"); ps = con.prepareStatement(Query_UpdatePlayerUUID);
ps.setString(1, player.getName()); ps.setString(1, player.getName());
ps.setString(2, player.getUniqueId().toString().replace("-", "")); ps.setString(2, player.getUniqueId().toString().replace("-", ""));
} }
@ -173,7 +195,7 @@ public void run()
{ {
rs.close(); rs.close();
ps.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()); ps.setString(1, player.getName());
if(plugin.UseUUIDs) if(plugin.UseUUIDs)
{ {
@ -200,7 +222,7 @@ public void SaveBackpack(Backpack backpack)
// Building the mysql statement // Building the mysql statement
if(backpack.getID() <= 0) 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) if(plugin.UseUUIDs)
{ {
ps.setString(1, backpack.getOwner().getUniqueId().toString().replace("-", "")); ps.setString(1, backpack.getOwner().getUniqueId().toString().replace("-", ""));
@ -221,7 +243,7 @@ public void SaveBackpack(Backpack backpack)
} }
rs.close(); rs.close();
ps.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.setInt(1, backpack.getID());
ps.setBytes(2, itsSerializer.Serialize(backpack.getBackpack())); ps.setBytes(2, itsSerializer.Serialize(backpack.getBackpack()));
ps.setInt(3, itsSerializer.getUsedVersion()); ps.setInt(3, itsSerializer.getUsedVersion());
@ -234,7 +256,7 @@ public void SaveBackpack(Backpack backpack)
} }
else 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.setBytes(1, itsSerializer.Serialize(backpack.getBackpack()));
ps.setInt(2, itsSerializer.getUsedVersion()); ps.setInt(2, itsSerializer.getUsedVersion());
ps.setInt(3, backpack.getID()); ps.setInt(3, backpack.getID());
@ -253,7 +275,7 @@ public Backpack LoadBackpack(OfflinePlayer player)
try try
{ {
PreparedStatement ps = null; // Statement Variable 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) if(plugin.UseUUIDs)
{ {
ps.setString(1, player.getUniqueId().toString().replace("-", "")); ps.setString(1, player.getUniqueId().toString().replace("-", ""));

View File

@ -17,6 +17,7 @@
package at.pcgamingfreaks.georgh.MinePacks; package at.pcgamingfreaks.georgh.MinePacks;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -40,8 +41,8 @@ public EventListener(MinePacks mp)
plugin = mp; plugin = mp;
drop_on_death = plugin.config.getDropOnDeath(); drop_on_death = plugin.config.getDropOnDeath();
Message_OwnBPClose = plugin.lang.Get("Ingame.OwnBackPackClose"); Message_OwnBPClose = ChatColor.translateAlternateColorCodes('&', plugin.lang.Get("Ingame.OwnBackPackClose"));
Message_PlayerBPClose = plugin.lang.Get("Ingame.PlayerBackPackClose"); Message_PlayerBPClose = ChatColor.translateAlternateColorCodes('&', plugin.lang.Get("Ingame.PlayerBackPackClose"));
} }
@EventHandler @EventHandler

View File

@ -56,7 +56,7 @@ public void onEnable()
getServer().getPluginManager().registerEvents(new EventListener(this), this); getServer().getPluginManager().registerEvents(new EventListener(this), this);
BackpackTitle = config.getBPTitle(); 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); getServer().getServicesManager().register(MinePacks.class, this, this, ServicePriority.Normal);
log.info(lang.Get("Console.Enabled")); log.info(lang.Get("Console.Enabled"));
} }

View File

@ -37,10 +37,10 @@ public class OnCommand implements CommandExecutor
public OnCommand(MinePacks mp) public OnCommand(MinePacks mp)
{ {
plugin = mp; plugin = mp;
Message_NotFromConsole = plugin.lang.Get("Console.NotFromConsole"); Message_NotFromConsole = ChatColor.translateAlternateColorCodes('&', plugin.lang.Get("Console.NotFromConsole"));
Message_NoPermission = ChatColor.RED + plugin.lang.Get("Ingame.NoPermission"); Message_NoPermission = ChatColor.translateAlternateColorCodes('&', ChatColor.RED + plugin.lang.Get("Ingame.NoPermission"));
Message_IvalidBackpack = ChatColor.RED + plugin.lang.Get("Ingame.IvalidBackpack"); Message_IvalidBackpack = ChatColor.translateAlternateColorCodes('&', ChatColor.RED + plugin.lang.Get("Ingame.IvalidBackpack"));
Message_BackpackCleaned = ChatColor.DARK_GREEN + plugin.lang.Get("Ingame.BackpackCleaned"); Message_BackpackCleaned = ChatColor.translateAlternateColorCodes('&', ChatColor.DARK_GREEN + plugin.lang.Get("Ingame.BackpackCleaned"));
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")