How to access an ArrayList from another class?

Discussion in 'Plugin Development' started by CloudVox, Jun 16, 2015.

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

    CloudVox

    I currently have an ArrayList that stores/removes players based on a command.

    I am trying to write a Listener that needs to access the ArrayList to do stuff according to whether or not they are on the ArrayList. I am just curious to see how I can access it?
     
  2. Offline

    HappyCow

    just change the arraylist to public static
    then access it by otherclass.arraylist
     
  3. Offline

    CloudVox

    Like how?

    I have
    Code:
    public static List<String> frozen = new ArrayList<String>();
    Then I am trying to access this in my Listener
    Code:
    if(frozen.contains(p.getName())){
     
  4. Offline

    HappyCow

    Code:
    if(otherclass.contains(p.getName()){
     
  5. Offline

    timtower Moderator Moderator

    @CloudVox Please use constructors and getters instead of static.
     
  6. Offline

    JBoss925

    Use a getter like:
    Code:
    public List<String> frozen = new ArrayList<String>();
    
    public List<String> getFrozen(){
        return frozen;
    }
    Then call it from the other class like:
    Code:
    public List<String> frozen = <<instance of class>>.getFrozen();
     
  7. Offline

    CloudVox

    Thank you!
    Thank you very much I will try this and get back to you with results :)

    In my main class I put this
    Code:
    public class GFreeze extends JavaPlugin implements Listener {
       
        public List<String> frozen = new ArrayList<String>();
       
        public List<String> getFrozen(){
            return frozen;
        }
    Then when I went to my "FreezeListener" class and put

    Code:
    Public class FreezeListener implements Listener{
        public List<String> frozen = FreezeListener.getFrozen();
    But it is telling me to make a method for getFrozen.

    Sorry I am so Noob at this :s

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jun 17, 2015
  8. Offline

    timtower Moderator Moderator

    @CloudVox You are trying to access it in a static way. Get an instance of GFreeze in FreezeListener, then you can access it.
    One of the first things that you learn when you are starting with java.
     
  9. Offline

    JBoss925

    You're telling the computer to look in the class "FreezeListener" for the method "getFrozen()". When it looks in "FreezeListener", it can't find it because "getFrozen()" is in "GFreeze". So you need to reference an instance of "GFreeze". So you have to add a global variable in "GFreeze" to get an instance of it because if you create a non static instance of it, it is essentially copying the javaplugin which is a big no-no because you're essentially running the plugin twice. So do:
    Code:
    public static GFreeze instance;
    
    public void onEnable(){
        this.instance = this;
    }
    
    public static GFreeze getInstance() {
        return instance;
    }
    
    public List<String> frozen = new ArrayList<String>();
    
    public List<String> getFrozen(){
        return frozen;
    }
    Then you would do something like:
    Code:
    public List<String> frozen = GFreeze.getInstance().getFrozen();
     
    Last edited: Jun 17, 2015
  10. Offline

    AdamQpzm

    @JBoss925 @HappyCow You guys should stop giving out bad practices like that - field shouldn't be public or static just for accessibility purposes.


    @CloudVox It's clear from your question and your code that you do not know much Java. You need to learn Java before you can make plugins. As Bukkit is written in Java, learning it is compulsory. If you don't, then you will always have basic issues like this and will have no idea how to solve them. I would recommend the Oracle tutorials or a Java book.
     
    KingFaris11 likes this.
  11. Offline

    JBoss925

    What do you mean? Are you speaking to the excessive public modifiers (specifically on the Arraylist) because if so, I generally don't use them but I was working with the code given.

    Also, can you explain how one would get an instance of the javaplugin class without using the method I use?
     
  12. Offline

    AdamQpzm

    This is one of the reasons you shouldn't try to 'fix' people's code for them, you should just teach them how to fix it. Regardless, you still make a public static field yourself, that needn't have been public static.

    The method you used isn't as bad as common static misuse I guess, but it's definitely not the only option, which anybody with Java experience would know - constructors for example :)
     
    timtower and (deleted member) like this.
  13. Offline

    JBoss925

    Yeah, I didn't really look at the code that closely, I just copied the code form his post and cut and pasted some stuff around.

    You can't use a constructor, GFreeze extends JavaPlugin. Constructing a new instance of it would throw errors. And generally, I try to stay away from static (unless they're mathematical constants or something though normally I just use the final modifier) in most cases but this is the workaround I've used for the past year or so to get the instance of the JavaPlugin class.

    EDIT: Ohhhh I see what you mean. I put public static before the instance, yes now I see what you mean. And furthermore, you're right in that sense, it doesn't seem to be necessary to have it be static or public(static is probably a pretty bad idea if you're storing values). Yeah, sorry, that was just a product of me being ignorant.
     
    Last edited: Jun 17, 2015
  14. Offline

    AdamQpzm

    I don't mean construct a new instance of the plugin class, that wouldn't work even if it didn't throw errors, since it would be a different object it would have different field values. What I mean is pass the plugin as a constructor argument to whatever class needs to access objects/methods in it.
     
  15. Offline

    KingFaris11

    @JBoss925 @AdamQpzm
    Errr, can't you just use JavaPlugin.getPlugin(<mainclass>.class)?

    If not, just cast the plugin instance to the class like: (<mainclass>) Bukkit.getServer().getPluginManager().getPlugin("<pluginname>");

    ^ If you don't want to pass through the plugin instance? Yeah, passing it through is a lot more efficient as not as much code is run just to get the instance, but, even though I can't really think of a case, there may be one for having to use static instead.

    Personally, I find that having a plugin instance passed through makes the code look ugly, as every class has "plugin" in it. But again, yeah, local is much more "safe" and quicker.
     
  16. Offline

    AdamQpzm

    Indeed you could - this is just part of my whole "there's more than one way to do this" thing. :)

    Couldn't disagree more. Constructors are supposed to be used for giving the class access to objects that it depends on - that's their whole point in life. Having a class that has a Plugin object in it is no uglier than a class that has a String in it, or an int in it.

    A common attitude I see around these forums is to treat things like the plugin's class, or any other class that the individual creates, as different to other classes found in Java or Bukkit. I think it's a really wrong way of looking at it, though. A Plugin field really is no different to a String field, or an Entity field, or whatever. Java is Java no matter where you do it. So, as I said above, I don't see why you would view having a Plugin field as "ugly", unless of course you view fields themselves as ugly, in which case OOP probably isn't for you :p
     
  17. Offline

    Drkmaster83

    @AdamQpzm True, but I suppose the "there's more than one way to do this" principle still applies. OOP is useful, but you wouldn't have to store the reference to the plugin instance that you pass in if you accomplished the same results without storing it from the constructor. I suppose it has slightly more relevance than formatting in terms of functionality (at least, in this scenario).

    Everyone has their own way of doing things, and even though it might not be the best (even though it irks me so when it is not... then again, even I may not know the "true" best way of doing something), we can't hold their hands through the whole process.
     
    KingFaris11 likes this.
  18. Offline

    AdamQpzm

    @Drkmaster83 I don't tend to object when somebody does something a different way, I just try to point out when I think someone is doing something in a bad or unconventional way - and having a public mutable field is often one of those times.
     
    teej107 likes this.
  19. Offline

    JBoss925

    Oh, that's actually quite smart, I suppose. Though I suppose this is a manifestation of the fact that when I first learned Bukkit, I was taught to use a getter.
    Overall, I still prefer the getter method. But in reality, I prefer to leave the JavaPlugin class alone unless it pertains to the actual operation of the plugin on the server. If I'm returning or creating anything needed for the plugin, I tend to move it to a different class so as to avoid this.
     
Thread Status:
Not open for further replies.

Share This Page