Jump to content

Recommended Posts

Posted

I am trying to mixin to the Vex class to give itself an effect if the owning evoker has said effect. The only problem is I can't reference the "this" object from the Vex class and there is no entity getter method or variable to get the vex entity (just the owner).

package gg.hipposgrumm.intangibility.effects.intangibility.special_cases;

// Imports Ommitted

@Mixin(Vex.class)
public class EvokerVexIntangibleLifelink {
    @Shadow
    @Nullable
    private Mob owner;

    @Inject(method = "Lnet/minecraft/world/entity/monster/Vex;tick()V", at = @At("HEAD"))
    private void intangibilitypotion_ifvexsummonerisintangiblebecomeintangible(CallbackInfo ci) {
        if (this.owner.hasEffect(Main.INTANGIBILITY.get())) {
            ((Vex)this).addEffect(new MobEffectInstance(Main.INTANGIBILITY.get(), this.owner.getEffect(Main.INTANGIBILITY.get()).getDuration()));
        }
    }
}

This is the method I am trying to inject into:

   public void tick() {
      this.noPhysics = true;
      super.tick();
      this.noPhysics = false;
      this.setNoGravity(true);
      if (this.hasLimitedLife && --this.limitedLifeTicks <= 0) {
         this.limitedLifeTicks = 20;
         this.hurt(DamageSource.STARVE, 1.0F);
      }
   }

Is there any easy way to do this?

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Posted

first your mixin should extend/implement everything the target class is extending or implementing

second, always declare your mixins abstract so you don't accidentally instantiate them.

then just cast ((object) (target class) this).->... it should work as both mixin class and target class are objects

  • 2 years later...
Posted
  On 2/2/2023 at 9:12 PM, Darkorg69 said:

first your mixin should extend/implement everything the target class is extending or implementing

second, always declare your mixins abstract so you don't accidentally instantiate them.

then just cast ((object) (target class) this).->... it should work as both mixin class and target class are objects

Expand  

For people seeing this in the future:

First: I've never once done this. You're just bloating your mixin classes.
Second: Good advice, even if not pertinent 
Third: Incorrect. You cast (TargetClass) (Object) this. Your example casts the wrong way around, ultimately casting to Object, which isn't very useful. I would also advise making this a private field at the top of the mixin called something like "self", so you can reference it without performing the cast every time.

It's kind of a strange concept to get your head around, but remember that this code is literally being executed inside the target class as if it's native code (more or less), so at runtime, "this" will actually refer to the target object.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.