Solved Getting arraylists from different classes

Discussion in 'Plugin Development' started by sgavster, Dec 3, 2013.

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

    sgavster

    Hi!

    I'm working on a minigame, and I need to get arraylist's from different classes.

    I tried this:

    PHP:
    /**Getting main class*/
    Main main = new Main();
    /**Checking if they are in the arraylist*/
    if(main.voted.contains(p.getName()))
                    {
                        
    p.sendMessage("§4You have already voted!");
                    }
                    else
                    {
                        
    main.vote Bukkit.createInventory(p9"Maps");
                        
    main.vote.setItem(0setNameAndLore(Material.MAP1Map.TEST.toString()));
                        
    p.openInventory(main.vote);
                    }
                }
    /**Adding them to the arraylist*/
    e.setCancelled(true);
                            
    Map m Map.TEST;
     
                            
    main.voted.add(p.getName());
     
                            
    main.mapTest++;
     
                            
    p.sendMessage("§6You voted for the map §2" m.toString() + "§6!");
                            
    p.sendMessage("§6Your map now has §2" Integer.toString(main.mapTest) + "§6 votes!");
                           
                            
    p.closeInventory();
    But, when I vote for one (add me to the arraylist), I can do the command (do the code where it checks if I'm in it) again. Any idea why?
    I'd also like to note that if I have the 2 methods in the same class it works, but with it in different classes it doesn't. :(
    Thanks!

    SkillSam I tried making a static main-getter it caused NPE's randomly.

    If I make arraylist's static, all my classes have to be static.

    Any other ways? :'(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    1Rogue

    Make a getter method...

    Code:java
    1. public ArrayList<E> getYourList() {
    2. return this.yourList;
    3. }
     
  3. Offline

    Erigitic

  4. Offline

    sgavster

    1Rogue Doesn't change anything D:
    Still lets them open the inventory after they vote
     
  5. Offline

    Not2EXceL

    Erigitic or just avoid the constructor with a static singleton getter
     
  6. Offline

    1Rogue


    Don't use singletons to assist lazy coding...


    Put in debug statements, or provide more (relevant) code, like the classes involved.
     
    Garris0n likes this.
  7. Offline

    Erigitic

    Not2EXceL Either way works. I just feel as if it is better to use constructors.
     
  8. Offline

    sgavster

    1Rogue

    Main:

    PHP:
    public ArrayList<Stringred = new ArrayList<String>();
        public 
    ArrayList<Stringblue = new ArrayList<String>();
       
        public 
    ArrayList<Stringvoted = new ArrayList<String>();
       
        public 
    ArrayList<Stringbruiser = new ArrayList<String>();
        public 
    ArrayList<Stringscout = new ArrayList<String>();
        public 
    ArrayList<Stringspawner = new ArrayList<String>();
        public 
    ArrayList<Stringalchemist = new ArrayList<String>();
        public 
    ArrayList<Stringpyro = new ArrayList<String>();
        public 
    ArrayList<Stringarcher = new ArrayList<String>();
       
        public 
    ArrayList<StringgetRedList()
        {
            return 
    this.red;
        }
       
        public 
    ArrayList<StringgetBlueList()
        {
            return 
    this.blue;
        }
       
        public 
    ArrayList<StringgetVoted()
        {
            return 
    this.voted;
        }
       
        public 
    ArrayList<StringgetBruiser()
        {
            return 
    this.bruiser;
        }
       
        public 
    ArrayList<StringgetScout()
        {
            return 
    this.scout;
        }
       
        public 
    ArrayList<StringgetSpawner()
        {
            return 
    this.spawner;
        }
       
        public 
    ArrayList<StringgetAlchemist()
        {
            return 
    this.alchemist;
        }
       
        public 
    ArrayList<StringgetPyro()
        {
            return 
    this.pyro;
        }
       
        public 
    ArrayList<StringgetArcher()
        {
            return 
    this.archer;
        }
        
    VoteClickListener
    PHP:
    public class VoteClickListener implements Listener
    {
        
    Main main = new Main();
        @
    EventHandler
        
    public void onClick(InventoryClickEvent e)
        {
            if(
    e.getInventory().getName().equalsIgnoreCase("maps"))
            {
                
    Player p = (Playere.getWhoClicked();
                if(
    e.getCurrentItem() != null && e.getCurrentItem().getType() != null)
                {
                    if(
    e.getCurrentItem().hasItemMeta() && e.getCurrentItem().getItemMeta().hasDisplayName())
                    {
                        if(
    e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(Map.TEST.toString()))
                        {
                            
    e.setCancelled(true);
                            
    Map m Map.TEST;
     
                            
    main.getVoted().add(p.getName());
     
                            
    main.mapTest++;
     
                            
    p.sendMessage("§6You voted for the map §2" m.toString() + "§6!");
                            
    p.sendMessage("§6Your map now has §2" Integer.toString(main.mapTest) + "§6 votes!");
                           
                            
    p.closeInventory();
                        }
                    }
                }
            }
        }
    }
    VoteCommand:

    PHP:
        Maps map = new Maps();
        
    Main main = new Main();
        public 
    boolean onCommand(CommandSender senderCommand cmdString labelString[] args)
        {
            if(
    cmd.getName().equalsIgnoreCase("vote"))
            {
                if(
    sender instanceof Player)
                {
                    
    Player p = (Playersender;
                    if(
    main.getVoted().contains(p.getName()))
                    {
                        
    p.sendMessage("§4You have already voted!");
                    }
                    else
                    {
                        
    main.vote Bukkit.createInventory(p9"Maps");
                        
    main.vote.setItem(0setNameAndLore(Material.MAP1Map.TEST.toString()));
                        
    p.openInventory(main.vote);
                    }
                }
                else
                {
                    
    sender.sendMessage("§4You must be a player to do this command!");
                }
            }
            return 
    false;
        }
        public 
    ItemStack setNameAndLore(Material materialint amountString nameString... lore)
        {
            
    ItemStack item = new ItemStack(materialamount);
            
    ItemMeta meta item.getItemMeta();
            
    meta.setDisplayName(ChatColor.translateAlternateColorCodes('&'name));
            
    ArrayList<Stringlorez = new ArrayList<String>();
            for(
    String mylores lore)
            {
                
    lorez.add(ChatColor.translateAlternateColorCodes('&'mylores));
            }
            
    meta.setLore(lorez);
            
    item.setItemMeta(meta);
            return 
    item;
        }
    }
     
  9. Offline

    1Rogue

    You need to pass the main class instance, not simply create a new one. Then you would reference that. (also don't make your fields public)

    Code:java
    1. public class Y0urClass {
    2.  
    3. private final Main plugin;
    4.  
    5. // constructor
    6. public YourClass(Main plugin) {
    7. this.plugin = plugin;
    8. }
    9.  
    10. // can now use this.plugin.getYourList(); etc...
    11.  
    12. }
     
    sgavster likes this.
  10. Offline

    Not2EXceL

    1Rogue singletons don't mean lazy coding. It all depends on when and where you're using them. I simply stated another alternative to passing an instance of the class. If its the main class and you won't have a dependency restriction you van use it unless you really plan on subclassing the main class. There's other reasons for and against but its not needed to be mentioned since the thread isn't about singletons.

    Didn't say nor imply that I use them to be lazy.

    On topic: sgavster you're getting that issue since your creating a new instance of your main class. Instead of using one instance for all your other classes
     
  11. Offline

    sgavster

    1Rogue You.. Are.. AMAZING! :p. It worked! thanks!
     
  12. Offline

    1Rogue


    A singleton in this case isn't justified at all...

    1) You don't need a single instance of the class
    2) It isn't being called from a multitude of unattached classes
    3) There's no need to independently control the concurrency of the operations in this class

    A singleton design pattern has its place, this is not one of them. One of the largest programming mistakes (imo) is people use singletons to replace passing variables ("lazy coding") because it can be simpler to type around.

    I'm stating this not because it isn't a possible answer, it certainly is possible, but it is unadvisable. You should not be using a singleton here.


    Not a problem.
     
  13. Offline

    sgavster

    1Rogue I know this is solved; but I do don't want to make another post.

    I have basicly the same problem, but with GameState (enum)

    When I try to access it like
    Code:java
    1. Game game = new Game();
    2. if(game.getState() == GameState.IN_GAME) { //do stuff }


    and set it like
    Code:java
    1. game.setState(GameState.ENDED);


    It doesn't save; I can set it and then check, it will not return correctly. Any idea why? This is my Game class:
    Code:java
    1. package me.sgavster.ghostsquadron.util;
    2.  
    3. public class Game
    4. {
    5.  
    6. private GameState gamestate;
    7.  
    8. public enum GameState
    9. {
    10. LOBBY, IN_GAME, ENDED;
    11. }
    12.  
    13. public GameState getState()
    14. {
    15. return gamestate;
    16. }
    17.  
    18. public void setGameState(GameState gamestate)
    19. {
    20. this.gamestate = gamestate;
    21. }
    22. }
    23.  


    And if I try to do
    Code:java
    1. private final Game game;
    2. public Main(Game game) {
    3. this.game = game;
    4. }

    It wont work (because I need to then get the game class in all my other classes)

    Thanks!
     
  14. Offline

    1Rogue

    You cannot access variables in a class by making a new instance. It will reset all fields within the class object you make with the new instance.

    The best way to connect things is to have public getters for separate sections of your plugin in the main class file, and then pass the plugin variable around.
     
  15. Offline

    sgavster

    1Rogue Okay, could I have an example, please? :D
     
  16. Offline

    1Rogue

  17. Offline

    sgavster

    1Rogue Is there any way you could show me an example here? I'm a bit confused with that. Sorry ;(
     
  18. Offline

    1Rogue


    I already did with this:


    You're just simply passing the plugin instance down to other managers via their constructors, and then you have public access methods at the plugin-level for those managers.

    (You can then somewhat repeat the style with the managers, where they have accessors for classes that are subs of them)
     
  19. Offline

    sgavster

    1Rogue But I want to put this in my Main class, so I can access the getState() method, that is why I'm confused ;/

    1Rogue After a while of trying, I still can't figure out how to do this in the main class, or any class that already has the Main plugin.. ect. :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  20. Offline

    iiHeroo

    I don't have any experience with game plugins, but try making it static :p

    So

    public static enum
     
  21. Offline

    1Rogue

    Main.java
    Code:java
    1. public class Main {
    2.  
    3. private SomeManager smanager;
    4. private OtherManager omanager;
    5.  
    6. public void onEnable() {
    7. this.smanager = new SomeManager(this);
    8. this.omanager = new OtherManager(this);
    9. }
    10.  
    11. public SomeManager getSomeManager() {
    12. return this.smanager;
    13. }
    14.  
    15. public OtherManager getOtherManager() {
    16. return this.omanager;
    17. }
    18.  
    19. }


    SomeManager/OtherManager

    Code:java
    1. public class SomeManager {
    2.  
    3. private final Main plugin;
    4.  
    5. public SomeManager(Main plugin) {
    6. this.plugin = plugin;
    7. }
    8.  
    9. // methods
    10.  
    11. }


    http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
    http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

    It's basically like having a tree data structure, or "branches". You have the main class which is the root, and it branches to managers, and those managers branch to subclasses, etc.

    To expand upon the last, you would be able to call "plugin.getOtherManager()" from the SomeManager class by this method now, and you would have access between those classes.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    sgavster likes this.
  22. Offline

    sgavster

    1Rogue Thanks a bunch; the only problem I have is this:

    In one of my classes, I have no need for the plugin variable: so it's unused. Is there anyway I can fix that? I just do not want to export it with jar file errors :p
     
  23. Offline

    1Rogue

    Don't pass the variable if you don't need it?
     
  24. Offline

    sgavster

    1Rogue *facepalm* I read your other post wrong lol.

    I thought I needed that code. Thanks for your help!
     
Thread Status:
Not open for further replies.

Share This Page