Toggle Players via command

Discussion in 'Resources' started by Matthewenderle, Jun 1, 2012.

Thread Status:
Not open for further replies.
  1. If you are wanting to make a command that will allow you to have your players disable and enable your plugin for them, use this handler. It's fast, safe , and efficient.

    Code:
     
      public static Vector<String> disabled_users;
     
     
    public static void toggleUser(Player player)
            {
                // make sure the player is real
                if(player == null) { return; }
               
                // initialize vector if not already intialized
                if(disabled_users == null) {  disabled_users = new Vector<String>(); }
               
                // see if the user is already disabled
                int indexOfPlayer = disabled_users.indexOf(player.getName());
                if(indexOfPlayer > -1)
                {
                    // remove the player from list
                     disabled_users .remove(indexOfPlayer);
                    // tell the player
                    player.sendMessage(ChatColor.RED + "------------------ " + ChatColor.GRAY + "" + Main.plugin.Name() + "" + ChatColor.RED + " ---------------------");
                    player.sendMessage("  " + ChatColor.GOLD + "You enabled " + Main.plugin.Name() + ".");
                }
                else
                {
                    // add the player to the list
                    disabled_users .add(player.getName());
                    // inform the player
                    player.sendMessage(ChatColor.RED + "------------------ " + ChatColor.GRAY + "" + Main.plugin.Name() + "" + ChatColor.RED + " ---------------------");
                    player.sendMessage("  " + ChatColor.GOLD + "You disabled  " + Main.plugin.Name() + ".");
                }
            }
    Add that to a class such as PlayerManager.class then call it with PlayerManager.toggleUser((Player) sender);

    Then just do something such as
    Code:
    if(PlayerManager.disabled_users.contains(event.getPlayer().getName())
    {
    //do something
    }
     
  2. Offline

    Njol

    You should change your Vector to a Set since the order of the players doesn't matter.
    Also you can initialize the field directly, you don't have to check whether it is null every time:
    Code:
      public final static Set<String> disabled_users = new HashSet<String>();
     
    public static void toggleUser(Player player)
            {
                // make sure the player is real
                if(player == null) { return; }
             
                // see if the user is already disabled
                if(disabled_users.contains(player.getName()))
                {
                    // remove the player from list
                    disabled_users.remove(player.getName());
                    // tell the player
                    player.sendMessage(ChatColor.RED + "------------------ " + ChatColor.GRAY + "" + Main.plugin.name() + "" + ChatColor.RED + " ---------------------");
                    player.sendMessage("  " + ChatColor.GOLD + "You enabled " + Main.plugin.name() + ".");
                }
                else
                {
                    // add the player to the list
                    disabled_users.add(player.getName());
                    // inform the player
                    player.sendMessage(ChatColor.RED + "------------------ " + ChatColor.GRAY + "" + Main.plugin.name() + "" + ChatColor.RED + " ---------------------");
                    player.sendMessage("  " + ChatColor.GOLD + "You disabled  " + Main.plugin.name() + ".");
                }
            }
     
Thread Status:
Not open for further replies.

Share This Page