From 409581578ed21385bbce15226d9d8350a45c985e Mon Sep 17 00:00:00 2001 From: hmzel Date: Sat, 20 Jul 2024 04:17:24 -0500 Subject: [PATCH] overhauled ParticleVibration, fixing offset and count and entity tracking (cherry picked from commit 76427f6e030456f9768033f335ef9a920acc60c4) --- .../particles/ParticleVibration.java | 109 ++++++------------ 1 file changed, 37 insertions(+), 72 deletions(-) diff --git a/src/main/java/hm/zelha/particlesfx/particles/ParticleVibration.java b/src/main/java/hm/zelha/particlesfx/particles/ParticleVibration.java index 1c74a4eb..3a6c9c8f 100644 --- a/src/main/java/hm/zelha/particlesfx/particles/ParticleVibration.java +++ b/src/main/java/hm/zelha/particlesfx/particles/ParticleVibration.java @@ -5,15 +5,16 @@ import hm.zelha.particlesfx.util.LVMath; import net.minecraft.core.BlockPosition; import net.minecraft.core.particles.VibrationParticleOption; -import net.minecraft.network.PacketDataSerializer; -import net.minecraft.resources.MinecraftKey; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.PacketPlayOutWorldParticles; +import net.minecraft.world.level.gameevent.BlockPositionSource; +import net.minecraft.world.level.gameevent.EntityPositionSource; +import net.minecraft.world.level.gameevent.PositionSource; import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_20_R1.entity.CraftEntity; import org.bukkit.entity.Entity; import org.bukkit.util.Vector; -import java.util.List; - public class ParticleVibration extends TravellingParticle { private Entity entity = null; @@ -91,27 +92,46 @@ public ParticleVibration clone() { } @Override - protected void display(Location location, List players) { - if (!(particle instanceof NMSVibrationParticle) || ((NMSVibrationParticle) particle).check(location, velocity, toGo, entity, arrivalTime)) { - particle = new NMSVibrationParticle(location, velocity, toGo, entity, arrivalTime); - } - - super.display(location, players); + protected Vector getXYZ(Location location) { + return LVMath.toVector(xyzHelper, location).add(generateFakeOffset()); } @Override - protected Vector getXYZ(Location location) { - return LVMath.toVector(xyzHelper, location); + protected Vector getOffsets(Location location) { + return offsetHelper.zero(); } @Override - protected Vector getOffsets(Location location) { - return offsetHelper.setX(offsetX).setY(offsetY).setZ(offsetZ); + protected int getPacketCount() { + return 0; } @Override - public int getPacketCount() { - return count; + protected Packet getStrangePacket(Location location) { + PositionSource source; + Vector xyz = getXYZ(location); + + if (entity != null) { + source = new EntityPositionSource(((CraftEntity) entity).getHandle(), (float) (entity.getHeight() / 2)); + } else { + BlockPosition.MutableBlockPosition destination = new BlockPosition.MutableBlockPosition(); + + destination.d(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + destination.e(fakeOffsetHelper.getBlockX(), fakeOffsetHelper.getBlockY(), fakeOffsetHelper.getBlockZ()); + + if (toGo != null) { + destination.d(toGo.getBlockX(), toGo.getBlockY(), toGo.getBlockZ()); + } else if (velocity != null) { + destination.e((int) velocity.getX(), (int) velocity.getY(), (int) velocity.getZ()); + } + + source = new BlockPositionSource(destination); + } + + return new PacketPlayOutWorldParticles( + new VibrationParticleOption(source, arrivalTime), true, (float) xyz.getX(), (float) xyz.getY(), (float) xyz.getZ(), 0f, + 0f, 0f, 1, 1 + ); } /** @@ -149,59 +169,4 @@ public Entity getEntity() { public int getArrivalTime() { return arrivalTime; } - - private static class NMSVibrationParticle extends VibrationParticleOption { - - private final BlockPosition destination; - private final Location location; - private final Vector velocity; - private final Location toGo; - private final Entity entity; - private final int arrivalTime; - - public NMSVibrationParticle(Location location, Vector velocity, Location toGo, Entity entity, int arrivalTime) { - super(null, 0); - - this.location = location.clone(); - this.velocity = (velocity != null) ? velocity.clone() : null; - this.toGo = (toGo != null) ? toGo.clone() : null; - this.entity = entity; - this.arrivalTime = arrivalTime; - - if (toGo != null) { - destination = new BlockPosition(toGo.getBlockX(), toGo.getBlockY(), toGo.getBlockZ()); - } else if (velocity != null) { - destination = new BlockPosition((int) (location.getX() + velocity.getX()), (int) (location.getY() + velocity.getY()), (int) (location.getZ() + velocity.getZ())); - } else { - destination = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()); - } - } - - @Override - public void a(PacketDataSerializer data) { - if (entity != null) { - data.a(new MinecraftKey("entity")); - data.d(entity.getEntityId()); - data.writeFloat((float) (entity.getHeight() * 0.85)); - } else { - data.a(new MinecraftKey("block")); - data.a(destination); - } - - data.d(arrivalTime); - } - - public boolean check(Location location, Vector velocity, Location toGo, Entity entity, int arrivalTime) { - if (this.entity == null) { - if (velocity != null && !velocity.equals(this.velocity)) return true; - if (toGo != null && !toGo.equals(this.toGo)) return true; - if (velocity == null && this.velocity != null) return true; - if (toGo == null && (this.toGo != null || !location.equals(this.location))) return true; - } else { - if (!this.entity.equals(entity)) return true; - } - - return arrivalTime != this.arrivalTime; - } - } } \ No newline at end of file