Skip to content

Commit

Permalink
🐛 Fix party popper sending 250 packets
Browse files Browse the repository at this point in the history
  • Loading branch information
CallMeEchoCodes committed Jul 12, 2024
1 parent aec93fd commit 93af22b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/callmeecho/bombastic/main/Bombastic.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
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;
import net.minecraft.item.Item;
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;
Expand Down Expand Up @@ -67,27 +72,12 @@ private static void explode(World world, PlayerEntity playerEntity, float power)
@Override
public TypedActionResult<ItemStack> 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(
Expand All @@ -109,7 +99,7 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity,
stack,
(enchantment, level) -> enchantment.value().modifyValue(
BombasticEnchantmentComponentTypeRegistrar.PARTY_POPPER_EXPLOSION,
random,
playerEntity.getRandom(),
level,
power
));
Expand All @@ -122,4 +112,4 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity,
stack.damage(1, playerEntity, EquipmentSlot.MAINHAND);
return TypedActionResult.success(playerEntity.getStackInHand(hand));
}
}
}
Original file line number Diff line number Diff line change
@@ -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<PartyPopperS2CPacket.PartyPopperPayload> {
public static final CustomPayload.Id<PartyPopperPayload> ID = new CustomPayload.Id<>(Identifier.of(Bombastic.MODID, "party_popper"));
public static final PacketCodec<ByteBuf, PartyPopperPayload> CODEC =
PacketCodec.tuple(
CabinetPacketCodecs.pair(
PacketCodecs.VECTOR3F,
PacketCodecs.VECTOR3F),
PartyPopperPayload::posRot,
PartyPopperPayload::new
);

public static final Singleton<PartyPopperS2CPacket> 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<? extends CustomPayload> getId() { return ID; }
@Override
public <B extends PacketByteBuf> PacketCodec<? super B, ? extends CustomPayload> getCodec() { return CODEC; }

public record PartyPopperPayload(Pair<Vector3f, Vector3f> posRot) implements CustomPayload {
@Override
public Id<? extends CustomPayload> getId() {
return PartyPopperS2CPacket.ID;
}
}

}

0 comments on commit 93af22b

Please sign in to comment.