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 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 Player player = (Player) sender;String pname = player.getName();if (alias.equalsIgnoreCase("gamemodetoggle") {if (name.contains(pname)) {name.remove(pname);player.setGameMode(GameMode.CREATIVE);} else {name.add(pname);player.setGameMode(GameMode.SURVIVAL);} } 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
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.
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.
Ah ok. Provided the code is used correctly though, there should never been duplicates. (I'm not the OP, I'm just saying that)
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.
TheGreenGamerHD I made it static becuase I know people like to use it in other classes. I should probably expalin that.