#Get source entity in LivingAttackEvent

68 messages · Page 1 of 1 (latest)

glacial matrix
#

With the mixin that KubeJS does on DamageSource, which adds actual, it seems the event doesn't recognize that source has that parameter, and therefore does not run the code at all because event.source.actual is null/undefined.

const $LivingEntity = java('net.minecraft.world.entity.LivingEntity')
const $EntityDamageSource = java('net.minecraft.world.damagesource.EntityDamageSource')

global.LivingAttackEventIndicator = function (/** @type {Internal.LivingAttackEvent} */ event) {
    if (!event.source) return
    if (!event.source.actual) return
    if (!event.entity) return
    if (!event.entity.asKJS().isPlayer()) return
    if (Platform.isClientEnvironment()) return
    event.setCanceled(true)
    let entity = event.entity.asKJS()
    let source = event.source.actual.asKJS()
    source.playSound('paucal:pet.bwop', 1, 1.2)
    entity.tell('test')
    Utils.server.scheduleInTicks(2, cb => {
        entity.tell('test2')
        entity.attack(new $EntityDamageSource(event.source.toString(), source.minecraftEntity), event.amount)
    })
}
onForgeEvent('net.minecraftforge.event.entity.living.LivingAttackEvent', event => {
    global.LivingAttackEventIndicator(event)
})

My goal is to delay the attack of an entity on a player and indicate an attack before the damage goes through (therefore making PvE better).

crystal cometBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

strong gazelle
#

so what you are telling is it event.source.actual will never exists?

glacial matrix
#

actual doesn't exist for some reason

strong gazelle
#

can you console.log(Object.keys(event.source))

#

just to be sure?

#

also

strong gazelle
#

console.log(event.source.class.simpleName)

#

what actual were supposed to be?

glacial matrix
#
[21:38:29] [Server thread/INFO] [KubeJS Startup/]: EntityDamageSource
[21:38:29] [Render thread/INFO] [KubeJS Startup/]: DamageSource
glacial matrix
strong gazelle
#

and the Object.keys?

#

can you link the mixin?

#

you are talking about

glacial matrix
#
[21:49:55] [Render thread/INFO] [KubeJS Startup/]: [magic,getClass,getPlayer,callBypassArmor,creativePlayer,getImmediate,type,setProjectile,isBypassMagic,isCreativePlayer,fire,setNoAggro,actual,isFire,noAggro,bypassInvul,setIsFall,sourcePosition,isExplosion,setMagic,getType,isMagic,immediate,isNoAggro,getSourcePosition,bypassMagic,isBypassArmor,getActual,wait,notifyAll,msgId,setScalesWithDifficulty,isProjectile,notify,foodExhaustion,bypassArmor,fall,hashCode,class,player,botania_setBypassArmor,isFall,isBypassInvul,isDamageHelmet,setIsFire,scalesWithDifficulty,callBypassInvul,damageHelmet,explosion,setExplosion,equals,localizedDeathMessage,getFoodExhaustion,callBypassMagic,toString,getLocalizedDeathMessage,projectile] [dev.latvian.mods.kubejs.util.ListJS]
#

actual is a method....

strong gazelle
#

so it is there

glacial matrix
#

but if i try to call event.source.actual then it crashes

#
The game crashed whilst ticking entity
Error: dev.latvian.mods.rhino.EcmaError: TypeError: Cannot convert null to an object. (startup_scripts:AttackIndicator.js#7)
#

null

strong gazelle
#

it is not saying the method doesn't exists

#

the actual code runs, but tries to do something that is doing null

#
    public EntityJS getActualKJS() {
        return AsKJS.wrapSafe(getEntity());
    }

something here maybe null

glacial matrix
#

line 7 is console.log(Object.keys(event.source.actual))

strong gazelle
#

like getEntity()

#

if you filter only for EntityDamageSource is enough for you or you need DamageSource?

glacial matrix
#

i don't need DamageSource, i could try doing that

strong gazelle
#

so use:

#
if (event.source instanceof $EntityDamageSource){
  // runs all your code here
}
glacial matrix
#
const $LivingEntity = java('net.minecraft.world.entity.LivingEntity')
const $EntityDamageSource = java('net.minecraft.world.damagesource.EntityDamageSource')

global.LivingAttackEventIndicator = function (/** @type {Internal.LivingAttackEvent} */ event) {
    if (!event.source) return
    console.log(event.source.class.simpleName)
    if (event.source instanceof $EntityDamageSource) {
        console.log(`${event.source.class.simpleName}, ${console.scriptLine}`)
        if (!event.source.entity) return
        console.log(event.source.entity)
        if (!event.entity) return
        if (!event.entity.asKJS().isPlayer()) return
        if (Platform.isClientEnvironment()) return
        event.setCanceled(true)
        let entity = event.entity.asKJS()
        let source = event.source.entity.asKJS()
        source.playSound('paucal:pet.bwop', 1, 1.2)
        entity.tell('test')
        Utils.server.scheduleInTicks(10, cb => {
            entity.tell('test2')
            entity.attack(new $EntityDamageSource(event.source.toString(), source.minecraftEntity), event.amount)
        })
    }
}
onForgeEvent('net.minecraftforge.event.entity.living.LivingAttackEvent', event => {
    global.LivingAttackEventIndicator(event)
})
#

got this so far

#

it won't go past if (!event.source.entity) return

strong gazelle
glacial matrix
#

that crashes the game but yes

strong gazelle
#

are you sure? can't find looking

#

can you point?

glacial matrix
#

ingame it says that it doesn't exist

[22:10:21] [Server thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: TypeError: Cannot find function getEntity in object EntityDamageSource (Zombie['Zombie'/137, l='ServerLevel[fml]', x=0.04, y=77.00, z=5.85]). (startup_scripts:AttackIndicator.js#9)
glacial matrix
#

oh

#

no it doesn't seem there is one

strong gazelle
#

so stop trying to call

glacial matrix
#

but that's on DamageSource, not EntityDamageSOurce

strong gazelle
#

the mixin is on damagesource, shouldn't be there too?

glacial matrix
#

getentity seems to hide from js

#

so i'll see if getEntityKJS() works

#

well

#

getActualKJS()

strong gazelle
#

wait, the actual is the entity bro

#

what are you trying to get anyway, i'm not familiar with this event

#

what do you NEED

glacial matrix
#

i explained it at the bottom of the post

strong gazelle
#

why are you doing source.entity

#

and not event.entity?

glacial matrix
#

event.entity links to the hurt entity

strong gazelle
#

are you testing this on single player?

strong gazelle
glacial matrix
#

the problem is stopping the entity's attack on the player, and yes this is on singleplayer

strong gazelle
#

that Platform stuff is returned on single player, stop using that, that is only for dedicated server

glacial matrix
#

hm

strong gazelle
#

do more console.logs to check where it is stoping

glacial matrix
#

it seems event.source.actual works but i just was having issues with the platform stuff

#

attack indicators are working as intended