Solved Remove playerUUID from HashMap?

Discussion in 'Plugin Development' started by kayc01, Nov 2, 2015.

Thread Status:
Not open for further replies.
  1. Quick question, i know i have posted a lot lately.

    I want to remove a player from a queue stored in:


    Code:
     private Map<Integer, List<UUID>> queues;
    
    public void onEnable() {
            queues = new HashMap<Integer, List<UUID>>();
            // Makes 32 queues.
            for (int i = 1; i <= 32; i++) {
                queues.put(i, new ArrayList<UUID>());
            }
        }
    My current code including the queue:


    Code:
    int gym;
                    
                      String gymArg = args[1].replace("gym", "");
                     
                      try {
    
                          // Set the gym variable to the number in arguement 1
    
                          gym = Integer.parseInt(gymArg);
    
                      } catch (NumberFormatException nfe) {
    
                          p.sendMessage(ChatColor.RED + args[1] + " is not a number!");
    
                          // Number was not a number
    
                          return true;
    
                      }
    
                      if (gym < 1 || gym > 32) {
    
                          // Number was not between 1 and 32
    
                          return true;
    
                      }
                     
                      if (args[0].equalsIgnoreCase("join")) {
                          queues.get(gym).add(p.getUniqueId());
                          p.sendMessage(ChatColor.GREEN + "Added to queue: " + ChatColor.YELLOW + ChatColor.BOLD + gym + ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour") + ChatColor.BOLD + getConfig().getString("config."+args[1]) + ChatColor.BLACK + ")"));
                          p.sendMessage(ChatColor.GREEN + "Notified gym leaders of gym"+gym+  ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour") + ChatColor.BOLD + getConfig().getString("config."+args[1]) + ChatColor.BLACK + ")" + ChatColor.GREEN + " that you are waiting to be battled!"));
                         
                          for (Player leader : Bukkit.getServer().getOnlinePlayers()) {
                         
                          if (leader.hasPermission("pixelgym."+args[1])) {
                              leader.sendMessage(ChatColor.BLUE + "A challenger has joined queue " + ChatColor.YELLOW + ChatColor.BOLD + gym + ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour") + ChatColor.BOLD + getConfig().getString("config."+args[1]) + ChatColor.BLACK + ")"));
                              leader.sendMessage(ChatColor.BLUE + "Type /gym next " +args[1] + " to teleport them to your gym.");
                          }
                        }
    
                      }
                     
                      else if (args[0].equalsIgnoreCase("leave") || (args[0].equalsIgnoreCase("quit"))) {
                          if(queues.containsKey(p.getUniqueId())){
                              queues.get(gym).remove(p.getUniqueId());
                            if (!queues.containsKey(p.getUniqueId())){
                              p.sendMessage(ChatColor.GREEN + "You have sucesfully been removed from the gym" +gym + " queue!" + ChatColor.BLACK + " (" + getConfig().getString("config.gym"+gym+"colour") + ChatColor.BOLD + getConfig().getString("config.gym"+gym) + ChatColor.BLACK + ")");
                            }
                            else {
                                queues.remove(p.getUniqueId());
                            }
                          }
                         
                        
                      }
                     
                      else if (args[0].equalsIgnoreCase("next")) {
                        if (p.hasPermission("pixelgym.leader")) {
                            if (p.hasPermission("pixelgym."+args[1])) {
                          Player playerToTeleport = null;
    
                          while (playerToTeleport == null) {
    
                              if (queues.get(gym).size() > 0) {
                                  if (settings.getData().getConfigurationSection("warps." + args[1]) == null) {
                                        p.sendMessage(ChatColor.RED + "Warp " + args[1] + " does not exist!");
                                        return true;
                                }
                                 
                                  UUID uuid = queues.get(gym).get(0);
    
                                  queues.get(gym).remove(0);
    
                                  playerToTeleport = Bukkit.getPlayer(uuid);
                                 
                                  p.sendMessage(ChatColor.GREEN + "Getting first player from queue " + ChatColor.YELLOW + ChatColor.BOLD +gym + ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym"+gym+"colour") + ChatColor.BOLD + getConfig().getString("config.gym"+gym) + ChatColor.BLACK + ")"));
    
                              } else {
    
                                  p.sendMessage(ChatColor.RED + "There are currently no people in the queue " + ChatColor.YELLOW + ChatColor.BOLD + gym + ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym"+gym+"colour")+ ChatColor.BOLD + getConfig().getString("config.gym"+gym) + ChatColor.BLACK + ")"));
    
                                  return true;
                              }
                      }      
                              
                        World w = Bukkit.getServer().getWorld(settings.getData().getString("warps." + args[1] + ".world"));
                            double x = settings.getData().getDouble("warps." + args[1] + ".x");
                            double y = settings.getData().getDouble("warps." + args[1] + ".y");
                            double z = settings.getData().getDouble("warps." + args[1] + ".z");
                            playerToTeleport.teleport(new Location(w, x, y, z));
                            playerToTeleport.sendMessage(ChatColor.GREEN + "Teleported to " + ChatColor.YELLOW + ChatColor.BOLD + args[1] + "!");
                            playerToTeleport.sendMessage(ChatColor.GREEN + "Welcome to " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour") + ChatColor.BOLD + getConfig().getString("config."+args[1]) + "!"));
                            playerToTeleport.sendMessage(ChatColor.GOLD + "----- "+getConfig().getString("config.gym"+gym)+" Gym Rules -----");
                            playerToTeleport.sendMessage("");
                            playerToTeleport.sendMessage(ChatColor.GREEN + "1) " + getConfig().getString("config.gym"+gym+"rule1"));
                             playerToTeleport.sendMessage(ChatColor.GREEN + "2) " + getConfig().getString("config.gym"+gym+"rule2"));
                             playerToTeleport.sendMessage(ChatColor.GREEN + "3) " + getConfig().getString("config.gym"+gym+"rule3"));
                             playerToTeleport.sendMessage(ChatColor.GREEN + "4) " + getConfig().getString("config.gym"+gym+"rule4"));
                             playerToTeleport.sendMessage(ChatColor.GREEN + "5) " + getConfig().getString("config.gym"+gym+"rule5"));
                     
                          //playerToTeleport.teleport(location);
                            }
                            else {
                                p.sendMessage(ChatColor.RED + "You do not have permission to open gym"+gym+"!");
                            }
                           
                   }
                        else {
                        p.sendMessage(ChatColor.RED + "You are not a gym leader, you do not have permission to do this command!");
                        }
                       
                      }
    Current code i have to make the player get removed from the queue's:


    Code:
    else if (args[0].equalsIgnoreCase("leave") || (args[0].equalsIgnoreCase("quit"))) {
                          if(queues.containsKey(p.getUniqueId())){
                              queues.get(gym).remove(p.getUniqueId());
                            if (!queues.containsKey(p.getUniqueId())){
                              p.sendMessage(ChatColor.GREEN + "You have sucesfully been removed from the gym" +gym + " queue!" + ChatColor.BLACK + " (" + getConfig().getString("config.gym"+gym+"colour") + ChatColor.BOLD + getConfig().getString("config.gym"+gym) + ChatColor.BLACK + ")");
                            }
                            else {
                                queues.remove(p.getUniqueId());
                            }
                          }
                         
                        
                      }
    I check if the player is in the queue's twice because currently they can enter multiple times.
    (How could i prevent that, if they are already in the queues.(gym) then they can't join the same queue again until they are removed.

    So yeah basically this code does not remove them at all, what do i need to do? Kind of new to HashMaps and stuff. Just dunno the correct line for checking if a UUID exists in the specific gym queue.
     
  2. Offline

    I Al Istannen

    @kayc01 You could use a Set, as that allows no duplicates. I would go for a HashSet. If the add method returns true, it was successfully added, otherwise the UUID was already in the Set. To remove a UUID use the remove method. To just get if the UUID is in the Set, use the contains method. Just remember a HashSet (just as a HashMap) requires the hashCode to be constant. That is no problem with a UUID, but for your own objects it might be.
     
  3. Hmm is there no way i can do it with what i have?
    I came up with this, just have not tested it yet:


    Code:
    if (args[0].equalsIgnoreCase("join")) {
                       
                         if (queues.get(gym).contains(p.getUniqueId())) {
                             //check if player UUID is already in gym queue.
                           
                             p.sendMessage(ChatColor.RED + "You are already in this queue, please wait to be teleported.");
                         }
                         else {
                            //if not, then add player to the queue.
                          
                          queues.get(gym).add(p.getUniqueId());
                          p.sendMessage(ChatColor.GREEN + "Added to queue: " + ChatColor.YELLOW + ChatColor.BOLD + gym + ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour") + ChatColor.BOLD + getConfig().getString("config."+args[1]) + ChatColor.BLACK + ")"));
                          p.sendMessage(ChatColor.GREEN + "Notified gym leaders of gym"+gym+  ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour") + ChatColor.BOLD + getConfig().getString("config."+args[1]) + ChatColor.BLACK + ")" + ChatColor.GREEN + " that you are waiting to be battled!"));
                        
                          for (Player leader : Bukkit.getServer().getOnlinePlayers()) {
                        
                          if (leader.hasPermission("pixelgym."+args[1])) {
                              leader.sendMessage(ChatColor.BLUE + "A challenger has joined queue " + ChatColor.YELLOW + ChatColor.BOLD + gym + ChatColor.BLACK + " (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour") + ChatColor.BOLD + getConfig().getString("config."+args[1]) + ChatColor.BLACK + ")"));
                              leader.sendMessage(ChatColor.BLUE + "Type /gym next " +args[1] + " to teleport them to your gym.");
                          }
                        }
                         }
    
                      }
                    
                      else if (args[0].equalsIgnoreCase("leave") || (args[0].equalsIgnoreCase("quit"))) {
                          if(queues.get(gym).contains(p.getUniqueId())){
                              //if player is in the gym queue, remove them
                              queues.get(gym).remove(p.getUniqueId());
                          
                              p.sendMessage(ChatColor.GREEN + "You have sucesfully been removed from the gym" +gym + " queue!" + ChatColor.BLACK + " (" + getConfig().getString("config.gym"+gym+"colour") + ChatColor.BOLD + getConfig().getString("config.gym"+gym) + ChatColor.BLACK + ")");
                            }
                          else {
                              //if player is not in a queue, tell them they are not.
                              p.sendMessage(ChatColor.RED + "You are not in queue " +gym);
                          }
                          
                            }
    Does that look okay?
    Read the comments for my changes.

    EDIT: Just tested, it works:
    [​IMG][/IMG] [​IMG][/IMG][​IMG]
     
    Last edited: Nov 2, 2015
  4. Offline

    I Al Istannen

    @kayc01 It looks correct on first sight, but I might have overlooked something. Just try it out :).
     
  5. It works fine as of now, ill update if i come across another issue :)

    Ok, i could just do with some help on this.

    When a player quits, it checks if they are in the the queue, if they are then remove them from it.
    But if they are not in the queue it gives a null error.

    Here is my code:


    Code:
    public void onLeave1(PlayerQuitEvent l) {
                Player p = l.getPlayer();
               
                UUID u = l.getPlayer().getUniqueId();
            
                if(queues.get(gym).contains(u)){
                    queues.get(gym).remove(u);
                }
                else {
                    //do nothing
                }
    Do i want to check?:

    Code:
    if (queues.get(gym) != null) {
    or


    Code:
    UUID u = l.getPlayer().getUniqueId();
             if (queues.get(gym).isEmpty()) {
                 //do nothing
             }
             else {
                if(queues.get(gym).contains(u)){
                    queues.get(gym).remove(u);
                }
                else {
                    //do nothing
                }
             }
    Or something else? Let me know.
    Also may be the case that gym is null.
    Because gym is:


    Code:
    int gym;
                    
                      String gymArg = args[1].replace("gym", "");
                     
                      try {
    
                          // Set the gym variable to the number in arguement 1
    
                          gym = Integer.parseInt(gymArg);
    
                      } catch (NumberFormatException nfe) {
    
                          p.sendMessage(ChatColor.RED + args[1] + " is not a number!");
    
                          // Number was not a number
    
                          return true;
    
                      }
    
                      if (gym < 1 || gym > 32) {
    
                          // Number was not between 1 and 32
    
                          return true;
    
                      }
    So gym is args[1] however no args are ever made on playerQuitEvent ... so not sure what i would do.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2015
  6. Offline

    RoboticPlayer

    Can you show us where you define the HashMap/HashSet/whatever you are using?
     
  7. Like here?


    Code:
     private Map<Integer, List<UUID>> queues;
    public void onEnable() {
            queues = new HashMap<Integer, List<UUID>>();
            // Makes 32 queues.
            for (int i = 1; i <= 32; i++) {
                queues.put(i, new ArrayList<UUID>());
            }
        }
    Fixed with:


    Code:
    UUID u = l.getPlayer().getUniqueId();
               for (int i = 1; i <= 32; i++) {
                if(queues.get(i).contains(u)){
                    queues.get(i).remove(u);
                }
                else {
                    //do nothing
                }
              }
    as i thought (gym) was the null. So i made an alternative to (gym) with (i) :)
     
    Last edited: Nov 2, 2015
Thread Status:
Not open for further replies.

Share This Page