#help with players inv save in tags

1 messages · Page 1 of 1 (latest)

sour furnace
#

i want it to save a player inv in a tag i need help with i bc i don't get it. Its save every inv slot with a tag

#

i did this```js
player.addTag(JSON.stringify(slots:[${i}], items:[${item.typeId}]))

limpid cosmos
#

whats issue

sour furnace
#

like it adds a tag to each slot

limpid cosmos
#

idk what that means

#

that sentence make no sense to me

sour furnace
#

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

limpid cosmos
#

just use for loop?

#
for (let i = 0; i < inv.size; i++) {
  let itm = inv.getItem(i)

  player.addTag( i + ' ' + itm.typeId )
}```
sour furnace
#

ik but its add a tag to each slot

limpid cosmos
#

huh

sour furnace
limpid cosmos
#

??

sour furnace
#

BRUh

#

now?

#

did you understand forloop make no sense

limpid cosmos
#

what do i understand from that xd

sour furnace
#

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

limpid cosmos
#

just make a string in loop then

spark jay
#

tag @s remove "\""

limpid cosmos
#
let str = ""

for (let i = 0; i < inv.size; i++) {
  let itm = inv.getItem(i)

  str +=  i + ' ' + itm.typeId + ';'
}

player.addTag( str )
sour furnace
#

ah thx that was i mean

sour furnace
limpid cosmos
#

what tag

pulsar coyote
#

This is what I needed lol

spark jay
#

its just typeId tho

sour furnace
#

get the tag str and add the item thats in the tag to the player

limpid cosmos
#
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 ) 
    )

} )```
sour furnace
#

ok thx i cant try it now

sour furnace
sour furnace
limpid cosmos
#

idt u can get that

sour furnace
#

how

limpid cosmos
#

idt = i dont think

sour furnace
#

oh

#

XD

#

good to know

spark jay
#

getSlot(0).data?

fast garden
#

.data property is removed from itemstack iirc

#

you have to use permutations

warm fox
fast garden
fast garden
#

though that gives you an itemstack

warm fox
fast garden
#

you will probably have to do put it into json with JSON.stringify()

#

and JSON.parse() when you want to use it

warm fox
#

Ig wont work

fast garden
#

why not

warm fox
#

Let me test

fast garden
#

hm empty object

#

why

limpid cosmos
#

u cant stringify api objects

fast garden
#

ahh

#

why doe

#

and is there any workaround

limpid cosmos
#

use for loop

#
function str(o)
 let r = {}
 for (let k in o) {
  r[k] = o[k]
 }
 return r
}```
#

thats a 1 level cloner

fast garden
#

ooo

#

ty

#

how would u do it recursively though

#

im guessing something like if typeof object do it again?

limpid cosmos
#

u can use if checks and call same function again if value is object

fast garden
#

hm alrighty thanks

warm fox
#

Remember Also Has One

limpid cosmos
#

yea many have one u just use they have

#

making own from scratch is bit not easy

fast garden
#
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
}
limpid cosmos
#

u gotta take care of edge cases

fast garden
#

tho wait this still doesnt save data like damage

#

nvm u can do .getComponents()

limpid cosmos
#
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
}
fast garden
#

[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"}

warm fox
#

Not gona work at all

fast garden
warm fox
fast garden
#

you can save this json though....

#

and then use it to create an itemstack

warm fox
#

Never gonna work tho

warm fox
#

Thats logs

fast garden
#

and?

warm fox
#

Try converting to item

#

U cant

#

Like never

fast garden
#

why couldnt i lmfao

warm fox
#

It

fast garden
#

alright

fast garden
#

everything apart from custom components and canPlaceOn/canDestroy is possible @warm fox

fast garden
#

yes

#

why wouldnt it

#

im just making enchants work rn

warm fox
fast garden
#

i saved itemstack as a json then created an itemstack and set the properties from the json

warm fox
#

If u did

#

Can i have ur codes

#

?

fast garden
#

bruh why wouldnt it work i just said it does after testing it

#

hold up let me record it

fast garden
#

im still working on enchants + why should i show you

warm fox
fast garden
#

no shit sherlock

warm fox
#

Okay leave

fast garden
#

why should i

warm fox
#

Your choice

fast garden
#

lol ok

fast garden
#

i can save enchants too now

#

@warm fox

warm fox
#

Cool

sour furnace
limpid cosmos
#

convert object to json

save

make a json to object converter

fast garden
#

^^

sour furnace
limpid cosmos
#

?

sour furnace
#

i forgot to write how bruh

limpid cosmos
#

then see docs for setting values back and put that code into function

#

how

fast garden
#

nope

#

u can json stringify the output of it

#

you cant json stringify api objects

spark jay
#

exactly

fast garden
#

if you try to json stringify an api object you get an empty object {}

spark jay
#

its a like self loop thing

limpid cosmos
#

its meant to clone object

spark jay
#

ye

fast garden
#

ye

spark jay
#

cant replicate native objects

limpid cosmos
#

idk

spark jay
#

gotta loop every keys

#

ye

#

i used similar function on globalThis object but it's too big

#

even async function's limits was reached

limpid cosmos
#

yea limit is 20k i think

#

of stack

sour furnace
limpid cosmos
#

make a convert like

let data = [
  { typeId: "minecraft:wool" }
]

let data2 = data.map( 
  v => new ItemStack(
    v.typeId
  ) 
)```
limpid cosmos
#

that was example

sour furnace
#
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)
    }
})
limpid cosmos
#

look at how save has stored values

sour furnace
#

and it didn't save can break: value

sour furnace
#

save yes but load no

spark jay