Jump to content

Trouble with LivingKnockBackEvent


d18240

Recommended Posts

Hey, I'm new to modding and new to this forum so hopefully I'm doing things alright.

I'm attempting to implement increased knockback for a custom weapon in my mod. I've created the item and am trying to figure out how to add the increased knockback effect.

This post suggests subscribing to `LivingKnockBackEvent` - which I have successfully done and I'm able to effect the degree of knockback by using `event.setStrength`. However, I'm unable to conditionally set the knockback strength, as I don't have access to the entity which attacked through this method. Which is odd, because both the forum post AND the 1.16.x documentation inside of the code allude to an `Entity attacker` parameter which does not exist. I went back and looked at an older version (1.15.x and earlier) of Forge, and there did indeed used to be an `Entity attacker` parameter, but it was removed in 1.16.x for a reason I can't find. Because of this, I'm unable to find the weapon that was used to cause the knockback event - which is required for my task.

Did this get removed because there's a more modern way to access a knockback attacker entity? Or am I just out of luck? Alternatively, is there another way to set an items' knockback amount? I tried setting the `ATTACK_KNOCKBACK` attribute in the weapon modifier properties, but it turns out that doesn't work on items - only on mobs (which is to say, it shows up on the item as "+20 Attack Knockback", but it has no effect).

Any help would be appreciated, thanks!

[As a note, I am using Forge 1.16.5-36.1.0]

Edited by d18240
Link to comment
Share on other sites

event.getEntity()? maybe event.getLiving()

Might have a different name, but as it is a subclass of EntityEvent (or LivingEntityEvent) it will have a reference to the relevant entity or entities. Take a look at the class and see what fields it, and its various parent classes, are available and what getter methods expose them.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I did try event.getEntity() and event.getEntityLiving() - they both return the same thing, which is the Entity being knocked back. I've also looked through all parent classes - LivingEvent, EntityEvent, and Event.

LivingKnockBackEvent and LivingEvent pass the entity through to EntityEvent which only handles the entity which is being effected, and Event has nothing relevant in it.

It looks like the 1.15.x code has exactly what I need:

public LivingKnockBackEvent(LivingEntity target, Entity attacker, float strength, double ratioX, double ratioZ)
    {
        super(target);
        this.attacker = this.originalAttacker = attacker;
        this.strength = this.originalStrength = strength;
        this.ratioX = this.originalRatioX = ratioX;
        this.ratioZ = this.originalRatioZ = ratioZ;
    }

But all traces of "attacker" or "originalAttacker" are removed in 1.16.x:

public LivingKnockBackEvent(LivingEntity target, float strength, double ratioX, double ratioZ)
    {
        super(target);
        this.strength = this.originalStrength = strength;
        this.ratioX = this.originalRatioX = ratioX;
        this.ratioZ = this.originalRatioZ = ratioZ;
    }

 

Edited by d18240
added full 1.15.x code
Link to comment
Share on other sites

@diesieben07 Thanks a bunch for the recommendations - it inspired me to look at the various Forge events available to me. I ended up going with LivingAttackEvent, as this seems to be a bit more generalized.

And for anyone stumbling upon this later, in order to "call knockback" you literally just call knockback - the LivingEntity class has a knockback method. I didn't know this and it stumped me for while. Including code for those curious (granted it's probably not good code, any suggestions welcome).

Spoiler

    @SubscribeEvent
    public void batKnockback(LivingAttackEvent event) {

        Entity entity = event.getSource().getEntity();
        if (entity instanceof ServerPlayerEntity) {

            ServerPlayerEntity playerEntity = (ServerPlayerEntity)entity;
            if (playerEntity.getMainHandItem().getItem() instanceof BatItem) {

                BatItem bat = (BatItem)playerEntity.getMainHandItem().getItem();
                float knockbackAmount = bat.getKnockbackAmount();
                double knockbackX = -playerEntity.getLookAngle().x();
                double knockbackZ = -playerEntity.getLookAngle().z();
                event.getEntityLiving().knockback(knockbackAmount, knockbackX, knockbackZ);
            }
        }
    }

 

 

Link to comment
Share on other sites

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.