Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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?

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...
On 2/2/2023 at 4: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

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.