Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed MixinEntityLivingBase reflection hack #384

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/com/lambda/EntityLivingBaseFireworkHelper.java
Original file line number Diff line number Diff line change
@@ -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;
}
);
}

}
45 changes: 6 additions & 39 deletions src/main/java/com/lambda/mixin/entity/MixinEntityLivingBase.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
);
}
}