Added ability to toggle individual player particle visibility with API

This commit is contained in:
Esophose 2021-10-27 09:46:32 -06:00
parent 1c48ff3894
commit 56840f771f
No known key found for this signature in database
GPG key ID: DE0E013CAE5C630A
14 changed files with 125 additions and 12 deletions

View file

@ -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
}

View file

@ -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");
}
}
}

View file

@ -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.
*

View file

@ -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<DataMigration> migrations;
private final List<DataMigration> 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()
);
}

View file

@ -11,7 +11,7 @@ public class ConsolePPlayer extends PPlayer {
private static final UUID uuid = UUID.fromString("ffffffff-ffff-ffff-ffff-ffffffffffff");
public ConsolePPlayer(Map<String, ParticleGroup> particleGroups, Map<Integer, FixedParticleEffect> fixedParticles) {
super(uuid, particleGroups, fixedParticles, false);
super(uuid, particleGroups, fixedParticles, false, false);
}
/**

View file

@ -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;
}

View file

@ -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<String, ParticleGroup> particleGroups, Map<Integer, FixedParticleEffect> fixedParticles, boolean particlesHidden) {
public PPlayer(UUID uuid, Map<String, ParticleGroup> particleGroups, Map<Integer, FixedParticleEffect> 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
*

View file

@ -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)

View file

@ -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");

View file

@ -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");

View file

@ -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");

View file

@ -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)

View file

@ -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");

View file

@ -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");