ArrayList Confusion

Discussion in 'Plugin Development' started by Konkz, Mar 1, 2014.

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

    Konkz

    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
    1. if (Players.contains(p.getName()){
    2. Bukkit.broadcastMessage("text");
    3. }


    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.
     
  2. Offline

    Timbals

    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
     
  3. Offline

    Konkz

    '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.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Then you should be putting all your classes into a source code repository.
     
  5. Offline

    Niknea

    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.
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    I can also see the situation where you are adding things correctly, but reading from the wrong instance of array list.
     
  7. Offline

    Konkz

    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
     
  8. Offline

    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.
     
  9. Offline

    Quantum64

    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.
     
  10. Offline

    hexaan

    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.
     
  11. Offline

    minecraft124_

    Add the "static" modifier to the ArrayList, then you will be able to access it from any other class.
     
  12. Offline

    Konkz

    hexaan Quantum64 minecraft124_ Niknea

    This is how I declare my ArrayList (It's in a Class called "ExtraStuff")

    Code:java
    1. 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
    1. if (ExtraStuff.Playing.size() >= 1) {
    2. p.sendMessage(es.gamename + "test");
    3. ExtraStuff.countdownStarted = true;
    4. startCountdown();
    5. }


    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
    1. public void startGame() {
    2. for (Player p : Bukkit.getOnlinePlayers()) {
    3. if (ExtraStuff.Playing.contains(p.getName())) {
    4. p.sendMessage("Potato, I started and you are in my array list - Players.");
    5. }
    6. }
    7. }


    And if I make it broadcast ExtraStuff.Playing.size(); then it will say 0.
     
  13. Offline

    Traks

    Did you add @EventHandler in front of your listener and did you register the listener?
     
  14. Offline

    xTrollxDudex

    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.
     
  15. Offline

    Konkz

    Indeed, I have.
     
  16. Offline

    hexaan

    Konkz
    What does the method startCountdown do?
     
  17. Offline

    Konkz

    It's basically a timer that goes from 10 to 0 and announces it, after the method startGame is called.
     
  18. Offline

    badboysteee98

    Konkz a bit off topic with your plugin but where do you make that Avatar
     
    Konkz likes this.
  19. Offline

    Konkz

    Manga face creator or something.
     
    badboysteee98 likes this.
  20. Offline

    hexaan

    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.
     
  21. Offline

    minecraft124_

    How would you suggest doing it then?
     
  22. Offline

    xTrollxDudex

    minecraft124_
    Pass class instance to access package local fields
     
Thread Status:
Not open for further replies.

Share This Page