package net.minecraft.network.protocol.game; import com.google.common.collect.Lists; import java.util.List; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; import net.minecraft.core.particles.ParticleOptions; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.PacketType; import net.minecraft.sounds.SoundEvent; import net.minecraft.util.Mth; import net.minecraft.world.level.Explosion; import net.minecraft.world.phys.Vec3; import org.jetbrains.annotations.Nullable; public class ClientboundExplodePacket implements Packet { public static final StreamCodec STREAM_CODEC = Packet.codec( ClientboundExplodePacket::write, ClientboundExplodePacket::new ); private final double x; private final double y; private final double z; private final float power; private final List toBlow; private final float knockbackX; private final float knockbackY; private final float knockbackZ; private final ParticleOptions smallExplosionParticles; private final ParticleOptions largeExplosionParticles; private final Explosion.BlockInteraction blockInteraction; private final Holder explosionSound; public ClientboundExplodePacket( double x, double y, double z, float power, List toBlow, @Nullable Vec3 knockback, Explosion.BlockInteraction blockInteraction, ParticleOptions smallExplosionParticles, ParticleOptions largeExplosionParticles, Holder explosionSound ) { this.x = x; this.y = y; this.z = z; this.power = power; this.toBlow = Lists.newArrayList(toBlow); this.explosionSound = explosionSound; if (knockback != null) { this.knockbackX = (float)knockback.x; this.knockbackY = (float)knockback.y; this.knockbackZ = (float)knockback.z; } else { this.knockbackX = 0.0F; this.knockbackY = 0.0F; this.knockbackZ = 0.0F; } this.blockInteraction = blockInteraction; this.smallExplosionParticles = smallExplosionParticles; this.largeExplosionParticles = largeExplosionParticles; } private ClientboundExplodePacket(RegistryFriendlyByteBuf buffer) { this.x = buffer.readDouble(); this.y = buffer.readDouble(); this.z = buffer.readDouble(); this.power = buffer.readFloat(); int i = Mth.floor(this.x); int j = Mth.floor(this.y); int k = Mth.floor(this.z); this.toBlow = buffer.readList(friendlyByteBuf -> { int l = friendlyByteBuf.readByte() + i; int m = friendlyByteBuf.readByte() + j; int n = friendlyByteBuf.readByte() + k; return new BlockPos(l, m, n); }); this.knockbackX = buffer.readFloat(); this.knockbackY = buffer.readFloat(); this.knockbackZ = buffer.readFloat(); this.blockInteraction = buffer.readEnum(Explosion.BlockInteraction.class); this.smallExplosionParticles = ParticleTypes.STREAM_CODEC.decode(buffer); this.largeExplosionParticles = ParticleTypes.STREAM_CODEC.decode(buffer); this.explosionSound = SoundEvent.STREAM_CODEC.decode(buffer); } private void write(RegistryFriendlyByteBuf buffer) { buffer.writeDouble(this.x); buffer.writeDouble(this.y); buffer.writeDouble(this.z); buffer.writeFloat(this.power); int i = Mth.floor(this.x); int j = Mth.floor(this.y); int k = Mth.floor(this.z); buffer.writeCollection(this.toBlow, (friendlyByteBuf, blockPos) -> { int l = blockPos.getX() - i; int m = blockPos.getY() - j; int n = blockPos.getZ() - k; friendlyByteBuf.writeByte(l); friendlyByteBuf.writeByte(m); friendlyByteBuf.writeByte(n); }); buffer.writeFloat(this.knockbackX); buffer.writeFloat(this.knockbackY); buffer.writeFloat(this.knockbackZ); buffer.writeEnum(this.blockInteraction); ParticleTypes.STREAM_CODEC.encode(buffer, this.smallExplosionParticles); ParticleTypes.STREAM_CODEC.encode(buffer, this.largeExplosionParticles); SoundEvent.STREAM_CODEC.encode(buffer, this.explosionSound); } @Override public PacketType type() { return GamePacketTypes.CLIENTBOUND_EXPLODE; } /** * Passes this Packet on to the NetHandler for processing. */ public void handle(ClientGamePacketListener handler) { handler.handleExplosion(this); } public float getKnockbackX() { return this.knockbackX; } public float getKnockbackY() { return this.knockbackY; } public float getKnockbackZ() { return this.knockbackZ; } public double getX() { return this.x; } public double getY() { return this.y; } public double getZ() { return this.z; } public float getPower() { return this.power; } public List getToBlow() { return this.toBlow; } public Explosion.BlockInteraction getBlockInteraction() { return this.blockInteraction; } public ParticleOptions getSmallExplosionParticles() { return this.smallExplosionParticles; } public ParticleOptions getLargeExplosionParticles() { return this.largeExplosionParticles; } public Holder getExplosionSound() { return this.explosionSound; } }