1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Skript Chest Inventory Serialization (SkBee)

Discussion in 'Tutorials' started by AsuDev, Feb 7, 2023.

  1. AsuDev

    VIP

    Joined:
    Jan 27, 2017
    Messages:
    244
    Likes Received:
    22
    This is a little tutorial on how to serialize chest inventories and items using SkBee.

    Main serialization function
    Code (Skript):
    1. function serializeInventoryContents(inv: inventory) :: objects:
    2.     loop (rows of {_inv})*9 times:
    3.         set {_i} to slot (loop-value - 1) of {_inv}
    4.         set {_contents::%loop-value%} to full nbt compound of {_i}
    5.     return {_contents::*}
    This function will take a chest inventory and convert every item in every slot into a full nbt compound.

    Main deserialization function
    Code (Skript):
    1. function deserializeInventoryContents(serialized: objects, inv: inventory) :: inventory:
    2.     if size of {_serialized::*}/9 = rows of {_inv}:
    3.         loop {_serialized::*}:
    4.             set {_i} to item from nbt loop-value
    5.             set {_index} to loop-index parsed as integer
    6.             set slot ({_index}-1) of {_inv} to {_i}
    7.         return {_inv}
    8.     else:
    9.         send "&c[ERROR] Gui size invalid. Serialized list is size %size of {_serialized::*}/9% while input inventory is size %rows of {_inv}%." to console
    10.     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)
    Code (Skript):
    1. function serializeInventoryContents(inv: inventory) :: strings:
    2.     loop (rows of {_inv})*9 times:
    3.         set {_i} to slot (loop-value - 1) of {_inv}
    4.         set {_contents::%loop-value%} to "%full nbt compound of {_i}%"
    5.     return {_contents::*}
    Deserialization function (strings)
    Code (Skript):
    1. function deserializeInventoryContents(serialized: strings, inv: inventory) :: inventory:
    2.     if size of {_serialized::*}/9 = rows of {_inv}:
    3.         loop {_serialized::*}:
    4.             set {_i} to item from nbt (nbt compound from loop-value)
    5.             set {_index} to loop-index parsed as integer
    6.             set slot ({_index}-1) of {_inv} to {_i}
    7.         return {_inv}
    8.     else:
    9.         send "&c[ERROR] Gui size invalid. Serialized list is size %size of {_serialized::*}/9% while input inventory is size %rows of {_inv}%." to console
    10.     return {_inv}
    Want to serialize an inventory all into one string? Here is how:
    Code (Skript):
    1. function getSerializedSingleString(inv: inventory) :: string:
    2.     loop (rows of {_inv})*9 times:
    3.         set {_i} to slot (loop-value - 1) of {_inv}
    4.         set {_contents::%loop-value%} to "%full nbt compound of {_i}%"
    5.     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)
    Code (Skript):
    1. function deserializeSingleString(serialized: string, inv: inventory) :: inventory:
    2.     return deserializeInventoryContents(split {_serialized} by "^*^", {_inv})
    Examples:

    Single string serialization/deserialization
    Code (Skript):
    1. command /teststringserial:
    2.     trigger:
    3.    
    4.         # Setup inventory
    5.         set {_inv} to chest inventory with 6 rows named "Hello"
    6.         set slot 4 of {_inv} to 1 of apple
    7.         set slot 15 of {_inv} to 1 of stone
    8.         set slot 43 of {_inv} to 1 of stone axe
    9.        
    10.         # Get inventory serialized as one string
    11.         set {_contents} to getSerializedSingleString({_inv})
    12.         broadcast {_contents}
    13.  
    14.         # Deserialize our string from earlier
    15.         set {_newinv} to deserializeSingleString({_contents}, chest inventory with 6 rows named "Hello")
    16.         open {_newinv} to player
    Using compounds list
    Code (Skript):
    1. command /testserial:
    2.     trigger:
    3.    
    4.         # Setup inventory
    5.         set {_inv} to chest inventory with 6 rows named "Hello"
    6.         set slot 4 of {_inv} to 1 of apple
    7.         set slot 15 of {_inv} to 1 of stone
    8.         set slot 43 of {_inv} to 1 of stone axe
    9.        
    10.         # Get inventory serialized as nbt compounds
    11.         set {_contents::*} to serializeInventoryContents({_inv})
    12.         broadcast "%{_contents::*}%"
    13.  
    14.         # Deserialize our compounds from earlier
    15.         set {_newinv} to deserializeInventoryContents({_contents::*}, chest inventory with 6 rows named "Hello")
    16.         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.
     

Share This Page

Loading...