Skip to content

Commit

Permalink
Improve syncing of confetti creepers
Browse files Browse the repository at this point in the history
- Now the chance to be confetti creeper is checked once on server-side spawn
  • Loading branch information
jchung01 committed Jun 29, 2024
1 parent dc35ddf commit 81b777d
Showing 1 changed file with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
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;
import org.spongepowered.asm.mixin.injection.At;
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<Boolean> ut$CONFETTI = EntityDataManager.createKey(EntityCreeper.class, DataSerializers.BOOLEAN);

protected UTCreeperConfettiMixin(World worldIn)
{
super(worldIn);
Expand All @@ -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 = "<init>", 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();
Expand All @@ -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();
}

0 comments on commit 81b777d

Please sign in to comment.