ArrayList not actually saving things?

Discussion in 'Plugin Development' started by MayoDwarf, May 23, 2014.

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

    MayoDwarf

    Golem Class
    SalvoAndChain Class

    tPlayer Class

    ----
    The problem is that when I am testing if the player is a golem with g.isGolem( p ), it always comes up as false, but they are added to it when they enter the golem. Please help me with this. An idea might be that it is making a new instance of Golem in class in SalvoAndChain every time it is accessed. Please help. Thanks - Jared

    UPDATE: Made a new class named tPlayer and it basically keeps track of the players who are a Titan. Problem is tPlay.hasGolem() does not return true when it should. Any ideas guys? Please help! Thanks!
     
  2. Offline

    Garris0n

    First of all, quit using the display name as a unique key. It's not unique, anybody can set it. Really, you should use UUIDs, but at least use their name. Not their display name.
     
  3. Offline

    MayoDwarf

    Garris0n Alright. Now can we solve the problem please?
     
  4. Offline

    Garris0n

    For all I know that is the problem. Something could be setting the display name elsewhere.
     
  5. Offline

    MayoDwarf

  6. Offline

    HeadGam3z

    Niknea likes this.
  7. Offline

    JeremyPark

    MayoDwarf

    You might need to set the ArrayList to static.
    If you want better help, please post your code.
     
  8. Offline

    1Rogue


    need? need? I would love to see an instance where a list of data needs to be static, where something like a getter + instantiation would not suffice and be a better alternative.
     
    Aengo, AdamQpzm and mazentheamazin like this.
  9. Offline

    JeremyPark


    Well, I guess static or a getter. I haven't really looked into the cons of static data, so I might have missed something. :)
     
  10. Offline

    1Rogue

    AdamQpzm likes this.
  11. Offline

    MayoDwarf

    UPDATE: Made a new class named tPlayer and it basically keeps track of the players who are a Titan. Problem is tPlay.hasGolem() does not return true when it should. Any ideas guys? Please help! Thanks!
    ---
    CHECK ORIGINAL POST

    1Rogue AdamQpzm JeremyPark HeadGam3z Garris0n
     
  12. Offline

    xTigerRebornx

    MayoDwarf The first problem I noticed is in your tPlayer class. The ArrayList that keeps track of who is/isn't a golem is different per-tPlayer Object. So, if you called this
    Code:
    tPlayer one = new tPlayer("Person");
    tPlayer two = new tPlayer("OtherPerson");
     
    one.setGolem();
     
    System.out.println(two.isGolem());
    // ^ This will return false
    The ArrayList in 'one' is a different ArrayList then the one in 'two'.

    You'd be better off using a Singleton to handle who is/isn't a Golem, their weapons, etc
     
    MayoDwarf likes this.
  13. Offline

    MayoDwarf

    Can you give an example on that and how I would use it mainly in my code? I really appreciate your reply so quick though :) xTigerRebornx
     
  14. Offline

    xTigerRebornx

    MayoDwarf The Singleton pattern (?) makes it so that there is only ever one instance of the class.
    Code:
    //Private Constructor to prevent initialization from other place
    private GolemManager(){
    // Constructor Stuffs
    }
    // Static instance + getter for static instance
    private static GolemManager instance = new GolemManager();
    public static GolemManager getInstance() { return instance; }
    public List<String> example = new ArrayList<String>();
     
    // Calling this in another class
    if(GolemManager.getInstance().example.contains("String")) return true;
    // Since there is only ever one static instance, that List is
    // the same one you will reference when you call it again
    A quick example of some pseudo-code, but you should be able to understand the general idea of only having one instance of the GolemManager, therefor you reference the same List.

    An example gist of how you'd be able to access a Plugin/JavaPlugin variable (I used an init() method that takes in the Plugin):
    https://gist.github.com/TigerReborn/e132eb7cfc43549867c1
    Note, all of the code I've provided is pseudo-code, not meant to be copied/pasted, and may have unexpected bugs/problems
     
  15. Offline

    1Rogue

    ...does not need to be static. If you're going for the guarantee that a class can only be instantiated once:

    Code:java
    1. public enum MySingleton {
    2.  
    3. INSTANCE;
    4.  
    5. public void something() {...}
    6.  
    7. }


    Alternatively, you can still follow the singleton pattern through the idea of "class managers", which handle the instantiation of sub-classes and allow you to retrieve those instances. The plugin's main class can be thought of as one big manager for managers, as you can see in example to many projects I make: https://github.com/1Rogue/Reginald/blob/master/src/main/java/com/rogue/reginald/Reginald.java
     
Thread Status:
Not open for further replies.

Share This Page