Chest Inventory Serialization (SkBee)
This is a little tutorial on how to serialize chest inventories and items using SkBee.
Main serialization function
This function will take a chest inventory and convert every item in every slot into a full nbt compound.
Main deserialization function
This function will take a serialized list of nbt compounds and reformat an inventory from serialized items.
This is pretty much all you need to actually serialize items using SkBee. If you want a version where items are stored as strings instead (for databases), you have to change the code slightly and create some extra functions.
Serialization function (strings)
Deserialization function (strings)
Want to serialize an inventory all into one string? Here is how:
Note: I'm using "^*^" as a delimiter in this case. You can choose to split by a different value, just make sure its a value that probably will never show up in an items data.
Deserialize single string (Using the function from above)
Examples:
Single string serialization/deserialization
Using compounds list
If you want a list of strings for serialized items, you'd basically use the same code as above, just make sure you have the functions formatted for strings instead of nbt compounds.
Main serialization function
function serializeInventoryContents(inv: inventory) :: objects:
loop (rows of {_inv})*9 times:
set {_i} to slot (loop-value - 1) of {_inv}
set {_contents::%loop-value%} to full nbt compound of {_i}
return {_contents::*}
loop (rows of {_inv})*9 times:
set {_i} to slot (loop-value - 1) of {_inv}
set {_contents::%loop-value%} to full nbt compound of {_i}
return {_contents::*}
This function will take a chest inventory and convert every item in every slot into a full nbt compound.
Main deserialization function
function deserializeInventoryContents(serialized: objects, inv: inventory) :: inventory:
if size of {_serialized::*}/9 = rows of {_inv}:
loop {_serialized::*}:
set {_i} to item from nbt loop-value
set {_index} to loop-index parsed as integer
set slot ({_index}-1) of {_inv} to {_i}
return {_inv}
else:
send "&c[ERROR] Gui size invalid. Serialized list is size %size of {_serialized::*}/9% while input inventory is size %rows of {_inv}%." to console
return {_inv}
if size of {_serialized::*}/9 = rows of {_inv}:
loop {_serialized::*}:
set {_i} to item from nbt loop-value
set {_index} to loop-index parsed as integer
set slot ({_index}-1) of {_inv} to {_i}
return {_inv}
else:
send "&c[ERROR] Gui size invalid. Serialized list is size %size of {_serialized::*}/9% while input inventory is size %rows of {_inv}%." to console
return {_inv}
This function will take a serialized list of nbt compounds and reformat an inventory from serialized items.
This is pretty much all you need to actually serialize items using SkBee. If you want a version where items are stored as strings instead (for databases), you have to change the code slightly and create some extra functions.
Serialization function (strings)
function serializeInventoryContents(inv: inventory) :: strings:
loop (rows of {_inv})*9 times:
set {_i} to slot (loop-value - 1) of {_inv}
set {_contents::%loop-value%} to "%full nbt compound of {_i}%"
return {_contents::*}
loop (rows of {_inv})*9 times:
set {_i} to slot (loop-value - 1) of {_inv}
set {_contents::%loop-value%} to "%full nbt compound of {_i}%"
return {_contents::*}
Deserialization function (strings)
function deserializeInventoryContents(serialized: strings, inv: inventory) :: inventory:
if size of {_serialized::*}/9 = rows of {_inv}:
loop {_serialized::*}:
set {_i} to item from nbt (nbt compound from loop-value)
set {_index} to loop-index parsed as integer
set slot ({_index}-1) of {_inv} to {_i}
return {_inv}
else:
send "&c[ERROR] Gui size invalid. Serialized list is size %size of {_serialized::*}/9% while input inventory is size %rows of {_inv}%." to console
return {_inv}
if size of {_serialized::*}/9 = rows of {_inv}:
loop {_serialized::*}:
set {_i} to item from nbt (nbt compound from loop-value)
set {_index} to loop-index parsed as integer
set slot ({_index}-1) of {_inv} to {_i}
return {_inv}
else:
send "&c[ERROR] Gui size invalid. Serialized list is size %size of {_serialized::*}/9% while input inventory is size %rows of {_inv}%." to console
return {_inv}
Want to serialize an inventory all into one string? Here is how:
function getSerializedSingleString(inv: inventory) :: string:
loop (rows of {_inv})*9 times:
set {_i} to slot (loop-value - 1) of {_inv}
set {_contents::%loop-value%} to "%full nbt compound of {_i}%"
return join {_contents::*} by "^*^"
loop (rows of {_inv})*9 times:
set {_i} to slot (loop-value - 1) of {_inv}
set {_contents::%loop-value%} to "%full nbt compound of {_i}%"
return join {_contents::*} by "^*^"
Note: I'm using "^*^" as a delimiter in this case. You can choose to split by a different value, just make sure its a value that probably will never show up in an items data.
Deserialize single string (Using the function from above)
function deserializeSingleString(serialized: string, inv: inventory) :: inventory:
return deserializeInventoryContents(split {_serialized} by "^*^", {_inv})
return deserializeInventoryContents(split {_serialized} by "^*^", {_inv})
Examples:
Single string serialization/deserialization
command /teststringserial:
trigger:
# Setup inventory
set {_inv} to chest inventory with 6 rows named "Hello"
set slot 4 of {_inv} to 1 of apple
set slot 15 of {_inv} to 1 of stone
set slot 43 of {_inv} to 1 of stone axe
# Get inventory serialized as one string
set {_contents} to getSerializedSingleString({_inv})
broadcast {_contents}
# Deserialize our string from earlier
set {_newinv} to deserializeSingleString({_contents}, chest inventory with 6 rows named "Hello")
open {_newinv} to player
trigger:
# Setup inventory
set {_inv} to chest inventory with 6 rows named "Hello"
set slot 4 of {_inv} to 1 of apple
set slot 15 of {_inv} to 1 of stone
set slot 43 of {_inv} to 1 of stone axe
# Get inventory serialized as one string
set {_contents} to getSerializedSingleString({_inv})
broadcast {_contents}
# Deserialize our string from earlier
set {_newinv} to deserializeSingleString({_contents}, chest inventory with 6 rows named "Hello")
open {_newinv} to player
Using compounds list
command /testserial:
trigger:
# Setup inventory
set {_inv} to chest inventory with 6 rows named "Hello"
set slot 4 of {_inv} to 1 of apple
set slot 15 of {_inv} to 1 of stone
set slot 43 of {_inv} to 1 of stone axe
# Get inventory serialized as nbt compounds
set {_contents::*} to serializeInventoryContents({_inv})
broadcast "%{_contents::*}%"
# Deserialize our compounds from earlier
set {_newinv} to deserializeInventoryContents({_contents::*}, chest inventory with 6 rows named "Hello")
open {_newinv} to player
trigger:
# Setup inventory
set {_inv} to chest inventory with 6 rows named "Hello"
set slot 4 of {_inv} to 1 of apple
set slot 15 of {_inv} to 1 of stone
set slot 43 of {_inv} to 1 of stone axe
# Get inventory serialized as nbt compounds
set {_contents::*} to serializeInventoryContents({_inv})
broadcast "%{_contents::*}%"
# Deserialize our compounds from earlier
set {_newinv} to deserializeInventoryContents({_contents::*}, chest inventory with 6 rows named "Hello")
open {_newinv} to player
If you want a list of strings for serialized items, you'd basically use the same code as above, just make sure you have the functions formatted for strings instead of nbt compounds.