Skip to content

Commit

Permalink
Added the option to spawn particles from far away
Browse files Browse the repository at this point in the history
  • Loading branch information
Esophose committed Feb 15, 2020
1 parent cad1ecb commit 621862a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ private void displayParticles(PPlayer pplayer, ParticlePair particle, Location l

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());
}
}
}
Expand All @@ -186,14 +186,15 @@ private void displayParticles(PPlayer pplayer, ParticlePair particle, Location l
* @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<PParticle> particles) {
public void displayParticles(Player player, World world, ParticlePair particle, List<PParticle> 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()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,19 @@ public static ParticleEffect fromInternalName(String internalName) {
*
* @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);
}
}

Expand All @@ -298,42 +298,38 @@ public static void display(ParticlePair particle, PParticle pparticle, boolean i
* @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);
}
}

/**
* Displays a single particle which is colored
*
* @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);
}
Expand All @@ -352,11 +348,11 @@ public void display(ParticleColor color, Location center, boolean isFixedEffect,
* @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");
}
Expand All @@ -370,30 +366,30 @@ public void display(Material spawnMaterial, double offsetX, double offsetY, doub
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);
}

/**
* 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<Player> getPlayersInRange(Location center, boolean isFixedEffect, Player owner) {
private List<Player> getPlayersInRange(Location center, boolean isLongRange, Player owner) {
List<Player> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,9 +22,12 @@ public class ParticleStyleArrows extends DefaultParticleStyle implements Listene
private int maxArrowsPerPlayer;
private boolean onlySpawnIfFlying;
private List<String> arrowEntityNames;
private ParticleManager particleManager;

public ParticleStyleArrows() {
super("arrows", false, false, 0);

this.particleManager = PlayerParticles.getInstance().getManager(ParticleManager.class);
}

@Override
Expand Down Expand Up @@ -59,6 +64,11 @@ public void updateTimers() {
}
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onBlockBreak(BlockBreakEvent event) {
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onBlockPlace(BlockPlaceEvent event) {
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void run() {
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 {
Expand All @@ -140,7 +140,7 @@ public void run() {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void onEntityDamage(EntityDamageEvent event) {
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void onPlayerMove(PlayerMoveEvent event) {
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void onEntityDamageEntity(EntityDamageByEntityEvent event) {
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);
}
}
}
Expand Down

0 comments on commit 621862a

Please sign in to comment.