Solved /mapvote Command

Discussion in 'Spigot Plugin Development' started by R3dThunderr, May 20, 2017.

  1. I am making a plugin for my server which allows players to switch the map.

    It's currently written with these logic steps:
    Integer: map1
    Arraylist<int> votedmaps;
    1) command: adds 1 to map1, and adds map1 to votedmaps
    2) check: if nothing in the arraylist, don't change the map
    3) else: find largest value in arraylist

    The problem is with step 3. I am able to find the largest value in the arraylist, but I am not able to find which map it's associated with. Also, it doesn't work well for ties between maps.

    Could someone point me in the right direction? Thanks!
     
  2. 1) command: adds 1 to map1, and adds map1 to votedmaps

    What does that mean?
     
  3. Well, maybe just use a HashMap for this, maybe something like

    HashMap<Map, Integer> mapvotes = new HashMap<>();
     
  4. Sorry for being unclear,
    When the player types a command, ex: "/mapvote map1", map1 (the integer) gets an additional value (map1(integer) = map1 +1)
    And, the integer map1 is added to an arraylist called votedmaps. That way I can iterate through them later
     
  5. Wouldn't that cause errors if they're all 0, since they'll overwrite each other? You could do this with a Map like @NormaalBart said above
     
  6. Code (Text):
    Collections.max();
    is what you're looking for.

    However, I believe I have a better voting system. It uses three HashMaps.
    One has player UUIDs and an Integer to go along with the map the user voted for. (This is to check to make sure they aren't voting multiple times and to allow them to change their vote)
    The second HashMap has two Integers. The first is the assigned map's number and the second integer is the amount of votes.
    Then the third HashMap assigns map names to integers.
    From there, you figured out which map has the highest votes.
     
  7. You could do that with a single hashmap, if you link the id to the config, then have a Map of <Integer /*ID*/, Set<UUID> /* List of people who voted*/>

    Then you can addAll() on all values, then check that the combined doesn't contain the UUID
     
    • Like Like x 1
  8. Ok so I've looked into all this.
    The_BloodHound, your system seems the best, however I don't need anything that complex

    With a hashmap, correct me if I'm wrong, but...

    HashMap<String, Integer> mapvotes
    Then, to check the highest voted map...
    String topVotedMap = Collections.max(mapvotes.keySet())
     
  9. That doesn't work. Collections.max() returns an Integer
     
    #9 The_BloodHound, May 20, 2017
    Last edited: May 20, 2017
  10. But, you can't have 2 elements with the same key.
     
  11. Oh crap I was thinking something else lmao

    my bad xd

    I fixed my post.
     
  12. So here is the problem now:
    Hashmap of mapnames and votes towards the map. How do I get the map with the most votes.
    Sorry if that was already answered, but I might need a little more explanation
     
  13. I honestly think the easiest way is the way I explained but not including the HashMap with the players.
     
    • Like Like x 1