Have a message broadcasted when 5 players do a Command

Discussion in 'Plugin Development' started by Jag.1000, Aug 4, 2012.

  1. Offline

    Jag.1000

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    if(cmd.getName().equalsIgnoreCase("impeach")){
                Bukkit.broadcastMessage(ChatColor.RED + sender.getName() + " has voted to impeach the King!");
            }
    
  2. Offline

    TheGreenGamerHD

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Maybe make a list for ints, and when ever the command is used, add 1 to the int.

    Then when a player uses the command, check if the ints > allowed. If so, do X to the user
  3. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Rather than storing an int, I'd make a HashSet of the Players that have voted and check it's length, never adding the same player more than once.
  4. Offline

    TheSmallBones

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    How would you check if the sender is already in the list/hash set?
  5. Offline

    Tirelessly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    if(list.contains(p.getName()))
  6. Offline

    TheSmallBones

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Awesome! I've quit a few projects because I don't know how to limit users to doing a command 1 time. This is great! :)
  7. Offline

    Tirelessly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    List<String> impeachers = new ArrayList<String>();
     
    if(cmd.getName().equalsIgnoreCase("impeach")){
    if(sender instanceof player){
    int counter=0;
    Player p=(Player)sender;
    final String username=p.getName();
    if(!impeachers.contains(username)){
    Bukkit.broadcastMessage(ChatColor.RED + sender.getName() + " has voted to impeach the King!");
    impeachers.add(username);
    counter++;
    }else{
    p.sendMessage("You have already voted to impeach the king.");
    }
    }
     
    }
    
    Put the list right under the start of the class. Then use if(counter>=5).... to remove the king.

    This post has been edited 3 times. It was last edited by Tirelessly Aug 4, 2012.
  8. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Actually, now that I'm looking at it again, HashSets don't allow for duplicate values, so you wouldn't even have to check if a Player is already in it. You would just add the player and check the length, if it was the same player they wouldn't be added again.
  9. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You definitely don't want to use an ArrayList for this. ArrayLists store indexes and aren't nearly as efficient as HashSet, especially considering you don't need to know the order and wouldn't have to check contains(player) in this situation.
  10. Offline

    Tirelessly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It's 5 values, efficiency isn't an issue. I don't see your contributing any code.
    TheSmallBones likes this.
  11. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Just because it's only 5 values is no reason to make it less efficient...

    Code:
        HashSet<Player> impeachers = new HashSet<Player>();
        
        if(cmd.getName().equalsIgnoreCase("impeach") && sender instanceof Player){
            if (impeachers.add((Player)sender)){
                Bukkit.broadcastMessage(ChatColor.RED + ((Player)sender).getName() + " has voted to impeach the King!");
                
                if (impeachers.size() >= 5){
                    //Do whatever
                }
            }
        }
    
  12. Offline

    Tirelessly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Much better, I bow to you. My clearly inferior ways have been bested.
  13. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It's not like you didn't ask for it.
  14. Offline

    Jag.1000

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    This is awesome! Thank you!
  15. Offline

    Jag.1000

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @Malikk
    Will the voting start over after 5 people vote?
  16. Offline

    Malikk

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Not as is it. You would add

    impeachers.clear();

    under where I wrote //Do Whatever
    Jag.1000 likes this.
  17. Offline

    Jag.1000

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Thanks!
  18. Offline

    TheSmallBones

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Also note, if you were to use an array list it would be removeAll()... I think.

Share This Page