1.14.4 Ghost Items with Inventories

Discussion in 'Spigot Plugin Development' started by ArKeid0s, Jan 18, 2020.

  1. Hello, I have a very strange bug with inventories.
    Here are my events related to my menus :

    Code (YAML):
    public class menuHandler implements Listener {

        mainclass plugin;

        public menuHandler(mainclass mainclass) {
            mainclass = plugin;
        }

        @EventHandler
        public void onInvClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            Inventory open = event.getClickedInventory();
            ItemStack item = event.getCurrentItem();

            if(open == null) {
                return;
            }

            //Define what each item in the main menu does
            if(event.getView().getTitle().equals(menusClass.invMainMenuName)) {
                event.setCancelled(true);
                if(item == null || !item.hasItemMeta()) {
                    return;
                }

                //Create your pet
                if(item.getItemMeta().getDisplayName().equals(menusClass.petItemCrea)) {
                    player.sendMessage("§aCreate your pet");
                    //Open the pet creation menu
                    menusClass petCreationMenu = new menusClass();
                    event.setCancelled(true);
                    player.closeInventory();
                    petCreationMenu.createPetCreationMenu(player);
                }

                //Edit your pet
                if(item.getItemMeta().getDisplayName().equals(menusClass.petItemEdit)) {
                    player.sendMessage("§bEdit your pet");
                    event.setCancelled(true);
                    player.closeInventory();
                }

                //Remove your pet
                if(item.getItemMeta().getDisplayName().equals(menusClass.petRemoveItem)) {
                    //Bukkit.getServer().dispatchCommand(event.getWhoClicked(), Commands.cmd); Execute une commande
                    player.sendMessage("§dRemove your pet");
                    player.closeInventory();
                }

                //Close the menu
                if(item.getItemMeta().getDisplayName().equals(menusClass.petCloseItem)) {
                    player.closeInventory();
                }
            }

            //Define what each item in the creation menu does
            if(event.getView().getTitle().equals(menusClass.invCreaMenuName)) {
                event.setCancelled(true);
                if(item == null || !item.hasItemMeta()) {
                    return;
                }
            }
        }


    }
    As you can see events are canceled but if you look the screenshots :

    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]

    I can pick up items by shift-clicking but they are not considered as items until I open my vanilla inventory.
    I noticed that this bug appears when i use the player.closeInventory(); but also with event.getView().close();

    Can someone help me with that?

    Thanks, ArKeid0s
     
  2. From the docs:
    Also, could you elaborate what the expected behaviour is?
     
  3. I wanted to send a player message when i click on the item and then close the inventory when the item is clicked, it is working fine but when i shift click it is buggy
     
  4. welp, did you try and close the inventory one tick later as mentioned above?
     
  5. nop not for now I i quite new to plugin dev I do not know how to achieve that ^^
     
  6. Instead of player.closeInventory(), do the following:
    Code (Java):
    Bukkit.getScheduler().runTaskLater(plugin, player::closeInventory, 1L);
    This is already a somewhat advanced method (nothing too wild, but it might confuse you when you are new), so if you want to understand what is going on, use this:
    Code (Java):
    Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
    @Override
    public void run() {
            player.closeInventory();
        }
    }, 1L);
    Both are equivalent to each other, the first however is more elegant since you only need one line.
     
    • Like Like x 1
  7. Actually there is no need to use #runTaskLater with 1 tick, you can just use #runTask and the task will be run in the next tick
     
  8. To what the 1L corresponds to?
    And could you provide me the link to the docs that you gave me earlier please it is very useful
     
  9. Interesting. Are the two options equivalent otherwise? In other words, is there any difference in whether I'd call runTaskLater with 1 tick or simply runTasks?
     
  10. Not sure. They are probably equivalent.
     
  11. Okay guys thanks a lot for your help I will try that later I'll tell you if it is working ;)
     
  12. Hmm it is not working :/
     
  13. I have this error in the console

    Code (Text):
    18.01 16:13:58 [Server] Server thread/ERROR Could not pass event InventoryClickEvent to ArkPets v0.1
    18.01 16:13:58 [Server] INFO org.bukkit.event.EventException: null
    18.01 16:13:58 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:2147) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.PacketPlayInWindowClick.a(SourceFile:32) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.aZ(MinecraftServer.java:917) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:910) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:894) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:827) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]
    18.01 16:13:58 [Server] INFO Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
    18.01 16:13:58 [Server] INFO at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:443) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:176) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:167) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:131) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at org.bukkit.craftbukkit.v1_15_R1.scheduler.CraftScheduler.runTask(CraftScheduler.java:100) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    18.01 16:13:58 [Server] INFO at fr.arkeidos.arkpets.menuHandler.onInvClick(menuHandler.java:59) ~[?:?]
    18.01 16:13:58 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_232]
    18.01 16:13:58 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_232]
     
  14. well... where I put plugin, you need to insert an Instance of your Main-class (the class that extends JavaPlugin).
     
  15. Code (YAML):
    public class menuHandler implements Listener {

        mainclass plugin;

        public menuHandler(mainclass mainclass) {
            mainclass = plugin;
        }
    You are talking about that ?
     
  16. The class that extends JavaPlugin, probably mainclass, maybe not
     
  17. yes it is mainclass that extends JavaPlugin but as you can see normally my plugin variable corresponds to that no ?
     
  18. I theory, yes. The problem that you have is, that your variable is null. Try to find out where you set that and why that is null. Maybe you have passed some invalid reference into the constructor?
     
  19. I'm so fucking dumb sorry .... I found my mistake i did mainclass = plugin in the constructor instead of plugin = mainclass