Solved Getting a List from a seperate Class

Discussion in 'Plugin Development' started by _Belknap_, Apr 23, 2014.

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

    _Belknap_

    Hi, I have this really annoying problem. You see, I can't seem to pass any information about an List<String> in one class to the other. I am trying to set it so that when a player places a sign with certain stuff on it, it sets the 3rd line to [sizeofplayersindungeon/maxplayers]. I can show the size of any List in that specific class, but not the size of the List in my seperate class. Here is my code:
    Code:java
    1. //Class with the SignChangeEvent(Dungeons)
    2. @EventHandler
    3. public void onSignChange(SignChangeEvent sign) {
    4. Player p = sign.getPlayer();
    5. String dName = sign.getLine(1);
    6. if (p.isOp()) {
    7. if (sign.getLine(0).equalsIgnoreCase("Dungeons"));
    8. sign.setLine(0, ChatColor.BOLD + "Dungeons");
    9. DungeonManager d = getDungeon(dName);
    10. sign.setLine(2,ChatColor.DARK_BLUE + "[" + d.getPlayers()".size + "/" + d.getMaxPlayers() + "]");
    11. }else{
    12.  
    13. }
    14. }

    Then, I use this List and a few method to get its information in another class:
    Code:java
    1. //Other class with List and methods:
    2. //List
    3. private List<String> players;
    4. //Methods:
    5. }
    6. public void addPlayer(Player p) {
    7. String name = p.getName();
    8. players.add(name);
    9. }
    10. public void removePlayer(Player p) {
    11. String name = p.getName();
    12. players.remove(name);
    13. }
    14. public boolean hasPlayer(Player p) {
    15. if (getPlayers().contains(p.getName())) {
    16. return true;
    17. }else{
    18. return false;
    19. }
    20. }
    21.  
    22. public List<String> getPlayers() {
    23. return players;
    24. }
     
  2. Offline

    ko47374737

    _Belknap_
    Lol the List is Private it needs to be public for other classes to access it
    so replace public with public static
     
  3. Offline

    _Belknap_

    ko47374737
    Lol I already tried that but I'll try it again

    Lol I am dying here I did this:
    Code:java
    1. public ArrayList<String> players;

    Instead of this:
    Code:java
    1. ArrayList<String> players = new ArrayList<String>


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

    gorbb

    _Belknap_

    The getPlayers method will work fine for getting the list, looks like your error is line 10 in the event code.

    You've put:
    Code:java
    1. sign.setLine(2,ChatColor.DARK_BLUE + "[" + d.getPlayers()".size + "/" + d.getMaxPlayers() + "]");


    Instead, try:
    Code:java
    1. sign.setLine(2,ChatColor.DARK_BLUE + "[" + d.getPlayers().size() + "/" + d.getMaxPlayers() + "]");


    Hope that helps. :)
     
  5. Offline

    1Rogue

    coasterman10 likes this.
  6. Offline

    _Belknap_

    1Rogue
    lol getPlayers() is my getter is there anything wrong with it that I can change?
    gorbb
    Lol sry that was a typo
     
  7. Offline

    1Rogue


    Depends, if you really want to have control over the list, I would make it unmodifiable outside the class and create aggregate functions for adding/removing:

    Code:java
    1. private final List<String> players; //set how you like
    2.  
    3. public List<String> getPlayers() {
    4. return Collections.unmodifiableList(this.players);
    5. }
    6.  
    7. public void addPlayer(String player) {
    8. this.players.add(player);
    9. }
    10.  
    11. public boolean removePlayer(String player) {
    12. return this.players.remove(player);
    13. }
     
  8. Offline

    coasterman10

    Very, very bad idea. Do you even understand what static is?
     
  9. Offline

    amhokies

    Ugh...
     
    coasterman10 likes this.
Thread Status:
Not open for further replies.

Share This Page