Checking ArrayList in another Class Help!

Discussion in 'Plugin Development' started by MrGiGimz, Jun 13, 2014.

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

    MrGiGimz

    Hello. So I'm checking when a sign is clicked like this:

    Code:java
    1. @EventHandler
    2. public void onJoinOne(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. Location loc = new Location(Bukkit.getWorld("world"), 0.5, 75, -13);
    5. loc.setPitch((float) 3);
    6. loc.setYaw((float)3);
    7.  
    8. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    9. if (e.getClickedBlock().getState() instanceof Sign) {
    10. Bukkit.broadcastMessage("DEBUG : 1");
    11. Sign s = (Sign) e.getClickedBlock().getState();
    12. if (s.getLine(1).equalsIgnoreCase("§4[Arena 1]")) {
    13. Bukkit.broadcastMessage("DEBUG : 2");
    14. if(s.getLine(3).equalsIgnoreCase("§bTo Leave Game")){
    15. Bukkit.broadcastMessage("DEBUG : 3");
    16. Block b = Bukkit.getWorld("world").getBlockAt(-8, 76, -29);
    17. Sign s1 = (Sign) b.getState();
    18. if(s1.getLine(3).equalsIgnoreCase("§b[1/6]")){
    19. if(ArenaOne.contains(p)){
    20. Bukkit.broadcastMessage("DEBUG : 4");
    21. }
    22. }
    23. }
    24. }
    25. }
    26. }


    and the class with the main Array List:

    Code:java
    1. ArrayList<Player> ArenaOne = new ArrayList<Player>();
    2. ArrayList<Player> P1 = new ArrayList<Player>();
    3. ArrayList<Player> P2 = new ArrayList<Player>();
    4. ArrayList<Player> P3 = new ArrayList<Player>();
    5. ArrayList<Player> P4 = new ArrayList<Player>();
    6. ArrayList<Player> P5 = new ArrayList<Player>();
    7. ArrayList<Player> P6 = new ArrayList<Player>();


    when I check for the debug it doesnt work. WhenI try to import that class it wont let me import that class name. How do I check if the arraylist contains a player across 2 different classes? The 2 classes are in the same package as well. Any help would be great!
     
  2. Offline

    1Rogue

    Pass your main class instance to the listener class when you create it.

    Also, why do you have 6 arraylists? Surely something like a map would be better suited.
     
    Garris0n likes this.
  3. Offline

    MrGiGimz

    1Rogue I dont have any clue how to use maps and i barley know how to use arraylist

    EDIT:

    this is my whole main class:

    Code:java
    1. package me.MrGiGimz.MiniUHC;
    2.  
    3.  
    4. import me.MrGiGimz.MiniUHC.ArenaOne.CreateJoinOne;
    5. import me.MrGiGimz.MiniUHC.ArenaOne.CreateLeaveOne;
    6. import me.MrGiGimz.MiniUHC.ArenaOne.JoinOne;
    7. import me.MrGiGimz.MiniUHC.ArenaOne.LeaveOne;
    8. import me.MrGiGimz.MiniUHC.ArenaTwo.CreateJoinTwo;
    9. import me.MrGiGimz.MiniUHC.ArenaTwo.JoinTwo;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.ChatColor;
    13. import org.bukkit.block.Block;
    14. import org.bukkit.block.Sign;
    15. import org.bukkit.command.Command;
    16. import org.bukkit.command.CommandSender;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.plugin.PluginManager;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class Main extends JavaPlugin implements Listener{
    23.  
    24. public String name = ChatColor.GREEN+"["+ChatColor.AQUA+"Mini-UHC"+ChatColor.GREEN+"]";
    25. public boolean status = true;
    26.  
    27. public void onEnable() {
    28. getLogger().info("Mini-UHC has been Enabled");
    29. registerListeners();
    30. }
    31.  
    32. public void onDisable() {
    33. getLogger().info("Mini-UHC has been Disabled");
    34. Block bOne = Bukkit.getWorld("world").getBlockAt(-2, 76, -19);
    35. Sign sOne = (Sign) bOne.getState();
    36. sOne.setLine(3, "§b[0/6]");
    37. sOne.update();
    38.  
    39. Block bbOne = Bukkit.getWorld("world").getBlockAt(-8, 76, -29);
    40. Sign ssOne = (Sign) bbOne.getState();
    41. ssOne.setLine(3, "§b[0/6]");
    42. ssOne.update();
    43.  
    44. Block bTwo = Bukkit.getWorld("world").getBlockAt(0, 76, -19);
    45. Sign sTwo = (Sign) bTwo.getState();
    46. sTwo.setLine(3, "§b[0/6]");
    47. sTwo.update();
    48.  
    49. Block bbTwo = Bukkit.getWorld("world").getBlockAt(0, 76, -29);
    50. Sign ssTwo = (Sign) bbTwo.getState();
    51. ssTwo.setLine(3, "§b[0/6]");
    52. ssTwo.update();
    53. }
    54.  
    55. public void registerListeners() {
    56. PluginManager pm = Bukkit.getServer().getPluginManager();
    57. pm.registerEvents(this, this);
    58. pm.registerEvents(new CreateJoinOne(), this);
    59. pm.registerEvents(new CreateLeaveOne(), this);
    60. pm.registerEvents(new JoinOne(), this);
    61. pm.registerEvents(new LeaveOne(), this);
    62. pm.registerEvents(new CreateJoinTwo(), this);
    63. pm.registerEvents(new JoinTwo(), this);
    64. }
    65.  
    66. public boolean onCommand(CommandSender sender, Command cmd, String label,
    67. String[] args) {
    68. Player p = (Player) sender;
    69. if(cmd.getLabel().equalsIgnoreCase("minireset")){
    70. if(p.isOp()){
    71. p.sendMessage(name+" Mini-UHC Arenas have been Reset!");
    72. Block bOne = Bukkit.getWorld("world").getBlockAt(-2, 76, -19);
    73. Sign sOne = (Sign) bOne.getState();
    74. sOne.setLine(3, "§b[0/6]");
    75. sOne.update();
    76.  
    77. Block bbOne = Bukkit.getWorld("world").getBlockAt(-8, 76, -29);
    78. Sign ssOne = (Sign) bbOne.getState();
    79. ssOne.setLine(3, "§b[0/6]");
    80. ssOne.update();
    81.  
    82. Block bTwo = Bukkit.getWorld("world").getBlockAt(0, 76, -19);
    83. Sign sTwo = (Sign) bTwo.getState();
    84. sTwo.setLine(3, "§b[0/6]");
    85. sTwo.update();
    86.  
    87. Block bbTwo = Bukkit.getWorld("world").getBlockAt(0, 76, -29);
    88. Sign ssTwo = (Sign) bbTwo.getState();
    89. ssTwo.setLine(3, "§b[0/6]");
    90. ssTwo.update();
    91. }
    92. }
    93. return false;
    94. }
    95.  
    96. }
     
  4. Offline

    1Rogue

  5. Offline

    MrGiGimz

    im kinda new to java non sense and i see you are a bukkitdev staff member so could you please explain to me visually what i should do? I dont understand any of that 1Rogue
     
  6. Offline

    Garris0n

  7. Offline

    MrGiGimz

  8. Offline

    Garris0n

    If you don't understand 1Rogue 's post, then yes, you are.

    And even if you weren't particularly new, the documentation gets fairly advanced over time...
     
  9. Offline

    xize

    MrGiGimz

    well one of the things to your question to let it work is to search what a constructor is, its not that hard.

    but the problem which is something what mocks me because ive had this time to: that is the problem with the ArrayLists, HashSets, and HashMaps from accessing from other classes!.

    on the internet is alot of false and good information but the thing what anoyed me is: what is the best ecouraged way since there are many many speculations about static is good or even bad to use it in classes.

    i've used alot of static which is actually not the good usage because you do not need it for anything!.

    so I want to write some code now (yes not with the constructor of course I'm not that easy, thats something what isn't to much confusing to search on google):

    now lets say you got 2 classes:

    class a:
    Code:
    public class Test {
             public ArrayList<Player> a = new ArrayList<Player>();
    }
    
    and this is class b:
    Code:
    public class Test2 {
           public void doSomething(Player p) {
                    Test test = new Test();
                    test.add(p);
           }
    }
    
    now what goes on is that atleast everytime you instance the Test class you create atleast one new ArrayList instance which means that you are using a other object and this results that it 'looks' the ArrayList does not work or is empty.

    one of the fixes:

    class a:
    Code:
    //constructor here or something
    private ArrayList<Player> players;
     
    public ArrayList<Player> getPlayers() {
           if(!(players instanceof ArrayList)) {
                 //arraylist not instanced make one now.
                this.players = new ArrayList<Player>();
           }
           return players;
    }
    
    then class b should be:
    Code:
    public void doSomething(Player p) {
                    Test test = new Test();
                    test.getPlayers().add(p); //if this throws a concurent modification exception or something else strangely make a getter or setter.
    
    in theory this should avoid instancing the ArrayList again if it still seems to happen you could either change it with the final keyword and do it like private final ArrayList<Player> players = new ArrayList<Player>(); or use the static keyword instead of final, but keep in mind that globalizing fields is actually a really discouraged practice you could better then globalize the class then and only if there is no other way around.

    again this is just a theory I made since a few weeks ago ive never tested it, but it could be a more encouraged way of working with HashMaps and ArrayLists instead of using the static approach which lots of people do and not to globalize but rather to make it not instanced again (which is not why static is designed imo).
     
  10. Offline

    Mrawesomecookie

    xize
    You don't need to spoon feed him code.
     
  11. Offline

    1Rogue


    He didn't. He supplied the OP with a thorough explanation and adaptation, which is very different from just doing the code for them.
     
    xize likes this.
Thread Status:
Not open for further replies.

Share This Page