Help with Saving/loading inventories

Discussion in 'Spigot Plugin Development' started by ItzMeSam, May 2, 2017.

  1. Hey i don't know how to save and load inventories
    Heres my code:
    MAIN:
    Code (Text):
    package me.illegalsloth.test;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin {
       
        public void onEnable() {
            System.out.println("Enabled the TestPlugin made by IllegalSloth (DeadoMemeo)");
            Bukkit.getPluginManager().registerEvents((Listener) this, this);
            getCommand("loadinv").setExecutor(new invload);
            getCommand("saveinv").setExecutor(new saveinv);
        }

    }
     
    invload.java
    Code (Text):
    package me.illegalsloth.test;

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;

    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;

    import me.illegalsloth.test.main;

    public class invload implements CommandExecutor {

        main plugin;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player play = (Player) sender;
            play.playSound(play.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 10, 30);
            play.sendTitle("§a§lLOADED INVENTORY!", "§aSave it again with §7/saveinv");
            @SuppressWarnings("unchecked")
            public void restoreInventory(Player p) throws IOException {
                File f = new File(plugin.getDataFolder().getAbsolutePath(), p.getName() + ".yml");
                FileConfiguration c = YamlConfiguration.loadConfiguration(f);
                ItemStack[] content = ((List<ItemStack>) c.get("inventory.armor")).toArray(new ItemStack[0]);
                p.getInventory().setArmorContents(content);
                content = ((List<ItemStack>) c.get("inventory.content")).toArray(new ItemStack[0]);
                p.getInventory().setContents(content);
            }

    }
     
    saveinv.java
    Code (Text):
    package me.illegalsloth.test;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    public class saveinv implements CommandExecutor {

        main plugin;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player play = (Player) sender;
            play.sendTitle("§e§lINVENTORY SAVED", "§eUse the command &7/invload§e to load it!");
            play.playSound(play.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 10, 20);
            public void saveInventory(Player p) throws IOException {
                File f = new File(plugin.getDataFolder().getAbsolutePath(), p.getName() + ".yml");
                FileConfiguration c = YamlConfiguration.loadConfiguration(f);
                c.set("inventory.armor", p.getInventory().getArmorContents());
                c.set("inventory.content", p.getInventory().getContents());
                c.save(f);
            }
                       
            }


    }
     
    ~Illegal
     
  2. Note: I copied something i found on another thread.
     
  3. What is your end goal here? When a player logs in/out their inventory is saved/loaded.

    If the game shuts down it is also saved. Explain your end goal there may be a much easier way to do what you're trying to do.


    Sent from my iPhone using Tapatalk
     
  4. The command will save their inventory incase of a lost item. They'll be able to load it again
     
  5. Define lost item? Seems like a great way to create a player run dupe factory.

    Your code that checks inventories is going to have to do some major police work to make sure you can not get an item out of the inventory by death, dropping, lag, inventory click, inventory move, etc.


    Sent from my iPhone using Tapatalk
     
  6. No, it will clear the current inventory.
     
  7. I'm saying player A's inventory gets saved. Thus if he "loses" an item it can get reloaded. Let's say he loses it on purpose? Dies over some hoppers, stashed it in a shulker box etc.

    Then player A says oh no I've lost my enchanted gear! I'll just use the spiffy command! He invokes the command and gets a new copy of his saved items. Then he does it again, next thing you know there's enchanted armor sets running around and stashed everywhere.


    Sent from my iPhone using Tapatalk
     
  8. Meaning they cant dupe it
     
  9. Can you just tell me how :p
     
  10. You should tranform the Inventory into Base64 and when reading it out back to the Object.