#help with players inv save in tags
1 messages · Page 1 of 1 (latest)
i did this```js
player.addTag(JSON.stringify(slots:[${i}], items:[${item.typeId}]))
whats issue
like it adds a tag to each slot
Like so```js
player.addTag("slots:[${0}], items:[${item.typeId}]")
player.addTag("slots:[${1}], items:[${item.typeId}]")
player.addTag("slots:[${2}], items:[${item.typeId}]")
if you don't understand than do you have a idea to save players inv with tag
just use for loop?
for (let i = 0; i < inv.size; i++) {
let itm = inv.getItem(i)
player.addTag( i + ' ' + itm.typeId )
}```
ik but its add a tag to each slot
huh
??
what do i understand from that xd
its add to every item a tag thats is in my inv but i want it to add one tag with all items in the inv
just make a string in loop then
tag @s remove "\""
let str = ""
for (let i = 0; i < inv.size; i++) {
let itm = inv.getItem(i)
str += i + ' ' + itm.typeId + ';'
}
player.addTag( str )
ah thx that was i mean
do you know how can i get the tag than give it to the player
what tag
This is what I needed lol
its just typeId tho
str
get the tag str and add the item thats in the tag to the player
let str = player.getTags().find(v => /^\d+\s+/.test(v) )
str
.split( ';' )
.map( v => v.split(' ') )
.forEach( ([ i, id ]) => {
inv
.setItem(
+i,
new ItemStack( id )
)
} )```
ok thx i cant try it now
try it and tell me if its work
it works thx
is there a way to get also the data of the item? or is now a problem bc data is now different
idt u can get that
how
idt = i dont think
getSlot(0).data?
Permutations in item
Huh?
oh wait nvm im stupid
u can use .clone() to create an exact clone of the itemstack with all the data
though that gives you an itemstack
Hmm and how are u gonna save that?
you will probably have to do put it into json with JSON.stringify()
and JSON.parse() when you want to use it
Ig wont work
why not
Let me test
u cant stringify api objects
use for loop
function str(o)
let r = {}
for (let k in o) {
r[k] = o[k]
}
return r
}```
thats a 1 level cloner
ooo
ty
how would u do it recursively though
im guessing something like if typeof object do it again?
u can use if checks and call same function again if value is object
hm alrighty thanks
Remember Also Has One
function str(o) {
let r = {}
for (let k in o) {
if (typeof o[k] === 'object') {
r[k] = str(o[k])
} else r[k] = o[k]
}
return r
}
u gotta take care of edge cases
function str(o) {
let r
if (Array.isArray(o)) {
r = []
}
else if (typeof o == "object") {
r = {}
)
else return o;
for (let k in o) {
r[k] = str( o[k] )
}
return r
}
[LOG] [{"enchantments":{"slot":512},"typeId":"minecraft:enchantments"},{"maxDurability":1561,"damage":50,"typeId":"minecraft:durability"},{"cooldownCategory":"","cooldownTicks":0,"typeId":"minecraft:cooldown"}]
[LOG] {"typeId":"minecraft:diamond_axe","amount":1,"maxAmount":1,"isStackable":false,"type":{"id":"minecraft:diamond_axe"},"keepOnDeath":false,"lockMode":"none"}
Not gona work at all
why not? works for me lol
U cant save itemStack
.
it is bruv
Thats logs
and?
why couldnt i lmfao
alright
everything apart from custom components and canPlaceOn/canDestroy is possible @warm fox
Worked?
You saved ItemStack?
At this form.
i saved itemstack as a json then created an itemstack and set the properties from the json
Nah i dont think it would work
If u did
Can i have ur codes
?
bruh why wouldnt it work i just said it does after testing it
hold up let me record it
@warm fox https://youtu.be/_t2GLO-c0kw
Code?
im still working on enchants + why should i show you
Dont show me your whole code
no shit sherlock
Okay leave
why should i
Your choice
lol ok
Cool
How
convert object to json
save
make a json to object converter
^^
Like how should i do it
?
use this to get json
then see docs for setting values back and put that code into function
how
exactly
if you try to json stringify an api object you get an empty object {}
its a like self loop thing
its meant to clone object
ye
ye
cant replicate native objects
idk
gotta loop every keys
ye
i used similar function on globalThis object but it's too big
even async function's limits was reached
how can i load the json
make a convert like
let data = [
{ typeId: "minecraft:wool" }
]
let data2 = data.map(
v => new ItemStack(
v.typeId
)
)```
that was example
let save = []
world.events.beforeChat.subscribe(data => {
let player = data.sender
let msg = data.message
const inv = player.getComponent("minecraft:inventory").container;
let selectedSlot = inv.getItem(player.selectedSlot)
if (msg.startsWith("!save")) {
save.push(JSON.stringify(str(selectedSlot)))
console.warn(save)
}
if (msg.startsWith("!load")) {
let data2 = save.map(v=> new ItemStack(v.typeId))
console.warn(data2)
}
})
look at how save has stored values
and it didn't save can break: value
Does it work?
save yes but load no