From 25e7d22110a55289f4d3528215f7368fcc39318b Mon Sep 17 00:00:00 2001 From: Esophose Date: Tue, 25 Sep 2018 19:41:00 -0600 Subject: [PATCH] Data management --- .../playerparticles/PlayerParticles.java | 32 +- .../command/ParticleCommandCompleter.java | 2 +- .../command/ParticleCommandExecutor.java | 2 +- .../database/DatabaseConnector.java | 41 - .../gui/PlayerParticlesGui.java | 2 +- .../playerparticles/manager/DataManager.java | 739 +++++++----------- .../manager/ParticleManager.java | 105 +-- .../playerparticles/particles/PPlayer.java | 104 ++- .../particles/ParticleEffect.java | 9 + .../particles/ParticleGroup.java | 34 + .../particles/ParticlePair.java | 100 ++- .../styles/ParticleStyleBlockBreak.java | 2 +- .../styles/ParticleStyleBlockEdit.java | 4 +- .../styles/ParticleStyleBlockPlace.java | 2 +- .../styles/ParticleStyleHurt.java | 2 +- .../styles/ParticleStyleMove.java | 2 +- .../styles/ParticleStyleSwords.java | 2 +- 17 files changed, 515 insertions(+), 669 deletions(-) create mode 100644 src/com/esophose/playerparticles/particles/ParticleGroup.java diff --git a/src/com/esophose/playerparticles/PlayerParticles.java b/src/com/esophose/playerparticles/PlayerParticles.java index 8ddffcc..924408d 100644 --- a/src/com/esophose/playerparticles/PlayerParticles.java +++ b/src/com/esophose/playerparticles/PlayerParticles.java @@ -7,9 +7,8 @@ */ /* - * TODO: v5.2 + * TODO: v5.3 * + Command to force set an effect/style for a player - * + Tab completion for fixed effects * + Add new style 'tornado' * + Add new style 'companion' * + Add new style 'atom' @@ -159,14 +158,14 @@ public class PlayerParticles extends JavaPlugin { try (Statement statement = connection.createStatement()) { ResultSet result = statement.executeQuery("SHOW TABLES LIKE 'pp_users'"); if (result.next()) { - databaseConnector.updateSQL( - "DROP TABLE pp_users;" + - "DROP TABLE pp_fixed;" + - "DROP TABLE pp_data_item;" + - "DROP TABLE pp_data_block;" + - "DROP TABLE pp_data_color;" + - "DROP TABLE pp_data_note;" - ); + Statement dropStatement = connection.createStatement(); + dropStatement.addBatch("DROP TABLE pp_users"); + dropStatement.addBatch("DROP TABLE pp_fixed"); + dropStatement.addBatch("DROP TABLE pp_data_item"); + dropStatement.addBatch("DROP TABLE pp_data_block"); + dropStatement.addBatch("DROP TABLE pp_data_color"); + dropStatement.addBatch("DROP TABLE pp_data_note"); + dropStatement.executeBatch(); } } @@ -174,12 +173,12 @@ public class PlayerParticles extends JavaPlugin { try (Statement statement = connection.createStatement()) { ResultSet result = statement.executeQuery("SHOW TABLES LIKE 'pp_group'"); if (!result.next()) { - databaseConnector.updateSQL( - "CREATE TABLE pp_player (uuid VARCHAR(36));" + - "CREATE TABLE pp_group (uuid VARCHAR(36), owner_uuid VARCHAR(36), id SMALLINT);" + - "CREATE TABLE pp_fixed (owner_uuid VARCHAR(36), id SMALLINT, particle_uuid VARCHAR(36), world VARCHAR(100), xPos DOUBLE, yPos DOUBLE, zPos DOUBLE);" + - "CREATE TABLE pp_particle (uuid VARCHAR(36), group_uuid VARCHAR(36), effect VARCHAR(100), style VARCHAR(100), item_material VARCHAR(100), block_material VARCHAR(100), note SMALLINT, r SMALLINT, g SMALLINT, b SMALLINT);" - ); + Statement createStatement = connection.createStatement(); + createStatement.addBatch("CREATE TABLE pp_player (uuid VARCHAR(36))"); + createStatement.addBatch("CREATE TABLE pp_group (uuid VARCHAR(36), owner_uuid VARCHAR(36), name VARCHAR(100))"); + createStatement.addBatch("CREATE TABLE pp_fixed (owner_uuid VARCHAR(36), id SMALLINT, particle_uuid VARCHAR(36), world VARCHAR(100), xPos DOUBLE, yPos DOUBLE, zPos DOUBLE)"); + createStatement.addBatch("CREATE TABLE pp_particle (uuid VARCHAR(36), group_uuid VARCHAR(36), id SMALLINT, effect VARCHAR(100), style VARCHAR(100), item_material VARCHAR(100), block_material VARCHAR(100), note SMALLINT, r SMALLINT, g SMALLINT, b SMALLINT)"); + createStatement.executeBatch(); } } } catch (SQLException ex) { @@ -199,7 +198,6 @@ public class PlayerParticles extends JavaPlugin { new BukkitRunnable() { public void run() { ParticleManager.refreshPPlayers(); // Add any online players who have particles - ParticleManager.addAllFixedEffects(); // Add all fixed effects PlayerParticlesGui.setup(); long ticks = getConfig().getLong("ticks-per-particle"); diff --git a/src/com/esophose/playerparticles/command/ParticleCommandCompleter.java b/src/com/esophose/playerparticles/command/ParticleCommandCompleter.java index 8d7db7e..9b913f9 100644 --- a/src/com/esophose/playerparticles/command/ParticleCommandCompleter.java +++ b/src/com/esophose/playerparticles/command/ParticleCommandCompleter.java @@ -65,7 +65,7 @@ public class ParticleCommandCompleter implements TabCompleter { completions.add(""); } } else if (args[0].equalsIgnoreCase("data")) { - PPlayer pplayer = DataManager.getInstance().getPPlayer(((Player) sender).getUniqueId()); + PPlayer pplayer = DataManager.getPPlayer(((Player) sender).getUniqueId()); if (pplayer == null) { completions.add(ChatColor.stripColor(MessageType.NO_DATA_USAGE.getMessage())); } else if (pplayer.getParticleEffect().hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA) && args.length == 2) { diff --git a/src/com/esophose/playerparticles/command/ParticleCommandExecutor.java b/src/com/esophose/playerparticles/command/ParticleCommandExecutor.java index 00c891a..2062af5 100644 --- a/src/com/esophose/playerparticles/command/ParticleCommandExecutor.java +++ b/src/com/esophose/playerparticles/command/ParticleCommandExecutor.java @@ -737,7 +737,7 @@ public class ParticleCommandExecutor implements CommandExecutor { .replaceAll("\\{4\\}", df.format(fixedEffect.getLocation().getZ()) + "") .replaceAll("\\{5\\}", fixedEffect.getParticleEffect().getName()) .replaceAll("\\{6\\}", fixedEffect.getParticleStyle().getName()) - .replaceAll("\\{7\\}", fixedEffect.getParticleDataString()); // @formatter:on + .replaceAll("\\{7\\}", fixedEffect.getDataString()); // @formatter:on MessageManager.sendCustomMessage(p, listMessage); }); } else if (cmd.equalsIgnoreCase("clear")) { diff --git a/src/com/esophose/playerparticles/database/DatabaseConnector.java b/src/com/esophose/playerparticles/database/DatabaseConnector.java index 359d545..349a2b8 100644 --- a/src/com/esophose/playerparticles/database/DatabaseConnector.java +++ b/src/com/esophose/playerparticles/database/DatabaseConnector.java @@ -2,7 +2,6 @@ package com.esophose.playerparticles.database; import java.sql.Connection; import java.sql.SQLException; -import java.sql.Statement; public abstract class DatabaseConnector { @@ -32,46 +31,6 @@ public abstract class DatabaseConnector { * @throws SQLException If an SQL problem occurs getting the connection */ protected abstract Connection getConnection() throws SQLException; - - /** - * Executes an update statement and cleans up all resources - * Allows batched statements separated by semicolons - * - * @param query The update statement to run - * @return An int with the status of the first statement in the query - * @throws SQLException If an SQL problem occurs executing the statement - */ - public int updateSQL(String query) throws SQLException { - Connection connection = null; - Statement statement = null; - - try { - connection = this.getConnection(); - statement = connection.createStatement(); - - int[] results; - - if (query.indexOf(';') != -1) { - String[] queries = query.split(";"); - for (String q : queries) { - statement.addBatch(q); - } - - results = statement.executeBatch(); - } else { - results = new int[] { statement.executeUpdate(query) }; - } - - statement.close(); - - return results[0]; - } catch (SQLException ex) { - throw ex; - } finally { - try { if (connection != null) connection.close(); } catch (Exception ex) { }; - try { if (statement != null) statement.close(); } catch (Exception ex) { }; - } - } /** * Allows Lambda expressions to be used to reduce duplicated code for getting connections diff --git a/src/com/esophose/playerparticles/gui/PlayerParticlesGui.java b/src/com/esophose/playerparticles/gui/PlayerParticlesGui.java index 6d476d3..c7b1b12 100644 --- a/src/com/esophose/playerparticles/gui/PlayerParticlesGui.java +++ b/src/com/esophose/playerparticles/gui/PlayerParticlesGui.java @@ -328,7 +328,7 @@ public class PlayerParticlesGui extends BukkitRunnable implements Listener { String[] currentIconLore = new String[3]; currentIconLore[0] = ChatColor.YELLOW + "Effect: " + ChatColor.AQUA + p.getParticleEffect().getName(); currentIconLore[1] = ChatColor.YELLOW + "Style: " + ChatColor.AQUA + p.getParticleStyle().getName(); - currentIconLore[2] = ChatColor.YELLOW + "Active Data: " + ChatColor.AQUA + p.getParticleDataString(); + currentIconLore[2] = ChatColor.YELLOW + "Active Data: " + ChatColor.AQUA + p.getDataString(); currentIconMeta.setLore(Arrays.asList(currentIconLore)); currentIconMeta.setOwner(player.getName()); //currentIconMeta.setOwningPlayer(Bukkit.getOfflinePlayer(player.getUniqueId())); // This doesn't exist in 1.9 diff --git a/src/com/esophose/playerparticles/manager/DataManager.java b/src/com/esophose/playerparticles/manager/DataManager.java index b6ea460..ab7dd2b 100644 --- a/src/com/esophose/playerparticles/manager/DataManager.java +++ b/src/com/esophose/playerparticles/manager/DataManager.java @@ -8,22 +8,14 @@ package com.esophose.playerparticles.manager; -import java.io.File; -import java.io.IOException; +import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.scheduler.BukkitRunnable; import com.esophose.playerparticles.PlayerParticles; @@ -32,11 +24,17 @@ import com.esophose.playerparticles.particles.PPlayer; import com.esophose.playerparticles.particles.ParticleEffect; import com.esophose.playerparticles.particles.ParticleEffect.NoteColor; import com.esophose.playerparticles.particles.ParticleEffect.OrdinaryColor; +import com.esophose.playerparticles.particles.ParticleGroup; +import com.esophose.playerparticles.particles.ParticlePair; import com.esophose.playerparticles.styles.api.ParticleStyle; import com.esophose.playerparticles.styles.api.ParticleStyleManager; import com.esophose.playerparticles.util.ParticleUtils; -public class DataManager { +/** + * All data changes to PPlayers such as group or fixed effect changes must be done through here, + * rather than directly on the PPlayer object + */ +public class DataManager { // @formatter:off /** * The disabled worlds cached for quick access @@ -70,7 +68,6 @@ public class DataManager { for (PPlayer pp : ParticleManager.particlePlayers) if (pp.getUniqueId() == playerUUID) return pp; - return null; } @@ -88,310 +85,258 @@ public class DataManager { return; } - // Either get an existing one from the database, or create a new one - buildPPlayer(playerUUID, true, (pplayer) -> { - ParticleManager.particlePlayers.add(pplayer); - callback.execute(pplayer); + async(() -> { + List groups = new ArrayList(); + List fixedParticles = new ArrayList(); + + PlayerParticles.databaseConnector.connect((connection) -> { + // Load particle groups + String groupQuery = "SELECT * FROM pp_group g WHERE g.owner_uuid = ? " + + "JOIN pp_particle p ON g.uuid = p.group_uuid"; + try (PreparedStatement statement = connection.prepareStatement(groupQuery)) { + statement.setString(1, playerUUID.toString()); + + ResultSet result = statement.executeQuery(); + while (result.next()) { + // Group properties + String groupName = result.getString("g.name"); + + // Particle properties + int id = result.getInt("p.id"); + ParticleEffect effect = ParticleEffect.fromName(result.getString("p.effect")); + ParticleStyle style = ParticleStyleManager.styleFromString(result.getString("p.style")); + Material itemMaterial = ParticleUtils.closestMatchWithFallback(result.getString("p.item_material")); + Material blockMaterial = ParticleUtils.closestMatchWithFallback(result.getString("p.block_material")); + NoteColor noteColor = new NoteColor(result.getInt("p.note")); + OrdinaryColor color = new OrdinaryColor(result.getInt("p.r"), result.getInt("p.g"), result.getInt("p.b")); + ParticlePair particle = new ParticlePair(playerUUID, id, effect, style, itemMaterial, blockMaterial, color, noteColor); + + // Try to add particle to an existing group + boolean groupAlreadyExists = false; + for (ParticleGroup group : groups) { + if (group.getName().equalsIgnoreCase(groupName)) { + group.getParticles().add(particle); + groupAlreadyExists = true; + break; + } + } + + // Add the particle to a new group if one didn't already exist + if (!groupAlreadyExists) { + List particles = new ArrayList(); + particles.add(particle); + ParticleGroup newGroup = new ParticleGroup(groupName, particles); + groups.add(newGroup); + } + } + } + + // Load fixed effects + String fixedQuery = "SELECT * FROM pp_fixed f WHERE f.owner_uuid = ? " + + "JOIN pp_particle p ON f.particle_uuid = p.uuid"; + try (PreparedStatement statement = connection.prepareStatement(fixedQuery)) { + statement.setString(1, playerUUID.toString()); + + ResultSet result = statement.executeQuery(); + while (result.next()) { + // Fixed effect properties + int fixedEffectId = result.getInt("f.id"); + String worldName = result.getString("f.world"); + double xPos = result.getDouble("f.xPos"); + double yPos = result.getDouble("f.yPos"); + double zPos = result.getDouble("f.zPos"); + + // Particle properties + int particleId = result.getInt("p.id"); + ParticleEffect effect = ParticleEffect.fromName(result.getString("p.effect")); + ParticleStyle style = ParticleStyleManager.styleFromString(result.getString("p.style")); + Material itemMaterial = ParticleUtils.closestMatchWithFallback(result.getString("p.item_material")); + Material blockMaterial = ParticleUtils.closestMatchWithFallback(result.getString("p.block_material")); + NoteColor noteColor = new NoteColor(result.getInt("p.note")); + OrdinaryColor color = new OrdinaryColor(result.getInt("p.r"), result.getInt("p.g"), result.getInt("p.b")); + ParticlePair particle = new ParticlePair(playerUUID, particleId, effect, style, itemMaterial, blockMaterial, color, noteColor); + + fixedParticles.add(new FixedParticleEffect(playerUUID, fixedEffectId, worldName, xPos, yPos, zPos, particle)); + } + } + + if (groups.size() == 0) { // If there aren't any groups then this is a brand new PPlayer and we need to save a new active group for them + ParticleGroup activeGroup = new ParticleGroup(null, new ArrayList()); + saveParticleGroup(playerUUID, activeGroup); + groups.add(activeGroup); + } + + PPlayer loadedPPlayer = new PPlayer(playerUUID, groups, fixedParticles); + ParticleManager.particlePlayers.add(loadedPPlayer); + + sync(() -> callback.execute(loadedPPlayer)); + }); }); } - - /** - * Gets a PPlayer matching the UUID given - * If createIfNotFound is true, one will be created if it doesn't exist - * - * @param playerUUID The UUID to match the PPlayer to - * @param createIfNotFound If true, creates a new PPlayer if the requested one doesn't exist - * @param callback The callback to execute with the built PPlayer - */ - private static void buildPPlayer(UUID playerUUID, boolean createIfNotFound, ConfigurationCallback callback) { - async(() -> { - String id = playerUUID.toString(); // @formatter:off - PlayerParticles.databaseConnector.connect((connection) -> { - String query = "SELECT * FROM pp_users u " + - "JOIN pp_data_item i ON u.player_uuid = i.uuid " + - "JOIN pp_data_block b ON u.player_uuid = b.uuid " + - "JOIN pp_data_color c ON u.player_uuid = c.uuid " + - "JOIN pp_data_note n ON u.player_uuid = n.uuid " + - "WHERE u.player_uuid = '" + id + "'"; - - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery(query)) { - - 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"))); - BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material"))); - OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b")); - NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note")); - - sync(() -> callback.execute(new PPlayer(playerUUID, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData))); - return; - } - - if (createIfNotFound) { // Didn't find an existing PPlayer, create and return a new one - PPlayer pplayer = PPlayer.getNewPPlayer(playerUUID); - saveNewPPlayer(pplayer); - sync(() -> callback.execute(pplayer)); - } - } - }); - }); - } - - /** - * Saves a new PPlayer to the database or the file - * - * @param pplayer The PPlayer to save - */ - public static void saveNewPPlayer(PPlayer pplayer) { - async(() -> { - PlayerParticles.databaseConnector.connect((connection) -> { - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery("SELECT * FROM pp_users WHERE player_uuid = '" + pplayer.getUniqueId() + "'")) { - - if (!res.next()) { - PlayerParticles.databaseConnector.updateSQL("INSERT INTO pp_users (player_uuid, effect, style) VALUES (" + - "'" + pplayer.getUniqueId().toString() + "', " + - "'" + pplayer.getParticleEffect().getName() + "', " + - "'" + pplayer.getParticleStyle().getName() + "'" + - "); " + - "INSERT INTO pp_data_item (uuid, material) VALUES (" + - "'" + pplayer.getUniqueId().toString() + "', " + - "'" + pplayer.getMaterialData().getMaterial().name() + "'" + - "); " + - "INSERT INTO pp_data_block (uuid, material) VALUES (" + - "'" + pplayer.getUniqueId().toString() + "', " + - "'" + pplayer.getBlockData().getMaterial().name() + "'" + - "); " + - "INSERT INTO pp_data_color (uuid, r, g, b) VALUES (" + - "'" + pplayer.getUniqueId().toString() + "', " + - pplayer.getColorData().getRed() + ", " + - pplayer.getColorData().getGreen() + ", " + - pplayer.getColorData().getBlue() + - "); " + - "INSERT INTO pp_data_note (uuid, note) VALUES (" + - "'" + pplayer.getUniqueId().toString() + "', " + - (byte) (pplayer.getNoteColorData().getValueX() * 24) + - ");" - ); - } else { - throw new RuntimeException("The user " + pplayer.getUniqueId() + " is already in the database. They can not be added."); - } - } - }); - }); - - ParticleManager.updateIfContains(pplayer); // Update the player in case this is a /pp reset - } /** - * Loads a PPlayer and caches it + * Saves a ParticleGroup. If it already exists, update it. * - * @param playerUUID The pplayer to load + * @param playerUUID The owner of the group + * @param group The group to create/update */ - public static void loadPPlayer(UUID playerUUID) { - for (PPlayer pplayer : ParticleManager.particlePlayers) - if (pplayer.getUniqueId() == playerUUID) - return; - - buildPPlayer(playerUUID, false, (pplayer) -> { - ParticleManager.particlePlayers.add(pplayer); - }); + public static void saveParticleGroup(UUID playerUUID, ParticleGroup group) { + async(() -> { + PlayerParticles.databaseConnector.connect((connection) -> { + String groupUUIDQuery = "SELECT uuid FROM pp_group WHERE owner_uuid = ? AND name = ?"; + try (PreparedStatement statement = connection.prepareStatement(groupUUIDQuery)) { + statement.setString(1, playerUUID.toString()); + statement.setString(2, group.getName()); + + String groupUUID = null; + + ResultSet result = statement.executeQuery(); + if (result.next()) { // Clear out particles from existing group + groupUUID = result.getString("uuid"); + + String particlesDeleteQuery = "DELETE FROM pp_particle WHERE group_uuid = ?"; + PreparedStatement particlesDeleteStatement = connection.prepareStatement(particlesDeleteQuery); + particlesDeleteStatement.setString(1, result.getString("uuid")); + + particlesDeleteStatement.executeUpdate(); + } else { // Create new group + groupUUID = UUID.randomUUID().toString(); + + String groupCreateQuery = "INSERT INTO pp_group (uuid, owner_uuid, name) VALUES (?, ?, ?)"; + PreparedStatement groupCreateStatement = connection.prepareStatement(groupCreateQuery); + groupCreateStatement.setString(1, groupUUID); + groupCreateStatement.setString(2, playerUUID.toString()); + groupCreateStatement.setString(3, group.getName()); + + groupCreateStatement.executeUpdate(); + } + + // Fill group with new particles + String createParticlesQuery = "INSERT INTO pp_particle (uuid, group_uuid, id, effect, style, item_material, block_material, note, r, g, b) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + PreparedStatement particlesStatement = connection.prepareStatement(createParticlesQuery); + for (ParticlePair particle : group.getParticles()) { + particlesStatement.setString(1, UUID.randomUUID().toString()); + particlesStatement.setString(2, groupUUID); + particlesStatement.setInt(3, particle.getId()); + particlesStatement.setString(4, particle.getEffect().getName()); + particlesStatement.setString(5, particle.getStyle().getName()); + particlesStatement.setString(6, particle.getItemMaterial().name()); + particlesStatement.setString(7, particle.getBlockMaterial().name()); + particlesStatement.setInt(8, particle.getNoteColor().getNote()); + particlesStatement.setInt(9, particle.getColor().getRed()); + particlesStatement.setInt(10, particle.getColor().getGreen()); + particlesStatement.setInt(11, particle.getColor().getBlue()); + particlesStatement.addBatch(); + } + } + }); + }); + + getPPlayer(playerUUID, (pplayer) -> { + for (ParticleGroup existing : pplayer.getParticles()) { + if (existing.getName().equalsIgnoreCase(group.getName())) { + pplayer.getParticles().remove(existing); + break; + } + } + pplayer.getParticles().add(group); + }); } - + /** - * Resets all saved information about a PPlayer - * This should be made into a single batch query in the future + * Removes a ParticleGroup * - * @param playerUUID The pplayer to reset + * @param playerUUID The owner of the group + * @param group The group to remove */ - public static void resetPPlayer(UUID playerUUID) { - PPlayer pplayer = PPlayer.getNewPPlayer(playerUUID); - savePPlayer(playerUUID, pplayer.getParticleEffect()); - savePPlayer(playerUUID, pplayer.getParticleStyle()); - savePPlayer(playerUUID, pplayer.getMaterialData()); - savePPlayer(playerUUID, pplayer.getBlockData()); - savePPlayer(playerUUID, pplayer.getColorData()); - savePPlayer(playerUUID, pplayer.getNoteColorData()); - } - - /** - * Saves the effect to the player's save file or database entry - * - * @param playerUUID The UUID of the player - * @param particleEffect The effect that is being saved - */ - public static void savePPlayer(UUID playerUUID, ParticleEffect particleEffect) { - getPPlayer(playerUUID, (pplayer) -> { - async(() -> { - try { - PlayerParticles.databaseConnector.updateSQL("UPDATE pp_users SET effect = '" + particleEffect.getName() + "' WHERE player_uuid = '" + playerUUID + "';"); - } catch (SQLException e) { - e.printStackTrace(); - } - }); - - pplayer.setParticleEffect(particleEffect); - }); - } - - /** - * Saves the style to the player's save file or database entry - * - * @param playerUUID The UUID of the player - * @param particleStyle The style that is being saved - */ - public static void savePPlayer(UUID playerUUID, ParticleStyle particleStyle) { - getPPlayer(playerUUID, (pplayer) -> { - async(() -> { - try { - PlayerParticles.databaseConnector.updateSQL("UPDATE pp_users SET style = '" + particleStyle.getName() + "' WHERE player_uuid = '" + playerUUID + "';"); - } catch (SQLException e) { - e.printStackTrace(); - } - }); - - pplayer.setParticleStyle(particleStyle); - }); - } - - /** - * Saves the item data to the player's save file or database entry - * - * @param playerUUID The UUID of the player - * @param particleItemData The data that is being saved - */ - public static void savePPlayer(UUID playerUUID, ItemData particleItemData) { - getPPlayer(playerUUID, (pplayer) -> { - async(() -> { - try { - PlayerParticles.databaseConnector.updateSQL("UPDATE pp_data_item SET material = '" + particleItemData.getMaterial().name() + "' WHERE uuid = '" + playerUUID + "';"); - } catch (SQLException e) { - e.printStackTrace(); - } - }); - - pplayer.setItemMaterial(particleItemData); - }); - } - - /** - * Saves the block data to the player's save file or database entry - * - * @param playerUUID The UUID of the player - * @param particleBlockData The data that is being saved - */ - public static void savePPlayer(UUID playerUUID, BlockData particleBlockData) { - getPPlayer(playerUUID, (pplayer) -> { - async(() -> { - try { - PlayerParticles.databaseConnector.updateSQL("UPDATE pp_data_block SET material = '" + particleBlockData.getMaterial().name() + "' WHERE uuid = '" + playerUUID + "';"); - } catch (SQLException e) { - e.printStackTrace(); - } - }); - - pplayer.setBlockData(particleBlockData); - }); - } - - /** - * Saves the color data to the player's save file or database entry - * - * @param playerUUID The UUID of the player - * @param particleColorData The data that is being saved - */ - public static void savePPlayer(UUID playerUUID, OrdinaryColor particleColorData) { - getPPlayer(playerUUID, (pplayer) -> { - async(() -> { - try { - PlayerParticles.databaseConnector.updateSQL("UPDATE pp_data_color SET r = " + particleColorData.getRed() + ", g = " + particleColorData.getGreen() + ", b = " + particleColorData.getBlue() + " WHERE uuid = '" + playerUUID + "';"); - } catch (SQLException e) { - e.printStackTrace(); - } - }); - - pplayer.setColorData(particleColorData); - }); - } - - /** - * Saves the note color data to the player's save file or database entry - * - * @param playerUUID The UUID of the player - * @param particleNoteColorData The data that is being saved - */ - public static void savePPlayer(UUID playerUUID, NoteColor particleNoteColorData) { - getPPlayer(playerUUID, (pplayer) -> { - async(() -> { - try { - PlayerParticles.databaseConnector.updateSQL("UPDATE pp_data_note SET note = " + (byte) (particleNoteColorData.getValueX() * 24) + " WHERE uuid = '" + playerUUID + "';"); - } catch (SQLException e) { - e.printStackTrace(); - } - }); - - pplayer.setNoteColorData(particleNoteColorData); - }); + public static void removeParticleGroup(UUID playerUUID, ParticleGroup group) { + async(() -> { + PlayerParticles.databaseConnector.connect((connection) -> { + String groupQuery = "SELECT * FROM pp_group WHERE owner_uuid = ? AND name = ?"; + String particleDeleteQuery = "DELETE FROM pp_particle WHERE group_uuid = ?"; + String groupDeleteQuery = "DELETE FROM pp_group WHERE uuid = ?"; + + // Execute group uuid query + String groupUUID = null; + try (PreparedStatement statement = connection.prepareStatement(groupQuery)) { + statement.setString(1, playerUUID.toString()); + statement.setString(2, group.getName()); + + ResultSet result = statement.executeQuery(); + groupUUID = result.getString("uuid"); + } + + // Execute particle delete update + try (PreparedStatement statement = connection.prepareStatement(particleDeleteQuery)) { + statement.setString(1, groupUUID); + + statement.executeUpdate(); + } + + // Execute group delete update + try (PreparedStatement statement = connection.prepareStatement(groupDeleteQuery)) { + statement.setString(1, groupUUID); + + statement.executeUpdate(); + } + }); + }); + + getPPlayer(playerUUID, (pplayer) -> { + pplayer.getParticles().remove(group); + }); } /** * Saves a fixed effect to save data + * Does not perform a check to see if a fixed effect with this id already exists * * @param fixedEffect The fixed effect to save */ public static void saveFixedEffect(FixedParticleEffect fixedEffect) { async(() -> { PlayerParticles.databaseConnector.connect((connection) -> { - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery("SELECT * FROM pp_fixed WHERE player_uuid = '" + fixedEffect.getOwnerUniqueId() + "' AND id = " + fixedEffect.getId())) { - - if (res.next()) { - return; - } - - String fixedEffectUUID = UUID.randomUUID().toString(); - - PlayerParticles.databaseConnector.updateSQL("INSERT INTO pp_fixed (uuid, player_uuid, id, effect, style, worldName, xPos, yPos, zPos) VALUES (" + - "'" + fixedEffectUUID + "', " + - "'" + fixedEffect.getOwnerUniqueId().toString() + "', " + - fixedEffect.getId() + ", " + - "'" + fixedEffect.getParticleEffect().getName() + "', " + - "'" + fixedEffect.getParticleStyle().getName() + "', " + - "'" + fixedEffect.getLocation().getWorld().getName() + "', " + - fixedEffect.getLocation().getX() + ", " + - fixedEffect.getLocation().getY() + ", " + - fixedEffect.getLocation().getZ() + - "); " + - "INSERT INTO pp_data_item (uuid, material) VALUES (" + - "'" + fixedEffectUUID + "', " + - "'" + fixedEffect.getMaterialData().getMaterial().name() + "'" + - "); " + - "INSERT INTO pp_data_block (uuid, material) VALUES (" + - "'" + fixedEffectUUID + "', " + - "'" + fixedEffect.getBlockData().getMaterial().name() + "'" + - "); " + - "INSERT INTO pp_data_color (uuid, r, g, b) VALUES (" + - "'" + fixedEffectUUID + "', " + - fixedEffect.getColorData().getRed() + ", " + - fixedEffect.getColorData().getGreen() + ", " + - fixedEffect.getColorData().getBlue() + - "); " + - "INSERT INTO pp_data_note (uuid, note) VALUES (" + - "'" + fixedEffectUUID + "', " + - (byte) (fixedEffect.getNoteColorData().getValueX() * 24) + - ");" - ); - - sync(() -> ParticleManager.addFixedEffect(fixedEffect)); + String particleUUID = UUID.randomUUID().toString(); + + String particleQuery = "INSERT INTO pp_particle (uuid, group_uuid, id, effect, style, item_material, block_material, note, r, g, b) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + try (PreparedStatement statement = connection.prepareStatement(particleQuery)) { + ParticlePair particle = fixedEffect.getParticlePair(); + statement.setString(1, particleUUID); + statement.setString(2, null); + statement.setInt(3, fixedEffect.getId()); + statement.setString(4, particle.getEffect().getName()); + statement.setString(5, particle.getStyle().getName()); + statement.setString(6, particle.getItemMaterial().name()); + statement.setString(7, particle.getBlockMaterial().name()); + statement.setInt(8, particle.getNoteColor().getNote()); + statement.setInt(9, particle.getColor().getRed()); + statement.setInt(10, particle.getColor().getGreen()); + statement.setInt(11, particle.getColor().getBlue()); + statement.executeUpdate(); + } + + String fixedEffectQuery = "INSERT INTO pp_fixed (owner_uuid, id, particle_uuid, world, xPos, yPos, zPos) VALUES (?, ?, ?, ?, ?, ?, ?)"; + try (PreparedStatement statement = connection.prepareStatement(fixedEffectQuery)) { + statement.setString(1, fixedEffect.getOwnerUniqueId().toString()); + statement.setInt(2, fixedEffect.getId()); + statement.setString(3, particleUUID); + statement.setString(4, fixedEffect.getLocation().getWorld().getName()); + statement.setDouble(5, fixedEffect.getLocation().getX()); + statement.setDouble(6, fixedEffect.getLocation().getY()); + statement.setDouble(7, fixedEffect.getLocation().getZ()); + statement.executeUpdate(); } }); }); + + getPPlayer(fixedEffect.getOwnerUniqueId(), (pplayer) -> { + pplayer.addFixedEffect(fixedEffect); + }); } /** * Deletes a fixed effect from save data + * Does not perform a check to see if a fixed effect with this id already exists * * @param playerUUID The player who owns the effect * @param id The id of the effect to remove @@ -400,130 +345,37 @@ public class DataManager { public static void removeFixedEffect(UUID playerUUID, int id, ConfigurationCallback callback) { async(() -> { PlayerParticles.databaseConnector.connect((connection) -> { - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery("SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "' AND id = " + id)) { - - if (!res.next()) { - callback.execute(false); - return; - } - - String uuid = res.getString("uuid"); - PlayerParticles.databaseConnector.updateSQL("DELETE FROM pp_fixed WHERE uuid = '" + uuid + "';" + - "DELETE FROM pp_data_item WHERE uuid = '" + uuid + "';" + - "DELETE FROM pp_data_block WHERE uuid = '" + uuid + "';" + - "DELETE FROM pp_data_color WHERE uuid = '" + uuid + "';" + - "DELETE FROM pp_data_note WHERE uuid = '" + uuid + "';" - ); - - sync(() -> { - ParticleManager.removeFixedEffectForPlayer(playerUUID, id); - callback.execute(true); - }); - } - }); - }); - } - - /** - * Resets all fixed effects for a given player - * - * @param playerUUID The player to remove all effects from - */ - public static void resetFixedEffects(UUID playerUUID) { - async(() -> { - try { // @formatter:off - PlayerParticles.databaseConnector.updateSQL("DELETE FROM pp_data_item WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + - "DELETE FROM pp_data_block WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + - "DELETE FROM pp_data_color WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + - "DELETE FROM pp_data_note WHERE uuid IN (SELECT uuid FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "');" + - "DELETE FROM pp_fixed WHERE player_uuid = '" + playerUUID.toString() + "';" - ); // @formatter:on - } catch (SQLException e) { - e.printStackTrace(); - } - }); - - ParticleManager.removeAllFixedEffectsForPlayer(playerUUID); - } - - /** - * Gets a list of all saved fixed particle effects - * - * @param callback The callback to execute with a list of all saved fixed particle effects - */ - public static void getAllFixedEffects(ConfigurationCallback> callback) { - async(() -> { - PlayerParticles.databaseConnector.connect((connection) -> { - String query = "SELECT * FROM pp_fixed f " + - "JOIN pp_data_item i ON f.uuid = i.uuid " + - "JOIN pp_data_block b ON f.uuid = b.uuid " + - "JOIN pp_data_color c ON f.uuid = c.uuid " + - "JOIN pp_data_note n ON f.uuid = n.uuid"; - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery(query)) { - - List fixedEffects = new ArrayList(); - - while (res.next()) { - UUID pplayerUUID = UUID.fromString(res.getString("f.player_uuid")); - int id = res.getInt("f.id"); - String worldName = res.getString("f.worldName"); - double xPos = res.getDouble("f.xPos"); - double yPos = res.getDouble("f.yPos"); - double zPos = res.getDouble("f.zPos"); - ParticleEffect particleEffect = ParticleManager.effectFromString(res.getString("f.effect")); - ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("f.style")); - ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material"))); - BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material"))); - OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b")); - NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note")); - - fixedEffects.add(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData)); - } - - sync(() -> callback.execute(fixedEffects)); - } - }); - }); - } - - /** - * Gets a fixed effect for a pplayer by its id - * - * @param pplayerUUID The player who owns the effect - * @param id The id for the effect to get - * @param callback The callback to execute with the effect, if one exists - */ - public static void getFixedEffectForPlayerById(UUID pplayerUUID, int id, ConfigurationCallback callback) { - async(() -> { - PlayerParticles.databaseConnector.connect((connection) -> { // @formatter:off - String query = "SELECT * FROM pp_fixed f " + - "JOIN pp_data_item i ON f.uuid = i.uuid " + - "JOIN pp_data_block b ON f.uuid = b.uuid " + - "JOIN pp_data_color c ON f.uuid = c.uuid " + - "JOIN pp_data_note n ON f.uuid = n.uuid " + - "WHERE f.player_uuid = '" + pplayerUUID.toString() + "' AND f.id = '" + id + "'"; // @formatter:on - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery(query)) { - - if (res.next()) { - String worldName = res.getString("f.worldName"); - double xPos = res.getDouble("f.xPos"); - double yPos = res.getDouble("f.yPos"); - double zPos = res.getDouble("f.zPos"); - ParticleEffect particleEffect = ParticleManager.effectFromString(res.getString("f.effect")); - ParticleStyle particleStyle = ParticleStyleManager.styleFromString(res.getString("f.style")); - ItemData particleItemData = new ItemData(Material.matchMaterial(res.getString("i.material"))); - BlockData particleBlockData = new BlockData(Material.matchMaterial(res.getString("b.material"))); - OrdinaryColor particleColorData = new OrdinaryColor(res.getInt("c.r"), res.getInt("c.g"), res.getInt("c.b")); - NoteColor particleNoteColorData = new NoteColor(res.getByte("n.note")); - - sync(() -> callback.execute(new FixedParticleEffect(pplayerUUID, id, worldName, xPos, yPos, zPos, particleEffect, particleStyle, particleItemData, particleBlockData, particleColorData, particleNoteColorData))); - } + String particleUUID = null; + + String particleUUIDQuery = "SELECT particle_uuid FROM pp_fixed WHERE owner_uuid = ? AND id = ?"; + try (PreparedStatement statement = connection.prepareStatement(particleUUIDQuery)) { + statement.setString(1, playerUUID.toString()); + statement.setInt(2, id); + + ResultSet result = statement.executeQuery(); + particleUUID = result.getString("particle_uuid"); + } + + String particleDeleteQuery = "DELETE FROM pp_particle WHERE uuid = ?"; + try (PreparedStatement statement = connection.prepareStatement(particleDeleteQuery)) { + statement.setString(1, particleUUID); + + statement.executeUpdate(); + } + + String fixedEffectDeleteQuery = "DELETE FROM pp_fixed WHERE owner_uuid = ? AND id = ?"; + try (PreparedStatement statement = connection.prepareStatement(fixedEffectDeleteQuery)) { + statement.setString(1, playerUUID.toString()); + statement.setInt(2, id); + + statement.executeUpdate(); } }); }); + + getPPlayer(playerUUID, (pplayer) -> { + pplayer.removeFixedEffect(id); + }); } /** @@ -533,21 +385,9 @@ public class DataManager { * @param callback The callback to execute with a list of all fixed effect ids for the given player */ public static void getFixedEffectIdsForPlayer(UUID pplayerUUID, ConfigurationCallback> callback) { - async(() -> { - PlayerParticles.databaseConnector.connect((connection) -> { - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery("SELECT id FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) { - - List ids = new ArrayList(); - - while (res.next()) { - ids.add(res.getInt(1)); - } - - sync(() -> callback.execute(ids)); - } - }); - }); + getPPlayer(pplayerUUID, (pplayer) -> { + callback.execute(pplayer.getFixedEffectIds()); + }); } /** @@ -566,21 +406,9 @@ public class DataManager { return; } - async(() -> { - PlayerParticles.databaseConnector.connect((connection) -> { - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery("SELECT COUNT(1) FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) { - - boolean hasReachedMax; - if (res.next()) - hasReachedMax = res.getInt(1) >= maxFixedEffects; - else - hasReachedMax = false; - - sync(() -> callback.execute(hasReachedMax)); - } - }); - }); + getPPlayer(pplayerUUID, (pplayer) -> { + callback.execute(pplayer.getFixedEffectIds().size() >= maxFixedEffects); + }); } /** @@ -590,30 +418,13 @@ public class DataManager { * @param callback The callback to execute with the smallest available Id the player can use */ public static void getNextFixedEffectId(UUID pplayerUUID, ConfigurationCallback callback) { - async(() -> { - PlayerParticles.databaseConnector.connect((connection) -> { - try (Statement statement = connection.createStatement(); - ResultSet res = statement.executeQuery("SELECT id FROM pp_fixed WHERE player_uuid = '" + pplayerUUID.toString() + "'")) { - - Set idsSet = new HashSet(); - - while (res.next()) - idsSet.add(res.getInt(1) + ""); - - if (idsSet.isEmpty()) { - sync(() -> callback.execute(1)); - return; - } - - int[] ids = new int[idsSet.size()]; - int i = 0; - for (String key : idsSet) - ids[i++] = Integer.parseInt(key); - - sync(() -> callback.execute(ParticleUtils.getSmallestPositiveInt(ids))); - } - }); - }); + getPPlayer(pplayerUUID, (pplayer) -> { + List fixedEffectIds = pplayer.getFixedEffectIds(); + int[] ids = new int[fixedEffectIds.size()]; + for (int i = 0; i < fixedEffectIds.size(); i++) + ids[i] = fixedEffectIds.get(i); + callback.execute(ParticleUtils.getSmallestPositiveInt(ids)); + }); } /** @@ -690,4 +501,4 @@ public class DataManager { public void execute(T obj); } -} +} // @formatter:on diff --git a/src/com/esophose/playerparticles/manager/ParticleManager.java b/src/com/esophose/playerparticles/manager/ParticleManager.java index b4d6a09..814e2b6 100644 --- a/src/com/esophose/playerparticles/manager/ParticleManager.java +++ b/src/com/esophose/playerparticles/manager/ParticleManager.java @@ -11,7 +11,6 @@ package com.esophose.playerparticles.manager; import java.awt.Color; import java.util.ArrayList; import java.util.List; -import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -37,15 +36,10 @@ import com.esophose.playerparticles.styles.api.ParticleStyleManager; public class ParticleManager extends BukkitRunnable implements Listener { /** - * The list containing all the player effect info + * The list containing all the loaded PPlayer info */ public static ArrayList particlePlayers = new ArrayList(); - /** - * The list containing all the fixed effect info - */ - public static ArrayList fixedParticleEffects = new ArrayList(); - /** * Rainbow particle effect hue and note color used for rainbow colorable effects * These should be moved to a more appropriate place later @@ -60,7 +54,7 @@ public class ParticleManager extends BukkitRunnable implements Listener { */ @EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent e) { - DataManager.loadPPlayer(e.getPlayer().getUniqueId()); + DataManager.getPPlayer(e.getPlayer().getUniqueId(), (pplayer) -> {}); // Loads the PPlayer from the database } /** @@ -76,54 +70,12 @@ public class ParticleManager extends BukkitRunnable implements Listener { } /** - * Adds all fixed effects from the config - */ - public static void addAllFixedEffects() { - DataManager.getAllFixedEffects((fixedEffects) -> { - fixedParticleEffects.addAll(fixedEffects); - }); - } - - /** - * Removes all fixed effects for the given pplayer - * - * @param pplayerUUID The pplayer to remove the fixed effects from - */ - public static void removeAllFixedEffectsForPlayer(UUID pplayerUUID) { - for (int i = fixedParticleEffects.size() - 1; i >= 0; i--) - if (fixedParticleEffects.get(i).getOwnerUniqueId().equals(pplayerUUID)) - fixedParticleEffects.remove(i); - } - - /** - * Adds a fixed effect - * - * @param fixedEffect The fixed effect to add - */ - public static void addFixedEffect(FixedParticleEffect fixedEffect) { - fixedParticleEffects.add(fixedEffect); - } - - /** - * Removes a fixed effect for the given pplayer with the given id - * - * @param pplayerUUID The pplayer to remove the fixed effect from - * @param id The id of the fixed effect to remove - */ - public static void removeFixedEffectForPlayer(UUID pplayerUUID, int id) { - for (int i = fixedParticleEffects.size() - 1; i >= 0; i--) - if (fixedParticleEffects.get(i).getOwnerUniqueId().equals(pplayerUUID) && fixedParticleEffects.get(i).getId() == id) - fixedParticleEffects.remove(i); - } - - /** - * Clears the list then adds everybody on the server - * Used for when the server reloads and we can't rely on players rejoining + * Load a PPlayer for all players on the server who have active particles or fixed effects */ public static void refreshPPlayers() { particlePlayers.clear(); for (Player player : Bukkit.getOnlinePlayers()) - DataManager.loadPPlayer(player.getUniqueId()); + DataManager.getPPlayer(player.getUniqueId(), (pplayer) -> {}); // Loads the PPlayer from the database } /** @@ -164,7 +116,7 @@ public class ParticleManager extends BukkitRunnable implements Listener { hue++; hue %= 360; - if (hue % 10 == 0) { // Only increment note by 2 notes per second + if (hue % 5 == 0) { // Only increment note by 4 notes per second note++; note %= 24; } @@ -174,44 +126,21 @@ public class ParticleManager extends BukkitRunnable implements Listener { PPlayer pplayer = particlePlayers.get(i); Player player = pplayer.getPlayer(); - if (player == null) { // Skip and remove, why are they still in the array if they are offline? - particlePlayers.remove(i); - continue; - } - - // Perform validity checks - boolean valid = true; - // Don't show their particles if they are in spectator mode - if (player.getGameMode() == GameMode.SPECTATOR) { - valid = false; + // Don't spawn particles if the world doesn't allow it + if (player != null && player.getGameMode() != GameMode.SPECTATOR && !DataManager.isWorldDisabled(player.getWorld().getName())) { + Location loc = player.getLocation(); + loc.setY(loc.getY() + 1); + + for (ParticlePair particles : pplayer.getActiveParticles()) + displayParticles(particles, loc); } - if (DataManager.isWorldDisabled(player.getWorld().getName())) { - valid = false; - } - - if (!valid) continue; - - Location loc = player.getLocation(); - loc.setY(loc.getY() + 1); - - for (ParticlePair particles : pplayer.getParticles()) - displayParticles(particles, loc); - } - - // Loop for FixedParticleEffects - for (FixedParticleEffect effect : fixedParticleEffects) { - boolean valid = true; - for (PPlayer pplayer : particlePlayers) { - if (pplayer.getUniqueId() == effect.getOwnerUniqueId()) { - valid = PermissionManager.canUseFixedEffects(Bukkit.getPlayer(pplayer.getUniqueId())); - } - } - - if (valid) { - displayFixedParticleEffect(effect); - } + // Loop for FixedParticleEffects + // Don't spawn particles if the world doesn't allow it + for (FixedParticleEffect effect : pplayer.getFixedParticles()) + if (!DataManager.isWorldDisabled(effect.getLocation().getWorld().getName())) + displayFixedParticleEffect(effect); } } diff --git a/src/com/esophose/playerparticles/particles/PPlayer.java b/src/com/esophose/playerparticles/particles/PPlayer.java index dbb4b9c..6fffa33 100644 --- a/src/com/esophose/playerparticles/particles/PPlayer.java +++ b/src/com/esophose/playerparticles/particles/PPlayer.java @@ -25,19 +25,26 @@ public class PPlayer { private final UUID playerUUID; /** - * A List of all particles the user has applied + * A List of all particle groups this player has, the active particle group has an id of null */ - private List particles; + private List particleGroups; + + /** + * A List of all fixed particles this user has applied + */ + private List fixedParticles; /** * Constructs a new PPlayer * * @param uuid The player UUID * @param particlePairs The ParticlePairs this PPlayer has + * @param fixedParticles2 The fixed ParticlePairs this PPlayer has */ - public PPlayer(UUID uuid, List particlePairs) { + public PPlayer(UUID uuid, List particleGroups, List fixedParticles) { this.playerUUID = uuid; - this.particles = particlePairs; + this.particleGroups = particleGroups; + this.fixedParticles = fixedParticles; } /** @@ -58,13 +65,38 @@ public class PPlayer { return Bukkit.getPlayer(this.playerUUID); } + /** + * Get a List of all particles this user has saved + * + * @return A list of all particle groups this player has + */ + public List getParticles() { + return this.particleGroups; + } + + /** + * Gets a ParticleGroup this player has by its name + * + * @param name The name of the ParticleGroup + * @return The target named ParticleGroup + */ + public ParticleGroup getParticlesByName(String name) { + for (ParticleGroup group : this.particleGroups) + if (group.getName().equalsIgnoreCase(name)) + return group; + return null; + } + /** * Get the effect/style/data for particles this player has set * - * @return + * @return A List of all particles this player has set */ - public List getParticles() { - return this.particles; + public List getActiveParticles() { + for (ParticleGroup group : this.particleGroups) + if (group.getName() == null) + return group.getParticles(); + return null; // This should never return null, there will always be at least one ParticleGroup } /** @@ -73,9 +105,9 @@ public class PPlayer { * @param style The style to match * @return A List with a matching style */ - public List getParticlesForStyle(ParticleStyle style) { + public List getActiveParticlesForStyle(ParticleStyle style) { List matches = new ArrayList(); - for (ParticlePair pair : this.particles) + for (ParticlePair pair : this.getActiveParticles()) if (pair.getStyle().equals(style)) matches.add(pair); return matches; @@ -87,22 +119,60 @@ public class PPlayer { * @param id The id of the ParticlePair * @return A ParticlePair with the given id, otherwise null */ - public ParticlePair getParticle(int id) { - for (ParticlePair particle : this.particles) + public ParticlePair getActiveParticle(int id) { + for (ParticlePair particle : this.getActiveParticles()) if (particle.getId() == id) return particle; return null; } + + /** + * Get the effect/style/data for all fixed particles this has has set + * + * @return A List of all fixed particles this player has set + */ + public List getFixedParticles() { + return this.fixedParticles; + } + + /** + * Get a FixedParticleEffect this player owns by id + * + * @param id The id + * @return The FixedParticleEffect the player owns + */ + public FixedParticleEffect getFixedEffectById(int id) { + for (FixedParticleEffect fixedEffect : this.fixedParticles) + if (fixedEffect.getId() == id) + return fixedEffect; + return null; + } + + public List getFixedEffectIds() { + List ids = new ArrayList(); + for (FixedParticleEffect fixedEffect : this.fixedParticles) + ids.add(fixedEffect.getId()); + return ids; + } /** - * Gets a default PPlayer - * Used for when a new PPlayer is being created + * Adds a fixed effect * - * @param playerUUID The player's UUID - * @return A default PPlayer + * @param fixedEffect The fixed effect to add */ - public static PPlayer getNewPPlayer(UUID playerUUID) { - return new PPlayer(playerUUID, new ArrayList()); + public void addFixedEffect(FixedParticleEffect fixedEffect) { + this.fixedParticles.add(fixedEffect); + } + + /** + * Removes a fixed effect for the given pplayer with the given id + * + * @param id The id of the fixed effect to remove + */ + public void removeFixedEffect(int id) { + for (int i = this.fixedParticles.size() - 1; i >= 0; i--) + if (this.fixedParticles.get(i).getId() == id) + this.fixedParticles.remove(i); } } diff --git a/src/com/esophose/playerparticles/particles/ParticleEffect.java b/src/com/esophose/playerparticles/particles/ParticleEffect.java index b3b0225..99a1816 100644 --- a/src/com/esophose/playerparticles/particles/ParticleEffect.java +++ b/src/com/esophose/playerparticles/particles/ParticleEffect.java @@ -500,6 +500,15 @@ public enum ParticleEffect { this.note = note; } } + + /** + * Returns the note value + * + * @return The note value + */ + public int getNote() { + return this.note; + } /** * Returns the note value divided by 24 diff --git a/src/com/esophose/playerparticles/particles/ParticleGroup.java b/src/com/esophose/playerparticles/particles/ParticleGroup.java new file mode 100644 index 0000000..9b9d91c --- /dev/null +++ b/src/com/esophose/playerparticles/particles/ParticleGroup.java @@ -0,0 +1,34 @@ +package com.esophose.playerparticles.particles; + +import java.util.List; + +public class ParticleGroup { + + private String name; + private List particles; + + public ParticleGroup(String name, List particles) { + this.name = name; + this.particles = particles; + } + + /** + * Get the player-given name of this ParticleGroup + * This will be null if it's the player's active ParticleGroup + * + * @return The name of this group + */ + public String getName() { + return this.name; + } + + /** + * Get the List of particles in this group + * + * @return The particles in this group + */ + public List getParticles() { + return this.particles; + } + +} diff --git a/src/com/esophose/playerparticles/particles/ParticlePair.java b/src/com/esophose/playerparticles/particles/ParticlePair.java index e372874..07d67f8 100644 --- a/src/com/esophose/playerparticles/particles/ParticlePair.java +++ b/src/com/esophose/playerparticles/particles/ParticlePair.java @@ -23,24 +23,24 @@ public class ParticlePair { private ParticleEffect effect; private ParticleStyle style; - private Material particleItemMaterial; - private Material particleBlockMaterial; - private OrdinaryColor particleColorData; - private NoteColor particleNoteColorData; + private Material itemMaterial; + private Material blockMaterial; + private OrdinaryColor color; + private NoteColor noteColor; - public ParticlePair(UUID ownerUUID, int id, ParticleEffect effect, ParticleStyle style, Material itemMaterial, Material blockMaterial, OrdinaryColor colorData, NoteColor noteColorData) { + public ParticlePair(UUID ownerUUID, int id, ParticleEffect effect, ParticleStyle style, Material itemMaterial, Material blockMaterial, OrdinaryColor color, NoteColor noteColor) { this.ownerUUID = ownerUUID; this.id = id; this.effect = effect; this.style = style; - this.setParticleEffect(effect); - this.setParticleStyle(style); + this.setEffect(effect); + this.setStyle(style); this.setItemMaterial(itemMaterial); - this.setBlockData(blockMaterial); - this.setColorData(colorData); - this.setNoteColorData(noteColorData); + this.setBlockMaterial(blockMaterial); + this.setColor(color); + this.setNoteColor(noteColor); } /** @@ -48,7 +48,7 @@ public class ParticlePair { * * @param effect The player's new particle effect */ - public void setParticleEffect(ParticleEffect effect) { + public void setEffect(ParticleEffect effect) { if (effect == null) effect = ParticleEffect.NONE; this.effect = effect; } @@ -58,7 +58,7 @@ public class ParticlePair { * * @param style The player's new particle style */ - public void setParticleStyle(ParticleStyle style) { + public void setStyle(ParticleStyle style) { if (style == null) style = DefaultStyles.NONE; this.style = style; } @@ -70,7 +70,7 @@ public class ParticlePair { */ public void setItemMaterial(Material itemMaterial) { if (itemMaterial == null || itemMaterial.isBlock()) itemMaterial = ParticleUtils.closestMatchWithFallback("IRON_SHOVEL", "IRON_SPADE"); - this.particleItemMaterial = itemMaterial; + this.itemMaterial = itemMaterial; } /** @@ -78,9 +78,9 @@ public class ParticlePair { * * @param blockMaterial The player's new block material */ - public void setBlockData(Material blockMaterial) { + public void setBlockMaterial(Material blockMaterial) { if (blockMaterial == null) blockMaterial = Material.STONE; - this.particleBlockMaterial = blockMaterial; + this.blockMaterial = blockMaterial; } /** @@ -88,9 +88,9 @@ public class ParticlePair { * * @param colorData The player's new color data */ - public void setColorData(OrdinaryColor colorData) { + public void setColor(OrdinaryColor colorData) { if (colorData == null) colorData = new OrdinaryColor(0, 0, 0); - this.particleColorData = colorData; + this.color = colorData; } /** @@ -98,9 +98,9 @@ public class ParticlePair { * * @param noteColorData The player's new note color data */ - public void setNoteColorData(NoteColor noteColorData) { + public void setNoteColor(NoteColor noteColorData) { if (noteColorData == null) noteColorData = new NoteColor(0); - this.particleNoteColorData = noteColorData; + this.noteColor = noteColorData; } /** @@ -138,6 +138,42 @@ public class ParticlePair { public ParticleStyle getStyle() { return this.style; } + + /** + * Get the item Material this particle uses + * + * @return The item Material + */ + public Material getItemMaterial() { + return this.itemMaterial; + } + + /** + * Get the block Material this particle uses + * + * @return The block Material + */ + public Material getBlockMaterial() { + return this.blockMaterial; + } + + /** + * Get the color this particle uses + * + * @return The color + */ + public OrdinaryColor getColor() { + return this.color; + } + + /** + * Get the note color this particle uses + * + * @return The note color + */ + public NoteColor getNoteColor() { + return this.noteColor; + } /** * Gets the color the current particle effect will spawn with @@ -147,15 +183,15 @@ public class ParticlePair { public ParticleColor getSpawnColor() { if (this.effect.hasProperty(ParticleProperty.COLORABLE)) { if (this.effect == ParticleEffect.NOTE) { - if (this.particleNoteColorData.getValueX() * 24 == 99) { + if (this.noteColor.getValueX() * 24 == 99) { return ParticleManager.getRainbowNoteParticleColor(); } - return this.particleNoteColorData; + return this.noteColor; } else { - if (this.particleColorData.getRed() == 999 && this.particleColorData.getGreen() == 999 && this.particleColorData.getBlue() == 999) { + if (this.color.getRed() == 999 && this.color.getGreen() == 999 && this.color.getBlue() == 999) { return ParticleManager.getRainbowParticleColor(); } else { - return this.particleColorData; + return this.color; } } } @@ -170,9 +206,9 @@ public class ParticlePair { public Material getSpawnMaterial() { if (this.effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) { if (this.effect == ParticleEffect.ITEM) { - return this.particleItemMaterial; + return this.itemMaterial; } else { - return this.particleBlockMaterial; + return this.blockMaterial; } } return null; @@ -183,22 +219,22 @@ public class ParticlePair { * * @return The particle data in a human-readable string */ - public String getParticleDataString() { + public String getDataString() { if (this.effect == ParticleEffect.BLOCK || this.effect == ParticleEffect.FALLING_DUST) { - return this.particleBlockMaterial.toString().toLowerCase(); + return this.blockMaterial.toString().toLowerCase(); } else if (this.effect == ParticleEffect.ITEM) { - return this.particleItemMaterial.toString().toLowerCase(); + return this.itemMaterial.toString().toLowerCase(); } else if (this.effect.hasProperty(ParticleProperty.COLORABLE)) { if (this.effect == ParticleEffect.NOTE) { - if (this.particleNoteColorData.getValueX() * 24 == 99) { + if (this.noteColor.getValueX() * 24 == 99) { return PlayerParticlesGui.rainbowName; } - return "note #" + (int) (this.particleNoteColorData.getValueX() * 24); + return "note #" + (int) (this.noteColor.getValueX() * 24); } else { - if (this.particleColorData.getRed() == 999 && this.particleColorData.getGreen() == 999 && this.particleColorData.getBlue() == 999) { + if (this.color.getRed() == 999 && this.color.getGreen() == 999 && this.color.getBlue() == 999) { return PlayerParticlesGui.rainbowName; } else { - return ChatColor.RED + "" + this.particleColorData.getRed() + " " + ChatColor.GREEN + this.particleColorData.getGreen() + " " + ChatColor.AQUA + this.particleColorData.getBlue(); + return ChatColor.RED + "" + this.color.getRed() + " " + ChatColor.GREEN + this.color.getGreen() + " " + ChatColor.AQUA + this.color.getBlue(); } } } diff --git a/src/com/esophose/playerparticles/styles/ParticleStyleBlockBreak.java b/src/com/esophose/playerparticles/styles/ParticleStyleBlockBreak.java index a86b4ff..a8bde4e 100644 --- a/src/com/esophose/playerparticles/styles/ParticleStyleBlockBreak.java +++ b/src/com/esophose/playerparticles/styles/ParticleStyleBlockBreak.java @@ -45,7 +45,7 @@ public class ParticleStyleBlockBreak implements ParticleStyle, Listener { Player player = event.getPlayer(); PPlayer pplayer = DataManager.getPPlayer(player.getUniqueId()); if (pplayer != null) { - for (ParticlePair particle : pplayer.getParticlesForStyle(DefaultStyles.BLOCKBREAK)) { + for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.BLOCKBREAK)) { Location loc = event.getBlock().getLocation(); ParticleManager.displayParticles(particle, DefaultStyles.BLOCKBREAK.getParticles(particle, loc)); } diff --git a/src/com/esophose/playerparticles/styles/ParticleStyleBlockEdit.java b/src/com/esophose/playerparticles/styles/ParticleStyleBlockEdit.java index 1816590..969d75e 100644 --- a/src/com/esophose/playerparticles/styles/ParticleStyleBlockEdit.java +++ b/src/com/esophose/playerparticles/styles/ParticleStyleBlockEdit.java @@ -41,7 +41,7 @@ public class ParticleStyleBlockEdit implements ParticleStyle, Listener { Player player = event.getPlayer(); PPlayer pplayer = DataManager.getPPlayer(player.getUniqueId()); if (pplayer != null) { - for (ParticlePair particle : pplayer.getParticlesForStyle(DefaultStyles.BLOCKEDIT)) { + for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.BLOCKEDIT)) { Location loc = event.getBlock().getLocation(); ParticleManager.displayParticles(particle, DefaultStyles.BLOCKBREAK.getParticles(particle, loc)); } @@ -53,7 +53,7 @@ public class ParticleStyleBlockEdit implements ParticleStyle, Listener { Player player = event.getPlayer(); PPlayer pplayer = DataManager.getPPlayer(player.getUniqueId()); if (pplayer != null) { - for (ParticlePair particle : pplayer.getParticlesForStyle(DefaultStyles.BLOCKEDIT)) { + for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.BLOCKEDIT)) { Location loc = event.getBlock().getLocation(); ParticleManager.displayParticles(particle, DefaultStyles.BLOCKPLACE.getParticles(particle, loc)); } diff --git a/src/com/esophose/playerparticles/styles/ParticleStyleBlockPlace.java b/src/com/esophose/playerparticles/styles/ParticleStyleBlockPlace.java index 363d814..976c624 100644 --- a/src/com/esophose/playerparticles/styles/ParticleStyleBlockPlace.java +++ b/src/com/esophose/playerparticles/styles/ParticleStyleBlockPlace.java @@ -45,7 +45,7 @@ public class ParticleStyleBlockPlace implements ParticleStyle, Listener { Player player = event.getPlayer(); PPlayer pplayer = DataManager.getPPlayer(player.getUniqueId()); if (pplayer != null) { - for (ParticlePair particle : pplayer.getParticlesForStyle(DefaultStyles.BLOCKPLACE)) { + for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.BLOCKPLACE)) { Location loc = event.getBlock().getLocation(); ParticleManager.displayParticles(particle, DefaultStyles.BLOCKPLACE.getParticles(particle, loc)); } diff --git a/src/com/esophose/playerparticles/styles/ParticleStyleHurt.java b/src/com/esophose/playerparticles/styles/ParticleStyleHurt.java index ad9506c..931cbca 100644 --- a/src/com/esophose/playerparticles/styles/ParticleStyleHurt.java +++ b/src/com/esophose/playerparticles/styles/ParticleStyleHurt.java @@ -49,7 +49,7 @@ public class ParticleStyleHurt implements ParticleStyle, Listener { Player player = (Player) event.getEntity(); PPlayer pplayer = DataManager.getPPlayer(player.getUniqueId()); if (pplayer != null) { - for (ParticlePair particle : pplayer.getParticlesForStyle(DefaultStyles.HURT)) { + for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.HURT)) { Location loc = player.getLocation().clone().add(0, 1, 0); ParticleManager.displayParticles(particle, DefaultStyles.HURT.getParticles(particle, loc)); } diff --git a/src/com/esophose/playerparticles/styles/ParticleStyleMove.java b/src/com/esophose/playerparticles/styles/ParticleStyleMove.java index 8430a4f..623e20a 100644 --- a/src/com/esophose/playerparticles/styles/ParticleStyleMove.java +++ b/src/com/esophose/playerparticles/styles/ParticleStyleMove.java @@ -37,7 +37,7 @@ public class ParticleStyleMove implements ParticleStyle, Listener { public void onPlayerMove(PlayerMoveEvent e) { PPlayer pplayer = DataManager.getPPlayer(e.getPlayer().getUniqueId()); if (pplayer != null) { - for (ParticlePair particle : pplayer.getParticlesForStyle(DefaultStyles.MOVE)) { + for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.MOVE)) { Location loc = e.getPlayer().getLocation(); loc.setY(loc.getY() + 0.05); ParticleManager.displayParticles(particle, DefaultStyles.MOVE.getParticles(particle, loc)); diff --git a/src/com/esophose/playerparticles/styles/ParticleStyleSwords.java b/src/com/esophose/playerparticles/styles/ParticleStyleSwords.java index 8beb7ab..3e01615 100644 --- a/src/com/esophose/playerparticles/styles/ParticleStyleSwords.java +++ b/src/com/esophose/playerparticles/styles/ParticleStyleSwords.java @@ -59,7 +59,7 @@ public class ParticleStyleSwords implements ParticleStyle, Listener { LivingEntity entity = (LivingEntity) event.getEntity(); PPlayer pplayer = DataManager.getPPlayer(player.getUniqueId()); if (pplayer != null) { - for (ParticlePair particle : pplayer.getParticlesForStyle(DefaultStyles.SWORDS)) { + for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.SWORDS)) { Location loc = entity.getLocation().clone().add(0, 1, 0); ParticleManager.displayParticles(particle, DefaultStyles.SWORDS.getParticles(particle, loc)); }