FabricMC/Mixin

Cannot inject into a constructor that doesn't call super() but calls this()

skyrising opened this issue · 0 comments

For example trying

@Mixin(TntEntity.class)
public abstract class TntEntityMixin extends Entity {
    public TntEntityMixin(EntityType<?> type, World world) {
        super(type, world);
    }

    @Redirect(
            method = "<init>(Lnet/minecraft/world/World;DDDLnet/minecraft/entity/LivingEntity;)V",
            at = @At(value = "INVOKE", target = "Ljava/util/Random;nextDouble()D")
    )
    private double myNextDouble(Random random) {
        return random.nextDouble();
    }
}

crashes with

java.lang.NullPointerException
	at org.spongepowered.asm.lib.tree.InsnList.indexOf(InsnList.java:133)
	at org.spongepowered.asm.mixin.injection.struct.Target.indexOf(Target.java:450)
	at org.spongepowered.asm.mixin.injection.invoke.InvokeInjector.checkTargetForNode(InvokeInjector.java:108)
	at org.spongepowered.asm.mixin.injection.invoke.RedirectInjector.inject(RedirectInjector.java:252)
	at org.spongepowered.asm.mixin.injection.code.Injector.inject(Injector.java:200)
	at org.spongepowered.asm.mixin.injection.struct.InjectionInfo.inject(InjectionInfo.java:267)

Because Bytecode.findSuperInit will return null because there is no call to Entity.<init>(...), but there is a call to TntEntity.<init>(...)