From 93af22be9e6db7efb2748ec7a568de02ce2a08eb Mon Sep 17 00:00:00 2001 From: CallMeEchoCodes Date: Fri, 12 Jul 2024 15:30:39 +1000 Subject: [PATCH] :bug: Fix party popper sending 250 packets --- gradle.properties | 2 +- .../bombastic/client/BombasticClient.java | 2 + .../callmeecho/bombastic/main/Bombastic.java | 4 + .../block/entity/FirecrackerBlockEntity.java | 2 +- .../bombastic/main/item/PartyPopperItem.java | 34 +++------ .../main/network/PartyPopperS2CPacket.java | 76 +++++++++++++++++++ 6 files changed, 96 insertions(+), 24 deletions(-) create mode 100644 src/main/java/dev/callmeecho/bombastic/main/network/PartyPopperS2CPacket.java diff --git a/gradle.properties b/gradle.properties index 4f8f8c7..1d92d32 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ org.gradle.jvmargs=-Xmx2G org.gradle.parallel=true -mod.version = 1.0.6 +mod.version = 1.0.7 mod.group = dev.callmeecho mod.id = bombastic diff --git a/src/main/java/dev/callmeecho/bombastic/client/BombasticClient.java b/src/main/java/dev/callmeecho/bombastic/client/BombasticClient.java index 02d1054..c390dd1 100644 --- a/src/main/java/dev/callmeecho/bombastic/client/BombasticClient.java +++ b/src/main/java/dev/callmeecho/bombastic/client/BombasticClient.java @@ -8,6 +8,7 @@ import dev.callmeecho.bombastic.client.render.entity.model.ClownHairEntityModel; import dev.callmeecho.bombastic.client.render.entity.model.JugglingBallEntityModel; import dev.callmeecho.bombastic.main.BombasticConfig; +import dev.callmeecho.bombastic.main.network.PartyPopperS2CPacket; import dev.callmeecho.bombastic.main.particle.ConfettiParticle; import dev.callmeecho.bombastic.main.particle.FirecrackerFlashParticle; import dev.callmeecho.bombastic.main.registry.*; @@ -54,6 +55,7 @@ public void onInitializeClient() { ParticleFactoryRegistry.getInstance().register(BombasticParticleRegistrar.FIRECRACKER_FLASH, FirecrackerFlashParticle.Factory::new); BlockEntityRendererFactories.register(BombasticBlockEntityRegistrar.CONFETTI_CANNON, ConfettiCannonBlockEntityRenderer::new); + PartyPopperS2CPacket.SINGLETON.getInstance().registerClient(); if (!FabricLoader.getInstance().isModLoaded("axiom")) ModMenuHelper.addConfig(MODID, BombasticConfig.class); diff --git a/src/main/java/dev/callmeecho/bombastic/main/Bombastic.java b/src/main/java/dev/callmeecho/bombastic/main/Bombastic.java index da869cc..e48da31 100644 --- a/src/main/java/dev/callmeecho/bombastic/main/Bombastic.java +++ b/src/main/java/dev/callmeecho/bombastic/main/Bombastic.java @@ -1,5 +1,6 @@ package dev.callmeecho.bombastic.main; +import dev.callmeecho.bombastic.main.network.PartyPopperS2CPacket; import dev.callmeecho.bombastic.main.registry.*; import dev.callmeecho.cabinetapi.config.ConfigHandler; import dev.callmeecho.cabinetapi.item.CabinetItemGroup; @@ -40,12 +41,15 @@ public void onInitialize() { RegistrarHandler.process(BombasticBlockEntityRegistrar.class, MODID); RegistrarHandler.process(BombasticEnchantmentComponentTypeRegistrar.class, MODID); + Registry.register( Registries.RECIPE_SERIALIZER, Identifier.of(MODID, "pipe_bomb"), PIPE_BOMB_RECIPE ); + PartyPopperS2CPacket.SINGLETON.getInstance().register(); + GROUP.initialize(); } } diff --git a/src/main/java/dev/callmeecho/bombastic/main/block/entity/FirecrackerBlockEntity.java b/src/main/java/dev/callmeecho/bombastic/main/block/entity/FirecrackerBlockEntity.java index 8da91f5..e96e3d0 100644 --- a/src/main/java/dev/callmeecho/bombastic/main/block/entity/FirecrackerBlockEntity.java +++ b/src/main/java/dev/callmeecho/bombastic/main/block/entity/FirecrackerBlockEntity.java @@ -88,7 +88,7 @@ else if (offsetState.isOf(Blocks.TNT)) { pos, SoundEvents.ENTITY_FIREWORK_ROCKET_BLAST, SoundCategory.BLOCKS, - 20F, + 1F, (float)Math.random() + 1.25F ); diff --git a/src/main/java/dev/callmeecho/bombastic/main/item/PartyPopperItem.java b/src/main/java/dev/callmeecho/bombastic/main/item/PartyPopperItem.java index d6c623a..f58ae41 100644 --- a/src/main/java/dev/callmeecho/bombastic/main/item/PartyPopperItem.java +++ b/src/main/java/dev/callmeecho/bombastic/main/item/PartyPopperItem.java @@ -1,9 +1,13 @@ package dev.callmeecho.bombastic.main.item; +import com.mojang.datafixers.util.Pair; +import dev.callmeecho.bombastic.main.network.PartyPopperS2CPacket; import dev.callmeecho.bombastic.main.registry.BombasticEnchantmentComponentTypeRegistrar; import dev.callmeecho.bombastic.main.registry.BombasticParticleRegistrar; import dev.callmeecho.bombastic.main.registry.BombasticSoundEventRegistrar; import dev.callmeecho.bombastic.main.utils.ChangingExplosionBehavior; +import net.fabricmc.fabric.api.networking.v1.PlayerLookup; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.player.PlayerEntity; @@ -11,6 +15,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.registry.Registries; import net.minecraft.registry.tag.BlockTags; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundEvents; import net.minecraft.util.Hand; @@ -67,27 +72,12 @@ private static void explode(World world, PlayerEntity playerEntity, float power) @Override public TypedActionResult use(World world, PlayerEntity playerEntity, Hand hand) { if (world.isClient) return TypedActionResult.success(playerEntity.getStackInHand(hand)); - Vec3d direction = playerEntity.getRotationVector(); - Random random = playerEntity.getRandom(); - for (int i = 0; i < 250; i++) { - Vec3d velocity = new Vec3d( - random.nextGaussian() * 0.15, - random.nextGaussian() * 0.15, - random.nextGaussian() * 0.15 - ); - velocity = velocity.add(direction.multiply(0.75F)); + PartyPopperS2CPacket.PartyPopperPayload payload = new PartyPopperS2CPacket.PartyPopperPayload( + new Pair<>(new Vec3d(playerEntity.getX(), playerEntity.getEyeY(), playerEntity.getZ()).toVector3f(), playerEntity.getRotationVector().toVector3f())); - ((ServerWorld) world).spawnParticles( - BombasticParticleRegistrar.CONFETTI, - playerEntity.getX(), - playerEntity.getEyeY() - 0.25F, - playerEntity.getZ(), - 0, - velocity.getX(), - velocity.getY(), - velocity.getZ(), - 1.0F - ); + PartyPopperS2CPacket.SINGLETON.getInstance().send(payload, (ServerPlayerEntity) playerEntity); + for (ServerPlayerEntity player : PlayerLookup.tracking(playerEntity)) { + PartyPopperS2CPacket.SINGLETON.getInstance().send(payload, player); } world.playSound( @@ -109,7 +99,7 @@ public TypedActionResult use(World world, PlayerEntity playerEntity, stack, (enchantment, level) -> enchantment.value().modifyValue( BombasticEnchantmentComponentTypeRegistrar.PARTY_POPPER_EXPLOSION, - random, + playerEntity.getRandom(), level, power )); @@ -122,4 +112,4 @@ public TypedActionResult use(World world, PlayerEntity playerEntity, stack.damage(1, playerEntity, EquipmentSlot.MAINHAND); return TypedActionResult.success(playerEntity.getStackInHand(hand)); } -} +} \ No newline at end of file diff --git a/src/main/java/dev/callmeecho/bombastic/main/network/PartyPopperS2CPacket.java b/src/main/java/dev/callmeecho/bombastic/main/network/PartyPopperS2CPacket.java new file mode 100644 index 0000000..3d4ba79 --- /dev/null +++ b/src/main/java/dev/callmeecho/bombastic/main/network/PartyPopperS2CPacket.java @@ -0,0 +1,76 @@ +package dev.callmeecho.bombastic.main.network; + +import com.mojang.datafixers.util.Pair; +import dev.callmeecho.bombastic.main.Bombastic; +import dev.callmeecho.bombastic.main.registry.BombasticParticleRegistrar; +import dev.callmeecho.cabinetapi.CabinetAPI; +import dev.callmeecho.cabinetapi.config.network.ConfigSyncPacket; +import dev.callmeecho.cabinetapi.config.network.ConfigSyncPayload; +import dev.callmeecho.cabinetapi.network.CabinetPacketCodecs; +import dev.callmeecho.cabinetapi.network.CabinetS2CPacket; +import dev.callmeecho.cabinetapi.util.Singleton; +import io.netty.buffer.ByteBuf; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.Vec3d; +import net.minecraft.util.math.random.Random; +import org.joml.Vector3f; + +import java.util.List; + +public class PartyPopperS2CPacket implements CabinetS2CPacket { + public static final CustomPayload.Id ID = new CustomPayload.Id<>(Identifier.of(Bombastic.MODID, "party_popper")); + public static final PacketCodec CODEC = + PacketCodec.tuple( + CabinetPacketCodecs.pair( + PacketCodecs.VECTOR3F, + PacketCodecs.VECTOR3F), + PartyPopperPayload::posRot, + PartyPopperPayload::new + ); + + public static final Singleton SINGLETON = new Singleton<>(PartyPopperS2CPacket.class); + + @Override + public void receive(PartyPopperPayload payload, ClientPlayNetworking.Context context) { + Random random = context.player().getRandom(); + Vec3d direction = new Vec3d(payload.posRot().getSecond()); + Vector3f pos = payload.posRot().getFirst(); + for (int i = 0; i < 250; i++) { + Vec3d velocity = new Vec3d( + random.nextGaussian() * 0.15, + random.nextGaussian() * 0.15, + random.nextGaussian() * 0.15 + ); + velocity = velocity.add(direction.multiply(0.75F)); + + context.player().getWorld().addParticle( + BombasticParticleRegistrar.CONFETTI, + pos.x(), + pos.y() - 0.25F, + pos.z(), + velocity.getX(), + velocity.getY(), + velocity.getZ() + ); + } + } + + @Override + public CustomPayload.Id getId() { return ID; } + @Override + public PacketCodec getCodec() { return CODEC; } + + public record PartyPopperPayload(Pair posRot) implements CustomPayload { + @Override + public Id getId() { + return PartyPopperS2CPacket.ID; + } + } + +}