Hey! I'm very confused why my code is not working, I am doing the following. I have an array list Code: ArrayList<String> Players = new ArrayList<String>(); I add people to it by doing Code: Players.add(p.getName()); But then later on in the method, on player join event I do the following Code:java if (Players.contains(p.getName()){Bukkit.broadcastMessage("text");} But the message is never sent, so I decided to check the size of the Array, 0. Any idea why? EDIT I noticed that the Array's size actually changes; the way I use the ArrayList is I have 3 classes. Class 1, Class 2 and Class 3. Class 1 is where the ArrayList is made, Class 2 is where I add the player (When I check size there, it equals 1) whereas in Class 3 when I use it then it says that size equals 0. I have, from this, gathered that the array list resets itself when used in different places.
Who is p? Where do you create the ArrayList? Where do you add the Players name to the list? A full code would be great
'P' is the player joining the server (PlayerJoinEvent). I have changed the OP a bit, to add more information. I add the player when it joins the server, same event. I can't paste full code seeing that the code is split into multiple classes.
Konkz Mind showing us the code? Also try to switch the ArrayList from string to player, and just add them using p and not getting their name. Tell me if that works, as if it does, your adding them to the ArrayList improperly.
I can also see the situation where you are adding things correctly, but reading from the wrong instance of array list.
The thing is that I have to be adding them correctly; the class that I add them in if I do if (ExtraStuff.Playing.size() >= 3) { Bukkit.broadcastMessage("lel"); } it won't send the message, but if I do if (ExtraStuff.Playing.size() >= 1) { Bukkit.broadcastMessage("lel"); } it works fine. Also, I got told by multiple people to avoid adding the player due to memory leaks or so. Sagacious_Zed Niknea
Konkz Just try adding them instead of adding them with their name, I just want to see if it works, not saying that you do it that way.
You NEVER add player objects to list, it's in the "most common developer mistakes" list. It cause crazy memory leaks. So for the problem. I hope you know that the list is going to reset when the server restarts, unless you save it to config. If you are testing with only one person, the issue is probably that only YOU don't see the message, other layers will be able to see it. This is because you are using the login event, which I believe fires the first time the "keepAlive" packet is received for the first time, but this packet is received before the player leaves the "Logging In..." screen. So chances are the message s broadcasted before you even have a chance to load the chat.
Konkz In Java ArrayLists are passed by reference. This means that if you use your ArrayList in different classes it will always point to the same piece of memory, unless you make a copy of the original. So your list is not resetting itself when used in other classes if it is pointing to the same list. A piece of your code would be handy to figure out what is wrong. Maybe post the code where the ArrayList is used and declared.
hexaan Quantum64 minecraft124_ Niknea This is how I declare my ArrayList (It's in a Class called "ExtraStuff") Code:java public static ArrayList<String> Playing = new ArrayList<String>(); Then I add people to it using the following code Class: JoinEvent Code: public void onJoin(PlayerJoinEvent e) { Player p = e.getPlayer(); ExtraStuff.Playing.add(p.getName()); } Class: JoinEvent Then, I use this code that starts the countdown: Code:java if (ExtraStuff.Playing.size() >= 1) { p.sendMessage(es.gamename + "test"); ExtraStuff.countdownStarted = true; startCountdown(); } It works 100% fine; the countdown starts and when I change the '1' to anything else, it won't start it etc. Now here comes the problem, Class: Started Code:java public void startGame() { for (Player p : Bukkit.getOnlinePlayers()) { if (ExtraStuff.Playing.contains(p.getName())) { p.sendMessage("Potato, I started and you are in my array list - Players."); } }} And if I make it broadcast ExtraStuff.Playing.size(); then it will say 0.
minecraft124_ The worst coding style I have ever seen. Static should be avoided at all costs unless absolutely neccessary. Even then, only one class in the entire plugin should cotain sttic. Should be used as accessor, not shortcut to ruin code.
It's basically a timer that goes from 10 to 0 and announces it, after the method startGame is called.
Konkz I have no idea what is going wrong in that code. Only guess I could make would be that the garbage collector is getting rid of the data because it does not have a reference to anything. I would just try and make it an instance variable in the class JoinEvent and send that to the class started.