diff --git a/src/com/esophose/playerparticles/ParticleCommandExecutor.java b/src/com/esophose/playerparticles/ParticleCommandExecutor.java index 3ae44e2..7ee99b5 100644 --- a/src/com/esophose/playerparticles/ParticleCommandExecutor.java +++ b/src/com/esophose/playerparticles/ParticleCommandExecutor.java @@ -129,7 +129,7 @@ public class ParticleCommandExecutor implements CommandExecutor { */ private void onData(Player p, String[] args) { ParticleEffect effect = ConfigManager.getInstance().getPPlayer(p.getUniqueId()).getParticleEffect(); - if (args.length == 1) { + if ((!effect.hasProperty(ParticleProperty.REQUIRES_DATA) && !effect.hasProperty(ParticleProperty.COLORABLE)) || args.length == 1) { if (effect.hasProperty(ParticleProperty.COLORABLE)) { if (effect == ParticleEffect.NOTE) { MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-note-data-usage", null), ChatColor.YELLOW); @@ -210,26 +210,32 @@ public class ParticleCommandExecutor implements CommandExecutor { int data = -1; try { - material = Material.matchMaterial(args[1]); + material = ParticlesUtil.closestMatch(args[1]); + if (material == null) material = Material.matchMaterial(args[1]); + if (material == null) throw new Exception(); } catch (Exception e) { - // Unknown item + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-item-data-unknown", args[1]), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } try { data = Integer.parseInt(args[2]); } catch (Exception e) { - // Invalid data + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-item-data-usage", null), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } if (material.isBlock()) { - // Material must be an item + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-item-data-mismatch", material.name()), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } if (data < 0 || data > 15) { - // Error data range must be between 0-15 + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-item-data-invalid-arguments", null), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } @@ -240,26 +246,32 @@ public class ParticleCommandExecutor implements CommandExecutor { int data = -1; try { - material = Material.matchMaterial(args[1]); + material = ParticlesUtil.closestMatch(args[1]); + if (material == null) material = Material.matchMaterial(args[1]); + if (material == null) throw new Exception(); } catch (Exception e) { - // Unknown block + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-block-data-unknown", args[1]), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } try { data = Integer.parseInt(args[2]); } catch (Exception e) { - // Invalid data + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-block-data-usage", null), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } - if (material.isBlock()) { - // Material must be a block + if (!material.isBlock()) { + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-block-data-mismatch", material.name()), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } if (data < 0 || data > 15) { - // Error data range must be between 0-15 + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-block-data-invalid-arguments", null), ChatColor.RED); + MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-usage", null) + ChatColor.AQUA + " /pp data <0-15>", ChatColor.YELLOW); return; } @@ -276,7 +288,7 @@ public class ParticleCommandExecutor implements CommandExecutor { * @param args The arguments for the command */ private void onReset(Player p, String[] args) { - ConfigManager.getInstance().saveEntirePPlayer(PPlayer.getNewPPlayer(p.getUniqueId())); + ConfigManager.getInstance().resetPPlayer(p.getUniqueId()); MessageManager.getInstance().sendMessage(p, MessageManager.getMessageFromConfig("message-reset", null), ChatColor.GREEN); } diff --git a/src/com/esophose/playerparticles/ParticlesUtil.java b/src/com/esophose/playerparticles/ParticlesUtil.java new file mode 100644 index 0000000..d36dcc5 --- /dev/null +++ b/src/com/esophose/playerparticles/ParticlesUtil.java @@ -0,0 +1,23 @@ +package com.esophose.playerparticles; + +import java.util.ArrayList; + +import org.bukkit.Material; + +public class ParticlesUtil { + + // TODO: Find a more reliable way of doing this that works better + @SuppressWarnings("deprecation") + public static Material closestMatch(String input) { + ArrayList matchList = new ArrayList(); + for (Material mat : Material.values()) + if (mat.name().replace("_", " ").toLowerCase().equals(input.toLowerCase()) || String.valueOf(mat.getId()).equals(input)) + return mat; + else if (mat.name().replace("_", " ").toLowerCase().contains(input.toLowerCase())) + matchList.add(mat); + + if (matchList.size() == 1) return matchList.get(0); + else return null; + } + +} diff --git a/src/com/esophose/playerparticles/PlayerParticles.java b/src/com/esophose/playerparticles/PlayerParticles.java index a773fa5..9ab86b0 100644 --- a/src/com/esophose/playerparticles/PlayerParticles.java +++ b/src/com/esophose/playerparticles/PlayerParticles.java @@ -113,7 +113,7 @@ public class PlayerParticles extends JavaPlugin { String user = getConfig().getString("database-user-name"); String pass = getConfig().getString("database-user-password"); mySQL = new MySQL(hostname, port, database, user, pass); - try (ResultSet res = mySQL.querySQL("SHOW TABLES LIKE 'playerparticles'")) { + try (ResultSet res = mySQL.querySQL("SHOW TABLES LIKE 'playerparticles'")) { // Clean up the old mess if (res.next()) { mySQL.updateSQL("DROP TABLE playerparticles"); } @@ -125,15 +125,16 @@ public class PlayerParticles extends JavaPlugin { try (ResultSet res = mySQL.querySQL("SHOW TABLES LIKE 'pp_users'")) { if (!res.next()) { // @formatter:off mySQL.updateSQL("CREATE TABLE pp_users (player_uuid VARCHAR(36), effect VARCHAR(32), style VARCHAR(32));" + - "CREATE TABLE pp_data_item (player_uuid VARCHAR(36), material VARCHAR(32), data TINYINT);" + - "CREATE TABLE pp_data_block (player_uuid VARCHAR(36), material VARCHAR(32), data TINYINT);" + - "CREATE TABLE pp_data_color (player_uuid VARCHAR(36), r TINYINT, g TINYINT, b TINYINT)" + - "CREATE TABLE pp_data_note (player_uuid VARCHAR(36), note TINYINT)" + "CREATE TABLE pp_data_item (player_uuid VARCHAR(36), material VARCHAR(32), data SMALLINT);" + + "CREATE TABLE pp_data_block (player_uuid VARCHAR(36), material VARCHAR(32), data SMALLINT);" + + "CREATE TABLE pp_data_color (player_uuid VARCHAR(36), r SMALLINT, g SMALLINT, b SMALLINT);" + + "CREATE TABLE pp_data_note (player_uuid VARCHAR(36), note SMALLINT);" ); // @formatter:on } useMySQL = true; } catch (ClassNotFoundException | SQLException e) { getLogger().info("[PlayerParticles] Failed to connect to the MySQL Database! Check to see if your login information is correct!"); + getLogger().info("Additional information: " + e.getMessage()); useMySQL = false; } } else { diff --git a/src/com/esophose/playerparticles/library/Database.java b/src/com/esophose/playerparticles/library/Database.java index a0420c6..a11281a 100644 --- a/src/com/esophose/playerparticles/library/Database.java +++ b/src/com/esophose/playerparticles/library/Database.java @@ -82,6 +82,8 @@ public abstract class Database { openConnection(); } + System.out.println("Running Query: " + query); + Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(query); @@ -94,6 +96,8 @@ public abstract class Database { * See {@link java.sql.Statement#executeUpdate(String)}
* If the connection is closed, it will be opened * + * Executes multiple updates broken up by semi-colons + * * @param query Query to be run * @return Result Code, see {@link java.sql.Statement#executeUpdate(String)} * @throws SQLException If the query cannot be executed @@ -106,11 +110,22 @@ public abstract class Database { } Statement statement = connection.createStatement(); + int[] results; - int result = statement.executeUpdate(query); + if (query.indexOf(';') != -1) { + String[] queries = query.split(";"); + for (String q : queries) { + statement.addBatch(q); + System.out.println("Running query: " + q); + } + results = statement.executeBatch(); + } else { + System.out.println("Running query: " + query); + results = new int[] { statement.executeUpdate(query) }; + } statement.close(); - return result; + return results[0]; } } diff --git a/src/com/esophose/playerparticles/library/ParticleEffect.java b/src/com/esophose/playerparticles/library/ParticleEffect.java index ddf04ee..54c5cf8 100644 --- a/src/com/esophose/playerparticles/library/ParticleEffect.java +++ b/src/com/esophose/playerparticles/library/ParticleEffect.java @@ -47,8 +47,14 @@ import com.esophose.playerparticles.library.ReflectionUtils.PackageType; /** * Modified a couple things for the plugin +<<<<<<< HEAD + * Updated to 1.10 + * + * @author (of changes) Esophose +======= * * @author Esophose +>>>>>>> refs/remotes/origin/master */ public enum ParticleEffect { diff --git a/src/com/esophose/playerparticles/manager/ConfigManager.java b/src/com/esophose/playerparticles/manager/ConfigManager.java index 87b2546..b2ba3f1 100644 --- a/src/com/esophose/playerparticles/manager/ConfigManager.java +++ b/src/com/esophose/playerparticles/manager/ConfigManager.java @@ -125,29 +125,30 @@ public class ConfigManager { return new PPlayer(playerUUID, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData); } else { PPlayer pplayer = PPlayer.getNewPPlayer(playerUUID); - saveEntirePPlayer(pplayer); + saveNewPPlayer(pplayer); return pplayer; } } else { String id = playerUUID.toString(); // @formatter:off try (ResultSet res = PlayerParticles.mySQL.querySQL("SELECT * FROM pp_users u " + "JOIN pp_data_item i ON u.player_uuid = i.player_uuid " + - "JOIN pp_data_block b ON u.player_uuid = b.player_uuid" + - "JOIN pp_data_color c ON u.player_uuid = c.player_uuid" + - "JOIN pp_data_note n ON u.player_uuid = n.player_uuid" + - "WHERE player_uuid = '" + id + "'")) { // @formatter:on + "JOIN pp_data_block b ON u.player_uuid = b.player_uuid " + + "JOIN pp_data_color c ON u.player_uuid = c.player_uuid " + + "JOIN pp_data_note n ON u.player_uuid = n.player_uuid " + + "WHERE u.player_uuid = '" + id + "'")) { // @formatter:on + if (res.next()) { ParticleEffect particleEffect = ParticleEffect.fromName(res.getString("u.effect")); ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("u.style")); ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material")), res.getByte("i.data")); BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material")), res.getByte("b.data")); - OrdinaryColor particleColorData = new OrdinaryColor(Color.fromRGB(res.getByte("c.r"), res.getByte("c.g"), res.getByte("c.b"))); + OrdinaryColor particleColorData = new OrdinaryColor(Color.fromRGB(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b"))); NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note")); return new PPlayer(playerUUID, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData); } else { PPlayer pplayer = PPlayer.getNewPPlayer(playerUUID); - saveEntirePPlayer(pplayer); + saveNewPPlayer(pplayer); return pplayer; } } catch (ClassNotFoundException | SQLException e) { @@ -155,18 +156,16 @@ public class ConfigManager { } } - return null; // This should never get called unless there is a database error + // This should only be returned if there is a database or config error + return null; } /** - * Saves an entire PPlayer to the database - * This is mainly used for when PPlayers are being created for the first time - * It can also be called when a player is getting reset with /pp reset - * Use the singular methods for updating individual parts of the PPlayer + * Saves a new PPlayer to the database or the file * * @param pplayer The PPlayer to save */ - public void saveEntirePPlayer(PPlayer pplayer) { + public void saveNewPPlayer(PPlayer pplayer) { if (!PlayerParticles.useMySQL) { if (!config.isConfigurationSection(pplayer.getUniqueId().toString())) { String id = pplayer.getUniqueId().toString(); @@ -197,52 +196,36 @@ public class ConfigManager { colorDataSection.set("g", pplayer.getColorData().getGreen()); colorDataSection.set("b", pplayer.getColorData().getBlue()); noteColorDataSection.set("note", (byte) (pplayer.getNoteColorData().getValueX() * 24)); - + save(); } else { - try (ResultSet res = PlayerParticles.mySQL.querySQL("SELECT COUNT(*) FROM pp_users WHERE player_uuid = '" + pplayer.getUniqueId() + "'")) { - if (res.next()) { // @formatter:off - PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET " + - "effect = '" + pplayer.getParticleEffect().getName() + "', " + - "style = '" + pplayer.getParticleStyle().getName() + "' " + - "WHERE player_uuid = " + pplayer.getUniqueId().toString() + "; " + - "UPDATE pp_data_item SET " + - "material = '" + pplayer.getItemData().getMaterial().name() + "', " + - "data = " + pplayer.getItemData().getData() + " " + - "WHERE player_uuid = " + pplayer.getUniqueId().toString() + "; " + - "UPDATE pp_date_block SET " + - "material = '" + pplayer.getBlockData().getMaterial().name() + "', " + - "data = " + pplayer.getBlockData().getData() + " " + - "WHERE player_uuid = " + pplayer.getUniqueId().toString() + "; " + - "UPDATE pp_data_color SET " + - "r = " + pplayer.getColorData().getRed() + ", " + - "g = " + pplayer.getColorData().getGreen() + ", " + - "b = " + pplayer.getColorData().getBlue() + " " + - "WHERE player_uuid = " + pplayer.getUniqueId().toString() + "; " + - "UPDATE pp_data_note SET" + - "note = " + (byte) (pplayer.getNoteColorData().getValueX() * 24) + " " + - "WHERE player_uuid = " + pplayer.getUniqueId().toString() + ";" - ); - } else { + try (ResultSet res = PlayerParticles.mySQL.querySQL("SELECT * FROM pp_users WHERE player_uuid = '" + pplayer.getUniqueId() + "'")) { + if (res.next()) { + throw new RuntimeException("The user " + pplayer.getUniqueId() + " is already in the database. They can not be added."); + } else { // @formatter:off PlayerParticles.mySQL.updateSQL("INSERT INTO pp_users (player_uuid, effect, style) VALUES (" + "'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getParticleEffect().getName() + "', " + "'" + pplayer.getParticleStyle().getName() + "'" + "); " + - "INSERT INTO pp_data_item (material, data) VALUES (" + + "INSERT INTO pp_data_item (player_uuid, material, data) VALUES (" + + "'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getItemData().getMaterial().name() + "', " + pplayer.getItemData().getData() + "); " + - "INSERT INTO pp_data_block (material, data) VALUES (" + + "INSERT INTO pp_data_block (player_uuid, material, data) VALUES (" + + "'" + pplayer.getUniqueId().toString() + "', " + "'" + pplayer.getBlockData().getMaterial().name() + "', " + pplayer.getBlockData().getData() + "); " + - "INSERT INTO pp_data_color (r, g, b) VALUES (" + + "INSERT INTO pp_data_color (player_uuid, r, g, b) VALUES (" + + "'" + pplayer.getUniqueId().toString() + "', " + pplayer.getColorData().getRed() + ", " + pplayer.getColorData().getGreen() + ", " + pplayer.getColorData().getBlue() + "); " + - "INSERT INTO pp_data_note (note) VALUES (" + + "INSERT INTO pp_data_note (player_uuid, note) VALUES (" + + "'" + pplayer.getUniqueId().toString() + "', " + (byte) (pplayer.getNoteColorData().getValueX() * 24) + ");" ); @@ -251,10 +234,26 @@ public class ConfigManager { e.printStackTrace(); } } - + ParticleCreator.updateIfContains(pplayer); // Update the player in case this is a /pp reset } + /** + * Resets all saved information about a PPlayer + * This should be made into a single batch query in the future + * + * @param playerUUID + */ + public void resetPPlayer(UUID playerUUID) { + PPlayer pplayer = PPlayer.getNewPPlayer(playerUUID); + savePPlayer(playerUUID, pplayer.getParticleEffect()); + savePPlayer(playerUUID, pplayer.getParticleStyle()); + savePPlayer(playerUUID, pplayer.getItemData()); + savePPlayer(playerUUID, pplayer.getBlockData()); + savePPlayer(playerUUID, pplayer.getColorData()); + savePPlayer(playerUUID, pplayer.getNoteColorData()); + } + /** * Saves the effect to the player's save file or database entry * @@ -268,7 +267,7 @@ public class ConfigManager { save(); } else { try { - PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET effect = '" + particleEffect.getName() + "' WHERE player_uuid = " + playerUUID + ";"); + PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET effect = '" + particleEffect.getName() + "' WHERE player_uuid = '" + playerUUID + "';"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } @@ -289,7 +288,7 @@ public class ConfigManager { save(); } else { try { - PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET style = '" + particleStyle.getName() + "' WHERE player_uuid = " + playerUUID + ";"); + PlayerParticles.mySQL.updateSQL("UPDATE pp_users SET style = '" + particleStyle.getName() + "' WHERE player_uuid = '" + playerUUID + "';"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } @@ -311,7 +310,7 @@ public class ConfigManager { save(); } else { try { - PlayerParticles.mySQL.updateSQL("UPDATE pp_data_item SET material = '" + particleItemData.getMaterial().name() + "', data = '" + particleItemData.getData() + "' WHERE player_uuid = " + playerUUID + ";"); + PlayerParticles.mySQL.updateSQL("UPDATE pp_data_item SET material = '" + particleItemData.getMaterial().name() + "', data = '" + particleItemData.getData() + "' WHERE player_uuid = '" + playerUUID + "';"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } @@ -333,7 +332,7 @@ public class ConfigManager { save(); } else { try { - PlayerParticles.mySQL.updateSQL("UPDATE pp_data_block SET material = '" + particleBlockData.getMaterial().name() + "', data = '" + particleBlockData.getData() + "' WHERE player_uuid = " + playerUUID + ";"); + PlayerParticles.mySQL.updateSQL("UPDATE pp_data_block SET material = '" + particleBlockData.getMaterial().name() + "', data = '" + particleBlockData.getData() + "' WHERE player_uuid = '" + playerUUID + "';"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } @@ -356,7 +355,7 @@ public class ConfigManager { save(); } else { try { - PlayerParticles.mySQL.updateSQL("UPDATE pp_data_color SET r = " + particleColorData.getRed() + ", g = " + particleColorData.getGreen() + ", b = " + particleColorData.getBlue() + " WHERE player_uuid = " + playerUUID + ";"); + PlayerParticles.mySQL.updateSQL("UPDATE pp_data_color SET r = " + particleColorData.getRed() + ", g = " + particleColorData.getGreen() + ", b = " + particleColorData.getBlue() + " WHERE player_uuid = '" + playerUUID + "';"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } @@ -377,7 +376,7 @@ public class ConfigManager { save(); } else { try { - PlayerParticles.mySQL.updateSQL("UPDATE pp_data_color SET note = " + (byte) (particleNoteColorData.getValueX() * 24) + " WHERE player_uuid = " + playerUUID + ";"); + PlayerParticles.mySQL.updateSQL("UPDATE pp_data_note SET note = " + (byte) (particleNoteColorData.getValueX() * 24) + " WHERE player_uuid = '" + playerUUID + "';"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } diff --git a/src/config.yml b/src/config.yml index b68ae2c..590fa5b 100644 --- a/src/config.yml +++ b/src/config.yml @@ -173,8 +173,11 @@ message-block-data-material-unknown: 'The block name you supplied is invalid' # Default: 'The item name you supplied is invalid' message-item-data-material-unknown: 'The item name you supplied is invalid' +<<<<<<< HEAD +======= +>>>>>>> refs/remotes/origin/master # ------------- # # Other # # ------------- # @@ -238,4 +241,7 @@ database-user-name: '' # Database User Password # Default: '' -database-user-password: '' \ No newline at end of file +database-user-password: '' + +# That's everything! You reached the end of the configuration +# Enjoy the plugin! \ No newline at end of file