Solved Access the List from another class.

Discussion in 'Plugin Development' started by theViTALiTY, May 31, 2014.

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

    theViTALiTY

    So i have the Main class and a class called Commands.


    In the Commands class ive got the onCommand() method which holds the ArrayList of players.

    Like this :
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2.  
    3. List<String> playerList = sm.players.getStringList("Players");
    4.  
    5. if(playerList == null) {
    6. playerList = new ArrayList<String>();
    7. }


    Now, im doing a piece of code in the Main class, and i need to access the playerList. How could i do it if anyone could make an example for me.
     
  2. Offline

    JungleSociety

    theViTALiTY
    We'll first, I highly recommend putting the list at the class level and taking it out of the method. Then create an instance of that class. Can't really put an example because I'm on an ipad :p. But to create an instance of a class you do ClassName main = new ClassName();
    Then you can access it with a method that returns the list.
     
    es359 likes this.
  3. Offline

    FlareLine

    To add an example to @JungleSociety's post:

    Main Class:
    Show Spoiler

    Code:java
    1. public class Main {
    2. CommandHandler cmd = new CommandHandler();
    3. ArrayList<String> players = new ArrayList<String>();
    4. players = cmd.getPlayers();
    5. }
    6.  


    CommandHandler Class:
    Show Spoiler

    Code:java
    1. List<String> playerList = sm.players.getStringList("Players");
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4.  
    5. if(playerList == null) {
    6. playerList = new ArrayList<String>();
    7. }
    8. }
    9.  
    10. public ArrayList<String> getPlayers(){
    11. return playerList;
    12. }
     
    JungleSociety likes this.
  4. Offline

    theViTALiTY

    FlareLine

    This is my main class:

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. SettingsManager sm = SettingsManager.getInstance();
    4.  
    5. public void onEnable() {
    6. sm.setup(this);
    7. getCommand("glist").setExecutor(new Commands(this));
    8. }
    9.  
    10. Commands cmds = new Commands();
    11.  
    12.  
    13. }
    14.  


    When i try to make an isntance of the Commands class, i get The constructor Commands() is undefined.


    Here is what i have in the Commands class before the onCommand method:

    Code:java
    1. public class Commands implements CommandExecutor {
    2.  
    3. String cprefix = ChatColor.LIGHT_PURPLE + "[GamersList] ";
    4.  
    5. SettingsManager sm = SettingsManager.getInstance();
    6.  
    7. List<String> playerList = sm.players.getStringList("Players");
    8.  
    9. public Main plugin;
    10.  
    11. public Commands(Main main) {
    12. plugin = main;
    13. }
     
  5. Offline

    Europia79

    1. inside Main, define the field outside/before onEnable()
    2. inside onEnable() instantiate a new object and assign it to the cmds field
    3. set the executor and pass in the cmds field as an argument

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. SettingsManager sm = SettingsManager.getInstance();
    4.  
    5. Commands cmds;
    6.  
    7. public void onEnable() {
    8.  
    9. sm.setup(this);
    10.  
    11. cmds = new Commands(this);
    12.  
    13. getCommand("glist").setExecutor(cmds);
    14. }
    15.  
    16. }
     
    theViTALiTY likes this.
Thread Status:
Not open for further replies.

Share This Page