#Invoking Java class functions/methods in KJS

1 messages · Page 1 of 1 (latest)

urban fog
#

turns out i lied :p GatewayEntity class has a nifty function called summonerOrClosest(), which fits my script perfectly, only problem is, i have no idea how to properly invoke functions from a java class in KJS

class function in reference: https://github.com/Shadows-of-Fire/GatewaysToEternity/blob/9b1df9781750be124009044127c03c96d3615fa3/src/main/java/shadows/gateways/entity/GatewayEntity.java#L222

let $gatewayEntity = java('shadows.gateways.entity.GatewayEntity$summonerOrClosest')

onForgeEvent('shadows.gateways.event.GateEvent$Failed', event => {
    console.log(event.entity.getGateway().getId())
    console.log(event.entity.getGateway()[$gatewayEntity])
    //let summoner = event.entity.getGateway()['$gatewayEntity$summonerOrClosest']
})```
this is where i'm at right now, it just crashes on startup claiming syntax errors, and it's right :p
tight stirrupBOT
#

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

urban fog
#

i guess converting the PlayerEntity into PlayerEntityJS would also be an important detail that needs to be done

keen bough
#

lets play the game where you actually give the context of what you want to accomplish 😄

urban fog
#

when a gate is failed, give back a gateway pearl of the failed gate

keen bough
#

makes sense

#

so you don't need that weird import

urban fog
#

i could probably also just pop the pearl item at the gate position, that would be a fitting alternative

keen bough
#

event.getEntity() gives you a GatewayEntity so you'd just
event.getEntity().summonOrClosest()

urban fog
#

oh, it's that easy?

#

pff

keen bough
#

anything you see in that gatewayentity class, that isn't private of course, can be run on the object you get back in that event from the entity

urban fog
#

alright, that's useful info

#

dev.latvian.mods.rhino.EcmaError: TypeError: Cannot find function summonerOrClosest in object shadows.gateways.gate.Gateway@65bbf074. (startup_scripts:return_gate_summon.js#3)
thonk

keen bough
#

show your work?

urban fog
#

that's a different classpath than where i was looking

#

hm

keen bough
#

event.entity is the GatewayEntity, not event.entity.getGateway()

urban fog
#
onForgeEvent('shadows.gateways.event.GateEvent$Failed', event => {
    console.log(event.entity.getGateway().getId())
    console.log(event.entity.getGateway().summonerOrClosest())
    //let summoner = event.entity.getGateway()['$gatewayEntity$']
})```
keen bough
#

bad dog

urban fog
#

oh so i just cut getgateway

#

derp

#

i can read code, usually

keen bough
#

heh

urban fog
#

ok cool

urban fog
#

alright, so i want to have a failsafe incase no players are found to just drop the item on the ground at the gate's position, but uh, quarter of an idea how to do that

#

tried copying homework™️ from tazz's entitySnipSnip, but things aren't the same on 1.16, problem calling createEntity() on undefined

#
onForgeEvent('shadows.gateways.event.GateEvent$Failed', event => {
    let gateID = event.entity.getGateway().getId()
    let summoner = event.entity.summonerOrClosest()
    let pearlOfGate = Item.of('gateways:gateway_pearl', `{gateway: ${gateID}}`)

    if(summoner.name){
        summoner.give(pearlOfGate)
    } else {
        let {x, y, z} = event.entity
        let pearlItemEntity = event.entity.world.createEntity('minecraft:item')
        pearlItemEntity.x = x
        pearlItemEntity.y = y
        pearlItemEntity.z = z
        pearlItemEntity.item = pearlOfGate
        pearlItemEntity.item.count = 1
        pearlItemEntity.spawn()
    }
})```
keen bough
#

event.entity.world.asKJS().createEntity('minecraft:item')

#

you can use .asKJS() on an entity, server, or world to wrap them in the kubejs version

urban fog
#

dev.latvian.mods.rhino.EcmaError: TypeError: Cannot call method "asKJS" of undefined (startup_scripts:return_gate_summon.js#10)

#

let pearlItemEntity = event.entity.world.asKJS().createEntity('minecraft:item')

#

wat da hel

keen bough
#

sure it's not level instead of world? I think that changed mid 16

urban fog
#

i've another script in the same instance using event.world

#

so i'd think not

#

i'll give it a whirl though

#

crashed faster, so uh, probably not

#

same error though, just a final tick on the gateway entity

keen bough
#

can always move it up, try event.entity.asKJS().world.create

urban fog
#

dev.latvian.mods.rhino.EvaluatorException: Cannot convert dev.latvian.mods.rhino.Undefined@0 to double (startup_scripts:return_gate_summon.js#11)

#

well uh, new error

#

so i guess that means it's working lol

#

think i got an idea for this though, event.entity.asKJS() for the xyz

#

yeah, seems to have done it

#

didn't crash

#

well, didn't crash when it expired, but didn't log anything

#

nevermind, that's expected with the current code

#

didn't get it back though, hm

keen bough
#

add logging everywhere, that's what I do lol

#

you should do the function trick so you don't have to keep restarting mc though, will make things faster

urban fog
#

o

#

wut function trick

#

have a function outside the event and invoke it within?

keen bough
#

si

#

can't reload events, but functions will

urban fog
#

mmm

keen bough
#
onForgeEvent('shadows.gateways.event.GateEvent$Failed', event => {
  global.GateFailed(event)
})

global.GateFailed = (event) => { 
    let gateID = event.entity.getGateway().getId()
    let summoner = event.entity.summonerOrClosest()
    let pearlOfGate = Item.of('gateways:gateway_pearl', `{gateway: ${gateID}}`)

    if(summoner.name){
        summoner.give(pearlOfGate)
    } else {
        let {x, y, z} = event.entity
        let pearlItemEntity = event.entity.world.createEntity('minecraft:item')
        pearlItemEntity.x = x
        pearlItemEntity.y = y
        pearlItemEntity.z = z
        pearlItemEntity.item = pearlOfGate
        pearlItemEntity.item.count = 1
        pearlItemEntity.spawn()
    }
}
#

now you just pop a nice /kubejs reload startup_scripts and voila

urban fog
#

mmm

#

tasty

keen bough
#

probably just need to convert the player to a kjs or the itemstack to mc

urban fog
#

nothing is logged thonk

#

do i need to /reload too?

keen bough
#

well you still have to do the initial mc restart yea?

urban fog
#

oh

#

maxint iq

keen bough
#

because the event still doesn't reload

#

hehe

#

yeah you need to do summoner.asKJS().give(pearlOfGate) I'm pretty sure

urban fog
#

yay no crash

#

the pearl is being logged as Item.empty though thonk

#

that doesn't bode well

#

maybe i should put this on a different gate event for the time being so i don't have to wait 45 seconds to see if it makes the game explode

#

alright, it seems like .asKJS() on the summoner makes it print my name instead of undefined, but the pearl is still Item.empty

keen bough
#

try

let pearlOfGate = Item.of('gateways:gateway_pearl', `{gateway: "${gateID.toString()}"}`)
#

make sure you get the " in there too

urban fog
#

you know, that uh, might have helped

#

hm, no dice

#

it is trying to give me the item though, so it's doing good there

#

also, a string of '' is considered false in JS, yeah?

#

pfff

#

found the problem

#

gateways:gate_pearl, not gateways:gateway_pearl

#

works perfectly now

keen bough
#

huzzah!

urban fog
#

agh, what's the array contains object function in JS, i swear i'm either never using it or always forgetting

keen bough
#

includes?

urban fog
#
    console.log(global.gatewayRefundBlacklist.includes(gateID))
    if(global.gatewayRefundBlacklist.includes(gateID)) return```
```[15:56:45] [INFO ] gateways:blaze_gate_small
[15:56:45] [INFO ] Emmerdog
[15:56:45] [INFO ] Item.of('gateways:gate_pearl', '{gateway:"gateways:blaze_gate_small"}')
[15:56:45] [INFO ] false
[15:56:45] [INFO ] Refunded Gate Pearl of "gateways:blaze_gate_small" to player "Emmerdog"```
#
    'gateways:blaze_gate_small'
]```
keen bough
#

gateID itself is a ResourceLocation, hence why I added the 'toString()' in my example of the item.of

#

rhino should cast it to a string I'd think to test the includes, but not sure

urban fog
#

applying .toString() didn't seem to give different results

#

and it's not that way in the pearlOfGate variable

urban fog
#

still confused on this stuf

molten crypt
urban fog
#

alright, so i got back on this, and if i .push() the gateID to the blacklist object, it works as it should on the next attempt, but .typeOf claims both are undefined