Solved How to make a chest inventory that saves contents put in

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

Goose

Supporter
Nov 23, 2019
439
33
28
Hey guys! I am making a backpack plugin, and I was wondering how I could make a chest inventory that opens on right click, and saves the contents put in by a player. How could I do this?

MY CODE:


Code:
on load:
    set {_BACKPACK} to skull of player with custom nbt "{SkullOwner:{Id:"8db4195d-2f3b-4d65-a65a-2edd3124da8c",Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNDBjYjFlNjdiNTEyYWIyZDRiZjNkN2FjZTBlYWFmNjFjMzJjZDQ2ODFkZGMzOTg3Y2ViMzI2NzA2YTMzZmEifX19"}]}}}"

command /backpack give <player> <text>:
    permission: backpacks.give
    permission message: &cYou do not have permission to execute this command!
    trigger:
        if arg-2 is "XS":
            give {_BACKPACK} named "&aExtra Small Backpack" with lore "Right click me to open your backpack!"
        if arg-2 is "Small":
            give {_BACKPACK} named "&aSmall Backpack"
        if arg-2 is "Medium":
            give {_BACKPACK} named "&aMedium Backpack"
        if arg-2 is "Large":
            give {_BACKPACK} named "&aLarge Backpack"
        if arg-2 is "XL":
            give {_BACKPACK} named "&aExtra Large Backpack"
 
I was wondering this as well

I remember that a long time ago, someone did it with actual chests and spawned them very far away.

Anyway here is one way of doing it with variables.

code_language.skript:
options:
  Smallest_Backpack: XSmall


function restoreBackpack(inv: inventory, player: player):
  loop chest size of {_inv} times:
    set slot loop-number -1 of {_inv} to {Backpack::p::%{_player}%::Items::%loop-number%}
  clear {Backpack::p::%{_player}%::Items::*}

function saveBackpack(inv: inventory, player: player):
  clear {Backpack::p::%{_player}%::Items::*}
  loop chest size of {_inv} times:
    add slot loop-number -1 of {_inv} to {Backpack::p::%{_player}%::Items::*}
  clear {Backpack::p::%{_player}%::Inv}

on inventory close:
  set {_inv} to event-inventory
  if {_inv} is {Backpack::p::%player%::Inv}:
    saveBackpack({_inv}, player)

command /openBackpack:
  trigger:
    set {_inv} to chest with 1 row named "{@Smallest_Backpack}"
    set {Backpack::p::%player%::Inv} to {_inv}
    restoreBackpack({_inv}, player)
    open {_inv} for player
 
yes
I remember that a long time ago, someone did it with actual chests and spawned them very far away.

Anyway here is one way of doing it with variables.

code_language.skript:
options:
  Smallest_Backpack: XSmall


function restoreBackpack(inv: inventory, player: player):
  loop chest size of {_inv} times:
    set slot loop-number -1 of {_inv} to {Backpack::p::%{_player}%::Items::%loop-number%}
  clear {Backpack::p::%{_player}%::Items::*}

function saveBackpack(inv: inventory, player: player):
  clear {Backpack::p::%{_player}%::Items::*}
  loop chest size of {_inv} times:
    add slot loop-number -1 of {_inv} to {Backpack::p::%{_player}%::Items::*}
  clear {Backpack::p::%{_player}%::Inv}

on inventory close:
  set {_inv} to event-inventory
  if {_inv} is {Backpack::p::%player%::Inv}:
    saveBackpack({_inv}, player)

command /openBackpack:
  trigger:
    set {_inv} to chest with 1 row named "{@Smallest_Backpack}"
    set {Backpack::p::%player%::Inv} to {_inv}
    restoreBackpack({_inv}, player)
    open {_inv} for player
 
Status
Not open for further replies.