[] Simple Toggles! []

Discussion in 'Resources' started by JPG2000, Aug 13, 2013.

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

    JPG2000

    This tutorial aims to help people, mainly newer coders, on how to succesfully make a toggle.

    Now, for thoose of you who don't know, I'm refering to a player (or console) type a command once, preforming code, then typing a command again a preforming a diffirent code. For an example, /gm toggles your gamemode.

    For this point and below, this will require that your class is set up succesfully.

    1: We are going to make a ArrayList to store the sender's name.
    Code:java
    1. public static final ArrayList<String> names = new ArrayList<String>();

    Note, we are making it public and static so that other Classes can use this ArrayList.

    2: When the command is typed, check if the sender is in the ArrayList, if so remove then. Else, add them. Here is an example for the command "gamemodetoggle".
    Code:java
    1. Player player = (Player) sender;
    2. String pname = player.getName();
    3. if (alias.equalsIgnoreCase("gamemodetoggle") {
    4. if (name.contains(pname)) {
    5. name.remove(pname);
    6. player.setGameMode(GameMode.CREATIVE);
    7. } else {
    8. name.add(pname);
    9. player.setGameMode(GameMode.SURVIVAL);
    10. }
    11.  
    12. }


    Basicly, I check if the player is in the arraylist. If so, remove and do code. If not, add and do code.

    Thank you for reading!! I hope this helped you!
    If you have any issues feel free to comment below!

    - Jake
     
    Wizehh likes this.
  2. Offline

    ZeusAllMighty11 Retired Staff

    Your list doesn't need to be static or final.

    You may also want to consider using a HashSet, as it's faster since you are only checking for a name.

    Other than that, nice tutorial for newer ones.
     
    hawkfalcon likes this.
  3. Offline

    macguy8

    What are the perks of using a HashSet over an ArrayList?
     
  4. Offline

    ZeusAllMighty11 Retired Staff


    it's moreso of Set vs List. A set doesn't contain duplicates, while a list does. So when you're checking for .contains(), there are more objects in it which it will have to check for.
     
  5. Offline

    macguy8

    Ah ok. Provided the code is used correctly though, there should never been duplicates. (I'm not the OP, I'm just saying that)
     
  6. Offline

    Comphenix

    In addition, the set interface doesn't enforce any particular ordering, whereas List orders its elements explicitly using an index.

    But the lack of duplicates is not really the reason sets, or HashSet in particular, performs so much better than your run of the mill ArrayList. It all comes down to Big O-notation - for HashSet, contains() is O(1) while ArrayList is O(n). Essentially, no matter how many items you add to HashSet, it's going to (roughly) take the same amount of time to look for a specific item. This is because a HashSet is implemented using a hash table.

    But an ArrayList is just backed by an array, and so you have to scan the entire list (half on average, assuming random values) to see if an item is present or not. Of course, O(1) doesn't mean the operation is necessary faster than O(n) in practice, as you may be discounting a huge constant time. In fact, I wouldn't be surprised if HashSet is slower than an ArrayList for n < 5 elements. But it usually doesn't take many elements before O(n) outclasses O(1) in time consumed.

    Bottom line, if you're using "contains" and don't care about ordering or duplicates, use HashSet. It's much, much faster.
     
    Chinwe, hawkfalcon, Minecrell and 2 others like this.
  7. Offline

    JPG2000

    TheGreenGamerHD I made it static becuase I know people like to use it in other classes. I should probably expalin that.
     
Thread Status:
Not open for further replies.

Share This Page