Player removed from arraylist?

Discussion in 'Plugin Development' started by Ty Cleere, Jul 3, 2013.

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

    Ty Cleere

    So during my match if somebody dies i want the server to broadcast a message saying that this player has died and there is _ people left in the match. So my question is is there a way to look at the arraylist i have made and then broadcast how many people are left after somebody dies and if so how can i do that.
     
  2. Offline

    Garris0n

    Well you could iterate through all the values in the arraylist and count each one, or just do yourarraylist.size() :p
     
  3. Offline

    skore87

    As long as the event listener has access to that arraylist then you can simply remove them from that list during the death event and broadcast your message at the same time.
     
  4. Offline

    Ty Cleere

    lol so how do i do that? in the EventHandler i would put "yourarraylist.size();" and broadcast that?

    skore87 so then i would remove the player that has died and then how do i broadcast the number of players in the arraylist?

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

    Garris0n

    Don't store player instances, and
    Bukkit.getServer().broadcastMessage("There are " + yourarraylist.size() + " players left.");

    What do you mean by "get it wrong", and offline servers aren't supported by Bukkit. Also, it's bad practice to store players or blocks.

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

    xXSilentYoshiXx

    Main Class contains:
    Code:
        public ArrayList<Player> players = new ArrayList<Player>();
    Command Class:
    Code:
        @Override
        public boolean onCommand(CommandSender s, Command cmd, String cl, String[] args) {
            if (cmd.getName().equalsIgnoreCase("joingame")) {
                if (s instanceof Player) {
                    Player player = (Player) s;
                 
                    MainClass.players.add(player);
                } else {
                    s.sendMessage("Players only!");
                }
            }
        }
    Death Listener (Or Whatever):
    Code:
        @EventHandler(priority = EventPriority.{PRIORITY}
        public void onPlayerDeath(PlayerDeathEvent e) {
            Player player = (Player) e.getEntity();
         
            if (MainClass.players.contains(player)) {
                MainClass.players.remove(player);
                Bukkit.broadcastMessage("[{PLUGINNAME}] There are " + MainClass.players.size() + " players remaining!");
            }
        }
    This is the "Storing Player" method. :p

    lol, thanks XD (Unless if it was sarcasm >_>)

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

    minecraft124_

    Don't store Player objects. Get the player's name and add that to your arraylist, then get the size. Storing Player objects is bad, it can lead to memory leaks and other issues.

    Well, for one, storing the ENTIRE player object, the player's health, potion effects, location, everything about that player in a map is unnecessary, since there is never a need to store all of that in a list. If you do have a Player stored in a map, and that player leaves the server, the instance of that Player is in the map, and java GC can't remove it. This will lead to memory leaks, and possibly issues with other plugins.

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

    Minnymin3

    T3h Cr33p3r
    DON'T STORE PLAYER INSTANCES! It will cause major memory leak on anything storing more than one or two players. It would be like doubling the number of players online.
     
    Garris0n likes this.
  9. Offline

    Garris0n

    You don't save them because it can lead to memory leaks. Search around if you want more information...
     
  10. Offline

    xXSilentYoshiXx

    OK, then.

    Main Class:
    Code:
        public ArrayList<String> players = new ArrayList<String>();
    Command Class:
    Code:
        @Override
        public boolean onCommand(CommandSender s, Command cmd, String cl, String[] args) {
            if (cmd.getName().equalsIgnoreCase("joingame")) {
                if (s instanceof Player) {
                    Player player = (Player) s;
               
                    MainClass.players.add(player.getName());
                } else {
                    s.sendMessage("Players only!");
                }
            }
        }
    Listener Class:
    Code:
        @EventHandler(priority = EventPriority.{PRIORITY}
        public void onPlayerDeath(PlayerDeathEvent e) {
            Player player = (Player) e.getEntity();
       
            if (MainClass.players.contains(player.getName())) {
                MainClass.players.remove(player.getName());
                Bukkit.broadcastMessage("[{PLUGINNAME}] There are " + MainClass.players.size() + " players remaining!");
            }
        }
    EDIT: This is the "Storing Name" method. :p
     
  11. Offline

    Rockon999

    Minnymin3 likes this.
  12. Offline

    Minnymin3

    Learn some more programming and computer usage lingo and you'll know why its bad.

    TENS OF PEOPLE DIE EVERY DECADE FROM MEMORY LEAKS! HELP PREVENT THIS BY MAKING YOUR PLUGIN NOT LEAK! :p

    Is there a dislike button on the forums?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
    Ty Cleere likes this.
Thread Status:
Not open for further replies.

Share This Page