Solved Inventory Holder

Discussion in 'Spigot Plugin Development' started by astaspasta, Apr 25, 2017.

Thread Status:
Not open for further replies.
  1. Hello guys i had a problem trying to create an private inventory menu...
    Well i want the inventory to be private to each player so each player will store his items but when i tried to do this i broke my whole plugin!

    i used this
    Code (Text):
    InventoryHolder player = inv.getHolder();
            inv = Bukkit.getServer().createInventory(inv.getHolder(), 9, ChatColor.GRAY + "[" + ChatColor.GOLD + ChatColor.MAGIC + "!" + ChatColor.GRAY + "] " +  ChatColor.GOLD + "Poor Backpack" + ChatColor.GRAY + " [" + ChatColor.GOLD + ChatColor.MAGIC + "!" + ChatColor.GRAY + "]");
    and not only it is not working but breaks my plugin everytime i do this...
    any ideas?
     
  2. Could you share your error log?
     
  3. Oh.. @PROgrm_JARvis i forgot to upload the error log... and i just turned my pc off and i cant turn it on again right now... anyway the error would not appear when i right click with the item but when it starts and when i run a command... i will send the error log tomorrow... sr
     
  4. You need to post your entire method for me to help you out here. All I see is you trying to use an inventoryHolder from somewhere in space.
     
    • Funny Funny x 1
  5. lol here:


    Code (Text):
    package me.xxastaspastaxx;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.Plugin;

    public class PoorBackpack implements Listener {

        private Inventory inv;
     
        public PoorBackpack(Plugin p) {

            inv = Bukkit.getServer().createInventory(inv.getHolder(), 9, ChatColor.GRAY + "[" + ChatColor.GOLD + ChatColor.MAGIC + "!" + ChatColor.GRAY + "] " +  ChatColor.GOLD + "Poor Backpack" + ChatColor.GRAY + " [" + ChatColor.GOLD + ChatColor.MAGIC + "!" + ChatColor.GRAY + "]");
         
         
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
        }
     

        public void show(Player p) {
            p.openInventory(inv);
        }
     
     
    }
     
    Code (Text):
    [13:33:19 ERROR]: Error occurred while enabling xBackpacks v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at me.xxastaspastaxx.PoorBackpack.<init>(PoorBackpack.java:17) ~[?:?]
            at me.xxastaspastaxx.Main.onEnable(Main.java:20) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:271) ~[craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugin(CraftServer.java:361) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.enablePlugins(CraftServer.java:323) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.reload(CraftServer.java:729) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.Bukkit.reload(Bukkit.java:549) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:629) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1295) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1155) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            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_11_R1.SystemUtils.a(SourceFile:46) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:695) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:360) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:650) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:554) [craftbukkit-1.11.2.jar:git-Bukkit-2453830]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112]

    The error appears only when the plugin starts and when i try to run another command of this plugin
     
    #5 astaspasta, Apr 26, 2017
    Last edited: Apr 26, 2017
  6. Let me point out the issue

    InventoryHolder player = inv.getHolder();
    inv = Bukkit.getServer().createInventory(inv.getHolder(), 9, ChatColor.GRAY + "[" + ChatColor.GOLD + ChatColor.MAGIC + "!" + ChatColor.GRAY + "] " + ChatColor.GOLD + "Poor Backpack" + ChatColor.GRAY + " [" + ChatColor.GOLD + ChatColor.MAGIC + "!" + ChatColor.GRAY + "]");
     
  7. You're grabbing a player from a non-existant inventory, which you then create after getting that null player.

    Java uses basic common sense. One step before the next.
     
    • Useful Useful x 1
  8. Did it work? If so, tell us how you did it for anyone else who has the same issue please! :)
     
  9. i am not going to test it now i have to do something rly important
     
  10. Well... I didn't solve it... i just need to create private inventories (different inventories for each player)
    and i tried what you said but it had no effect... anyway i will keep searching... i am sure its too easy but i can't find it
     
  11. This might work out for you @xXastaspastaXx.
    Code (Text):
    package me.xxastaspastaxx;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.Plugin;

    public class PoorBackpack {

    public static Inventory backpackInventory(Player player) {
            Inventory inv = Bukkit.createInventory(player, 9, /*your inventory name*/);
            return inv;
        }
        public void showInventory(Player p) {
            p.openInventory(backpackInventory);
        }
    }
    By the way, is this your main class? ...because you were trying to register a listener with a class and a player (do you know how to use a listener?) in it.
     
    • Agree Agree x 1
    • Funny Funny x 1
  12. @_diam Umm i think the code you gaveme wont work because i tried doi that and yeah... its not my main class and i am usin the public class when i tried adding player i also added it on h main class but idk what should i use there... i don't wont it to be null because anyone will have the sane inv but i tried adding a string to the player but it needs to be inventory holder...
     
  13. He has literally given you everything you need, "its not my main class and i am usin the public class" you can still access the ShowInventory method either by adding static to showInventory or make an object reference to access the method (classname name = new classname(); ). It returns as inv, so it wont be null and no they wont have the same inventory with this method.
     
    • Agree Agree x 1
  14. You get null cuz you want to register events which you dont even have
     
    • Like Like x 1
  15. The issue isn't the code I gave you. It's your lack of basic skills and knowledge in Java. Make sure you learn Java before you attempt plugins.
     
    • Agree Agree x 1

  16. you could do:

    Code (Java):

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.Plugin;

    public class PoorBackpack {

         private UUID uuid;
         private Inventory inv;

    public PoorBackpack(UUID uuid) {
            this.uuid = uuid;
            this.inv = Bukkit.createInventory(player, 9, /*your inventory name*/);
        }

        public void showInventory() {
            if (Bukkit.getPlayer(uuid) == null) {
                return;
            }
            Bukkit.getPlayer(uuid).openInventory(this.inv);
        }
    }
     
    and then store the objects somewhere
     
    • Like Like x 2
    • Agree Agree x 2
    • Informative Informative x 1
Thread Status:
Not open for further replies.