Saving and reading items from mysql

  • 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!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

pepper82

Member
Jan 26, 2017
272
1
18
41
Hi all,
does andybody know how to store player's tool or inventory in a mysql table and later give it to him again?
I know the mysql part, but I have no clue about how to save / restore the item in a mysql table.
 
Serialize the item into a string and store it that way. When you retrieve, you can de-serialize the string back into an item. You can do this pretty simply with skript-json.

Example code (Serializing a single item):
code_language.skript:
function serializeItem(i: item) :: string:
    add {_i} to {_items::*}
    return {_items::*}'s serialized json form

function deserializeItem(s: string) :: item:
    map json {_s} to {_items::*}
    return {_items::1}

Example code 2 (Serializing a player inventory):
code_language.skript:
function serializeInventory(inventory: inventory) :: String:
    set {_size} to {_inventory}'s amount of rows * 9
    set {_c} to 0
    loop {_size} times:
        add slot {_c} of {_inventory} to {_items::*}
        add 1 to {_c}
    if "%{_inventory}%" is not "inventory of <none>":
        loop 5 times:
            set {_i} to slot {_c} of {_inventory}
            if "%{_i}%" is "<none>":
                add air to {_items::*}
            else:
                add slot {_c} of {_inventory} to {_items::*}
            add 1 to {_c}
            delete {_i}
    return {_items::*}'s serialized json form
 
function deserializedInventory(inventory: inventory, json: string, includeArmor: boolean):
    map json {_json} to {_items::*}
    set {_c} to 1
    loop ({_inventory}'s amount of rows * 9) times:
        set slot {_c}-1 of {_inventory} to {_items::%{_c}%}
        add 1 to {_c}
    if "%{_inventory}%" is not "inventory of <none>":
        if {_includeArmor} is true:
            loop 5 times:
                set slot {_c}-1 of {_inventory} to {_items::%{_c}%}
                add 1 to {_c}
 
Last edited:
Thanks for the reply. But is there any way without having to install another skript addon?
 
Status
Not open for further replies.