Errors retrieving itemstacks from config. I can save itemstacks but not read them.

Discussion in 'Plugin Development' started by nickdeveloper, Jan 21, 2019.

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

    nickdeveloper

    Hello,

    The purpose of this plugin is virtual chests. A player runs /vc and they can store their items similar to how an ender chest works. I will paste my code and the error that comes up when I run it.

    Code:
    package io.github.**********.VChestReloaded;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map.Entry;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class VChestReloaded extends JavaPlugin {
        HashMap<Player, Inventory> inventories = new HashMap<Player, Inventory>();
      
        @Override
        public void onEnable() {
            getLogger().info("VChestReloaded started");
          
            // read config values in to inventories HashMap
          
        }
      
        public void writeInventory(Player P) {
            for (int i = 0; i < inventories.get(P).getSize(); i++)  {
                ItemStack currentItem = inventories.get(P).getItem(i);
                // itemList.add(currentItem);
                getConfig().set(P.getName() + "." + i, currentItem);;
            }
        }
      
        public void readInventory(Player P) {
            // this should update the hashmap with the most recent values stored in the config
            List<ItemStack> items = new ArrayList<ItemStack>();
          
            int counter = 0;
            for (int i = 0; i < getConfig().getList(P.getName()).size(); i++) {
                items.add(((ItemStack) getConfig().getList(P.getName()).get(counter)));
                counter++;
            }
    
            Inventory inv = Bukkit.createInventory(null, 54);
            inv.setStorageContents((ItemStack[]) items.toArray());
          
            inventories.put(P, inv);
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player P = (Player) sender;
          
            if (cmd.getName().equalsIgnoreCase("vc")) {
                P.sendMessage(ChatColor.GRAY + "Opening VChest.");
              
                if (inventories.get(P) != null) {
                    readInventory(P);
                  
                    P.openInventory(inventories.get(P));
                  
                    writeInventory(P);
                    saveConfig();
                    reloadConfig();
                    return true;
                  
                } else {
                    // in the case that a player doesn't have a vchest, we will make one
                    Inventory inv = Bukkit.createInventory(null, 54, P.getName() + "'s Virtual Chest");
                    inventories.put(P, inv);
                    P.openInventory(inventories.get(P));
                  
                    writeInventory(P);
                    saveConfig();
                    reloadConfig();
                  
                    getLogger().info("Saved " + P.getName() + " vchest.");
                    return true;
                }
            } else if (cmd.getName().equalsIgnoreCase("getinventories")) {
                for (Entry<Player, Inventory> entry : inventories.entrySet()) {
                    P.sendMessage(entry.getKey().getName() + " : " + entry.getValue().getSize());
                    return true;
                }
            }
          
            return true;
        }
      
        @Override
        public void onDisable() {
            getLogger().info("VChestReloaded stopped");
        }
      
    }
    
    Error:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'vc' in plugin VChestReloaded v1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:138) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:682) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PlayerConnection.handleCommand(PlayerConnection.java:1579) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:1439) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(SourceFile:37) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(SourceFile:9) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.PlayerConnectionUtils.a(SourceFile:10) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514) [?:?]
            at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
            at net.minecraft.server.v1_13_R2.SystemUtils.a(SourceFile:199) [craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:838) [craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:382) [craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:791) [craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:694) [craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            at java.lang.Thread.run(Thread.java:844) [?:?]
    Caused by: java.lang.NullPointerException
            at io.github.**********.VChestReloaded.VChestReloaded.readInventory(VChestReloaded.java:41) ~[?:?]
            at io.github.**********.VChestReloaded.VChestReloaded.onCommand(VChestReloaded.java:60) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.13.2.jar:git-Bukkit-a5b9c7b]
            ... 15 more
    
    When I run it without using readInventory() to get the itemstacks, the plugin works, but it resets the hashmap on a reload. I want to read and write to the config every time a player uses this command. But I can't read from the config every time, as it throws this error. Only can write. When it works, this is what the config looks like:

    Code:
    airpod_salesman:
      '0':
        ==: org.bukkit.inventory.ItemStack
        v: 1631
        type: COBBLESTONE
        amount: 32
      '1':
        ==: org.bukkit.inventory.ItemStack
        v: 1631
        type: COBBLESTONE
        amount: 32
    
    With the error causing readInventory(), the config simply looks like this:
    Code:
    airpod_salesman: {}
    
    Please comment if you have any questions to ask about it. I have been pulling my hair out from 2 AM to 5 AM trying to fix this one bug and none of my solutions work.

    Regards
     
  2. Online

    timtower Administrator Administrator Moderator

    @nickdeveloper 1. Sleep is more important that a Bukkit plugin, regardless of the situation.
    2. Use UUID's for the map instead of Player objects.
    3. Bukkit has getItemStack, might be of some use for you.
     
    nickdeveloper likes this.
  3. Offline

    nickdeveloper

    I know it has GetItemStack, I use the Java docs for bukkit. Using GetItemStack did not work in the numerous ways I tried it.

    I don't care about the UUID issue at the moment, just want to build this prototype.
     
  4. Online

    timtower Administrator Administrator Moderator

    The first issue is that you are getting a list, there is no list.
     
    nickdeveloper likes this.
Thread Status:
Not open for further replies.

Share This Page