From 9576843d8328b592805f6f8aa4b83fde19aac4fd Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 3 Apr 2023 21:52:45 +0200 Subject: [PATCH] Add element for new particle system except WingParticle --- .../{Particle.java => ParticleData.java} | 8 +-- .../lobby/otherparticle/ParticleElement.java | 44 +++++++++++++ .../lobby/otherparticle/ParticleEnum.java | 2 +- .../lobby/otherparticle/ParticleTickData.java | 36 ++++++++++ .../lobby/otherparticle/ParticleTickType.java | 9 +++ .../lobby/otherparticle/elements/Always.java | 27 ++++++++ .../lobby/otherparticle/elements/Circle.java | 28 ++++++++ .../lobby/otherparticle/elements/Cloud.java | 31 +++++++++ .../lobby/otherparticle/elements/Delayed.java | 48 ++++++++++++++ .../elements/DelegatingParticleElement.java | 30 +++++++++ .../otherparticle/elements/DoubleCircle.java | 47 +++++++++++++ .../otherparticle/elements/DustParticle.java | 44 +++++++++++++ .../lobby/otherparticle/elements/Group.java | 27 ++++++++ .../elements/LocationMutator.java | 28 ++++++++ .../otherparticle/elements/NonFloor.java | 23 +++++++ .../otherparticle/elements/NonFlying.java | 24 +++++++ .../elements/SimpleParticle.java | 66 +++++++++++++++++++ 17 files changed, 517 insertions(+), 5 deletions(-) rename src/de/steamwar/lobby/otherparticle/{Particle.java => ParticleData.java} (88%) create mode 100644 src/de/steamwar/lobby/otherparticle/ParticleElement.java create mode 100644 src/de/steamwar/lobby/otherparticle/ParticleTickData.java create mode 100644 src/de/steamwar/lobby/otherparticle/ParticleTickType.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/Always.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/Circle.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/Cloud.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/Delayed.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/DelegatingParticleElement.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/DoubleCircle.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/DustParticle.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/Group.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/LocationMutator.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/NonFloor.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/NonFlying.java create mode 100644 src/de/steamwar/lobby/otherparticle/elements/SimpleParticle.java diff --git a/src/de/steamwar/lobby/otherparticle/Particle.java b/src/de/steamwar/lobby/otherparticle/ParticleData.java similarity index 88% rename from src/de/steamwar/lobby/otherparticle/Particle.java rename to src/de/steamwar/lobby/otherparticle/ParticleData.java index 3477d3a..e65dd57 100644 --- a/src/de/steamwar/lobby/otherparticle/Particle.java +++ b/src/de/steamwar/lobby/otherparticle/ParticleData.java @@ -10,26 +10,26 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; -public class Particle { +public class ParticleData { private final Material material; private final String name; private final Set attributes = new LinkedHashSet<>(); private final ParticleRequirement requirement; - public Particle(Material material, String name) { + public ParticleData(Material material, String name) { this.material = material; this.name = name; this.requirement = ParticleRequirement.NO_REQUIRMENT; } - public Particle(Material material, String name, ParticleRequirement requirement) { + public ParticleData(Material material, String name, ParticleRequirement requirement) { this.material = material; this.name = name; this.requirement = requirement; } - public Particle add(String attribute) { + public ParticleData add(String attribute) { attributes.add(attribute); return this; } diff --git a/src/de/steamwar/lobby/otherparticle/ParticleElement.java b/src/de/steamwar/lobby/otherparticle/ParticleElement.java new file mode 100644 index 0000000..470f92d --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/ParticleElement.java @@ -0,0 +1,44 @@ +package de.steamwar.lobby.otherparticle; + +import org.bukkit.Bukkit; +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.entity.Player; + +import java.util.Random; +import java.util.function.Consumer; + +public interface ParticleElement { + Random RANDOM = new Random(); + + default Color randomColor() { + return Color.fromRGB(RANDOM.nextInt(255), RANDOM.nextInt(255), RANDOM.nextInt(255)); + } + + default float randomSize() { + return RANDOM.nextFloat() / 2 + 1; + } + + default Particle.DustOptions randomParticleDust() { + return new Particle.DustOptions(randomColor(), randomSize()); + } + + default void display(Location location, Consumer consumer) { + Bukkit.getOnlinePlayers().forEach(player -> { + int viewDistance = player.getClientViewDistance() * 16; + if (location.distanceSquared(player.getLocation()) <= viewDistance * viewDistance) { + consumer.accept(player); + } + }); + } + + default void aggregateAttributes(ParticleData particleData) { + } + + default ParticleTickType tickType() { + return ParticleTickType.MOVE; + } + + void tick(ParticleTickData particleTickData); +} diff --git a/src/de/steamwar/lobby/otherparticle/ParticleEnum.java b/src/de/steamwar/lobby/otherparticle/ParticleEnum.java index 2e32953..ef4c4b5 100644 --- a/src/de/steamwar/lobby/otherparticle/ParticleEnum.java +++ b/src/de/steamwar/lobby/otherparticle/ParticleEnum.java @@ -1,5 +1,5 @@ package de.steamwar.lobby.otherparticle; public interface ParticleEnum { - Particle getParticle(); + ParticleData getParticle(); } diff --git a/src/de/steamwar/lobby/otherparticle/ParticleTickData.java b/src/de/steamwar/lobby/otherparticle/ParticleTickData.java new file mode 100644 index 0000000..b2aa909 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/ParticleTickData.java @@ -0,0 +1,36 @@ +package de.steamwar.lobby.otherparticle; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; + +@RequiredArgsConstructor +@AllArgsConstructor +@Getter +public class ParticleTickData { + + private final World world; + private final Player player; + private Location location; + private final double deg; + + public ParticleTickData withLocation(Location location) { + ParticleTickData particleTickData = copy(); + particleTickData.location = location; + return particleTickData; + } + + public Location getLocation() { + if (location == null) { + return player.getLocation(); + } + return location; + } + + public ParticleTickData copy() { + return new ParticleTickData(world, player, location, deg); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/ParticleTickType.java b/src/de/steamwar/lobby/otherparticle/ParticleTickType.java new file mode 100644 index 0000000..ac54999 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/ParticleTickType.java @@ -0,0 +1,9 @@ +package de.steamwar.lobby.otherparticle; + +public enum ParticleTickType { + + ALWAYS, + MOVE, + SNEAK, + ; +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/Always.java b/src/de/steamwar/lobby/otherparticle/elements/Always.java new file mode 100644 index 0000000..f35738d --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/Always.java @@ -0,0 +1,27 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import de.steamwar.lobby.otherparticle.ParticleTickType; + +public class Always extends DelegatingParticleElement { + + public Always(ParticleElement particleElement) { + super(particleElement); + } + + @Override + public String attribute() { + return "PARTICLE_ATTRIBUTE_TICK"; + } + + @Override + public ParticleTickType tickType() { + return ParticleTickType.ALWAYS; + } + + @Override + public void tick(ParticleTickData particleTickData) { + particleElement.tick(particleTickData); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/Circle.java b/src/de/steamwar/lobby/otherparticle/elements/Circle.java new file mode 100644 index 0000000..fd1f864 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/Circle.java @@ -0,0 +1,28 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import org.bukkit.Location; +import org.bukkit.util.Vector; + +public class Circle extends DelegatingParticleElement { + + public Circle(ParticleElement particleElement) { + super(particleElement); + } + + @Override + public String attribute() { + return "PARTICLE_ATTRIBUTE_CIRCLE"; + } + + @Override + public void tick(ParticleTickData particleTickData) { + Location location = particleTickData.getLocation(); + + Vector vector = new Vector(1, 0, 0); + vector.rotateAroundY(particleTickData.getDeg()); + ParticleTickData nParticleTickData = particleTickData.withLocation(location.clone().add(vector)); + particleElement.tick(nParticleTickData); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/Cloud.java b/src/de/steamwar/lobby/otherparticle/elements/Cloud.java new file mode 100644 index 0000000..3ad3834 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/Cloud.java @@ -0,0 +1,31 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import org.bukkit.Material; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +public class Cloud extends DelegatingParticleElement { + + public Cloud(ParticleElement particleElement) { + super(particleElement); + } + + @Override + public String attribute() { + return "PARTICLE_ATTRIBUTE_CLOUD"; + } + + @Override + public void tick(ParticleTickData particleTickData) { + if (particleTickData.getWorld().getBlockAt(particleTickData.getPlayer().getLocation().subtract(0, 0.5, 0)).getType() == Material.AIR) { + particleTickData.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 5, 2, false, false, false)); + } else { + particleTickData.getPlayer().removePotionEffect(PotionEffectType.SLOW_FALLING); + return; + } + ParticleTickData nParticleTickData = particleTickData.withLocation(particleTickData.getLocation().subtract(0, -0.2, 0)); + particleElement.tick(nParticleTickData); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/Delayed.java b/src/de/steamwar/lobby/otherparticle/elements/Delayed.java new file mode 100644 index 0000000..472f339 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/Delayed.java @@ -0,0 +1,48 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.LobbySystem; +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; + +import java.util.HashMap; +import java.util.Map; + +public class Delayed extends DelegatingParticleElement { + + private DelayedData delayedData = new DelayedData(); + private int interval; + + private static class DelayedData implements Listener { + private Map data = new HashMap<>(); + + @EventHandler(ignoreCancelled = true) + public void onPlayerQuit(PlayerQuitEvent event) { + data.remove(event.getPlayer()); + } + } + + public Delayed(ParticleElement particleElement, int interval) { + super(particleElement); + Bukkit.getPluginManager().registerEvents(delayedData, LobbySystem.getPlugin()); + } + + @Override + public String attribute() { + return null; + } + + @Override + public void tick(ParticleTickData particleTickData) { + int currentNumber = delayedData.data.getOrDefault(particleTickData.getPlayer(), 0) % interval; + delayedData.data.put(particleTickData.getPlayer(), currentNumber + 1); + if (currentNumber != 0) { + return; + } + particleElement.tick(particleTickData); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/DelegatingParticleElement.java b/src/de/steamwar/lobby/otherparticle/elements/DelegatingParticleElement.java new file mode 100644 index 0000000..e961f29 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/DelegatingParticleElement.java @@ -0,0 +1,30 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleData; +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickType; + +public abstract class DelegatingParticleElement implements ParticleElement { + + protected final ParticleElement particleElement; + + protected DelegatingParticleElement(ParticleElement particleElement) { + this.particleElement = particleElement; + } + + public abstract String attribute(); + + @Override + public void aggregateAttributes(ParticleData particleData) { + String attribute = attribute(); + if (attribute != null) { + particleData.add(attribute); + } + particleElement.aggregateAttributes(particleData); + } + + @Override + public ParticleTickType tickType() { + return particleElement.tickType(); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/DoubleCircle.java b/src/de/steamwar/lobby/otherparticle/elements/DoubleCircle.java new file mode 100644 index 0000000..dbb838e --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/DoubleCircle.java @@ -0,0 +1,47 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import de.steamwar.lobby.otherparticle.ParticleTickType; +import org.bukkit.Location; +import org.bukkit.util.Vector; + +public class DoubleCircle extends Circle { + + private ParticleElement second; + + public DoubleCircle(ParticleElement first, ParticleElement second) { + super(first); + this.second = second; + } + + @Override + public String attribute() { + return "PARTICLE_ATTRIBUTE_BI_CIRCLE"; + } + + @Override + public ParticleTickType tickType() { + if (particleElement.tickType() == second.tickType()) { + return particleElement.tickType(); + } + // TODO: This could be improved but I will not do it + return ParticleTickType.MOVE; + } + + @Override + public void tick(ParticleTickData particleTickData) { + Location location = particleTickData.getLocation(); + + Vector vector = new Vector(1, 0, 0); + vector.rotateAroundY(particleTickData.getDeg()); + ParticleTickData nParticleTickData = particleTickData.withLocation(location.clone().add(vector)); + particleElement.tick(nParticleTickData); + + vector.setX(-vector.getX()); + vector.setZ(-vector.getZ()); + + nParticleTickData = particleTickData.withLocation(location.clone().add(vector)); + second.tick(nParticleTickData); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/DustParticle.java b/src/de/steamwar/lobby/otherparticle/elements/DustParticle.java new file mode 100644 index 0000000..55ca442 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/DustParticle.java @@ -0,0 +1,44 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import org.bukkit.Location; +import org.bukkit.Particle; + +public class DustParticle implements ParticleElement { + + private Particle particle; + private float vx = 0.01f; + private float vy = 0.01f; + private float vz = 0.01f; + private double speed = 0.01; + private int count = 5; + + public DustParticle(Particle particle) { + this.particle = particle; + } + + public DustParticle(Particle particle, float vx, float vy, float vz) { + this.particle = particle; + this.vx = vx; + this.vy = vy; + this.vz = vz; + } + + public DustParticle(Particle particle, float vx, float vy, float vz, float speed, int count) { + this.particle = particle; + this.vx = vx; + this.vy = vy; + this.vz = vz; + this.speed = speed; + this.count = count; + } + + @Override + public void tick(ParticleTickData particleTickData) { + Location location = particleTickData.getLocation().add(0.0, 0.2, 0.0); + display(location, player -> { + player.spawnParticle(particle, location, count, vx, vy, vz, speed, randomParticleDust()); + }); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/Group.java b/src/de/steamwar/lobby/otherparticle/elements/Group.java new file mode 100644 index 0000000..8f0e9a7 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/Group.java @@ -0,0 +1,27 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; + +public class Group extends DelegatingParticleElement { + + private final ParticleElement[] rest; + + public Group(ParticleElement particleElement, ParticleElement... rest) { + super(particleElement); + this.rest = rest; + } + + @Override + public String attribute() { + return null; + } + + @Override + public void tick(ParticleTickData particleTickData) { + particleElement.tick(particleTickData); + for (ParticleElement particleElement : rest) { + particleElement.tick(particleTickData); + } + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/LocationMutator.java b/src/de/steamwar/lobby/otherparticle/elements/LocationMutator.java new file mode 100644 index 0000000..f0e389d --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/LocationMutator.java @@ -0,0 +1,28 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import org.bukkit.Location; + +import java.util.function.UnaryOperator; + +public class LocationMutator extends DelegatingParticleElement { + + private UnaryOperator mutator; + + public LocationMutator(ParticleElement particleElement, UnaryOperator mutator) { + super(particleElement); + this.mutator = mutator; + } + + @Override + public String attribute() { + return null; + } + + @Override + public void tick(ParticleTickData particleTickData) { + ParticleTickData nParticleTickData = particleTickData.withLocation(mutator.apply(particleTickData.getLocation())); + particleElement.tick(nParticleTickData); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/NonFloor.java b/src/de/steamwar/lobby/otherparticle/elements/NonFloor.java new file mode 100644 index 0000000..f99dc71 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/NonFloor.java @@ -0,0 +1,23 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; + +public class NonFloor extends DelegatingParticleElement { + + public NonFloor(ParticleElement particleElement) { + super(particleElement); + } + + @Override + public String attribute() { + return "PARTICLE_ATTRIBUTE_NON_FLOOR"; + } + + @Override + public void tick(ParticleTickData particleTickData) { + if (particleTickData.getWorld().getBlockAt(particleTickData.getPlayer().getLocation().subtract(0, 0.5, 0)).getType().isAir()) { + particleElement.tick(particleTickData); + } + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/NonFlying.java b/src/de/steamwar/lobby/otherparticle/elements/NonFlying.java new file mode 100644 index 0000000..85d7037 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/NonFlying.java @@ -0,0 +1,24 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; + +public class NonFlying extends DelegatingParticleElement { + + public NonFlying(ParticleElement particleElement) { + super(particleElement); + } + + @Override + public String attribute() { + return null; + } + + @Override + public void tick(ParticleTickData particleTickData) { + if (particleTickData.getPlayer().isGliding()) { + return; + } + particleElement.tick(particleTickData); + } +} diff --git a/src/de/steamwar/lobby/otherparticle/elements/SimpleParticle.java b/src/de/steamwar/lobby/otherparticle/elements/SimpleParticle.java new file mode 100644 index 0000000..27164a2 --- /dev/null +++ b/src/de/steamwar/lobby/otherparticle/elements/SimpleParticle.java @@ -0,0 +1,66 @@ +package de.steamwar.lobby.otherparticle.elements; + +import de.steamwar.lobby.otherparticle.ParticleElement; +import de.steamwar.lobby.otherparticle.ParticleTickData; +import org.bukkit.Location; +import org.bukkit.Particle; + +public class SimpleParticle implements ParticleElement { + + private final Particle particle; + private boolean customVelocity = false; + private float vx; + private float vy; + private float vz; + private double time = 0.01; + private int count = 5; + + public SimpleParticle(Particle particle) { + this.particle = particle; + } + + public SimpleParticle(Particle particle, float vx, float vy, float vz) { + this.particle = particle; + this.customVelocity = true; + this.vx = vx; + this.vy = vy; + this.vz = vz; + } + + public SimpleParticle(Particle particle, float vx, float vy, float vz, double time) { + this.particle = particle; + this.customVelocity = true; + this.vx = vx; + this.vy = vy; + this.vz = vz; + this.time = time; + } + + public SimpleParticle(Particle particle, float vx, float vy, float vz, double time, int count) { + this.particle = particle; + this.customVelocity = true; + this.vx = vx; + this.vy = vy; + this.vz = vz; + this.time = time; + this.count = count; + } + + public SimpleParticle(Particle particle, double time, int count) { + this.particle = particle; + this.time = time; + this.count = count; + } + + @Override + public void tick(ParticleTickData particleTickData) { + Location location = particleTickData.getLocation().add(0.0, 0.2, 0.0); + display(location, player -> { + if (customVelocity) { + player.spawnParticle(particle, location, count, vx, vy, vz, time); + } else { + player.spawnParticle(particle, location, count); + } + }); + } +}