From aa7febdf5d1f6f357e923e8ed602bf6eba37220b Mon Sep 17 00:00:00 2001 From: 3arthqu4ke <56741599+3arthqu4ke@users.noreply.github.com> Date: Tue, 4 Oct 2022 01:45:19 +0200 Subject: [PATCH] Removed Firework Mixin hack (#384) --- .../EntityLivingBaseFireworkHelper.java | 33 ++++++++++++++ .../mixin/entity/MixinEntityLivingBase.java | 45 +++---------------- 2 files changed, 39 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/lambda/EntityLivingBaseFireworkHelper.java diff --git a/src/main/java/com/lambda/EntityLivingBaseFireworkHelper.java b/src/main/java/com/lambda/EntityLivingBaseFireworkHelper.java new file mode 100644 index 000000000..21112abde --- /dev/null +++ b/src/main/java/com/lambda/EntityLivingBaseFireworkHelper.java @@ -0,0 +1,33 @@ +package com.lambda; + +import com.lambda.client.module.modules.movement.ElytraFlight; +import com.lambda.mixin.accessor.AccessorEntityFireworkRocket; +import com.lambda.mixin.entity.MixinEntityLivingBase; +import net.minecraft.client.entity.EntityPlayerSP; +import net.minecraft.entity.EntityLivingBase; + +/** + * Using {@link AccessorEntityFireworkRocket} in {@link MixinEntityLivingBase} causes a crash on older + * Mixin versions (like the one Impact uses). Putting the methods using AccessorEntityFireworkRocket outside + * the MixinEntityLivingBase seems to fix the issue. + */ +public class EntityLivingBaseFireworkHelper { + public static boolean shouldWork(EntityLivingBase entity) { + return EntityPlayerSP.class.isAssignableFrom(entity.getClass()) + && ElytraFlight.INSTANCE.isEnabled() + && ElytraFlight.INSTANCE.getMode().getValue() == ElytraFlight.ElytraFlightMode.VANILLA; + } + + public static boolean shouldModify(EntityLivingBase entity) { + return shouldWork(entity) && entity.world.loadedEntityList.stream().anyMatch(firework -> { + if (firework instanceof AccessorEntityFireworkRocket) { + EntityLivingBase boosted = ((AccessorEntityFireworkRocket) firework).getBoostedEntity(); + return boosted != null && boosted.equals(entity); + } + + return false; + } + ); + } + +} diff --git a/src/main/java/com/lambda/mixin/entity/MixinEntityLivingBase.java b/src/main/java/com/lambda/mixin/entity/MixinEntityLivingBase.java index 8d5386504..285031014 100644 --- a/src/main/java/com/lambda/mixin/entity/MixinEntityLivingBase.java +++ b/src/main/java/com/lambda/mixin/entity/MixinEntityLivingBase.java @@ -1,14 +1,12 @@ package com.lambda.mixin.entity; +import com.lambda.EntityLivingBaseFireworkHelper; import com.lambda.client.module.modules.movement.ElytraFlight; -import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.item.EntityFireworkRocket; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; -import net.minecraftforge.fml.common.ObfuscationReflectionHelper; import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; @@ -19,19 +17,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; -import java.lang.reflect.Field; - @Mixin(EntityLivingBase.class) public abstract class MixinEntityLivingBase extends Entity { @Unique private Vec3d modifiedVec = null; - // This is a bit silly and bad for performance but fixes compatibility with old mixin versions like the one used by impact - @Unique - private static final Field boostedEntity; - - static { - boostedEntity = ObfuscationReflectionHelper.findField(EntityFireworkRocket.class, "field_191513_e"); - } public MixinEntityLivingBase(World worldIn) { super(worldIn); @@ -42,7 +31,7 @@ public MixinEntityLivingBase(World worldIn) { at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/entity/EntityLivingBase;getLookVec()Lnet/minecraft/util/math/Vec3d;", ordinal = 0) ) private Vec3d vec3d(Vec3d original) { - if (shouldWork()) { + if (EntityLivingBaseFireworkHelper.shouldWork(EntityLivingBase.class.cast(this))) { float negPacketPitch = -ElytraFlight.INSTANCE.getPacketPitch(); float f0 = MathHelper.cos((float) (-this.rotationYaw * 0.017453292f - Math.PI)); float f1 = MathHelper.sin((float) (-this.rotationYaw * 0.017453292f - Math.PI)); @@ -60,7 +49,7 @@ private Vec3d vec3d(Vec3d original) { ordinal = 3 ) private float f(float original) { - if (shouldWork()) { + if (EntityLivingBaseFireworkHelper.shouldWork(EntityLivingBase.class.cast(this))) { return ElytraFlight.INSTANCE.getPacketPitch() * 0.017453292f; } return original; @@ -87,7 +76,7 @@ private void getVec( at = @At(value = "FIELD", opcode = Opcodes.GETFIELD, target = "Lnet/minecraft/entity/EntityLivingBase;motionX:D", ordinal = 7) ) public double motionX(EntityLivingBase it) { - if (shouldModify()) { + if (EntityLivingBaseFireworkHelper.shouldModify(EntityLivingBase.class.cast(this))) { it.motionX += modifiedVec.x * 0.1 + (modifiedVec.x * 1.5 - this.motionX) * 0.5; } return it.motionX; @@ -98,7 +87,7 @@ public double motionX(EntityLivingBase it) { at = @At(value = "FIELD", opcode = Opcodes.GETFIELD, target = "Lnet/minecraft/entity/EntityLivingBase;motionY:D", ordinal = 7) ) public double motionY(EntityLivingBase it) { - if (shouldModify()) { + if (EntityLivingBaseFireworkHelper.shouldModify(EntityLivingBase.class.cast(this))) { it.motionY += modifiedVec.y * 0.1 + (modifiedVec.y * 1.5 - this.motionY) * 0.5; } return it.motionY; @@ -109,32 +98,10 @@ public double motionY(EntityLivingBase it) { at = @At(value = "FIELD", opcode = Opcodes.GETFIELD, target = "Lnet/minecraft/entity/EntityLivingBase;motionZ:D", ordinal = 7) ) public double motionZ(EntityLivingBase it) { - if (shouldModify()) { + if (EntityLivingBaseFireworkHelper.shouldModify(EntityLivingBase.class.cast(this))) { it.motionZ += modifiedVec.z * 0.1 + (modifiedVec.z * 1.5 - this.motionZ) * 0.5; } return it.motionZ; } - @Unique - private boolean shouldWork() { - return EntityPlayerSP.class.isAssignableFrom(getClass()) - && ElytraFlight.INSTANCE.isEnabled() - && ElytraFlight.INSTANCE.getMode().getValue() == ElytraFlight.ElytraFlightMode.VANILLA; - } - - @Unique - private boolean shouldModify() { - return shouldWork() && world.loadedEntityList.stream().anyMatch(entity -> { - if (entity instanceof EntityFireworkRocket) { - try { - EntityLivingBase boosted = (EntityLivingBase) boostedEntity.get(entity); - return boosted != null && boosted.equals(this); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); // This should absolutely never happen - } - } - return false; - } - ); - } }