From 56840f771f294b115438e77de53f77326e2b2f79 Mon Sep 17 00:00:00 2001 From: Esophose Date: Wed, 27 Oct 2021 09:46:32 -0600 Subject: [PATCH] Added ability to toggle individual player particle visibility with API --- .../api/PlayerParticlesAPI.java | 16 +++++++++++ .../_3_Add_Setting_Toggle_Self_Column.java | 23 +++++++++++++++ .../playerparticles/manager/DataManager.java | 27 ++++++++++++++++-- .../manager/DataMigrationManager.java | 6 ++-- .../particles/ConsolePPlayer.java | 2 +- .../particles/OtherPPlayer.java | 6 ++-- .../playerparticles/particles/PPlayer.java | 28 ++++++++++++++++++- .../particles/spawning/ParticleSpawner.java | 2 +- .../styles/ParticleStyleBlockBreak.java | 5 ++++ .../styles/ParticleStyleBlockPlace.java | 5 ++++ .../styles/ParticleStyleDeath.java | 5 ++++ .../styles/ParticleStyleFishing.java | 2 +- .../styles/ParticleStyleSwords.java | 5 ++++ .../styles/ParticleStyleTeleport.java | 5 ++++ 14 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 src/main/java/dev/esophose/playerparticles/database/migrations/_3_Add_Setting_Toggle_Self_Column.java diff --git a/src/main/java/dev/esophose/playerparticles/api/PlayerParticlesAPI.java b/src/main/java/dev/esophose/playerparticles/api/PlayerParticlesAPI.java index 31b4535..af74347 100644 --- a/src/main/java/dev/esophose/playerparticles/api/PlayerParticlesAPI.java +++ b/src/main/java/dev/esophose/playerparticles/api/PlayerParticlesAPI.java @@ -1125,6 +1125,22 @@ public final class PlayerParticlesAPI { dataManager.updateSettingParticlesHidden(player.getUniqueId(), particlesHidden); } + /** + * Toggles a player's particle visibility for their own particles on/off + * + * @param player The player to toggle visibility for + * @param particlesHidden true if the player's own particles should be hidden, or false for visible + */ + public void togglePlayerParticleSelfVisibility(@NotNull Player player, boolean particlesHidden) { + DataManager dataManager = this.playerParticles.getManager(DataManager.class); + PPlayer pplayer = this.getPPlayer(player); + if (pplayer == null) + return; + + pplayer.setParticlesHiddenSelf(particlesHidden); + dataManager.updateSettingParticlesHiddenSelf(player.getUniqueId(), particlesHidden); + } + //endregion } diff --git a/src/main/java/dev/esophose/playerparticles/database/migrations/_3_Add_Setting_Toggle_Self_Column.java b/src/main/java/dev/esophose/playerparticles/database/migrations/_3_Add_Setting_Toggle_Self_Column.java new file mode 100644 index 0000000..5b8403b --- /dev/null +++ b/src/main/java/dev/esophose/playerparticles/database/migrations/_3_Add_Setting_Toggle_Self_Column.java @@ -0,0 +1,23 @@ +package dev.esophose.playerparticles.database.migrations; + +import dev.esophose.playerparticles.database.DataMigration; +import dev.esophose.playerparticles.database.DatabaseConnector; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +public class _3_Add_Setting_Toggle_Self_Column extends DataMigration { + + public _3_Add_Setting_Toggle_Self_Column() { + super(3); + } + + @Override + public void migrate(DatabaseConnector connector, Connection connection, String tablePrefix) throws SQLException { + try (Statement statement = connection.createStatement()) { + statement.executeUpdate("ALTER TABLE " + tablePrefix + "settings ADD COLUMN particles_hidden_self TINYINT DEFAULT 0"); + } + } + +} + diff --git a/src/main/java/dev/esophose/playerparticles/manager/DataManager.java b/src/main/java/dev/esophose/playerparticles/manager/DataManager.java index 041e7eb..4effcc1 100644 --- a/src/main/java/dev/esophose/playerparticles/manager/DataManager.java +++ b/src/main/java/dev/esophose/playerparticles/manager/DataManager.java @@ -118,20 +118,23 @@ public class DataManager extends Manager { this.databaseConnector.connect((connection) -> { // Load settings boolean particlesHidden = false; - String settingsQuery = "SELECT particles_hidden FROM " + this.getTablePrefix() + "settings WHERE player_uuid = ?"; + boolean particlesHiddenSelf = false; + String settingsQuery = "SELECT particles_hidden, particles_hidden_self FROM " + this.getTablePrefix() + "settings WHERE player_uuid = ?"; try (PreparedStatement statement = connection.prepareStatement(settingsQuery)) { statement.setString(1, playerUUID.toString()); ResultSet result = statement.executeQuery(); if (result.next()) { particlesHidden = result.getBoolean("particles_hidden"); + particlesHiddenSelf = result.getBoolean("particles_hidden_self"); } else { statement.close(); - String updateQuery = "INSERT INTO " + this.getTablePrefix() + "settings (player_uuid, particles_hidden) VALUES (?, ?)"; + String updateQuery = "INSERT INTO " + this.getTablePrefix() + "settings (player_uuid, particles_hidden, particles_hidden_self) VALUES (?, ?)"; try (PreparedStatement updateStatement = connection.prepareStatement(updateQuery)) { updateStatement.setString(1, playerUUID.toString()); updateStatement.setBoolean(2, false); + updateStatement.setBoolean(3, false); updateStatement.executeUpdate(); } @@ -259,7 +262,7 @@ public class DataManager extends Manager { PPlayer loadedPPlayer; if (!playerUUID.equals(ConsolePPlayer.getUUID())) { - loadedPPlayer = new PPlayer(playerUUID, groups, fixedParticles, particlesHidden); + loadedPPlayer = new PPlayer(playerUUID, groups, fixedParticles, particlesHidden, particlesHiddenSelf); } else { loadedPPlayer = new ConsolePPlayer(groups, fixedParticles); } @@ -306,6 +309,24 @@ public class DataManager extends Manager { })); } + /** + * Updates the particles_hidden_self setting in the database and for the PPlayer + * + * @param playerUUID The player to hide their own PlayerParticles from + * @param particlesHidden True if the particles should be hidden, otherwise False + */ + public void updateSettingParticlesHiddenSelf(UUID playerUUID, boolean particlesHidden) { + this.async(() -> this.databaseConnector.connect((connection) -> { + String updateQuery = "UPDATE " + this.getTablePrefix() + "settings SET particles_hidden_self = ? WHERE player_uuid = ?"; + try (PreparedStatement updateStatement = connection.prepareStatement(updateQuery)) { + updateStatement.setBoolean(1, particlesHidden); + updateStatement.setString(2, playerUUID.toString()); + + updateStatement.executeUpdate(); + } + })); + } + /** * Saves a ParticleGroup. If it already exists, update it. If it's empty, delete it. * diff --git a/src/main/java/dev/esophose/playerparticles/manager/DataMigrationManager.java b/src/main/java/dev/esophose/playerparticles/manager/DataMigrationManager.java index 4169041..afa14b2 100644 --- a/src/main/java/dev/esophose/playerparticles/manager/DataMigrationManager.java +++ b/src/main/java/dev/esophose/playerparticles/manager/DataMigrationManager.java @@ -6,6 +6,7 @@ import dev.esophose.playerparticles.database.DatabaseConnector; import dev.esophose.playerparticles.database.SQLiteConnector; import dev.esophose.playerparticles.database.migrations._1_InitialMigration; import dev.esophose.playerparticles.database.migrations._2_Add_Data_Columns; +import dev.esophose.playerparticles.database.migrations._3_Add_Setting_Toggle_Self_Column; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Arrays; @@ -15,14 +16,15 @@ import java.util.stream.Collectors; public class DataMigrationManager extends Manager { - private List migrations; + private final List migrations; public DataMigrationManager(PlayerParticles playerParticles) { super(playerParticles); this.migrations = Arrays.asList( new _1_InitialMigration(), - new _2_Add_Data_Columns() + new _2_Add_Data_Columns(), + new _3_Add_Setting_Toggle_Self_Column() ); } diff --git a/src/main/java/dev/esophose/playerparticles/particles/ConsolePPlayer.java b/src/main/java/dev/esophose/playerparticles/particles/ConsolePPlayer.java index fb02e43..685bc6e 100644 --- a/src/main/java/dev/esophose/playerparticles/particles/ConsolePPlayer.java +++ b/src/main/java/dev/esophose/playerparticles/particles/ConsolePPlayer.java @@ -11,7 +11,7 @@ public class ConsolePPlayer extends PPlayer { private static final UUID uuid = UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff"); public ConsolePPlayer(Map particleGroups, Map fixedParticles) { - super(uuid, particleGroups, fixedParticles, false); + super(uuid, particleGroups, fixedParticles, false, false); } /** diff --git a/src/main/java/dev/esophose/playerparticles/particles/OtherPPlayer.java b/src/main/java/dev/esophose/playerparticles/particles/OtherPPlayer.java index f84a30f..59a09ce 100644 --- a/src/main/java/dev/esophose/playerparticles/particles/OtherPPlayer.java +++ b/src/main/java/dev/esophose/playerparticles/particles/OtherPPlayer.java @@ -6,16 +6,16 @@ import org.bukkit.command.CommandSender; public class OtherPPlayer extends PPlayer { - private CommandSender sender; + private final CommandSender sender; public OtherPPlayer(CommandSender sender) { - super(UUID.randomUUID(), Collections.emptyMap(), Collections.emptyMap(), false); + super(UUID.randomUUID(), Collections.emptyMap(), Collections.emptyMap(), false, false); this.sender = sender; } public OtherPPlayer(CommandSender sender, PPlayer other) { - super(other.getUniqueId(), other.getParticleGroups(), other.getFixedParticlesMap(), !other.canSeeParticles()); + super(other.getUniqueId(), other.getParticleGroups(), other.getFixedParticlesMap(), !other.canSeeParticles(), !other.canSeeOwnParticles()); this.sender = sender; } diff --git a/src/main/java/dev/esophose/playerparticles/particles/PPlayer.java b/src/main/java/dev/esophose/playerparticles/particles/PPlayer.java index 235550b..fb4573f 100644 --- a/src/main/java/dev/esophose/playerparticles/particles/PPlayer.java +++ b/src/main/java/dev/esophose/playerparticles/particles/PPlayer.java @@ -37,6 +37,11 @@ public class PPlayer { * If True, the player will not see any particles spawned by the plugin */ private boolean particlesHidden; + + /** + * If True, the player will not see their own particles spawned by the plugin (excludes fixed effects) + */ + private boolean particlesHiddenSelf; /** * If the player is moving @@ -60,13 +65,16 @@ public class PPlayer { * @param particleGroups The ParticleGroups this PPlayer has * @param fixedParticles The FixedParticleEffects this PPlayer has * @param particlesHidden If this player has all particles hidden from view + * @param particlesHiddenSelf If this player has their own particles hidden from view */ - public PPlayer(UUID uuid, Map particleGroups, Map fixedParticles, boolean particlesHidden) { + public PPlayer(UUID uuid, Map particleGroups, Map fixedParticles, boolean particlesHidden, boolean particlesHiddenSelf) { this.playerUUID = uuid; this.particleGroups = particleGroups; this.fixedParticles = fixedParticles; this.particlesHidden = particlesHidden; + this.particlesHiddenSelf = particlesHiddenSelf; + this.isMoving = false; this.inCombat = false; this.inAllowedRegion = true; @@ -114,6 +122,15 @@ public class PPlayer { public boolean canSeeParticles() { return !this.particlesHidden; } + + /** + * Gets if the Player can see their own particles spawned by the plugin or not + * + * @return True if the player can see their own particles, otherwise false + */ + public boolean canSeeOwnParticles() { + return !this.particlesHiddenSelf; + } /** * Sets if the player can see particles spawned by the plugin or not @@ -124,6 +141,15 @@ public class PPlayer { this.particlesHidden = hidden; } + /** + * Sets if the player can see their own particles spawned by the plugin or not + * + * @param hidden True if the player can see their own particles, otherwise false + */ + public void setParticlesHiddenSelf(boolean hidden) { + this.particlesHiddenSelf = hidden; + } + /** * Get a map of ParticleGroups this user has saved * diff --git a/src/main/java/dev/esophose/playerparticles/particles/spawning/ParticleSpawner.java b/src/main/java/dev/esophose/playerparticles/particles/spawning/ParticleSpawner.java index 1788565..f04097a 100644 --- a/src/main/java/dev/esophose/playerparticles/particles/spawning/ParticleSpawner.java +++ b/src/main/java/dev/esophose/playerparticles/particles/spawning/ParticleSpawner.java @@ -117,7 +117,7 @@ public abstract class ParticleSpawner { for (PPlayer pplayer : PlayerParticles.getInstance().getManager(ParticleManager.class).getPPlayers().values()) { Player p = pplayer.getPlayer(); - if (!canSee(p, owner)) + if (!canSee(p, owner) || (!pplayer.canSeeOwnParticles() && p == owner && !isLongRange)) continue; if (p != null && pplayer.canSeeParticles() && p.getWorld().equals(center.getWorld()) && center.distanceSquared(p.getLocation()) <= range) diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java index 0c6c617..95a8912 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java @@ -49,6 +49,11 @@ public class ParticleStyleBlockBreak extends DefaultParticleStyle implements Lis return Collections.singletonList("IRON_PICKAXE"); } + @Override + public boolean hasLongRangeVisibility() { + return true; + } + @Override protected void setDefaultSettings(CommentedFileConfiguration config) { this.setIfNotExists("particle-amount", 10, "The number of particles to spawn"); diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java index 2be6326..d3ed9ac 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java @@ -49,6 +49,11 @@ public class ParticleStyleBlockPlace extends DefaultParticleStyle implements Lis return Arrays.asList("OAK_PLANKS", "WOOD"); } + @Override + public boolean hasLongRangeVisibility() { + return true; + } + @Override protected void setDefaultSettings(CommentedFileConfiguration config) { this.setIfNotExists("particle-amount", 10, "The number of particles to spawn"); diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleDeath.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleDeath.java index 474955f..e8339f1 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleDeath.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleDeath.java @@ -50,6 +50,11 @@ public class ParticleStyleDeath extends DefaultParticleStyle implements Listener return Arrays.asList("TOTEM_OF_UNDYING", "TOTEM", "BED"); } + @Override + public boolean hasLongRangeVisibility() { + return true; + } + @Override protected void setDefaultSettings(CommentedFileConfiguration config) { this.setIfNotExists("style", "whirl", "The name of the style to be displayed"); diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleFishing.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleFishing.java index 7b99da0..e6ac119 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleFishing.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleFishing.java @@ -69,7 +69,7 @@ public class ParticleStyleFishing extends DefaultParticleStyle implements Listen @Override public boolean hasLongRangeVisibility() { - return false; + return true; } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java index 3820cdd..e7baf26 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java @@ -57,6 +57,11 @@ public class ParticleStyleSwords extends DefaultParticleStyle implements Listene return Collections.singletonList("IRON_SWORD"); } + @Override + public boolean hasLongRangeVisibility() { + return true; + } + @Override protected void setDefaultSettings(CommentedFileConfiguration config) { this.setIfNotExists("multiplier", 15, "The multiplier for the number of particles to spawn", "This style uses the same spawning as the 'normal' style"); diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTeleport.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTeleport.java index 3fe17a3..b2ded41 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTeleport.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTeleport.java @@ -52,6 +52,11 @@ public class ParticleStyleTeleport extends DefaultParticleStyle implements Liste return Collections.singletonList("ENDER_PEARL"); } + @Override + public boolean hasLongRangeVisibility() { + return true; + } + @Override protected void setDefaultSettings(CommentedFileConfiguration config) { this.setIfNotExists("before", true, "Spawn the particles at the teleporting position");