From 621862a5be422f87947c2e8b5c67b1a398d9534f Mon Sep 17 00:00:00 2001 From: Esophose Date: Fri, 14 Feb 2020 19:10:52 -0700 Subject: [PATCH] Added the option to spawn particles from far away --- .../manager/ParticleManager.java | 9 ++-- .../particles/ParticleEffect.java | 52 +++++++++---------- .../playerparticles/styles/ParticleStyle.java | 25 +++++++-- .../styles/ParticleStyleArrows.java | 10 ++++ .../styles/ParticleStyleBlockBreak.java | 2 +- .../styles/ParticleStyleBlockPlace.java | 2 +- .../styles/ParticleStyleCelebration.java | 4 +- .../styles/ParticleStyleHurt.java | 2 +- .../styles/ParticleStyleMove.java | 2 +- .../styles/ParticleStyleSwords.java | 2 +- 10 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/main/java/dev/esophose/playerparticles/manager/ParticleManager.java b/src/main/java/dev/esophose/playerparticles/manager/ParticleManager.java index 8f6fe38..ce56e00 100644 --- a/src/main/java/dev/esophose/playerparticles/manager/ParticleManager.java +++ b/src/main/java/dev/esophose/playerparticles/manager/ParticleManager.java @@ -171,10 +171,10 @@ public class ParticleManager extends Manager implements Listener, Runnable { if (Setting.TOGGLE_ON_MOVE.getBoolean() && particle.getStyle().canToggleWithMovement() && pplayer.isMoving()) { for (PParticle pparticle : DefaultStyles.FEET.getParticles(particle, location)) - ParticleEffect.display(particle, pparticle, false, pplayer.getPlayer()); + ParticleEffect.display(particle, pparticle, particle.getStyle().hasLongRangeVisibility(), pplayer.getPlayer()); } else { for (PParticle pparticle : particle.getStyle().getParticles(particle, location)) - ParticleEffect.display(particle, pparticle, false, pplayer.getPlayer()); + ParticleEffect.display(particle, pparticle, particle.getStyle().hasLongRangeVisibility(), pplayer.getPlayer()); } } } @@ -186,14 +186,15 @@ public class ParticleManager extends Manager implements Listener, Runnable { * @param world The world the particles are spawning in * @param particle The ParticlePair to use for getting particle settings * @param particles The particles to display + * @param isLongRange If the particle can be viewed from long range */ - public void displayParticles(Player player, World world, ParticlePair particle, List particles) { + public void displayParticles(Player player, World world, ParticlePair particle, List particles, boolean isLongRange) { PermissionManager permissionManager = this.playerParticles.getManager(PermissionManager.class); if ((player != null && player.getGameMode() == GameMode.SPECTATOR) || !permissionManager.isWorldEnabled(world.getName())) return; for (PParticle pparticle : particles) - ParticleEffect.display(particle, pparticle, false, Bukkit.getPlayer(particle.getOwnerUniqueId())); + ParticleEffect.display(particle, pparticle, isLongRange, Bukkit.getPlayer(particle.getOwnerUniqueId())); } /** diff --git a/src/main/java/dev/esophose/playerparticles/particles/ParticleEffect.java b/src/main/java/dev/esophose/playerparticles/particles/ParticleEffect.java index 8e419a1..41f1396 100644 --- a/src/main/java/dev/esophose/playerparticles/particles/ParticleEffect.java +++ b/src/main/java/dev/esophose/playerparticles/particles/ParticleEffect.java @@ -273,19 +273,19 @@ public enum ParticleEffect { * * @param particle The ParticlePair, given the effect/style/data * @param pparticle The particle spawn information - * @param isFixedEffect If the particle is spawned from a fixed effect + * @param isLongRange If the particle can be viewed from long range * @param owner The player that owns the particles */ - public static void display(ParticlePair particle, PParticle pparticle, boolean isFixedEffect, Player owner) { + public static void display(ParticlePair particle, PParticle pparticle, boolean isLongRange, Player owner) { ParticleEffect effect = particle.getEffect(); int count = pparticle.isDirectional() ? 0 : 1; if (effect.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) { - effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(false), isFixedEffect, owner); + effect.display(particle.getSpawnMaterial(), pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), 1, pparticle.getLocation(false), isLongRange, owner); } else if (effect.hasProperty(ParticleProperty.COLORABLE)) { - effect.display(particle.getSpawnColor(), pparticle.getLocation(true), isFixedEffect, owner); + effect.display(particle.getSpawnColor(), pparticle.getLocation(true), isLongRange, owner); } else { - effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), count, pparticle.getLocation(false), isFixedEffect, owner); + effect.display(pparticle.getXOff(), pparticle.getYOff(), pparticle.getZOff(), pparticle.getSpeed(), count, pparticle.getLocation(false), isLongRange, owner); } } @@ -298,18 +298,16 @@ public enum ParticleEffect { * @param speed Display speed of the particles * @param amount Amount of particles * @param center Center location of the effect - * @param isFixedEffect If the particle is spawned from a fixed effect + * @param isLongRange If the particle can be viewed from long range * @param owner The player that owns the particles * @throws ParticleDataException If the particle effect requires additional data */ - public void display(double offsetX, double offsetY, double offsetZ, double speed, int amount, Location center, boolean isFixedEffect, Player owner) throws ParticleDataException { - if (this.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) { + public void display(double offsetX, double offsetY, double offsetZ, double speed, int amount, Location center, boolean isLongRange, Player owner) throws ParticleDataException { + if (this.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) throw new ParticleDataException("This particle effect requires additional data"); - } - for (Player player : this.getPlayersInRange(center, isFixedEffect, owner)) { + for (Player player : this.getPlayersInRange(center, isLongRange, owner)) player.spawnParticle(this.internalEnum, center.getX(), center.getY(), center.getZ(), amount, offsetX, offsetY, offsetZ, speed); - } } /** @@ -317,23 +315,21 @@ public enum ParticleEffect { * * @param color Color of the particle * @param center Center location of the effect - * @param isFixedEffect If the particle is spawned from a fixed effect + * @param isLongRange If the particle can be viewed from long range * @param owner The player that owns the particles * @throws ParticleColorException If the particle effect is not colorable or the color type is incorrect */ - public void display(ParticleColor color, Location center, boolean isFixedEffect, Player owner) throws ParticleColorException { - if (!this.hasProperty(ParticleProperty.COLORABLE)) { + public void display(ParticleColor color, Location center, boolean isLongRange, Player owner) throws ParticleColorException { + if (!this.hasProperty(ParticleProperty.COLORABLE)) throw new ParticleColorException("This particle effect is not colorable"); - } - if (this == DUST && NMSUtil.getVersionNumber() >= 13) { // DUST uses a special data object for spawning in 1.13 + if (this == DUST && NMSUtil.getVersionNumber() >= 13) { // DUST uses a special data object for spawning in 1.13+ OrdinaryColor dustColor = (OrdinaryColor) color; DustOptions dustOptions = new DustOptions(Color.fromRGB(dustColor.getRed(), dustColor.getGreen(), dustColor.getBlue()), Setting.DUST_SIZE.getFloat()); - for (Player player : this.getPlayersInRange(center, isFixedEffect, owner)) { + for (Player player : this.getPlayersInRange(center, isLongRange, owner)) player.spawnParticle(this.internalEnum, center.getX(), center.getY(), center.getZ(), 1, 0, 0, 0, 0, dustOptions); - } } else { - for (Player player : this.getPlayersInRange(center, isFixedEffect, owner)) { + for (Player player : this.getPlayersInRange(center, isLongRange, owner)) { // Minecraft clients require that you pass a non-zero value if the Red value should be zero player.spawnParticle(this.internalEnum, center.getX(), center.getY(), center.getZ(), 0, this == ParticleEffect.DUST && color.getValueX() == 0 ? Float.MIN_VALUE : color.getValueX(), color.getValueY(), color.getValueZ(), 1); } @@ -352,11 +348,11 @@ public enum ParticleEffect { * @param speed Display speed of the particles * @param amount Amount of particles * @param center Center location of the effect - * @param isFixedEffect If the particle is spawned from a fixed effect + * @param isLongRange If the particle can be viewed from long range * @param owner The player that owns the particles * @throws ParticleDataException If the particle effect does not require additional data or if the data type is incorrect */ - public void display(Material spawnMaterial, double offsetX, double offsetY, double offsetZ, double speed, int amount, Location center, boolean isFixedEffect, Player owner) throws ParticleDataException { + public void display(Material spawnMaterial, double offsetX, double offsetY, double offsetZ, double speed, int amount, Location center, boolean isLongRange, Player owner) throws ParticleDataException { if (!this.hasProperty(ParticleProperty.REQUIRES_MATERIAL_DATA)) { throw new ParticleDataException("This particle effect does not require additional data"); } @@ -370,7 +366,7 @@ public enum ParticleEffect { extraData = new MaterialData(spawnMaterial); // Deprecated, only used in versions < 1.13 } - for (Player player : this.getPlayersInRange(center, isFixedEffect, owner)) + for (Player player : this.getPlayersInRange(center, isLongRange, owner)) player.spawnParticle(this.internalEnum, center.getX(), center.getY(), center.getZ(), amount, offsetX, offsetY, offsetZ, speed, extraData); } @@ -378,22 +374,22 @@ public enum ParticleEffect { * Gets a List of Players within the particle display range * * @param center The center of the radius to check around - * @param isFixedEffect If the particle is spawned from a fixed effect + * @param isLongRange If the particle can be viewed from long range * @param owner The player that owns the particles * @return A List of Players within the particle display range */ - private List getPlayersInRange(Location center, boolean isFixedEffect, Player owner) { + private List getPlayersInRange(Location center, boolean isLongRange, Player owner) { List players = new ArrayList<>(); - int range = !isFixedEffect ? Setting.PARTICLE_RENDER_RANGE_PLAYER.getInt() : Setting.PARTICLE_RENDER_RANGE_FIXED_EFFECT.getInt(); + int range = !isLongRange ? Setting.PARTICLE_RENDER_RANGE_PLAYER.getInt() : Setting.PARTICLE_RENDER_RANGE_FIXED_EFFECT.getInt(); + range *= range; for (PPlayer pplayer : PlayerParticles.getInstance().getManager(ParticleManager.class).getPPlayers()) { Player p = pplayer.getPlayer(); - if (!isFixedEffect && !this.canSee(p, owner)) + if (!this.canSee(p, owner)) continue; - if (p != null && pplayer.canSeeParticles() && p.getWorld().equals(center.getWorld()) && center.distanceSquared(p.getLocation()) <= range * range) { + if (p != null && pplayer.canSeeParticles() && p.getWorld().equals(center.getWorld()) && center.distanceSquared(p.getLocation()) <= range) players.add(p); - } } return players; diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyle.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyle.java index b19a858..fa84857 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyle.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyle.java @@ -26,7 +26,9 @@ public interface ParticleStyle { /** * @return true if the style is enabled, false otherwise */ - boolean isEnabled(); + default boolean isEnabled() { + return true; + } /** * @return The style's internal name that will always remain constant @@ -36,7 +38,9 @@ public interface ParticleStyle { /** * @return The name that the style will display to the users as */ - String getName(); + default String getName() { + return this.getInternalName(); + } /** * Gets if the style can be used in a FixedParticleEffect @@ -50,15 +54,26 @@ public interface ParticleStyle { * * @return True if it can be, otherwise False */ - boolean canToggleWithMovement(); + default boolean canToggleWithMovement() { + return true; + } /** * The Y-axis offset to be applied when using '/pp fixed create looking' * * @return How far to move the style up or down to get it centered on the block properly */ - double getFixedEffectOffset(); - + default double getFixedEffectOffset() { + return 0; + } + + /** + * @return true if the particle should be seen from the fixed effect distance instead of the player distance, or false otherwise + */ + default boolean hasLongRangeVisibility() { + return false; + } + /** * Gets the ParticleStyle with the name given, returns null if not found * diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleArrows.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleArrows.java index 0f6f2fd..e52f19b 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleArrows.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleArrows.java @@ -1,6 +1,8 @@ package dev.esophose.playerparticles.styles; +import dev.esophose.playerparticles.PlayerParticles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; +import dev.esophose.playerparticles.manager.ParticleManager; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; import java.util.ArrayList; @@ -20,9 +22,12 @@ public class ParticleStyleArrows extends DefaultParticleStyle implements Listene private int maxArrowsPerPlayer; private boolean onlySpawnIfFlying; private List arrowEntityNames; + private ParticleManager particleManager; public ParticleStyleArrows() { super("arrows", false, false, 0); + + this.particleManager = PlayerParticles.getInstance().getManager(ParticleManager.class); } @Override @@ -59,6 +64,11 @@ public class ParticleStyleArrows extends DefaultParticleStyle implements Listene } } + @Override + public boolean hasLongRangeVisibility() { + return true; + } + /** * The event used to get all projectiles fired by players * Adds all projectiles fired from players to the array diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java index 76c8bb6..4ab81d7 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockBreak.java @@ -66,7 +66,7 @@ public class ParticleStyleBlockBreak extends DefaultParticleStyle implements Lis if (pplayer != null) { for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.BLOCKBREAK)) { Location loc = event.getBlock().getLocation().clone(); - particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.BLOCKBREAK.getParticles(particle, loc)); + particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.BLOCKBREAK.getParticles(particle, loc), false); } } } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java index 2cc7a3a..3b5aa5c 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBlockPlace.java @@ -66,7 +66,7 @@ public class ParticleStyleBlockPlace extends DefaultParticleStyle implements Lis if (pplayer != null) { for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.BLOCKPLACE)) { Location loc = event.getBlock().getLocation().clone(); - particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.BLOCKPLACE.getParticles(particle, loc)); + particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.BLOCKPLACE.getParticles(particle, loc), false); } } } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java index d0ad526..647fb1d 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java @@ -123,7 +123,7 @@ public class ParticleStyleCelebration extends DefaultParticleStyle { trail.setEffect(ParticleStyleCelebration.this.fuseEffect); trail.setStyle(DefaultStyles.CELEBRATION); - particleManager.displayParticles(player, this.location.getWorld(), trail, Collections.singletonList(new PParticle(this.location))); + particleManager.displayParticles(player, this.location.getWorld(), trail, Collections.singletonList(new PParticle(this.location)), true); this.location.add(0, ParticleStyleCelebration.this.fuseSpacing, 0); } else { @@ -140,7 +140,7 @@ public class ParticleStyleCelebration extends DefaultParticleStyle { particles.add(new PParticle(this.location.clone().add(dx, dy, dz))); } - particleManager.displayParticles(player, this.location.getWorld(), particle, particles); + particleManager.displayParticles(player, this.location.getWorld(), particle, particles, true); this.cancel(); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHurt.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHurt.java index 99a9618..e2ca3aa 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHurt.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHurt.java @@ -49,7 +49,7 @@ public class ParticleStyleHurt extends DefaultParticleStyle implements Listener if (pplayer != null) { for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.HURT)) { Location loc = player.getLocation().clone().add(0, 1, 0); - particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.HURT.getParticles(particle, loc)); + particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.HURT.getParticles(particle, loc), false); } } } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleMove.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleMove.java index be48f41..9ea856a 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleMove.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleMove.java @@ -59,7 +59,7 @@ public class ParticleStyleMove extends DefaultParticleStyle implements Listener Location loc = event.getPlayer().getLocation().clone(); loc.setY(loc.getY() + 0.05); Player player = event.getPlayer(); - particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.MOVE.getParticles(particle, loc)); + particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.MOVE.getParticles(particle, loc), false); } } } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java index ceb4834..398dc6c 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSwords.java @@ -69,7 +69,7 @@ public class ParticleStyleSwords extends DefaultParticleStyle implements Listene if (pplayer != null && SWORD_NAMES.contains(player.getInventory().getItemInMainHand().getType().name())) { for (ParticlePair particle : pplayer.getActiveParticlesForStyle(DefaultStyles.SWORDS)) { Location loc = entity.getLocation().clone().add(0, 1, 0); - particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.SWORDS.getParticles(particle, loc)); + particleManager.displayParticles(player, player.getWorld(), particle, DefaultStyles.SWORDS.getParticles(particle, loc), false); } } }