Went from a Boolean to a Hashmap and it blew up

Discussion in 'Spigot Plugin Development' started by yhl, Apr 30, 2017.

  1. yhl

    yhl

    Dont worry it didnt actually blow up :p


    I'm working on a gui rn and I have a ton of code so I won't give you useless code, but I had a Boolean that toggled the gui on and off (and item in the gui would turn into a diamond and redstone block), but when I made it a hashmap it would change into a diamond block but wouldnt go back, and the toggle wouldnt work at all (but I'll fix that later). Heres the code that changes the block.

    (Variables: t: hashmap at: another class object)

    Code (Text):
       if(p.hasPermission("Miner.use")){
                    if(at.t.get(p) == false){
                        p.sendMessage(": " + at.t.get(p));
                        at.getItem(Material.DIAMOND_BLOCK, "&3&lON", e.getInventory(), 29);
                        at.t.put(p, true);
                        p.sendMessage(": " + at.t.get(p));
                    } else if(at.t.get(p) == true){
                        at.getItem(Material.REDSTONE_BLOCK, "&c&lOFF", e.getInventory(), 29);
                        p.sendMessage(": " + at.t.get(p));
                        at.t.put(p, false);
                        p.sendMessage(": " + at.t.get(p));
                    }
                    } else{
                        p.sendMessage(ChatColor.DARK_RED + "You do not have permission to do this.");
                        e.setCancelled(true);
                        return;
                    }
    Here's the rest of the code if you need it:

    ListenerClass:
    https://pastebin.com/CQJeQfhV

    AnimationTimer:
    https://pastebin.com/7zB8HZBx

    Miner(Main):
    https://pastebin.com/4TQbz6r2
     
  2. WAS

    WAS

    From looking at it, it seems it should function as specified. Maybe for shits n' giggles try.
    Code (Java):
    if(!at.t.get(p)){
    and
    Code (Java):
    } else if(at.t.get(p)){
     
  3. yhl

    yhl

    I did lmao but it didnt work so I changed it to see if this would work (it didnt :/)
     
  4. WAS

    WAS

    that's actually what I assumed you may have done. Was just checking hehe. By chance, are you using "Boolean" and starting with a null value by mistake?
     
  5. yhl

    yhl

    I dont think so. I initialize it in both classes
     
  6. Maybe try changing
    Code (Java):
    at.t.get(p) == false
    into
    Code (Java):
    at.t.getOrDefault(p, false) == false
    just so you know for sure you're handling null cases?
     
    • Agree Agree x 1
  7. yhl

    yhl

    Getting an error: https://pastebin.com/7b0Jjse9
     
    • Like Like x 1
  8. yhl

    yhl

  9. Well you could try it out, and I could add what you think is missing. But if you'd prefer not, then go ahead :)
     
    • Agree Agree x 1
  10. yhl

    yhl

    Alright :) and I just realized the irony in what I said (Spigot is an API xd)
     
  11. I gotta get people to try it somehow, eh?
     
  12. The error says the problem is at line 90 of AnimationTimer. Based on the pastebin, I'm guessing that line says Player p = (Player) inv.getViewers().get(0);, in which case you're assuming there's always a viewer. The error occurred because there were no viewers, so it couldn't do get(0)
     
  13. yhl

    yhl

    I get the error when I close the gui so how would I fix it
     
  14. I suggest that you go look other the basics of Java again. This isn't a bukkit problem. dealing with IndexOutOfBoundsExceptions is part of the basics of Java. You must check the length of an array/List before trying to get a value from it.
     
  15. yhl

    yhl

    I fixed it lmao. I'm back to the first problem. I printed out the boolean value and it becomes true so it should work but when I click it, it does nothing. (no errors)
     
  16. yhl

    yhl

  17. yhl

    yhl

    Does anyone know?
     
  18. yhl

    yhl

    I can't find this out and I need mental help after this (jk chill)