TreeSet returns empty once queried in a different method

Discussion in 'Spigot Plugin Development' started by Vert3x, Apr 18, 2017.

  1. Hello, I'm creating a really simple command to "handle" enderchests "virtually" also allowing to view others' contents. There's no problem (or almost) during the whole coding but I'm encountering an issue once I get to try knowing who gets an open virtual enderchest; to do this I simply use a private TreeSet which I update into the onCommand method and query into a onInventoryClose method. So, all the methods I'm showing are in the same class and are being smoothly fired, but the set field which contains (or should do it) the players who still have an open inventory returns empty when not queried into the onCommand method. In fact when I print the set in the onCommand method it returns all its actual content (UUID's) but when I do in the onInventoryClose method it is completely empty. I don't understand what I'm missing.
    Here's some code:
    Code (Java):

    public class EnderchestCommand implements CommandExecutor, Listener {

        private Set<UUID> enderchestsViewers = new TreeSet<>();

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("EnderChest")) {
                if (sender instanceof Player) {
                    //Obviously the actual code contains furhter stuff, this is just sample
                    enderchestsViewers.add(((Player) sender).getUniqueId());
                    System.out.println(enderchestsViewers);
                    //^ = Prints a list of UUID's including any other player who performed the command previously.
                }
            }
            return true;
        }

        @EventHandler
        public void onInventoryClose(InventoryCloseEvent event) {
            UUID id = event.getPlayer().getUniqueId();
            System.out.println(enderchestsViewers);
            //^ = Prints "[]" (empty collection)
            if (enderchestsViewers.contains(id)) {
                enderchestsViewers.remove(id);
            }
        }

    }
     
    As I stated within the code, the actual code contains more stuff, but it is basically this.
    If you are wondering this, the purpose of this is actually to play the enderchest closing sound once it's closed.
     
    #1 Vert3x, Apr 18, 2017
    Last edited: Apr 18, 2017
  2. Your "other code" is most likely hindering the UUID to be added properly. I tried replicating it with the given code above, and it worked fine.
     
  3. That's strange though.. That's a really simple command.
    Edit: I just tried it using the code I wrote here; it didn't work (I added just a void method to open me whatever GUI to make me close it and still the problem persists.
     
    #3 Vert3x, Apr 18, 2017
    Last edited: Apr 18, 2017