Solved Error.. Listener called twice?

Discussion in 'Spigot Plugin Development' started by TheSupreme, Apr 16, 2017.

  1. Code (Text):
    [18:06:48 WARN]: Failed to handle packet for /192.168.11.105:54099
    java.lang.IllegalArgumentException: Listener already listening
            at net.minecraft.server.v1_11_R1.Container.addSlotListener(Container.java:56) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.EntityPlayer.syncInventory(EntityPlayer.java:195) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.PlayerList.a(PlayerList.java:213) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.LoginListener.b(LoginListener.java:147) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.LoginListener.F_(LoginListener.java:57) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.NetworkManager.a(NetworkManager.java:233) ~[spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.ServerConnection.c(ServerConnection.java:140) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:842) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-54ec0b8-1ac133e]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    [18:06:49 INFO]: TellTale lost connection: Internal server error
    [18:06:49 INFO]: TellTale left the game
    Code (Text):
    public class Main extends JavaPlugin implements Listener {
       
        private static HashMap<String, PlayerObject> Players = new HashMap<String, PlayerObject>();
       
        @Override
        public void onEnable()
        {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
        }

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            Player player = event.getPlayer();
           
            if (!Players.containsKey(player.getName()))
                Players.put(player.getName(), new PlayerObject(player));
            player.openInventory(SetupInventory.Inventory);
        }
       
        @EventHandler
        public void InventoryManager(InventoryClickEvent event)
        {
            event.setCancelled(true);
           
            Player player = (Player) event.getWhoClicked();
            PlayerObject p = Players.get(player.getName());
           
            ItemStack itemClicked = event.getCurrentItem();
            Inventory currentInventory = event.getInventory();
           
            if (currentInventory == SetupInventory.Inventory)
            {
                switch(itemClicked.getItemMeta().getDisplayName())
                {
                    case "lol":
                        player.sendMessage("lol");
                    break;
                }
            }
        }
    }
     
     
  2. You could fix that by adding a small delay at opening the inventory

    Edit: Maybe it works also by modifying the monitor of the join event so it gets called later
    Edit 2: Modifying the EventPriority won't work
     
    #2 Phloxz, Apr 16, 2017
    Last edited: Apr 16, 2017
    • Agree Agree x 1
    • Winner Winner x 1
  3. I think your problem lies in opening an inventory for a player directly on join. There needs to be a delay. Please try this:
    Code (Java):
    public void onPlayerJoin(PlayerJoinEvent event){
        Player player = event.getPlayer();
     
        if (!Players.containsKey(player.getName()))
            Players.put(player.getName(), new PlayerObject(player));
        Bukkit.getScheduler().runTaskLater(this, new Runnable(){
            @Override
            public void run(){
                player.openInventory(SetupInventory.Inventory);
            }
        }, 10L);
    }
     
     
    • Winner Winner x 1
  4. I assume you're talking about maybe a delayed task runnable?
     
  5. Yes, but i edited my post, maybe it works also by modifying the hole join event monitor so it gets automatically ordered in another way with the integrated delay.
     
  6. Ah, yes for events to work with the player, you need to have a runnable, delayed to be specific. @Nikl did a good job of showing that. He didn't even have to use 10L tho, (half a second). You could even use 1.
     
  7. Thank you all, it worked. :)
     
  8. Why 10L. Just do 1 tick. Works fine
     
  9. I said that lol...
     
  10. Oh, I guess I didn't read that. Sorry