From 0bb237f7373750be8666d73391f8549325560c74 Mon Sep 17 00:00:00 2001 From: DeidaraMC <117625071+DeidaraMC@users.noreply.github.com> Date: Sat, 6 Jan 2024 02:16:21 -0500 Subject: [PATCH] explosion packet fix, add bundle packet, rename tick rate packet to match wiki.vg (#103) * packet fixes * explosion packet touchup (cherry picked from commit d576228655dfe45d05e05b0562d5ac2c53437824) --- .../packet/server/play/BundlePacket.java | 26 ++++++ .../packet/server/play/ExplosionPacket.java | 90 +++++++++++++++++-- ...atePacket.java => SetTickStatePacket.java} | 4 +- 3 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 src/main/java/net/minestom/server/network/packet/server/play/BundlePacket.java rename src/main/java/net/minestom/server/network/packet/server/play/{TickStatePacket.java => SetTickStatePacket.java} (85%) diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BundlePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BundlePacket.java new file mode 100644 index 000000000..6c4faf941 --- /dev/null +++ b/src/main/java/net/minestom/server/network/packet/server/play/BundlePacket.java @@ -0,0 +1,26 @@ +package net.minestom.server.network.packet.server.play; + +import net.minestom.server.network.ConnectionState; +import net.minestom.server.network.NetworkBuffer; +import net.minestom.server.network.packet.server.ServerPacket; +import net.minestom.server.network.packet.server.ServerPacketIdentifier; +import net.minestom.server.utils.PacketUtils; +import org.jetbrains.annotations.NotNull; + +public record BundlePacket() implements ServerPacket { + public BundlePacket(@NotNull NetworkBuffer reader) { + this(); + } + + @Override + public void write(@NotNull NetworkBuffer writer) { + } + + @Override + public int getId(@NotNull ConnectionState state) { + return switch (state) { + case PLAY -> ServerPacketIdentifier.BUNDLE; + default -> PacketUtils.invalidPacketState(getClass(), state, ConnectionState.PLAY); + }; + } +} diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java index fa64f6fd2..bb60bebdf 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java @@ -4,17 +4,85 @@ import net.minestom.server.network.ConnectionState; import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; +import net.minestom.server.particle.Particle; +import net.minestom.server.sound.SoundEvent; import net.minestom.server.utils.PacketUtils; +import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import static net.minestom.server.network.NetworkBuffer.*; -public record ExplosionPacket(double x, double y, double z, float radius, byte @NotNull [] records, - float playerMotionX, float playerMotionY, float playerMotionZ) implements ServerPacket { +public record ExplosionPacket(double x, double y, double z, float radius, + byte @NotNull [] records, + float playerMotionX, float playerMotionY, float playerMotionZ, + @NotNull BlockInteraction blockInteraction, + int smallParticleId, byte @NotNull [] smallParticleData, + int largeParticleId, byte @NotNull [] largeParticleData, + @NotNull String soundName, boolean hasFixedSoundRange, float soundRange) implements ServerPacket { + private static @NotNull ExplosionPacket fromReader(@NotNull NetworkBuffer reader) { + double x = reader.read(DOUBLE), y = reader.read(DOUBLE), z = reader.read(DOUBLE); + float radius = reader.read(FLOAT); + byte[] records = reader.readBytes(reader.read(VAR_INT) * 3); + float playerMotionX = reader.read(FLOAT), playerMotionY = reader.read(FLOAT), playerMotionZ = reader.read(FLOAT); + BlockInteraction blockInteraction = BlockInteraction.values()[reader.read(VAR_INT)]; + int smallParticleId = reader.read(VAR_INT); + byte[] smallParticleData = readParticleData(reader, Particle.fromId(smallParticleId)); + int largeParticleId = reader.read(VAR_INT); + byte[] largeParticleData = readParticleData(reader, Particle.fromId(largeParticleId)); + String soundName = reader.read(STRING); + boolean hasFixedSoundRange = reader.read(BOOLEAN); + float soundRange = hasFixedSoundRange ? reader.read(FLOAT) : 0; + return new ExplosionPacket(x, y, z, radius, records, playerMotionX, playerMotionY, playerMotionZ, + blockInteraction, smallParticleId, smallParticleData, largeParticleId, largeParticleData, + soundName, hasFixedSoundRange, soundRange); + } + + private static byte @NotNull [] readParticleData(@NotNull NetworkBuffer reader, Particle particle) { + //Need to do this because particle data isn't at the end of the packet + BinaryWriter writer = new BinaryWriter(); + if (particle.equals(Particle.BLOCK) || particle.equals(Particle.BLOCK_MARKER) || particle.equals(Particle.FALLING_DUST) || particle.equals(Particle.SHRIEK)) { + writer.writeVarInt(reader.read(VAR_INT)); + } + else if (particle.equals(Particle.VIBRATION)) { + writer.writeVarInt(reader.read(VAR_INT)); + writer.writeBlockPosition(reader.read(BLOCK_POSITION)); + writer.writeVarInt(reader.read(VAR_INT)); + writer.writeFloat(reader.read(FLOAT)); + writer.writeVarInt(reader.read(VAR_INT)); + } + else if (particle.equals(Particle.SCULK_CHARGE)) { + writer.writeFloat(reader.read(FLOAT)); + return writer.toByteArray(); + } + else if (particle.equals(Particle.ITEM)) { + writer.writeItemStack(reader.read(ITEM)); + } + else if (particle.equals(Particle.DUST_COLOR_TRANSITION)) { + for (int i = 0; i < 7; i++) writer.writeFloat(reader.read(FLOAT)); + } + else if (particle.equals(Particle.DUST)) { + for (int i = 0; i < 4; i++) writer.writeFloat(reader.read(FLOAT)); + } + + return writer.toByteArray(); + } + public ExplosionPacket(@NotNull NetworkBuffer reader) { - this(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), - reader.read(FLOAT), reader.readBytes(reader.read(VAR_INT) * 3), - reader.read(FLOAT), reader.read(FLOAT), reader.read(FLOAT)); + this(fromReader(reader)); + } + + public ExplosionPacket(double x, double y, double z, float radius, byte @NotNull [] records, + float playerMotionX, float playerMotionY, float playerMotionZ) { + this(x, y, z, radius, records, playerMotionX, playerMotionY, playerMotionZ, + BlockInteraction.DESTROY, Particle.EXPLOSION.id(), new byte[] {}, + Particle.EXPLOSION_EMITTER.id(), new byte[] {}, + SoundEvent.ENTITY_GENERIC_EXPLODE.name(), false, 0); + } + + private ExplosionPacket(@NotNull ExplosionPacket packet) { + this(packet.x, packet.y, packet.z, packet.radius, packet.records, packet.playerMotionX, packet.playerMotionY, packet.playerMotionZ, + packet.blockInteraction, packet.smallParticleId, packet.smallParticleData, packet.largeParticleId, packet.largeParticleData, + packet.soundName, packet.hasFixedSoundRange, packet.soundRange); } @Override @@ -28,6 +96,14 @@ public record ExplosionPacket(double x, double y, double z, float radius, byte @ writer.write(FLOAT, playerMotionX); writer.write(FLOAT, playerMotionY); writer.write(FLOAT, playerMotionZ); + writer.write(VAR_INT, blockInteraction.ordinal()); + writer.write(VAR_INT, smallParticleId); + writer.write(RAW_BYTES, smallParticleData); + writer.write(VAR_INT, largeParticleId); + writer.write(RAW_BYTES, largeParticleData); + writer.write(STRING, soundName); + writer.write(BOOLEAN, hasFixedSoundRange); + if (hasFixedSoundRange) writer.write(FLOAT, soundRange); } @Override @@ -37,4 +113,8 @@ public record ExplosionPacket(double x, double y, double z, float radius, byte @ default -> PacketUtils.invalidPacketState(getClass(), state, ConnectionState.PLAY); }; } + + public enum BlockInteraction { + KEEP, DESTROY, DESTROY_WITH_DECAY, TRIGGER_BLOCK + } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TickStatePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetTickStatePacket.java similarity index 85% rename from src/main/java/net/minestom/server/network/packet/server/play/TickStatePacket.java rename to src/main/java/net/minestom/server/network/packet/server/play/SetTickStatePacket.java index 5b4147eeb..7b398422b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TickStatePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetTickStatePacket.java @@ -9,9 +9,9 @@ import org.jetbrains.annotations.NotNull; import static net.minestom.server.network.NetworkBuffer.*; -public record TickStatePacket(float tickRate, boolean isFrozen) implements ServerPacket { +public record SetTickStatePacket(float tickRate, boolean isFrozen) implements ServerPacket { - public TickStatePacket(@NotNull NetworkBuffer reader) { + public SetTickStatePacket(@NotNull NetworkBuffer reader) { this(reader.read(FLOAT), reader.read(BOOLEAN)); }