Solved Backpack plugin help

Discussion in 'Spigot Plugin Development' started by KrisZone, Mar 28, 2020.

  1. I'm quite new to plugin development and I am trying to create a backpack plugin.
    When I try to use the command in the server I get a NullPointerException, but I don't know why. I think the problem is in the config file when I try to initialize inven. Maybe it fails to turn serialize and deserialize the ItemStack array? The config class does it automatically, right? Can anyone help?
    Here is my code:
    Main Class:
    Code (Java):
    public class Main extends JavaPlugin {

        @Override
        public void onEnable(){
            this.getServer().getPluginCommand("getBackpack").setExecutor(new getBackpack());

            this.getServer().getPluginManager().registerEvents(new listener(), this);
        }
    }
    Config class where I created the methods that save the inventory to the config file:
    Code (Java):
    public class Config {
        static JavaPlugin plugin = JavaPlugin.getProvidingPlugin(Main.class);


        public static void saveInventory(String uuid, Inventory inv){
            ArrayList<ItemStack[]> inven = (ArrayList<ItemStack[]>)plugin.getConfig().getConfigurationSection(uuid);
            ItemStack[] items = inv.getContents();
            inven.add(items);
            plugin.getConfig().set(uuid, inven);
            plugin.saveConfig();
        }
        public static void saveInventory(String uuid, Inventory inv, int num){
            ArrayList<ItemStack[]> inven = (ArrayList<ItemStack[]>)plugin.getConfig().getConfigurationSection(uuid);
            ItemStack[] items = inv.getContents();
            inven.set(num,items);
            plugin.getConfig().set(uuid, inven);
            plugin.saveConfig();
        }
        public static void createSection(String uuid){
            ArrayList<ItemStack[]> inven = new ArrayList<ItemStack[]>();
            plugin.getConfig().set(uuid, inven);
            plugin.saveConfig();
        }

        public static int getNumberInt(String uuid){
            ArrayList<ItemStack[]> inven = (ArrayList<ItemStack[]>)plugin.getConfig().getConfigurationSection(uuid);
            return inven.size();
        }

        public static Inventory getInv(String uuid, int number){
            ArrayList<ItemStack[]> inven = (ArrayList<ItemStack[]>) plugin.getConfig().getConfigurationSection(uuid);
            Inventory inventory = Bukkit.createInventory(null, 3*9,  Chat.colorize("Backpack"));
            for(ItemStack i: inven.get(number)){
                inventory.addItem(i);
            }
            return inventory;
        }
    }
     
    Listener class:
    Code (Java):
    public class listener implements Listener {

        @EventHandler(ignoreCancelled = true)
        public void onBackpackUsage(PlayerInteractEvent event, InventoryCloseEvent event2){
            Player player = event.getPlayer();
            int num = 0;
            if(player.getItemInHand().getItemMeta().getLore().get(0).contains(Chat.colorize("&2Backpack"))){
                num = Integer.parseInt(player.getItemInHand().getItemMeta().getLore().get(1).replaceAll("[^0-9]", ""));
                player.openInventory(Config.getInv(player.getUniqueId().toString(),num));
            }
            Player player2 = (Player)event2.getPlayer();
            if(player.equals(player2)){
                Config.saveInventory(player.getUniqueId().toString(), event2.getInventory(), num);
            }
        }
    }
    BackpackItem class where I create the item that opens the backpack:
    Code (Java):
    public class BackpackItem {

        public ItemStack makeItem(Player player) {
            ItemStack item = new ItemStack(Material.CHEST, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(Chat.colorize("&2Backpack"));
            List<String> lore = new ArrayList<>();
            lore.add("Number " + Config.getNumberInt(player.getUniqueId().toString()));
            meta.setLore(lore);
            item.setItemMeta(meta);
            item.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
            return item;
        }
    }
    Backpack class where I create the inventory for the backpack:
    Code (Java):
    public class Backpack {
        public void makeInventory(Player player){
            Inventory inv = Bukkit.createInventory(player, 3 * 9, Chat.colorize("Backpack"));
            Config.saveInventory(player.getUniqueId().toString(),inv);
        }
    }
    And finally the command I use to give a player a backpack:
    Code (Java):
    public class getBackpack implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof ConsoleCommandSender){
                sender.sendMessage(ChatColor.RED + "Console can't use this command.");
            }

            Player player = (Player) sender;
            Config.createSection(player.getUniqueId().toString());
            new Backpack().makeInventory(player);
            player.getInventory().addItem(new BackpackItem().makeItem(player));

            return true;
        }
    }
     
    #1 KrisZone, Mar 28, 2020
    Last edited: Mar 28, 2020
  2. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter


    Can you please post the stacktrace?
     
  3. Code (Text):
    [INFO] KrisZone issued server command: /getbackpack

    [ERROR] null

    org.bukkit.command.CommandException: Unhandled exception executing command 'getbackpack' in plugin Backpacks v1.0.0

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:723) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1658) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1498) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.PlayerConnectionUtils$$Lambda$3073.00000000FC2EA320.run(Unknown Source) ~[?:?]

    at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeAll(SourceFile:103) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:894) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    at java.lang.Thread.run(Unknown Source) [?:?]

    Caused by: java.lang.NullPointerException

    at me.kriszone.backpacks.configuration.Config.saveInventory(Config.java:20) ~[?:?]

    at me.kriszone.backpacks.initialization.Backpack.makeInventory(Backpack.java:12) ~[?:?]

    at me.kriszone.backpacks.commands.getBackpack.onCommand(getBackpack.java:25) ~[?:?]

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.15.2.jar:git-Spigot-6de3d4b-796eb15]

    ... 18 more
     
  4. I found my problem. I don't know why I wrote .getConfigurationSection(). Closing thread.