Getting Multiple Players

Discussion in 'Plugin Development' started by JDJ, Jan 15, 2015.

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

    JDJ

    Basically, I am creating a minigame. I have added a feature where you can add and remove players from the game, but I want to know how I can use the list. So for example, here is the list:

    Code:
      public static List<Player> playersInGame = new ArrayList<Player>();
    I want to know how to take the players in that list, and (for example) send them all a message.

    Also, would using their names in the list be more effective and less buggy?
     
  2. Offline

    nverdier

    @JDJ It's not recommended to use Player in a List. Just use their names, it will be fine. Just make sure to remove them if they log out etc. You would just iterate through it and send the player in the current iteration a message. But why is the List static?
     
  3. Offline

    JDJ

    I needed to access it through multiple classes pretty much. Can you give me an example of code?
     
  4. Offline

    nverdier

    @JDJ Use a constructor to pass an instance of the class.
     
  5. Offline

    JDJ

    I mean like can I have an actual example of code. I have a hard time understanding people sometimes.
     
    Last edited: Jan 15, 2015
  6. Offline

    Skionz

    @JDJ
    Class one
    Code:
    public class Student() {
        private String name;
        private int grade;
    
        public Student(String name, int grade) {
            this.name = name;
            this.grade = grade;
        }
      
        public String getName() [
            return this.name;
        }
    
        public int getGrade() [
            return this.grade;
        }
    }
    Class two
    Code:
    public class Main() {
        public static void main(String[] args) {
            Student student = new Student("Joe", 12);
            System.out.println(student.getName());
        }
    }
     
  7. Offline

    mine-care

    @JDJ guys above are right, extreme static leads to ram leakage when not handled correctly, especially when it contains a player. You can keep lists in main class since it is te one that does not get initialized more than 1ce and then pass the list through a constructor, @Skionz provided a great example.
     
  8. Offline

    unrealdesign

    Technically it can easily get initialized more than once, just you shouldn't :p
     
  9. Offline

    nverdier

    @unrealdesign No, you cannot initialize the Main class more than once.


    And.... 1,000 posts.
     
  10. Offline

    unrealdesign

    Oh wow, never knew they canceled that, smart lol.
     
Thread Status:
Not open for further replies.

Share This Page