Friend System

Discussion in 'Plugin Development' started by TerroDoor, Sep 7, 2020.

  1. Offline

    TerroDoor

    I have a friend system I’m working on and it allows players to have friends and send friend requests.
    In saying this, I use a HashMap<UUID,Set<UUID>> for storing both requests and friends and upon requests to store the target and player into the map along with target and player into the set.
    The request part works but I can accept the target players requests, where it should be the target player accepting the request from the player.
    Also, if a player has two incoming requests, then accepts one, the second request dissapears


    Sent from my iPhone using Tapatalk
     
  2. Online

    timtower Moderator Moderator

    @TerroDoor Could also use a list of pair<uuid, uuid>
    And based on what little information you shared: you are removing the key from the hashmap.
     
  3. Offline

    TerroDoor

    @timtower Thanks for the quick response, i have a hastebin with my cmd class attached, i also check for if a player has no friends/isnt in the friends Map and it seems to work, i think the set clears the other entries when i add a new player
    https://hastebin.com/hucojenobi.cs
    Also, I have the main instance for when i implement a request timer incase you're wondering
     
  4. Online

    timtower Moderator Moderator

    @TerroDoor Line 49, you are making a new Set while you should have reused the old one.
    Same for 79 and 105
     
  5. Offline

    TerroDoor

    @timtower, thankyou i have updated the line 49 to:
    Code:
     Set<UUID> set = request.get(p.getUnique()); 
    However, i use this else statement in the request cmd:
    Code:
                if (request.containsKey(p.getUniqueId())) {
    that checks if the player is in the request Map, and the else statement from that contains this:
    Code:
    }else{
                            Set<UUID> set = new HashSet<>();
                            set.add(target.getUniqueId());
                            request.put(p.getUniqueId(), set);
                            p.sendMessage("request sent!");
                            target.sendMessage("request recieved!");
    should i be using the same set if the player isnt in one?

    @timtower i think using new HashSet is fine for when the request Map doesnt contain the player, it seems like the players only become friends when both players have accepted a request from eachother. Also, i cant accept requests from a player i can only accept the request thru the player that sent it. it's abit confusing..
    another bug is when i use 3 accounts and a player1 becomes friends with player2, then becomes friends with player3, player2 is no longer player1's friend..
    updated class: https://hastebin.com/eduxigasim.cs
     
    Last edited: Sep 8, 2020
  6. Online

    timtower Moderator Moderator

    @TerroDoor If there s a set then you should use that one, if not then you make one.
    Code:
    Set<UUID> set;
    if (request.containsKey(p.getUniqueId())) {
       set = request.get(p.getUniqueID();
    }else{
       set = new HashSet<UUID>();
    }
    if (request.get(p.getUniqueId()).contains(target.getUniqueId())) {
       p.sendMessage("you already have a pending request with this player!");
    } else {
       set.add(target.getUniqueId());
       request.put(p.getUniqueId(), set);
    
       p.sendMessage("request sent!");
       target.sendMessage("request recieved!");
    }
    Use that system for all.

    What it does: get the old set if one exists, make one if there isn't one.
     
  7. Offline

    TerroDoor

    @timtower Thanks, i appreicate the help!
    ive implemented it and all, however when i goto add a player this line is throwing a NPE
    Code:
    if (request.get(p.getUniqueId()).contains(target.getUniqueId())) {
    it's because the player isnt in the request Map when they send this request
    would i need to check the set itself and use set.contains?
     
  8. Online

    timtower Moderator Moderator

    @TerroDoor Yeah, check the set itself, missed that one.
     
  9. Offline

    TerroDoor

    Thank you, can the Set variable be a private class field or should they be just method fields

    @timtower also, the problem I’m having at the moment is
    - the player can only have one friend at a time, ever new friend overrides the old one
    - the player that sends the request has to accept the request, when the target player should be the one accepting the request

    Appreciate your time
    Sent from my iPhone using Tapatalk
     
  10. Online

    timtower Moderator Moderator

    @TerroDoor If you make them a private field then everybody will use the same one...
    Don't think that you want that.

    Scope should be as small as possible.
     
    TerroDoor likes this.
  11. Offline

    TerroDoor

    Thanks, can you help me with my current problems I listed them above and I’m trying sure where it’s wrong


    Sent from my iPhone using Tapatalk
     
  12. Online

    timtower Moderator Moderator

  13. Offline

    TerroDoor

  14. Online

    timtower Moderator Moderator

    @TerroDoor You are getting the wrong sets.
    friend.put(p.getUniqueId(), set);
    That is using this set:
    set = request.get(p.getUniqueId());
     
  15. Offline

    TerroDoor

    @timtower i never put the target player into the map, only the set. is it meant to be
    Code:
    set = request.get(target.getUnieeuqId());
    @timtower im not to sure what you mean sorry
     
  16. Online

    timtower Moderator Moderator

    @TerroDoor Not when you are setting the friend though, then you need to get the friend Set and add the new friend to it.
     
  17. Offline

    TerroDoor

    I’m kinda following, so the target never receives a request because I never put them into the set? Both players need to be i the request set?


    Sent from my iPhone using Tapatalk
     
  18. Online

    timtower Moderator Moderator

    @TerroDoor The part that I am talking about is about losing the other friends.
     

Share This Page