Solved Can't reopen inventory |Listener already litening

Discussion in 'Plugin Development' started by harry_potter4567, May 15, 2016.

Thread Status:
Not open for further replies.
  1. Hi guys, I'm trying really hard to figure out a solution for this:
    Basically, I'm trying to reopen an inventory, after an anvil inventory was shown (to get text input).
    The stacktrace is really short but I can't figure out what's the problem. I allready schedule the openInventory because it is called in the InventoryCloseEvent

    Code:
    [19:14:32 WARN]: [GameDesigner] Task #4 for GameDesigner v0.0.1 generated an exc
    eption
    java.lang.IllegalArgumentException: Listener already listening
            at net.minecraft.server.v1_8_R3.Container.addSlotListener(Container.java
    :55) ~[craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at net.minecraft.server.v1_8_R3.ContainerAnvil.addSlotListener(Container
    Anvil.java:308) ~[craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at org.bukkit.craftbukkit.v1_8_R3.entity.CraftHumanEntity.openInventory(
    CraftHumanEntity.java:340) ~[craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at tk.mctechniclp.gamedesigner.gui.popupguis.PopupGUI$1.run(PopupGUI.jav
    a:32) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java
    :53) ~[craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:349) [craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:6
    79) [craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    35) [craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    28) [craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :536) [craftbukkit1.8.8.jar:git-Bukkit-880a532]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_65]
    Code in dark syntax highlighting (open)
    Code:java
    1. private InventoryView previous;
    2.  
    3. public PopupGUI(Player p, String title) {
    4. super(p, title);
    5. previous = p.getOpenInventory();
    6. }
    7.  
    8. @Override
    9. protected void show(Player p, String title) {
    10. if(previous != null) previous.close();
    11. view = p.openInventory(initInventory(title));
    12. }
    13.  
    14. @Override
    15. public void handleClose() {
    16. super.handleClose();
    17. if(previous == null) return;
    18.  
    19. Main.instance.getServer().getScheduler().scheduleSyncDelayedTask(Main.instance, new Runnable() {
    20.  
    21. @Override
    22. public void run() {
    23. previous.getPlayer().openInventory(previous); //<--- Error appears here
    24. }
    25. }, 20L);
    26. }


    Here are my listeners
    Code:java
    1. @EventHandler
    2. public void onClick(InventoryClickEvent ev) {
    3. for(GUI gui : new ArrayList<GUI>(guis)) {
    4. if(!ev.getInventory().equals(gui.getInventory())) continue;
    5. gui.onClick(ev);
    6. ev.setCancelled(true);
    7. }
    8. }
    9.  
    10. @EventHandler
    11. public void onClose(InventoryCloseEvent ev) {
    12. System.out.println("fired");
    13. for(GUI gui : new ArrayList<GUI>(guis)) {
    14. if(ev.getInventory().equals(gui.getInventory())) {
    15. gui.handleClose();
    16. }
    17. }
    18. }


    That's how the previous inventory is created

    Code:java
    1. @Override
    2. public Inventory initInventory(String title) {
    3. Inventory inv = Bukkit.createInventory(null, 27, title);
    4. return inv;
    5. }


    And here the one which is open

    Code:java
    1. @Override
    2. protected void show(Player pl, String title) {
    3. EntityPlayer p = ((CraftPlayer) pl).getHandle();
    4.  
    5. AnvilContainer container = new AnvilContainer(p);
    6.  
    7. view = container.getBukkitView();
    8. ItemStack is = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
    9. ItemMeta im = is.getItemMeta();
    10. im.setDisplayName(value);
    11. is.setItemMeta(im);
    12.  
    13. view.getTopInventory().setItem(0, is);
    14.  
    15.  
    16. int c = p.nextContainerCounter();
    17. p.playerConnection.sendPacket(new PacketPlayOutOpenWindow(c, "minecraft:anvil", new ChatMessage("Repairing"), 0));
    18.  
    19. p.activeContainer = container;
    20. p.activeContainer.windowId = c;
    21.  
    22. p.activeContainer.addSlotListener(p);
    23. }
    24.  
    25. private class AnvilContainer extends ContainerAnvil {
    26. public AnvilContainer(EntityHuman entity) {
    27. super(entity.inventory, entity.world, new BlockPosition(0, 0, 0), entity);
    28. }
    29.  
    30. @Override
    31. public boolean a(EntityHuman entityhuman) {
    32. return true;
    33. }
    34. }


    Code in light syntax highlighting (open)

    Code:
    private InventoryView previous;
    
        public PopupGUI(Player p, String title) {
            super(p, title);
            previous = p.getOpenInventory();
        }
    
        @Override
        protected void show(Player p, String title) {
            if(previous != null) previous.close();
            view = p.openInventory(initInventory(title));
        }
    
        @Override
        public void handleClose() {
            super.handleClose();
            if(previous == null) return;
       
            Main.instance.getServer().getScheduler().scheduleSyncDelayedTask(Main.instance, new Runnable() {
           
                @Override
                public void run() {
                    previous.getPlayer().openInventory(previous); //<--- Error appears here
                }
            }, 20L);
        }
    Here are my listeners
    Code:
        @EventHandler
        public void onClick(InventoryClickEvent ev) {
            for(GUI gui : new ArrayList<GUI>(guis)) {
                if(!ev.getInventory().equals(gui.getInventory())) continue;
                gui.onClick(ev);
                ev.setCancelled(true);
            }
        }
    
        @EventHandler
        public void onClose(InventoryCloseEvent ev) {
            System.out.println("fired");
            for(GUI gui : new ArrayList<GUI>(guis)) {
                if(ev.getInventory().equals(gui.getInventory())) {
                    gui.handleClose();
                }
            }
        }
    That's how the previous inventory is created

    Code:
        @Override
        public Inventory initInventory(String title) {
            Inventory inv = Bukkit.createInventory(null, 27, title);
            return inv;
        }
    And here the one which is open

    Code:
            @Override
        protected void show(Player pl, String title) {
            EntityPlayer p = ((CraftPlayer) pl).getHandle();
    
            AnvilContainer container = new AnvilContainer(p);
           
            view = container.getBukkitView();
            ItemStack is = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(value);
            is.setItemMeta(im);
           
            view.getTopInventory().setItem(0, is);
           
           
            int c = p.nextContainerCounter();
            p.playerConnection.sendPacket(new PacketPlayOutOpenWindow(c, "minecraft:anvil", new ChatMessage("Repairing"), 0));
           
            p.activeContainer = container;
            p.activeContainer.windowId = c;
           
            p.activeContainer.addSlotListener(p);
        }
    
    private class AnvilContainer extends ContainerAnvil {
            public AnvilContainer(EntityHuman entity) {
                super(entity.inventory, entity.world, new BlockPosition(0, 0, 0), entity);
            }
    
            @Override
            public boolean a(EntityHuman entityhuman) {
                return true;
            }
        }


    Thanks in advance for any help
     
    Last edited: May 16, 2016
  2. Offline

    Zombie_Striker

    You most likely registered the same class twice.
     
  3. That's not possible, im registering the listeners in the onEnable() part. I think it's an internal listener

    Code:java
    1. public static Main instance;
    2.  
    3. @Override
    4. public void onEnable() {
    5. instance = this;
    6. registerListeners();
    7. }
    8.  
    9. private void registerListeners() {
    10. Bukkit.getPluginManager().registerEvents(new VillagerListener(), this);
    11. Bukkit.getPluginManager().registerEvents(new GUIManager(), this);
    12. }
     
  4. Offline

    Zombie_Striker

    This looks like you have a sub class inside the PopupGUI class. Post line 32 of PopupGUI/ post the subclass inside PopupGUI.
     
  5. PopupGUI has no subclass and line 32 is where I made the comment
    Code:java
    1. //<--- Error appears here
    in the first code I posted
     
  6. Offline

    Zombie_Striker

    @harry_potter4567
    I highly doubt the server made a mistake.
    The $1 at the end of PopupGUI means that you have a subclass inside PopupGUI, and line 32 means the 32 line of that subclass. Please post the run method that is inside PopupGUI's subclass.
     
  7. Offline

    I Al Istannen

    @Zombie_Striker
    The subclass is the runnable. At least I would think so.

    Code:
    new Runnable() {
      @Override
      public void run() {
         previous.getPlayer().openInventory(previous); //<--- Error appears here
      }
    }
    Doesn't that create an anonymous inner class?

    @harry_potter4567 Sad you won't get the alert, but whatever. Have you tried saving the Inventory and not the InventoryView? And then opening the Inventory and not the InventoryView? Seems like the InventoryView already contains the player. You can find the error here, also I don't know what to do with it. It seems like opening the Inventory adds a Listener to the InventoryView, which won't get cleared. Later you try to open it again, creating the second similar Listener which throws the error. Just speculating at this point though. Not sure ==> Smaller font
     
    Last edited: May 17, 2016
  8. Offline

    DoggyCode™

    What is previous?
     
  9. Nice idea, Im gonna give this a try :D

    previous is an InventoryView that I saved to reopen the Inventory later

    EDIT: I expected like thousands more errors when this one is fixed but... IT JUST WORKS NOW
    I <3 u @I Al Istannen :D. Also thanks to @Zombie_Striker and @DoggyCode™ for trying to help <3 :D



    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: May 18, 2016
Thread Status:
Not open for further replies.

Share This Page