From 81b777d7c0484301dcd16fd58448091c3cb64edb Mon Sep 17 00:00:00 2001 From: jchung01 Date: Fri, 28 Jun 2024 18:19:10 -0700 Subject: [PATCH] Improve syncing of confetti creepers - Now the chance to be confetti creeper is checked once on server-side spawn --- .../mixin/UTCreeperConfettiMixin.java | 69 ++++++++++++------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/src/main/java/mod/acgaming/universaltweaks/tweaks/entities/spawning/creeper/confetti/mixin/UTCreeperConfettiMixin.java b/src/main/java/mod/acgaming/universaltweaks/tweaks/entities/spawning/creeper/confetti/mixin/UTCreeperConfettiMixin.java index 634bbfbb..2f56419d 100644 --- a/src/main/java/mod/acgaming/universaltweaks/tweaks/entities/spawning/creeper/confetti/mixin/UTCreeperConfettiMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/tweaks/entities/spawning/creeper/confetti/mixin/UTCreeperConfettiMixin.java @@ -1,27 +1,22 @@ package mod.acgaming.universaltweaks.tweaks.entities.spawning.creeper.confetti.mixin; import java.util.List; -import java.util.Random; - import com.google.common.collect.Lists; -import net.minecraft.client.Minecraft; -import net.minecraft.client.particle.ParticleFirework; -import net.minecraft.client.particle.ParticleManager; +import mod.acgaming.universaltweaks.UniversalTweaks; +import mod.acgaming.universaltweaks.config.UTConfigGeneral; +import mod.acgaming.universaltweaks.config.UTConfigTweaks; +import mod.acgaming.universaltweaks.util.UTRandomUtil; import net.minecraft.entity.monster.EntityCreeper; import net.minecraft.entity.monster.EntityMob; import net.minecraft.item.ItemDye; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; +import net.minecraft.network.datasync.DataParameter; +import net.minecraft.network.datasync.DataSerializers; +import net.minecraft.network.datasync.EntityDataManager; import net.minecraft.util.math.BlockPos; import net.minecraft.world.Explosion; import net.minecraft.world.World; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; - -import mod.acgaming.universaltweaks.UniversalTweaks; -import mod.acgaming.universaltweaks.config.UTConfigGeneral; -import mod.acgaming.universaltweaks.config.UTConfigTweaks; -import mod.acgaming.universaltweaks.util.UTRandomUtil; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -29,10 +24,14 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -// Courtesy of SR2610 +// Courtesy of SR2610, jchung01 @Mixin(EntityCreeper.class) public abstract class UTCreeperConfettiMixin extends EntityMob { + @SuppressWarnings("WrongEntityDataParameterClass") + @Unique + private static final DataParameter ut$CONFETTI = EntityDataManager.createKey(EntityCreeper.class, DataSerializers.BOOLEAN); + protected UTCreeperConfettiMixin(World worldIn) { super(worldIn); @@ -41,30 +40,57 @@ protected UTCreeperConfettiMixin(World worldIn) @Shadow public abstract boolean getPowered(); - @Inject(method = "explode", at = @At("HEAD"), cancellable = true) - public void utCreeperConfetti(CallbackInfo ci) + @Shadow + protected abstract void spawnLingeringCloud(); + + /** + * Register confetti DataParameter. + * @author jchung01 + */ + @Inject(method = "entityInit", at = @At(value = "TAIL")) + private void utRegisterConfetti(CallbackInfo ci) { + this.dataManager.register(ut$CONFETTI, Boolean.FALSE); + } + + /** + * Determine on spawn if this creeper should explode into confetti. + * @author jchung01 + */ + @Inject(method = "", at = @At(value = "TAIL")) + private void utInitConfetti(World worldIn, CallbackInfo ci) + { + if (this.world.isRemote) return; double chargedChance = UTConfigTweaks.ENTITIES.CREEPER_CONFETTI.utCreeperConfettiChance; - if (chargedChance <= 0.0D || !UTRandomUtil.chance(chargedChance, new Random(this.getUniqueID().getMostSignificantBits() & Long.MAX_VALUE))) return; + if (chargedChance > 0.0D && UTRandomUtil.chance(chargedChance, this.rand)) + { + this.dataManager.set(ut$CONFETTI, Boolean.TRUE); + } + } + + @Inject(method = "explode", at = @At("HEAD"), cancellable = true) + private void utDoConfetti(CallbackInfo ci) + { + if (!this.dataManager.get(ut$CONFETTI)) return; if (UTConfigGeneral.DEBUG.utDebugToggle) UniversalTweaks.LOGGER.debug("UTCreeperConfetti ::: Explode"); if (this.world.isRemote) { BlockPos pos = this.getPosition(); boolean powered = this.getPowered(); - this.world.makeFireworks(pos.getX(), pos.getY() + (powered ? 2.5F : 0.5F), pos.getZ(), 0, 0, 0, generateTag(powered)); + this.world.makeFireworks(pos.getX(), pos.getY() + (powered ? 2.5F : 0.5F), pos.getZ(), 0, 0, 0, ut$generateTag(powered)); } else { this.dead = true; this.setDead(); this.spawnLingeringCloud(); - this.damagePlayers(((EntityCreeper) (Object) this)); + this.ut$damagePlayers(((EntityCreeper) (Object) this)); } ci.cancel(); } @Unique - public NBTTagCompound generateTag(boolean powered) + private NBTTagCompound ut$generateTag(boolean powered) { NBTTagCompound fireworkTag = new NBTTagCompound(); NBTTagCompound fireworkItemTag = new NBTTagCompound(); @@ -85,13 +111,10 @@ public NBTTagCompound generateTag(boolean powered) } @Unique - public void damagePlayers(EntityCreeper creeper) + private void ut$damagePlayers(EntityCreeper creeper) { float explosionStrength = (float) (creeper.getPowered() ? UTConfigTweaks.ENTITIES.CREEPER_CONFETTI.utCreeperConfettiDamage * 2 : UTConfigTweaks.ENTITIES.CREEPER_CONFETTI.utCreeperConfettiDamage); Explosion explosion = new Explosion(creeper.getEntityWorld(), creeper, creeper.posX, creeper.posY, creeper.posZ, 3.0F * explosionStrength, false, false); explosion.doExplosionA(); } - - @Shadow - protected abstract void spawnLingeringCloud(); } \ No newline at end of file