Solved HashMap- not the right usage?

Discussion in 'Plugin Development' started by WizKhalifa, Jul 11, 2013.

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

    WizKhalifa

    I am learning the bukkit-api and java in general.. Seem to have a grasp on things, but cannot seem to find an answer to this,

    I am trying to make a 1v1 plugin where when players do /1v1 they are entered in a list and when there are 2 players, but cannot teleport players after being added to the hash.

    I know how to use HashMaps but not sure if/how I would use a Set here...
    Code:java
    1. if(commandLabel.equalsIgnoreCase("1v1")){
    2. if(teams.containsKey(sender)){
    3. teams.remove(sender);
    4. sender.sendMessage("Removed from queue.");
    5. }else{
    6. teams.put((Player) sender);
    7. sender.sendMessage("Added to 1v1 queue");
    8. }if(teams.size() == 1){
    9. Location loc = new Location(((Location) sender).getWorld(), getConfig().getInt("X"), getConfig().getInt("Y"), getConfig().getInt("Z"));
    10. String players = teams.toString();
    11. }
    12.  


    Any help is appreciated!!
     
  2. Offline

    tills13

    A map is a mapping from one object to another - one object is used as the key. As far as I can tell, you don't need this...

    A List might be more what you're looking for.

    This:
    Code:
    teams.put((Player) sender);
    shouldn't even work...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  3. Offline

    slayr288

    Use a list for entries that don't have data assotiated with it.
     
  4. Offline

    WizKhalifa

    awesome, thanks! Anyone know a good tutorial on lists?
     
  5. Offline

    tills13

  6. Offline

    WizKhalifa

    okay, so this may be considered a new question, but I decided to put it here because it relates to the above..

    I used a set instead of a hashmap, and think I am heading in the right direction.. Just not sure where to go/ what's wrong with the code so far.

    Code:java
    1. public Set<String> players = new LinkedHashSet<String>();
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    4. Player player = (Player)sender;
    5. Location loc = new Location(player.getWorld(), 0, 100, 0);
    6. if(commandLabel.equalsIgnoreCase("fight")){
    7. if(players.contains(player.getName())){
    8. players.remove(player.getName());
    9. player.sendMessage("Left queue");
    10. }else{
    11. players.add(player.getName());
    12. if(players.size() >= 1){
    13. for(String s : players){
    14. Player playa = Bukkit.getPlayerExact(s);
    15. if(s == null){
    16. players.remove(s);
    17. playa.sendMessage("You are in next queue!");
    18. }else{
    19. player.teleport(loc);
    20. playa.teleport(loc);
    21. player.sendMessage("1v1 started");
    22. playa.sendMessage("1v1 started");
    23.  
    24. }
    25. }
    26. }
    27. }
    28. }
    29.  
    30. return false;
    31. }
     
  7. Offline

    tills13

    This flow right here doesn't make any sense:
    Code:
     Player playa = Bukkit.getPlayerExact(s);
                            if(s == null){
                                players.remove(s);
                                playa.sendMessage("You are in next queue!");
                            }
    I think what you meant is:

    Code:
    Player playa = Bukkit.getPlayerExact(s);
    if(playa == null){
        players.remove(s);
        // basically, that player left the game between when s/he signed up to fight and when s/he was queued.
    }
     
  8. Offline

    WizKhalifa

    ok- thanks, but it doesn't seem to solve the problem. :/
     
  9. Offline

    WizKhalifa

    anyone know why this isn't working? Been searching for awhile
     
  10. Did you register command? Put some output line in there to say what is running and what isn't.
     
  11. Offline

    WizKhalifa

    BorisTheTerrible I registered command in the plugin.yml, not sure what you mean by the output lines to show what is running or not.
     
  12. Offline

    tills13

    System.out.println every other line to see where it's failing.
     
  13. Offline

    fanaticmw2

    Where are you handling your commands? In a seperate class or in your main class?
     
  14. Offline

    WizKhalifa

    got the answer. Was using Set instead of ArrayList. thanks
     
Thread Status:
Not open for further replies.

Share This Page