Solved Trouble using Maps

Discussion in 'Plugin Development' started by Theztc, May 25, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    Theztc

    Hi, I am trying to make a Map add a value just so it doesn't return null. Here's what I have
    Code:
    public static Map<UUID, Boolean> permissionsLevel2 = Maps.newHashMap();
    
    public void onPlayerJoin(PlayerJoinEvent event) {
            UUID u = event.getPlayer().getUniqueId();
            Player p = event.getPlayer();
            if(permissionsLevel2.containsKey(u)) {
                //Sending Messages to debug
                p.sendMessage("1");
            }else {
                p.sendMessage("2");
                permissionsLevel2.put(u, false);
            }
    
        }
    I'm trying to check if it has a value and if it doesn't I want to add it immediately. I turned off saving for the Map to test it, and when I first join after a reboot I get a "2" which is expected. It should then set my Map to false so when I relog I expect to get a 1 because it should no longer be null at that point. But I keep getting 2s every time I log in. I am not new to Java, but I've spent 2 days researching and trying different things, and it's getting frustrating :) Thanks!
     
    Last edited: May 25, 2021
  2. Offline

    davidclue

    Code:
    public static HashMap<UUID, Boolean> permissionsLevel2 = new HashMap<UUID, Boolean>();
     
  3. Offline

    KarimAKL

    @Theztc Is the field static because you want to access it from another class? If so, are you removing a key from the map anywhere in your project?
     
  4. Offline

    Theztc

    Don't those work the same? I learned hashmaps using Maps.newHashMap so that's what I've continued to use.
    I just tried that way and it's still the same.
    I originally had another class that accessed it, but I merged them. I just removed the static and I'm not removing any keys in my code. When the server reboots it deletes all the hashmaps (Because I disabled my config saving) and they all should be null. The hashmap is empty when I first log on, but when I relog it should have my UUID saved as false because I've already logged in.
     
  5. Offline

    Shqep

    They're both the same. Maps.newHashMap() recalls the HashMap() constructor anyway.

    It's true that it should have your UUID saved after the first login. Might wanna post some code updates or anything that interacts with the map variable?
     
  6. Offline

    Theztc


    I made a command to manually set it -
    Here's that code from my commands class
    Code:
    public String cmd6 = "setPermLevel2";
         
         
    if(cmd.getName().equalsIgnoreCase(cmd6)) {
        EventsClass data = new EventsClass();
        Player p = Bukkit.getPlayer(args[0]);
        String tru = "True";
        String fal = "False";
        if(args[1].equalsIgnoreCase(tru) ||
                args[1].equalsIgnoreCase(fal)) {
            Boolean bln = Boolean.parseBoolean(args[1]);
            data.setPermLevel2(p.getUniqueID, bln);
        }
    }    
    Here are my constructors in my EventsClass
    Code:
    public boolean checkPermLevel2(UUID u) {
        return permissionsLevel2.get(u);
    }
    public void setPermLevel2(UUID u, Boolean b) {
        permissionsLevel2.put(u, b);
    }
    I'll move those to a new class when I'm done debugging. Having constructors in my EC really bugs me lol.

    So now even when I manually set the HashMap it still isn't picked up by the containsKey when I log in (Still getting 2s). I'm guessing that's where my bug is? That hashmap never changes throughout any class other than the command and the onPlayerJoin. I'm so confused at this point...
     
  7. Offline

    Shqep

    @Theztc
    That's very weird then. I suggest creating a BukkitRunnable timer async, set the intervals to around 5s or so, let it keep printing out the values from the map, so you can see whether it's changed, removed or something. There are a few things to note about in your codes tho:

    This was in your commands class. Why exactly? You're creating another instance of the class, which has another instance of the map? If you want the map to be accessible by all classes, and no other instances of it can be created from constructors, make the map static.
    Code:
    EventsClass data = new EventsClass();
     
  8. Offline

    Theztc

    I've been working on this for a few weeks and I didn't understand static/nonstatic at the time most of this was written lol. I'll rework that after I get this fixed.

    So the runnable - First of all great idea! I got a 2 after reboot and then a few seconds later it was 1s! However, after relogging I got another 2 and then 1s again. So it seems my HashMap is getting cleared when I relog? I don't have like an onPlayerLeave or anything like that so I'm confused. Do Hashmaps containing UUID clear after relog? Thank you for the help so far btw
     
  9. Offline

    davidclue

    @Theztc No, they only clear after a server reloads or restarts. Also when you are putting a value in your hashmap you always use .put() but make sure that if you are changing a player's value and they are already part of the hashmap you use .replace() other than that I have no idea why your code wouldn't be working. Also, you can just directly enter strings in your code like this instead of creating a new variable
    Code:
    if(args[1].equalsIgnoreCase("true"))
    I believe your issue may be that you are creating a new instance of your hashmap and putting the value of the player into it every time. Either make a static hashmap or reevaluate any code that creates a new instance of your class.
     
  10. Offline

    Theztc

    I will do that, Thanks everyone!
     
Thread Status:
Not open for further replies.

Share This Page