#Chest key

33 messages · Page 1 of 1 (latest)

ruby trail
#

Idea from https://discord.com/channels/303440391124942858/1111374764225871892

// startup_script
StartupEvents.registry('item', event => {
  event.create('loot_key')
    .maxDamage(10)
})
``````js
// server_script
BlockEvents.rightClicked('minecraft:chest', event => {
  const { item, player } = event

  if (item.id !== 'kubejs:loot_key') {
    player.tell('You must hold Loot Key to open this chest')
    event.cancel()
  } else {
    item.damageValue++
    if (item.damageValue > item.maxDamage) item.count--
  }
})
dapper canyon
#

you dont need .displayName() 😜

ruby trail
granite pollen
#

also instead of !(item.id === 'kubejs:loot_key') you can do item.id !== 'kubejs:loot_key'

brave blaze
#

this will applied to every chest, add condition to check chest requires key or not will better

tawny locust
#

item.setDamageValue(item.damageValue + 1) can be replaced with item.damageValue++ too

ruby trail
tawny locust
#

event.block.properties is a map of property name to property value

#

but... chests dont have any properties that you can just change for telling if it needs a lock or not

ruby trail
#

can I add a custom properties?

tawny locust
#

no

#

you could add nbt, but i cant remember how you work with that with blocks
event.block.nbt maybe

ruby trail
#

maybe I can use Lock entity data?

ruby trail
tawny locust
#

did you really not read my message

#

i cant remember how you work with that

placid gate
#

has anyone figured out how to get this script to work for a specific chest?

tawny locust
#

if (event.block.x != 0 || event.block.y != 64 || event.block.z != 0) return

placid gate
#

What about block nbt data?

#

Such as the custom name of a chest

tawny locust
#

yeah

#

log the nbt, see what you need to check

#

console.log(event.block.blockEntity.nbt) iirc
then event.block.blockEntity.nbt.Soemthing.Else.Name

placid gate
#

where do I use console.log(event.block.blockEntity.nbt)?

tawny locust
#

in your script

placid gate
#

Does it export a file somewhere on reload or something?

tawny locust
#

no, it logs something to the log

slate geodeBOT
#

Please send your server.txt log file.
You can find it here: minecraft/logs/kubejs/server.txt

tawny locust
#

that one

#

and it logs when that code runs

#

so when it runs depends on what event you put it in

placid gate
#
BlockEvents.rightClicked('minecraft:chest', event => {
    const { item, player } = event
    
    if (event.block.x == 63 && event.block.y == -60 && event.block.z == 190) {
        console.log(event.block.blockEntity.nbt)
        if (item.id !== 'riftrealmscore:ars_nouveau_dimension') {
            player.tell('You must hold Archwood Key to open this chest')
            event.cancel()
        } else {
            item.damageValue++
            if (item.damageValue > item.maxDamage) item.count--
            
        }
    }
})

here is the log I got
! [20:56:07] [ERR] #5: Error occurred while handling event 'BlockEvents.rightClicked': TypeError: Cannot read property "nbt" from undefined (server_scripts:lock.js#5)

tawny locust
#

make a support ticket