Problems saving inventory, then loading inventory.

Discussion in 'Spigot Plugin Development' started by JacobUNIQUE, Aug 7, 2018.

  1. Ok, I know I have made another post about this, but there have been some revelations so it's ok. So my problem is that, I am trying to save a players inventory, switch inventories, and then reload the original inventory. I have switching inventories down, I just cant save the players inventory (which i discovered after vigorous testing). Whenever I try to access the players inventory, it is null. And I think that is because I am calling clear(). But I thought it was ok if I didn't call clear() until after I saved it, but I guess it doesn't work that way. I need a way to save my inventory, and I just can't figure out how. Here is what I have been working with,
    Code (Java):

    package xyz.phoenix.phoneix.player;

    import net.md_5.bungee.api.ChatColor;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import xyz.phoenix.phoneix.Main;
    import xyz.phoenix.phoneix.items.wands.Items;
    import xyz.phoenix.phoneix.spells.Spell;
    import java.io.File;
    import java.io.IOException;
    import java.util.*;

    public class Wizard {

        static Main main;
        static YamlConfiguration yaml;
        static List<Wizard> wizards = new ArrayList<>();

        static {
            main = Main.getInstance();
            yaml = main.getWizardConfig();
        }
        private HashMap<Spell, Integer> spells = new HashMap<>();
        private Player player;
        private Spell spokenSpell;
        private int year;
        private String name;
        private HashMap<UUID, PlayerInventory> inv = new HashMap<>();
        private Type type;
        private List<String> permissions;
        private double level;
        private Map<Spell, Integer> spellSlot = new HashMap<>();
        private boolean wandRaised = false;


        //Save type, permissions, level, UUID in config
        public Wizard(Player player) {
            this.player = player;
            this.spokenSpell = Spell.NONE;
            UUID uuid = player.getUniqueId();
            //Loading info from config into memory
            this.year = yaml.isSet(uuid + ".year") ? yaml.getInt(uuid + ".year") : 0;

            this.name = yaml.getString(uuid + ".name");
            this.permissions = yaml.getStringList(uuid + ".permissions");
            this.type = yaml.isSet(uuid + ".type") ? Type.valueOf(yaml.getString(uuid + ".type")) : Type.UNSORTED;

            this.level = yaml.isSet(uuid  + ".level") ? yaml.getDouble(uuid + ".level") : 0;
            if(permissions == null) {
                permissions.add("wizard");
            }
            wizards.add(this);

            save();
        }


        public void loadPanes() {

            this.inv.put(player.getUniqueId(), player.getInventory());
            PlayerInventory inv = player.getInventory();
            inv.clear();

            inv.addItem(Items.PANE_1.getItem());
            inv.addItem(Items.PANE_2.getItem());
            inv.addItem(Items.PANE_3.getItem());
            inv.addItem(Items.PANE_4.getItem());
            inv.addItem(Items.PANE_5.getItem());
            inv.addItem(Items.PANE_6.getItem());
            inv.addItem(Items.PANE_7.getItem());
            inv.addItem(Items.PANE_8.getItem());
            inv.addItem(Items.PANE_9.getItem());

            for(ItemStack item : inv.getContents()) {

                if(item == null) continue;
                player.sendMessage(ChatColor.RED + item.getType().name());

            }
            for(ItemStack item : this.inv.get(player.getUniqueId())) {

                if(item == null) continue;
                player.sendMessage(ChatColor.GREEN + item.getType().name());

            }

        }
        public void addPerm(String Perm) {
            permissions.add(Perm);
            save();

        }

        public void removePerm(String perm) {
            permissions.remove(perm);
            save();
        }

        public void setType(Type newType) {
            type = newType;
            save();
        }

        /**
         * Restores {@link Wizard#getPlayer()}'s original inventory after modifying panes.
         */

        public void restoreInventory() {

            player.getInventory().clear();
            for(ItemStack item : this.inv.get(player.getUniqueId())) {

                if(item == null) continue;
                player.sendMessage(ChatColor.GREEN + item.getType().name());

            }
            for(ItemStack item : inv.get(player.getUniqueId())) {

                if (item == null) {continue;}
                player.sendMessage(item.getType().name());

                    player.getInventory().addItem(item);

            }


        }

        /**
         * @param p Player you're trying to get the wizard class for.
         * @return Player's wizard object; else null.
         */

        public static Wizard getWizardByPlayer(Player p) {
            for (Wizard wizard : wizards) {
                if(wizard.getPlayer().equals(p)) return wizard;
            }
            return null;
        }
        public enum Type {
            HORNED_SERPENT,
            THUNDERBIRD,
            WAMPUS,
            UNSORTED,
            PUKWUDGIE;
        }
        public void save() {

            UUID uuid = player.getUniqueId();

            yaml.set(uuid + ".type", this.getType().name());
            yaml.set(uuid + ".year", this.year);
            yaml.set(uuid + ".level", level);
            yaml.set(uuid + ".permissions", permissions);
            yaml.set(uuid + ".name", player.getDisplayName());
            try {
                yaml.save(Main.getInstance().getDataFolder().getPath() + "/wizards.yml");

            }catch (IOException e) {
                Bukkit.broadcastMessage(org.bukkit.ChatColor.RED + "COULD NOT SAVE WIZARDS.YML");
            }

        }

        public void saveSpells() {
            UUID uuid = player.getUniqueId();

            Main.INSTANCE.getSpellConfig().set(uuid + ".spells.lumos", spells.get(Spell.LUMOS));
            try {
                Main.INSTANCE.getSpellConfig().save(Main.INSTANCE.getDataFolder().getPath() + "/spells.yml");
            }catch (IOException ex) {
                player.sendMessage(ChatColor.RED + "ERROR, could not save spells.yml, please contact server staff.");
            }


        }
        public HashMap<Spell, Integer> getSpells() {
            return spells;
        }
        public void setSpokenSpell(Spell spell) {
            spokenSpell = spell;
        }
        public int getYear() {return year;}
        public Spell getSpokenSpell() {
            return spokenSpell;
        }
        public double getLevel() {
            return level;
        }
        public void setLevel(double newLevel) {
            level = newLevel;
        }
        public List getPerms() {return permissions;}
        public Type getType() {
            return type;
        }
        public boolean isWandRaised() {
            return wandRaised;
        }
        public Player getPlayer() {
            return player;
        }
        public static List<Wizard> getWizards() {
            return wizards;
        }
        public Map<Spell, Integer> getSpellSlot() {
            return spellSlot;
        }
        public void setPlayer(Player player) {
            this.player = player;
        }
        public String getName() {
           return this.name;
        }
        public void setSpellSlot(Map<Spell, Integer> spellSlot) {
            this.spellSlot = spellSlot;
        }
        public void setWandRaised(boolean wandRaised) {
            this.wandRaised = wandRaised;
        }
        public String getStringType() {
            if(type.equals(Type.HORNED_SERPENT)) {
                return "Horned Serpent";
            }
            if(type.equals(Type.THUNDERBIRD)) {
                return "Thunderbird";
            }
            if(type.equals(Type.PUKWUDGIE)) {
                return "Pukwudgie";
            }
            if(type.equals(Type.WAMPUS)) {
                return "Wampus";
            }
            if(type.equals(Type.UNSORTED)) {
                return "Unsorted";
            }
            return "Could not get name of type.";
        }
        public String getColoredStringType() {
            if(type.equals(Type.HORNED_SERPENT)) {
                return ChatColor.BLUE + "Horned Serpent";
            }
            if(type.equals(Type.THUNDERBIRD)) {
                return ChatColor.DARK_PURPLE + "Thunderbird";
            }
            if(type.equals(Type.PUKWUDGIE)) {
                return ChatColor.DARK_RED + "Pukwudgie";
            }
            if(type.equals(Type.WAMPUS)) {
                return ChatColor.DARK_GREEN + "Wampus";
            }
            if(type.equals(Type.UNSORTED)) {
                return ChatColor.BLACK + "Unsorted";
            }
            return "Could not get name of type.";
        }


        public String getTypeColor() {
            if(type.equals(Type.HORNED_SERPENT)) {
                return ChatColor.BLUE + "";
            }
            if(type.equals(Type.THUNDERBIRD)) {
                return ChatColor.DARK_PURPLE + "";
            }
            if(type.equals(Type.PUKWUDGIE)) {
                return ChatColor.DARK_RED + "";
            }
            if(type.equals(Type.WAMPUS)) {
                return ChatColor.DARK_GREEN + "";
            }
            if(type.equals(Type.UNSORTED)) {
                return ChatColor.BLACK + "";
            }
            return ChatColor.WHITE + "";
        }
        public void loadSpells() {
            UUID uuid = player.getUniqueId();
            YamlConfiguration spellConfig = Main.getInstance().getSpellConfig();
            try {
                spellConfig.load(new File(Main.INSTANCE.getDataFolder(), "spells.yml"));
            }catch (IOException | InvalidConfigurationException ex) {

            }


            int lumos = spellConfig.isSet(uuid + ".spells.lumos") ? spellConfig.getInt(uuid+".spells.lumos") : 0;
            spells.put(Spell.LUMOS, lumos);



        }
    }
     
    There is the entire class, some people yell at me when I don't put the whole thing in so...
     
    #1 JacobUNIQUE, Aug 7, 2018
    Last edited: Aug 7, 2018
  2. I refuse to read this much code as plain text.

    Please put in in java code tags (you can edit your fist post) [CODE=Java]...[/CODE].
    This makes it much easier for all.

    EDIT: and please fix the indents.
     
  3. Use an HashMap?
    Code (Text):
      Map<UUID, ItemStack[] > inventory = new HashMap<>() ;
     
  4. Too many statics
    Did you use ms notepad
    What's that formatting
     
  5. Its the spoiler to code thing, hold on, i fixed it
     
  6. Maybe more like a:
    Code (Java):
    Map<UUID, Map<Integer, ItemStack>> inventory = new HashMap<>();
    Key of the first map is the player UUID, the key of the second is the slot id.
    https://wiki.vg/Inventory#Player_Inventory
    You can use this slot id again for #setItem(slot, item).
     
  7. You can try this as you are saving the whole inventory
    Code (Text):
    Map<UUID, Inventory> inventory = new HashMap<>();
     
  8. So, I did the hashmap thing, and it still isn't working :'(
     
  9. Would you mind to show us what you did, so we might help you out by searching for what isn't working?
     
  10. done
    *filler because this is to short*
     
  11. bump bc this is still not solved
     
  12. This right there?
    Do you expect to get your previous items as chat messages?
    And none appears?

    Try to clone the inventory, when putting it into the map.
     
    • Useful Useful x 1
  13. how does one "clone" it. I'm not a java expert obviously, or a bukkit expert.
     
  14. Ops… bukkit won't let you clone inventories.
    There's a method for all Java objects called clone(). However it's "not visible" for a bukkit inventory.

    So i guess a Map<UUID, Inventory> won't work, since the map inventory will be always the same object as the players actual inventory (or is it, what messages do you receive?).

    EDIT: That's the clone: java.lang.Object#clone
     
    • Agree Agree x 1
  15. THANK YOU SO MUCH, LIKE THREE DAYS WAS SPENT TRYING TO FIX THIS, I JUST CLONED THE ITEM STACK AND THROUGH A BUNCH OF FOR LOOPS, it is finally kinda working. You are amazing. You didn't give me the answer, but you helped me get there, which is just as good. Besides, people learn nothing through spoon feeding, so thank you