Solved design peace/friend system

Discussion in 'Plugin Development' started by DopeBrot, Dec 8, 2022.

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

    DopeBrot

    Hey there.
    so what im currently working on requires some sort of system to track who has peace with who and vice versa.

    my first idea was to create 2 hashmaps, and check if both players are in both hashmaps:
    Code:
            HashMap<Player,Player> foo = new HashMap<>();
            HashMap<Player,Player> bar = new HashMap<>();
            foo.put(player1,player2);
            bar.put(player2,player1);
            if (foo.containsKey(player1) && foo.get(player1) == player2) {
                if (bar.containsKey(player2) && bar.get(player2) == Player1) {
                    // do stuff
                }
            }
    
    but i think there is a better way to design this.
    because i need to save this in a config as well: (something like this)
    Code:
         
         "peace": [
            {"uuid": "playersUUID"},
            {"uuid": "playersUUID2"}
          ],
    
    json

    let me know if you have an idea.

    -Brot
     
  2. Offline

    timtower Administrator Administrator Moderator

    @DopeBrot 1. Don't store Player objects, use their UUID instead.
    2. Do both sides have the same status or can player1 have peace with player2 while player2 is at war with player1?
     
  3. Offline

    DopeBrot

    ok i did change that

    both players need to have "peace" with another, so that they can't damage each other
     
    Last edited: Dec 9, 2022
  4. Offline

    timtower Administrator Administrator Moderator

    So they can have different statuses?
     
  5. Offline

    DopeBrot

    no they must both need have peace with another
    if one of them removes the peace both can damage another again
     
  6. Offline

    timtower Administrator Administrator Moderator

    Map<UUID, Map<UUID, Peace/War/Neutral enum>>
    Single map, not multiple.
     
  7. Offline

    DopeBrot

    i don't know much about maps thats why i always use HashMaps, but i still tried to implement it and i did.
    but one thing was bothering me.
    A player can have multiple "peaces" (player1 has peace with player2 and player3).
    but it would'nt work (or im missing something):
    Code:
     //not real code btw
            HashMap<UUID,UUID> peaceMap = new HashMap<>();
            peaceMap.put(player1.getUniqueId(),player2.getUniqueId());
            peaceMap.put(player1.getUniqueId(),player3.getUniqueId());
            peaceMap.get(player1.getUniqueId()) // returns player3
    
    so i had another idea.
    i created a record that saved both uuids:
    Code:
            public record Peace(
            UUID sender,
            UUID receiver) {
    }
    
    which i put in an arraylist.
    + i need to check with for loops and stuff.
    but i still think there is a better way to do this.

    -Brot
     
  8. Offline

    timtower Administrator Administrator Moderator

    @DopeBrot A map is a single key value pair, can't put multiple values in it for the same key
     
  9. Offline

    Strahan

    I've personally never been a fan of nested collections. I'd just make a custom object for it.
     
    DopeBrot likes this.
Thread Status:
Not open for further replies.

Share This Page