How To Use a Command To Toggle a Player's Vision on a Hashmap?

Discussion in 'Plugin Development' started by captainawesome7, Apr 11, 2011.

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


    Ok, so I am coding a plugin. I hooked in to permissions, (I think) and I want to know how I can make a command with a specified playername toggle a player on a hashmap. For Example:
    /commandexample captainawesome7
    I want that to toggle captainawesome7 on a hashmap, to make certain features work for me and not others, but also to make it configurable (so others can add themselves and other people as well). For another example, lets say my plugin kicks you from the server if you break TNT without being on the hashmap. How would I implement that? Btw, that isn't what my plugin is going to do, it was just an example. Or if a player breaks a block and he is on the hashmap, how would I make a command that can add a specified playername to a hashmap?
  2. Offline


    Assuming you haven't figured it out by now yet:

    You may not want to use a HashMap unless you have a value you're wanting to store. An ArrayList may be better for the example you gave.

    import java.util.ArrayList;
    ArrayList<String> list = new ArrayList<String>();

    Then, you can add someone to the list:

    And then, you can check if that player is not on the list
    //kick player
  3. Offline


    But i dont want to get someone's name, I want to add the name in the commad to the array list.
  4. Offline


  5. Offline


    but by using player.getName() you are getting the name of the player that sent the command, not the specified player...
  6. Offline


  7. Offline


    OK, still confused... If the command in game looks like "/annoy playername" how does getName come into that?
  8. Offline


    Don't Have a clue but id like to know :p
  9. Offline


  10. Offline


    As far as I'm aware, you'd check the args[] (in your case args[0] for the first passed argument) of the command; perform checks to make sure it's a real player and then add it to your hashmap (although as said above, unless you're storing values for each player, an array would be better).
  11. Offline


    If you want to lookup a player by name (in one example, if a player who is not in a hashmap break's a block of tnt) it is possible to perform a lookup in O(1) time using a hashmap, while the ArrayList mentioned above would take O(n) time.

    If no data is associated with the player's name beyond its presence in the hashmap, it does seem a little silly to use, but depending on the operations being performed can be more efficient than a List.

    In all likelyhood, since the problem has not been described, there is additional data which may be stored with those who ARE in the hashmap to differentiate them from each other as well as from those not in the hashmap.
Thread Status:
Not open for further replies.

Share This Page