Stack overflow when using entity.attack()
Sly-Little-Fox opened this issue · 2 comments
Minecraft Version
1.19.2
KubeJS Version
1902.6.0-build.142
Rhino Version
1902.2.2-build.268
Architectury Version
6.5.85-fabric
Forge/Fabric Version
Fabric 0.14.19
Describe your issue
When using entity.attack()
, KubeJS spams the console with huge stacktraces, which cause a stack overflow and completely freeze the server.
Code:
EntityEvents.hurt(event => {
event.cancel();
event.entity.attack(event.getSource(), 2.0)
});
Crash report/logs
This is because entity#attack causes the hurt event to fire again, thus triggering your code again and so on.
To solve this use a variable like so:
let noRecurse = false
EntityEvents.hurt(event => {
if (noRecurse) return
noRecurse = true
event.entity.attack(event.getSource(), 2)
noRecurse = false
})
This is because entity#attack causes the hurt event to fire again, thus triggering your code again and so on.
To solve this use a variable like so:
let noRecurse = false EntityEvents.hurt(event => { if (noRecurse) return noRecurse = true event.entity.attack(event.getSource(), 2) noRecurse = false })
How did I not think of this 🤦♂️
Thanks!