[TUTORIAL] Voting/Polls

Discussion in 'Resources' started by FabeGabeMC, Nov 11, 2014.

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

    FabeGabeMC

    Have you ever wanted to make your players vote for a poll or arena selection?
    This is a tutorial on how to make voting possible using Java!
    Example:
    Let's say I am playing Survival Games and I want to play on a certain map. I would vote for it.
    Let's look at how it's done.
    Step 1.
    Create a HashMap storing what each player voted for and the value being the Object you want to be voted for (in my case, an arena).
    Code:java
    1. private Map<String, Arena> vote = new HashMap<String, Arena>();

    Step 2.
    Make a method that gets the votes of your object. (I will explain).
    Code:java
    1. public int getVotes(Arena a) {
    2. if(a == null) // If the arena doesn't exist, return 0.
    3. return 0;
    4. int count = 0; // Default votes.
    5. for(String s : vote.keySet()) { // Loop through all keys (player names/UUIDs)
    6. if(Bukkit.getPlayer(s) == null) { // Checks if player isn't online.
    7. vote.remove(s); // Removes this 'ghost' vote (Player vote doesn't count) to avoid bugs.
    8. continue; // Skips the player.
    9. }
    10. if(vote.get(s) == a) // If the value of the player's name/UUID is equal to the arena (a),
    11. count++; // We add 1 to the count.
    12. }
    13. return count; // Returns votes.
    14. }

    Step 3.
    Compare the votes of all your Objects.
    Code:java
    1. public Arena getMostPopular() {
    2. Arena chosen = null;
    3. for(Arena a : array) { // Replace array with your array/list.
    4. if(a == null) { // Safe null check.
    5. array.remove(a); // Removes arena from array.
    6. continue; // Skips.
    7. }
    8. if(chosen == null) { // Checks if chosen is null.
    9. chosen = a; // Sets chosen to that arena.
    10. continue;
    11. }
    12. if(getVotes(a) >= getVotes(chosen)) { // Checks if votes are higher than chosen's.
    13. chosen = a; // Sets chosen to that arena.
    14. continue;
    15. }
    16. }
    17. return chosen; // Returns the chosen arena.
    18. // This will not return null unless the array is empty.
    19. }

    Then of course you can use this method to start a game with that specified arena.
    Thanks for reading,
    Gabe.
     
  2. Offline

    xTrollxDudex

    Why not Arena, Integer (heck, an integer wrapper that automatically increments for you) so you can do something like:
    PHP:
    class IntWrapper {
        
    int wrapped 0;
        
        public static 
    IntWrapper wrap(int in) { wrapped in; }
        public 
    int wrapped() { return wrapped; }
        public 
    IntWrapper increment() { wrapped += 1; return this; }
    }

    private final 
    Map<ArenaIntWrappervotes // ...

    // Increment (Arena arena)
    votes.put(arenavotes.get(arena).increment());

    // Get (Arena arena)
    int votes votes.get(arena).wrapped();
     
  3. Offline

    FabeGabeMC

    xTrollxDudex
    Because the player might leave and the game might take a ghost vote (if player is not online), however, that's another possible way using a null check.
     
  4. Offline

    xTrollxDudex

    You could back IntWrapper with an ArrayList with player strings and get the size each time, then when you get the amount of votes, update the list.
     
  5. Offline

    DevRosemberg

  6. Offline

    FabeGabeMC

  7. Offline

    teej107

    Regablith likes this.
  8. Offline

    xTrollxDudex

    Because I like the colors, as well as in honor of desht and Henzz
     
    Regablith likes this.
Thread Status:
Not open for further replies.

Share This Page