[Util] Saving/getting inventories from a file

Discussion in 'Resources' started by KingFaris11, Jul 31, 2013.

Thread Status:
Not open for further replies.
  1. On this page I will tell you how to save and get inventories from a file. You can modify this how ever you like, for example making it a player's inventory instead of an inventory.
    Saving the inventory: http://pastie.org/8273357
    Getting/reading the inventory: http://pastie.org/8273366

    An example of using this:
    @EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (this.saveInventoryToFile(event.getPlayer().getInventory(), this.getDataFolder(), event.getPlayer().getName())) event.getPlayer().sendMessage(ChatColor.RED + "Saved your inventory to: " + new File(this.getDataFolder(), event.getPlayer().getName() + ".invsave").getAbsolutePath());
            else event.getPlayer().sendMessage(ChatColor.RED + "Could not save your inventory.");
        } else if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
            Inventory playerInventory = this.getInventoryFromFile(new File(this.getDataFolder(), event.getPlayer().getName() + ".invsave"));
            if (playerInventory != null) event.getPlayer().openInventory(playerInventory);
            else event.getPlayer().sendMessage(ChatColor.RED + "You do not have a saved inventory!");
    I hope this helps everyone!
    Update: If you don't want it to be easy to read/edit, you can use a serializable way:
    Ty Cleere likes this.
  2. Offline

    Ty Cleere

  3. Thanks =)
    Note: Can you please stop tagging me many times :) You can just reply, I'm watching all the threads. I've got 5 notifications from you in less than 5 seconds. :p
    glen3b and Ty Cleere like this.
  4. Offline

    Ty Cleere

    Lol sorry i am used to tagging :p
  5. Offline

    Liam Allan

    I've had this bookmarked for sometime since I was about to use it.
    Now the links seem to broken and/or the code doesn't show up on the page.

    Any chance you could repost them or PM them to me?
    Thanks in advanced.
  6. Oh no, the code has gone from hastebin. I should upload the code directly here.
  7. I'm fixing the link...
    Edit: Link fixed.
  8. Offline


    You know you could just use ConfigurationSerializable... and also I think that Bukkit Inventories might already be savable directly to a FileConfiguration. config.set(path, inventory);
  9. Offline

    Ty Cleere

    KingFaris11 Hey does this also save the Armour too? Like if you are wearing it?
  10. I sort of did, for the ItemStack. Also, you might be able to save objects directly to a FileConfiguration, but you can't read them. Luckily, FileConfiguration can save and read ItemStacks so there's less trouble. It also would look neater and you can add more detail into the configuration if you use my method instead of saving the inventory directly as it probably would ignore a few details or add too much detail.
    No, this saves an "Inventory" not a "PlayerInventory". This could easily be done by modifying it to "PlayerInventory" and then adding armour in the save method.
  11. Offline

    Ty Cleere

    KingFaris11 How do i add armour to the save method? I thought that i had it but i dont :p

    Nevermind i just got it lol

    EDITEDIT: haha ok how do i set the Armour back to the player. i tho it was this but it isnt...
    1. inventory.setArmourContents(invArmour);
  12. 1. It's Edit 2, not EDITEDIT :p
    2. It's setArmorContents because Bukkit's API is American English, not English. Also, make sure invArmour is a ItemStack[] object with max 4 items inside. If there's anymore than 4 items, it probably won't work. Just use,
    Ty Cleere likes this.
  13. Offline

    Ty Cleere

    Lol i figures it was that but i tho EDITEDIT looked cool ahha
  14. xP
  15. New code! This is a way to save and load inventories without storing it to a file. The disadvantage is that if the server reloads/restarts/stops, the saved inventories will be lost. The advantage is you don't need to save files for every player if you want to use this to load and save inventories if someone is joining/leaving an arena.
  16. Offline


    KingFaris11 Could you possibly help me with this I'm trying to save a inventory thats unique to every player and that a player can have as many of these inventories as they want. PM if you can help I'm fairly new to coding with java and with the bukkit API :p, thanks cya soon
  17. Add my Skype: live:business_faris
Thread Status:
Not open for further replies.

Share This Page