List traveling?

Discussion in 'Plugin Development' started by CdoingBaddie, Aug 27, 2013.

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

    CdoingBaddie

    How do I make it so if I make a list in 1 place, I can make it go to another? For instance: Making a list in package: me.bukkit.org and in class: main and then making it so it can go to package: me.bukkit.cmd and class: main2
    Help please?
     
  2. Offline

    Compressions

  3. Offline

    CdoingBaddie

    What's that?
     
  4. Offline

    FurmigaHumana

    Leys say your main class is called MainClass and you need to access the list on the class called OtherClass.

    Show Spoiler

    Code:java
    1. public class MainClass extends JavaPlugin {
    2.  
    3. // this is the list that will be used in another class/package
    4. // is must be set as public (the keyword public the other guy told you)
    5. // the public keyword means it can be accessed from any package
    6. public List<String> thelist = new ArrayList<String>();
    7.  
    8. public void onEnable() {
    9.  
    10. // constructing a new OtherClass, using the MainClass
    11. OtherClass otherclass = new OtherClass(this);
    12.  
    13. }
    14.  
    15. public void onDisable() {
    16. ... // the disable method, not improtant now
    17. }
    18.  
    19. public void onCommand(...) {
    20. // using the list on the MainClass, it is the same class of the list, so we can access it directly
    21. thelist.add(sender.getName());
    22. }
    23. }
    24.  
    25. public class OtherClass {
    26.  
    27. // this is the other class that will access that list, in way to do that
    28. // we just need to define an reference to the MainClass, like this:
    29. public MainClass main;
    30.  
    31. public OtherClass(MainClass main) {
    32. this.main = main;
    33. }
    34.  
    35. public void someMethod() {
    36. // using the list inside an method on the other class
    37. List<String> same_list = main.thelist;
    38.  
    39. }
    40.  
    41. public boolean anotherMethod() {
    42. // using the list inside an method on the other class
    43. return main.thelist.contains("bla");
    44. }
    45. }



    Or you could try static access, but it does not fit in some cases.

    Show Spoiler

    Code:java
    1. public class MainClass extends JavaPlugin {
    2.  
    3. // using static, it can be directly accessed from any package using MainClass.thelist
    4. public static List<String> thelist = new ArrayList<String>();
    5.  
    6. public void onEnable() {
    7.  
    8. // constructing a new OtherClass, using the MainClass, not "this" argument.
    9. OtherClass otherclass = new OtherClass();
    10.  
    11. }
    12.  
    13. public void onDisable() {
    14. ... // the disable method, not improtant now
    15. }
    16.  
    17. public void onCommand(...) {
    18. // using the list on the MainClass, it is the same class of the list, so we can access it directly
    19. thelist.add(sender.getName());
    20. }
    21. }
    22.  
    23. public class OtherClass {
    24.  
    25. public void someMethod() {
    26. List<String> same_list = MainClass.thelist;
    27.  
    28. }
    29.  
    30. public boolean anotherMethod() {
    31. return MainClass.thelist.contains("bla");
    32. }
    33. }



    This is not easy to explain but is pure basic java :) :)
     
  5. Offline

    newboyhun

Thread Status:
Not open for further replies.

Share This Page