Solved Storing ItemStack not working

Discussion in 'Plugin Development' started by MinecraftBoxGut, Aug 23, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    MinecraftBoxGut

    I am trying to save the BackPack when a player closes it. Whenever I try to open it everything is null. Is this because of the HashMap type for the entry not allowing any items in the array?

    Code:
    package com.trueboxguy.backpack;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    
    import java.util.HashMap;
    import java.util.UUID;
    
    public class InventoryHandler {
        HashMap<UUID,ItemStack[]> inventories = new HashMap<>();
        Player player;
    
        /**
         * Constructor.
         *
         * @param player - The player you want to create a BackPack for.
         */
        public InventoryHandler(Player player) {
            UUID playersUUID = player.getUniqueId();
            this.player = player;
            if (!inventories.containsKey(playersUUID)) {
                inventories.put(playersUUID, new ItemStack[]{});
            }
        }
    
        /**
         * Opens the players backpack
         *
         * @return boolean - If the player has a BackPack (it should as the constructor creates a BackPack for the player)
         */
        public boolean open() {
            UUID playersUUID = player.getUniqueId();
            if (inventories.containsKey(playersUUID)) {
                Inventory inventory = Bukkit.createInventory(null, 5*9, ChatColor.GOLD + "BackPack");
                inventory.setContents(inventories.get(playersUUID));
                player.openInventory(inventory);
                return true;
            }
            return false;
        }
    
        /**
         * Updates a players BackPack
         *
         * @param player - The player who's backpack you want to update.
         * @param newInventory -  The inventory that you want to replace the player's old backpack.
         *
         * @return boolean - If the player has an inventory
         */
        public boolean set(Player player, Inventory newInventory) {
            UUID playersUUID = player.getUniqueId();
            if (inventories.containsKey(playersUUID)) {
                inventories.put(playersUUID,newInventory.getContents()); //it overwrites. See javadoc
                return true;
    
            }
            return false;
        }
    }
    
     
  2. I don't know if it will fix your problem, but use the UUID string instead of the UUID object.

    Code:
    Map<String, ItemStack[]> backpacks = new HashMap<>();
    
    String UUID = player.getUniqueId().toString();
    I'm not in my environment right now, yet, it should be something very similar to the code above.
     
  3. Offline

    MinecraftBoxGut

    I need to make it static. It was so obvious that I thought I already did it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @MinecraftBoxGut You don't need to make it static, you need to store the InventoryHandler instances in the class that is using them.
     
  5. Offline

    MinecraftBoxGut

    @timtower What I meant was that I need to make the
    HashMap inventories static. Because whenever I made a new instance there would be a new instance of the HashMap. This mean't that it wouldn't be stored.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @MinecraftBoxGut If you need to store them in that class yes, my idea was to store the hashmap in a class where there only is 1 instance.
     
  7. Offline

    MinecraftBoxGut

    @timtower many people have had that idea. The bad thing about it is that when I will make my backpack have more features and maybe run more methods. It will be harder. You're right that it would be better right now.
     
Thread Status:
Not open for further replies.

Share This Page