#Setting items in slot with NBT data

192 messages · Page 1 of 1 (latest)

signal stump
#

I am getting server side only KubeJS stuff setup for a java server with bedrock compatibility. As you can guess, this is a nightmare.

PlayerEvents.inventoryChanged(event => {
    console.log("Inv Changed")
    event.player.tell(event.slot)
    if (event.item.id == "minecraft:iron_helmet" && event.slot == 5) {
        
    }
})

In the if statement I need to be able to set the item in helmet slot to an item (using paper for testing).
This is all to get around the fact I can't get bedrock players to be able to put non armour items in head slot

timid crowBOT
#

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

signal stump
#

This should be a pretty easy fix

#

but I'm tired and stupid so ¯_(ツ)_/¯

#

Also if there is a better way of doing this please tell me

#

because I also need to do the exact inverse when removing the item from the helmet slot

#

and I have no idea where to start with this

signal stump
#

is there a way for me to just set an item in a slot?

dreamy egret
#

event.player.headArmorItem = "mod:id"

signal stump
#

is there one I could use for other slots

#

like inv slots

#

as well

dreamy egret
#

You can use event.player.inventory.set(<index>, "mod:id")

signal stump
#

thanks

buoyant acornBOT
#

🗒️**Send the code!**🗒️
You may have an issue with a KubeJS script and you explain it to the best of your ability yet without the actual code in question we have very little to go off of in trying to assist you.

#

Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full error message/crash report/log files!

signal stump
#
PlayerEvents.inventoryChanged(event => {
    console.log("Inv Changed")
    event.player.tell(event.slot)
    if (event.item.id == "minecraft:iron_helmet" && (event.slot == 5 || event.slot == 6 || event.slot == 7 || event.slot == 8)) {
        event.player.inventory.set(event.slot, "minecraft:paper")
    }
    else if(event.item.id == "minecraft:paper" && (event.slot != 5 && event.slot != 6 && event.slot != 7 && event.slot != 8)) {
        event.player.inventory.set(event.slot, "minecraft:iron_helmet")
    }
})
signal stump
buoyant acornBOT
#

Try !help

signal stump
#

what you sent

#

so saying that doesn't work is completely valid

dreamy egret
#

You can't Insert paper into the Armor Slots. So would recommend changing that.

signal stump
#

the whole point

#

Im forcing paper into armour slots

#

xD

dreamy egret
#

Can you do this through using the /item command? Cause If Not then this May be impossible

#

Check the Wiki If you want to know how to use it.

#

Also does the Event fire

#

If you click with paper into the slot

signal stump
#

so event definitely fires

#

and the console.log and player.tell runs

#

its just it doesnt know the methdo

signal stump
dreamy egret
signal stump
#

cos as you see in the bottom one I'm trying to set everything but the armour slots

#

so I need that functionality too

#

sorry my messages took 5 mins to send for some reason

dreamy egret
dreamy egret
signal stump
#

but I replied to your method??

#

so it is known?

signal stump
dreamy egret
#

ah it's .setItem() in 1.19/1.20

signal stump
#

k thanks

#

that does not seem to be working right at all

#

idk even know what is happening

#

wait 1 sec

#

when I put the helmet on slot 6 in my hotbar becomes paper

#
PlayerEvents.inventoryChanged(event => {
    console.log("Inv Changed")
    event.player.tell(event.slot)
    if (event.item.id == "minecraft:iron_helmet" && event.slot == 5) {
        event.player.inventory.setItem(event.slot, "minecraft:paper")
    }
})
dreamy egret
#

probably .setItem doesn't support armor

signal stump
#

ye I'll try using the armour stuff just for armour

#

I just need to figure out the turning back to iron

dreamy egret
#

so you would need to use the 4 other methods for the armor and the .setItem for the rest

signal stump
#

what is the name of the other non-head slots

signal stump
dreamy egret
#

.chestArmorItem, .legsArmorItem and .feetArmorItem

signal stump
#

figured it out just before you sent that xD

#

anyway thanks

#

lemme test the rest

#

okay

#

it all works bar in the hotbar

#

items do not get changed back in the hotbar

#

is there a way for me to just change the item in the cursor instead...

#

when I pick it up

#

cos that'd work

#

just manually bodged together a fix

#
PlayerEvents.inventoryChanged(event => {
    console.log("Inv Changed")
    event.player.tell(event.slot)
    if (event.item.id == "minecraft:iron_helmet" && event.slot == 5) {
        event.player.headArmorItem = "minecraft:paper"
    } else if (event.item.id == "minecraft:iron_chestplate" && event.slot == 6) {
        event.player.chestArmorItem = "minecraft:bamboo"
    } else if (event.item.id == "minecraft:iron_leggings" && event.slot == 7) {
        event.player.legsArmorItem = "minecraft:sugar"
    } else if (event.item.id == "minecraft:iron_boots" && event.slot == 8) {
        event.player.feetArmorItem = "minecraft:wheat_seeds"
    } else if(event.item.id == "minecraft:paper" && event.slot >= 9 && event.slot <= 35) {
        event.player.inventory.setItem(event.slot, "minecraft:iron_helmet")
    } else if(event.item.id == "minecraft:paper" && event.slot >= 36 && event.slot <= 44) {
        event.player.inventory.setItem(event.slot - 36, "minecraft:iron_helmet")
    }
})
#

just going to clean the code up a bit...

#
PlayerEvents.inventoryChanged(event => {
    console.log("Inv Changed")
    event.player.tell(event.slot)
    if (event.item.id in ["minecraft:iron_helmet","minecraft:iron_chestplate","minecraft:iron_leggings","minecraft:iron_boots"]) {
        if (event.slot == 5) {
            event.player.headArmorItem = "minecraft:paper"
        } else if (event.slot == 6) {
            event.player.chestArmorItem = "minecraft:bamboo"
        } else if (event.slot == 7) {
            event.player.legsArmorItem = "minecraft:sugar"
        } else if (event.slot == 8) {
            event.player.feetArmorItem = "minecraft:wheat_seeds"
        }
    } else if(event.item.id == "minecraft:paper" ) {
        if (event.slot >= 9 && event.slot <= 35) {
            event.player.inventory.setItem(event.slot, "minecraft:iron_helmet")
        } else if(event.slot >= 36 && event.slot <= 44) {
            event.player.inventory.setItem(event.slot - 36, "minecraft:iron_helmet")
        }
    }
})
#

cleaned it up to this

#

but not it isn't working

#

what've I done wrong

#
PlayerEvents.inventoryChanged(event => {
    console.log("Inv Changed")
    event.player.tell(event.slot)
    if (["minecraft:iron_helmet","minecraft:iron_chestplate","minecraft:iron_leggings","minecraft:iron_boots"].includes(event.item.id)) {
        if (event.slot == 5) {
            event.player.headArmorItem = "minecraft:paper"
        } else if (event.slot == 6) {
            event.player.chestArmorItem = "minecraft:bamboo"
        } else if (event.slot == 7) {
            event.player.legsArmorItem = "minecraft:sugar"
        } else if (event.slot == 8) {
            event.player.feetArmorItem = "minecraft:wheat_seeds"
        }
    } else if(event.item.id == "minecraft:paper" ) {
        if (event.slot >= 9 && event.slot <= 35) {
            event.player.inventory.setItem(event.slot, "minecraft:iron_helmet")
        } else if(event.slot >= 36 && event.slot <= 44) {
            event.player.inventory.setItem(event.slot - 36, "minecraft:iron_helmet")
        }
    }
})
#

fixed it

#

in wasn't the right keyword

#

got some nightmarish code to work

buoyant acornBOT
#

Paste version of armour.js from @signal stump

signal stump
#

going to try to further clean it up

dreamy egret
signal stump
#
PlayerEvents.inventoryChanged(event => {
    function replaceInv(item) {
        if (event.slot >= 9 && event.slot <= 35) {
            event.player.inventory.setItem(event.slot, item)
        } else if(event.slot >= 36 && event.slot <= 44) {
            event.player.inventory.setItem(event.slot - 36, item)
        }
    }
    console.log("Inv Changed")
    event.player.tell(event.slot)
    if (["minecraft:iron_helmet","minecraft:iron_chestplate","minecraft:iron_leggings","minecraft:iron_boots"].includes(event.item.id)) {
        if (event.slot == 5) {
            event.player.headArmorItem = "minecraft:paper"
        } else if (event.slot == 6) {
            event.player.chestArmorItem = "minecraft:bamboo"
        } else if (event.slot == 7) {
            event.player.legsArmorItem = "minecraft:sugar"
        } else if (event.slot == 8) {
            event.player.feetArmorItem = "minecraft:wheat_seeds"
        }
    } else if(event.item.id == "minecraft:paper") {
        replaceInv("iron_helmet")
    } else if(event.item.id == "minecraft:bamboo") {
        replaceInv("iron_chestplate")
    } else if(event.item.id == "minecraft:sugar") {
        replaceInv("iron_leggings")
    } else if(event.item.id == "minecraft:wheat_seeds") {
        replaceInv("iron_boots")
    }
})
#

thats a lot better

dreamy egret
#

you can do something like

if(event.slot >= 5 && event.slot <= 8){
  [event.player.headArmorItem, event.player.chestArmorItem, event.player.legsArmorItem, event.player.feetArmorItem][event.slot - 5](event.slot, "item")
}
signal stump
dreamy egret
#

yes

#

this doesn this

signal stump
#

I can't really even tell what you are doing xD

dreamy egret
#

it indexes by the event.slot which to call

signal stump
#

oh I get it now

dreamy egret
#

we add the function to an array, then choose the one at the index whe want an call it

signal stump
#

ye I don't think the loss of readability is worth it

dreamy egret
#

yeah that's true

#

but technically you could

signal stump
#

I think I've got it to a point of compactness/readability I'm fine with

#

now for the ungodly part

dreamy egret
#

yeah

signal stump
#

I need to get it to work with nbt

#

rather than normal items xD

#

cos this is useless as is

dreamy egret
#

shouldn't be to complicated. just use Item.of

signal stump
#

ye but I now can't just check id

#

I have to check data as well

#

I'm going to use tags to distinguish stuff

dreamy egret
#

just make a check function then

#

isn't it checking for the tag if you just do: event.item == Item.of("item", "nbt")

signal stump
#

I'd rather not check all the nbt each time tho

#

and just check item tags

#

which is the annoying bit

dreamy egret
#

Item tags won't work?

#

they aren't different for nbt on an item

signal stump
#

lemme just show you what I mean

#

wait 1 sec

#

/give @p fishing_rod{tag:smith_outpost,display:{Name:'[{"text":"Smith Outpost","italic":false,"color":"gray"}]',Lore:['[{"text":"An Island with a smith\'s settlement on it, this smith could help you craft more advanced recipes!","italic":false,"color":"dark_gray"}]']}}

#

that's an item from a skyblock datapack I was making

#

I just want to check for NBT tags that's the issue

dreamy egret
#

oh i get it now

signal stump
#

and ignore all the rest

#

of the NBT

#

so I can go helmet with tag space armour etc

#

and it turns to glass when you put it on

#

and the glass makes you have water breathing etc

dreamy egret
#

"smith_outpost" in this case

signal stump
#

just a really basic example

#

but you get what I mean

signal stump
dreamy egret
#

ah, ok then you need to make a custom function

signal stump
#

Checking for certain NBT tags on items

dreamy egret
signal stump
#

k

dreamy egret
#

so it isn't reregistered constantly

signal stump
#

This is better practice and getting started for modular code

buoyant acornBOT
#

Paste version of armor.js from @signal stump

signal stump
#

also renamed it to the american spelling for consistency

#

even tho I'm British and it feels like Treason

dreamy egret
#

Something like this for the check

function checkTag(item1, item2){
  if(item1.id != item2.id) return false
  if(item1.nbt == undefined || item2.nbt == undefined) return false
  if(item1.nbt.tag != item2.nbt.tag) return false
  return true
}
signal stump
#

ooh lemme try

#

thats a pretty efficient method

#

now how do I make an item with a tag

#

to compare with

#

cos I can set item1 to event.item

#

but item2 needs to be Item.of("minecraft:paper", 1) but with a tag

dreamy egret
#

you can do: Item.of("minecraft:paper", '{"tag": "this_is_my_tag"}')

signal stump
#

does that work?

dreamy egret
#

yeah this would be item2 and item1 event.item

signal stump
#

or could I just do:

function checkTag(input, id, tag){
    if(input.id != id) return false
    if(input.nbt.tag != tag) return false
    return true
}
#

cos to me that seems more efficient

dreamy egret
#

you can also do that, but need to check that input.item is defined before the third line here

#
function checkTag(input, id, tag){
  if(input.id != id) return false
  if(input.nbt == undefined) return false
  if(input.nbt.tag != tag) return false
  return true
}
signal stump
#

you sure I need to check cos surely the 3rd line of the function would return false if it was undefined anyway?

dreamy egret
#

no it would error cause it cannot call method/property of undefined

signal stump
#

ah k

dreamy egret
#

you need to check if nbt is defined

signal stump
#

k

#

just trying to figure out how to implement that function into it

#

okay so I've got this, now I just need to actually set the items to have that tag in the first place

buoyant acornBOT
#

Paste version of armor.js from @signal stump

signal stump
#

(had to switch to sending file as it got too big for discord)

#

using space suit as a test

#

Setting items in slot with NBT data

#

I need to switch this so instead of settings items without data it sets items with that NBT tag and also all of the other data

buoyant acornBOT
#

Paste version of armor.js from @signal stump

signal stump
#

well the items only need the tag, custom name, lore and modeldata

#

the actual paper that gets equipped also needs a bunch of stats like protection value

#

so that'll be fun to figure out

#

and Ideally I need to make a function for all of that

#

or multiple functions

left pivot
mild fulcrum
#
function checkTag(item, id, tag) {
  return (
    Ingredient.of(id).test(item) && 
    item?.nbt?.tag.includes(tag)
  );
}
signal stump
#

Is there any easy way to add NBT to an item in KubeJS

dreamy egret
#

yes: Item.of("mod:id", "nbtjsonhere")

#

or wdym

signal stump
#

oh okay that's easy enough

#

I really shouldn't be doing this at 8pm my brain is dead

signal stump
buoyant acornBOT
#

Paste version of message.txt from @signal stump

signal stump
buoyant acornBOT
#

Paste version of armor.js from @signal stump

signal stump
#

that throws this error when I have glass in inventory

#

it doesn't throw the error if I just remove the NBT data I'm trying to add to the leather helmet

#

it doesn't like some of the data but I'm not sure what exactly is causing it

#

Nevermind ran the stuff through a command block then used /kubejs hand on it and now it works

#

must be some slight difference

#

think it had to do with escape characters

#

all that matter is it works now though