1.15.2 Help With HashMaps

Discussion in 'Spigot Plugin Development' started by will3500, Mar 30, 2020.

  1. hello i need a help with a hashmaps, i have two hashmaps in a duel plugin.
    to store players names in duel:
    HashMap<String, String> duplastring = new HashMap<String, String>();
    and to store players:
    HashMap<Player, Player> dupla = new HashMap<Player, Player>();

    i want to use the second one to end the duel if any of the two players leave the server, im trying to use this:

    @EventHandler
    public void onDisconnect(PlayerQuitEvent quitevent) {
    Player pquit = quitevent.getPlayer();
    String pquitS = pquit.getDisplayName();
    for(int d=0; d < duplastring.size(); d++) {
    if( duplastring.containsValue(pquitS)) {
    dupla.put(pquit, null);


    }
    if( duplastring.containsKey(pquitS)) {
    dupla.put(pquit, null);
    dupla.get(pquit).sendMessage("seu oponenta saiu2");


    }
    }
    }

    how to get the oponent player of the players o leave?
    tnks
     
  2. you shoudn't use HashMap or you should put both players as key and values on the map like

    map.put(player1,player2)
    map.put(player2,player1)

    similar for removing, you should remove both
    map.remove(map.remove(playerToRemove))

    duplastring would give you inconsistecy problems:
    - player.getDisplayName() may change over time
    - you already have a map for your players, if you have a name and you want to get the player use Bukkit.getPlayer(playerName);

    what is d ? for(int d=0; d < duplastring.size(); d++)
    you shouldn't use the for at all on that anyway
     
  3. Spigot-Forums has a code-field, please use it.

    Code (Java):
    // your hashmap where you added both players in a match
    HashMap<Player,Player> hm = new HashMap<>();

    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
    // player who go offline
    Player player = event.getPlayer();
    // if the player was in a match ( in the hashmap)
    if(hm.containsKey(player)) {
    // send the player a message who left
    hm.get(player).sendMessage("your opponent : " + player.getName() + "left");
    // removes the opponent of the quittet player from the hashmap
    hm.remove(hm.get(player));
    // remove the quittet player from the hashmap
    hm.remove(player);
    }

    }
    do you try to achieve this?
     
  4. what if the player who left is the opponent(value of hashmap not key)?
    there is a better way than use hashmap?
     
  5. than the exact same would happen like in the code because this player is than the quitPlayer
     
  6. I can't figure out what you're trying to ask. Please put time into your post and use proper grammar/spelling so the community can better assist you. Also, you want to look at this thread.
     
  7. my bad, what if is the chalanger that quit?
     
  8. than would the challenger be the quitPlayer :D
     
  9. but my hashmap is <player, player> so <Chalanger, Chalanged>.
     
  10. as we told, you should add both players to the hashmap as key, and both as values

    or you need to use 2 hashmaps and check them both
    <Chalanger, Chalanged>
    <Chalanged, Chalanger>
     
  11. You add both player to one hashmap
    something like :

    PSEUDOCODE:
    Code (Java):
    // our hashmap from before
    HashMap<Player, Player> hm = new HashMap<>();
    // where you let the players go into 1 match
    onMatch{
    // put both players with each other into the hashmap
    hm.put(challenger, challenged);
    hm.put(challenged, challenger);
    }

     
     
  12. any hashmap alternatives to this problem?
     
  13. You could save the players in a file or database, but I advise against that.
    HashMaps are the best way for your problem