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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I tried installing several mods that improves fps with other several gameplay mods that i have added previously and tested to be playable. But when I have added the new mods that improves the fps, Java crashes. (version is 1.20.1)   Here is the crash log:    ---- Minecraft Crash Report ---- // But it works on my machine. Time: 2023-12-08 17:22:22 Description: Initializing game java.lang.IncompatibleClassChangeError: class net.coderbot.iris.gui.option.ShadowDistanceOption cannot inherit from final class net.minecraft.client.OptionInstance     at java.lang.ClassLoader.defineClass1(Native Method) ~[?:?] {}     at java.lang.ClassLoader.defineClass(ClassLoader.java:1017) ~[?:?] {}     at cpw.mods.cl.ModuleClassLoader.readerToClass(ModuleClassLoader.java:119) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.lambda$findClass$15(ModuleClassLoader.java:219) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.loadFromModule(ModuleClassLoader.java:229) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.findClass(ModuleClassLoader.java:219) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.loadClass(ModuleClassLoader.java:135) ~[securejarhandler-2.1.10.jar:?] {}     at java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[?:?] {}     at net.coderbot.iris.config.IrisConfig.load(IrisConfig.java:142) ~[oculus-mc1.20.1-1.6.9.jar%23179!/:?] {re:classloading}     at net.coderbot.iris.config.IrisConfig.initialize(IrisConfig.java:58) ~[oculus-mc1.20.1-1.6.9.jar%23179!/:?] {re:classloading}     at net.coderbot.iris.Iris.onEarlyInitialize(Iris.java:146) ~[oculus-mc1.20.1-1.6.9.jar%23179!/:?] {re:mixin,re:classloading}     at net.minecraft.client.Options.handler$zgi000$iris$beforeLoadOptions(Options.java:1430) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:mixins.oculus.json:MixinOptions_Entrypoint,pl:mixin:APP:mixins.oculus.fixes.maxfpscrash.json:MixinMaxFpsCrashFix,pl:mixin:APP:mixins.oculus.json:sky.MixinOptions_CloudsOverride,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Options.m_92140_(Options.java) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:mixins.oculus.json:MixinOptions_Entrypoint,pl:mixin:APP:mixins.oculus.fixes.maxfpscrash.json:MixinMaxFpsCrashFix,pl:mixin:APP:mixins.oculus.json:sky.MixinOptions_CloudsOverride,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Options.<init>(Options.java:888) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:mixins.oculus.json:MixinOptions_Entrypoint,pl:mixin:APP:mixins.oculus.fixes.maxfpscrash.json:MixinMaxFpsCrashFix,pl:mixin:APP:mixins.oculus.json:sky.MixinOptions_CloudsOverride,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.<init>(Minecraft.java:425) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:rubidium_extras.mixins.json:FrameCounter.FpsAccessorMixin,pl:mixin:APP:rubidium.mixins.json:core.MinecraftClientMixin,pl:mixin:APP:mixins.oculus.json:MixinMinecraft_PipelineManagement,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:182) ~[1.20.1-forge-47.2.0.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.runTarget(CommonLaunchHandler.java:111) ~[fmlloader-1.20.1-47.2.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.clientService(CommonLaunchHandler.java:99) ~[fmlloader-1.20.1-47.2.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonClientLaunchHandler.lambda$makeService$0(CommonClientLaunchHandler.java:25) ~[fmlloader-1.20.1-47.2.0.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:108) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:78) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {} A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Render thread Stacktrace:     at java.lang.ClassLoader.defineClass1(Native Method) ~[?:?] {}     at java.lang.ClassLoader.defineClass(ClassLoader.java:1017) ~[?:?] {}     at cpw.mods.cl.ModuleClassLoader.readerToClass(ModuleClassLoader.java:119) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.lambda$findClass$15(ModuleClassLoader.java:219) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.loadFromModule(ModuleClassLoader.java:229) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.findClass(ModuleClassLoader.java:219) ~[securejarhandler-2.1.10.jar:?] {}     at cpw.mods.cl.ModuleClassLoader.loadClass(ModuleClassLoader.java:135) ~[securejarhandler-2.1.10.jar:?] {}     at java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[?:?] {}     at net.coderbot.iris.config.IrisConfig.load(IrisConfig.java:142) ~[oculus-mc1.20.1-1.6.9.jar%23179!/:?] {re:classloading}     at net.coderbot.iris.config.IrisConfig.initialize(IrisConfig.java:58) ~[oculus-mc1.20.1-1.6.9.jar%23179!/:?] {re:classloading}     at net.coderbot.iris.Iris.onEarlyInitialize(Iris.java:146) ~[oculus-mc1.20.1-1.6.9.jar%23179!/:?] {re:mixin,re:classloading}     at net.minecraft.client.Options.handler$zgi000$iris$beforeLoadOptions(Options.java:1430) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:mixins.oculus.json:MixinOptions_Entrypoint,pl:mixin:APP:mixins.oculus.fixes.maxfpscrash.json:MixinMaxFpsCrashFix,pl:mixin:APP:mixins.oculus.json:sky.MixinOptions_CloudsOverride,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Options.m_92140_(Options.java) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:mixins.oculus.json:MixinOptions_Entrypoint,pl:mixin:APP:mixins.oculus.fixes.maxfpscrash.json:MixinMaxFpsCrashFix,pl:mixin:APP:mixins.oculus.json:sky.MixinOptions_CloudsOverride,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Options.<init>(Options.java:888) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:mixins.oculus.json:MixinOptions_Entrypoint,pl:mixin:APP:mixins.oculus.fixes.maxfpscrash.json:MixinMaxFpsCrashFix,pl:mixin:APP:mixins.oculus.json:sky.MixinOptions_CloudsOverride,pl:mixin:A,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.<init>(Minecraft.java:425) ~[client-1.20.1-20230612.114412-srg.jar%23189!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:rubidium_extras.mixins.json:FrameCounter.FpsAccessorMixin,pl:mixin:APP:rubidium.mixins.json:core.MinecraftClientMixin,pl:mixin:APP:mixins.oculus.json:MixinMinecraft_PipelineManagement,pl:mixin:A,pl:runtimedistcleaner:A} -- Initialization -- Details:     Modules:  Stacktrace:     at net.minecraft.client.main.Main.main(Main.java:182) ~[1.20.1-forge-47.2.0.jar:?] {re:classloading,pl:runtimedistcleaner:A}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}     at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}     at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.runTarget(CommonLaunchHandler.java:111) ~[fmlloader-1.20.1-47.2.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.clientService(CommonLaunchHandler.java:99) ~[fmlloader-1.20.1-47.2.0.jar:?] {}     at net.minecraftforge.fml.loading.targets.CommonClientLaunchHandler.lambda$makeService$0(CommonClientLaunchHandler.java:25) ~[fmlloader-1.20.1-47.2.0.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:108) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:78) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) ~[modlauncher-10.0.9.jar:?] {}     at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {} -- System Details -- Details:     Minecraft Version: 1.20.1     Minecraft Version ID: 1.20.1     Operating System: Mac OS X (aarch64) version 14.1.2     Java Version: 17.0.8, Microsoft     Java VM Version: OpenJDK 64-Bit Server VM (mixed mode), Microsoft     Memory: 285189552 bytes (271 MiB) / 536870912 bytes (512 MiB) up to 2147483648 bytes (2048 MiB)     CPUs: 8     Processor Vendor: Apple Inc.     Processor Name: Apple M1     Identifier: Apple Inc. Family 0x1b588bb3 Model 0 Stepping 0     Microarchitecture: ARM64 SoC: Firestorm + Icestorm     Frequency (GHz): 3.20     Number of physical packages: 1     Number of physical CPUs: 8     Number of logical CPUs: 8     Graphics card #0 name: Apple M1     Graphics card #0 vendor: Apple (0x106b)     Graphics card #0 VRAM (MB): 0.00     Graphics card #0 deviceId: unknown     Graphics card #0 versionInfo: unknown     Memory slot #0 capacity (MB): 0.00     Memory slot #0 clockSpeed (GHz): 0.00     Memory slot #0 type: unknown     Virtual memory max (MB): 11264.00     Virtual memory used (MB): 7656.59     Swap memory total (MB): 3072.00     Swap memory used (MB): 1509.94     JVM Flags: 7 total; -Xmx2G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M     Launched Version: 1.20.1-forge-47.2.0     Backend library: LWJGL version 3.3.1 build 7     Backend API: Unknown     Window size: <not initialized>     GL Caps: Using framebuffer using OpenGL 3.2     GL debug messages: <disabled>     Using VBOs: Yes     Is Modded: Definitely; Client brand changed to 'forge'     Type: Client (map_client.txt)     CPU: <unknown>
    • I have created a mod which applies x shader to the player after consuming x item for a certain time, but if the player presses f5 to change the camera the shader stops working. The only way I have found to prevent the shader from being deactivated is to load it on all ticks until it is disabled but this greatly reduces the performance of the game. I have also tried to cancel the event of changing the camera but this is not cancelable. Any advice, I've been trying to solve this for several days 😵‍💫  
    • Minecraft 1.20.1 Forge 47.2.0 log and crash report: https://pastebin.com/P7tQymQS ; (crash report) I launch the game and i got this error: Someone could help me please?  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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