Load and inventory in the config.yml

Discussion in 'Spigot Plugin Development' started by KCVault, Feb 13, 2018.

  1. Here we go, I have this plugin:
    Code (Java):
    @SuppressWarnings({ "deprecation", "unchecked" })
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e)
        {
          Player localPlayer = (Player)e.getWhoClicked();
          String str2 = e.getWhoClicked().getName();
          World localWorld = localPlayer.getWorld();
         
          if (!e.getInventory().getName().equalsIgnoreCase("§2"+e.getWhoClicked().getName()+"§0's inventories")){return;}
          if ((e.getCurrentItem()==null)||(e.getCurrentItem().getType()==Material.AIR)){return;}
          //Save section.
          if (e.getSlot() == 0){
            localWorld.playSound(localPlayer.getLocation(), Sound.NOTE_PLING, 10.0F, 1.0F);
            e.setCancelled(true);
            if(localPlayer.hasPermission("potpvp.useinv.1")){
                inv1.add(localPlayer.getName());
                Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().set("players.inv.one."+localPlayer.getName(),
                        localPlayer.getInventory().getContents());
                Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().set("players.armor.one."+localPlayer.getName(),
                        localPlayer.getInventory().getArmorContents());
                Bukkit.getPluginManager().getPlugin("PotPvP").saveConfig();
                localPlayer.closeInventory();
                localPlayer.sendMessage(Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().getString("messages.save.one").replace("&", "§"));
            }else{localPlayer.sendMessage(Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().getString("messages.save.noPermissions").replace("&", "§").replace("{inv}", "1"));localPlayer.closeInventory();}
          }
          if(e.getSlot()==9||e.getSlot()==10||e.getSlot()==11||e.getSlot()==12
                  ||e.getSlot()==13||e.getSlot()==14||e.getSlot()==15||e.getSlot()==16||e.getSlot()==17){e.setCancelled(true);}
          //Load section.
          boolean bool1 = this.inv1.contains(str2);
          boolean bool2 = this.inv2.contains(str2);
          boolean bool3 = this.inv3.contains(str2);
          boolean bool4 = this.inv4.contains(str2);
          boolean bool5 = this.inv5.contains(str2);
          boolean bool6 = this.inv6.contains(str2);
          boolean bool7 = this.inv7.contains(str2);
          boolean bool8 = this.inv8.contains(str2);
          boolean bool9 = this.inv9.contains(str2);
          if (e.getSlot() == 18) {
            if(localPlayer.hasPermission("potpvp.useinv.1")){
                if (bool1){
                      localWorld.playSound(localPlayer.getLocation(), Sound.LEVEL_UP, 10.0F, 1.0F);
                      e.setCancelled(true);localPlayer.closeInventory();
                      localPlayer.getInventory().clear();
                      localPlayer.getInventory().setArmorContents(null);
                      List<ItemStack> items = (List<ItemStack>) Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().getList("players.inv.one."+localPlayer.getName());
                      localPlayer.getInventory().setContents(items.toArray(new ItemStack[items.size()]));
                      List<ItemStack> aitems = (List<ItemStack>) Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().getList("players.armor.one."+localPlayer.getName());
                      localPlayer.getInventory().setArmorContents(aitems.toArray(new ItemStack[aitems.size()]));
                      localPlayer.updateInventory();
                      localPlayer.sendMessage(Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().getString("messages.load.one").replace("&", "§"));
                    }else if (!bool1){
                          localWorld.playSound(localPlayer.getLocation(), Sound.BAT_TAKEOFF, 10.0F, 1.0F);
                          e.setCancelled(true);
                          localPlayer.closeInventory();
                          localPlayer.sendMessage("§cPlease first save the inventory §61§c!");
                    }
            }else{localPlayer.closeInventory();localPlayer.sendMessage(Bukkit.getPluginManager().getPlugin("PotPvP").getConfig().getString("messages.load.noPermissions").replace("&", "§").replace("{inv}", "1"));}
          }
    }
    and the console send me this error (
    HTML:
    [17:19:00 ERROR]: Could not pass event InventoryClickEvent to PotPvP v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_112]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_112]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112]
    Caused by: java.lang.NullPointerException
            at com.zjar84.potpvp.listeners.AnvilManager.onInventoryClick(AnvilManager.java:419) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_112]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_112]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_112]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 15 more
    ) when a player try to load the inventory. Can someone help me? :C
     
  2. I recommend learning java before you start creating plugins.

    Here's a good start https://docs.oracle.com/javase/tutorial/

    Also, your issue is extremely simple to fix (I don't know it since I can't see the line numbers). But learning java properly will lead to you also understanding how to read a stacktrace and thus solve these simple errors yourself :D
     
  3. You can think about the line 419, is this;
    Code (Java):
    localPlayer.getInventory().setContents(items.toArray(new ItemStack[items.size()]));
     
    • Funny Funny x 1
  4. The items is probably null
     
    • Agree Agree x 1

Share This Page