ArrayLists

Discussion in 'Plugin Development' started by Skylandz, Feb 19, 2016.

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

    Skylandz

    So, basically i've followed an earlier thread in this forum to use a getter to acces an ArrayList from another class, but when i did so it gave the error: "Cannot make a static reference to the non-static method getFrozen() from the type Core".

    I've added 'static' to both the the things in Core, and it didn't throw the error anymore, but im wondering if i'll break anything if i continue to use this method.

    Code:

    [CORE]

    Code:
    package staff.core;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Core extends JavaPlugin {
    
        public static ArrayList<Player> frozen = new ArrayList<Player>();
    
        public static ArrayList<Player> getFrozen(){
            return frozen;
        
        }
    
        public void onEnable(){
            Bukkit.getServer().getLogger().info("[Staff] Plugin v" + getDescription().getVersion() + " made by " + getDescription().getAuthors() +  " has been enabled!");
        }
    
        public void onDisable(){
            Bukkit.getServer().getLogger().info("[Staff] Plugin v" + getDescription().getVersion() + " made by " + getDescription().getAuthors() +  " has been disabled!");
        }
    
    }
    [Listener]

    Code:
    package staff.listeners;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import staff.core.Core;
    
    public class Listeners implements Listener {
    
        /*
         * Class created on 16 February 2016, by MikkelHD
         */
    
        public ArrayList<Player> frozen = Core.getFrozen();
    
        @EventHandler
        public void onPlayerJoin (PlayerJoinEvent e){
            Player p = e.getPlayer();
    
            if(p.isOp() || p.hasPermission("ln.staffteam")){
            
                p.sendMessage(ChatColor.GREEN + "You have entered the game in Patrouille mode.");
                e.setJoinMessage(null);
            
            }
        
        }
    
    }
    
    Link to the thread i followed: https://bukkit.org/threads/how-to-access-an-arraylist-from-another-class.367513/
     
  2. Offline

    teej107

    @Skylandz Getters/Setters are only good if you follow encapsulation. Make the field private and remove that static as well.
     
  3. Offline

    Skylandz

    Done that, but now it says: "The method getFrozen() from type Core is not visible"
     
  4. Offline

    teej107

    I didn't say "make the method private"
     
  5. Offline

    george132222222

    Don't use player in an array list it causes memory leaks. Use strings and get the players name. Also use a for loop when returning frozen players not returning them. For(string s: Frozen). if the array list is static, it means in your listener class you can access it using yournameclass.frozen, don't make another array list and if you do, make that static as well
     
  6. Offline

    teej107

    No that is wrong.
    Adding Players to Collections doesn't cause memory leaks. Not removing the player from the Collection when they leave causes the memory leak.

    Stop abusing static. Static doesn't make your fields accessible anywhere. Public makes things accessible from anywhere. Public static fields should not be used as a lame way to access info from anywhere.

    @Skylandz I think you should read this as well: https://bukkit.org/threads/arraylists-are-bad-guide-to-collections.407651/
     
    Konato_K likes this.
  7. Offline

    Konato_K

    Let's leave lists aside and use a Set :D they are freee!! :DDD
     
Thread Status:
Not open for further replies.

Share This Page