Best way to get the area with most players?

Discussion in 'Plugin Development' started by Swakiny, Mar 21, 2016.

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

    Swakiny

    I need to get which area has more player, which is the best way?

    I really need to loop through all players and get nearby players or there is other way?
     
  2. Loop through all players and check if their location is inside a arena (loop through all areas) to see how many players are in which area
     
  3. Offline

    mcdorli

    Why would looping trough the players be a problem?
     
  4. Offline

    mine-care

    @mcdorli mabe if the code runs many times it would be inefficient.
    An alternative would be to cache the arena and who is in it (An arena object with a list of who is in it for instance) and instead of looping through all players, determining their arena, and then keeping counters for each of them, loop directly through all Arena's and check the ammount of players each contains. That heavilty relies on the way you have implemented the code so far, and may be more inefficient than the pre-mentioned way(s)
     
  5. Offline

    mcdorli

    The biggest server I've ever seen was around 500-600. Now, looping trough those 600 instances getting their locations and incrementing an integer with one, if that location is inside a cuboid area shouldn't take more than couple of nanoseconds
     
  6. Offline

    Swakiny

    I'm trying to do a event that occurs every 10-30 minuts, checking in the whole world which area has more players then do something
     
  7. Offline

    mythbusterma

    @Swakiny

    That's fine. Just outright do it.
     
  8. Offline

    mine-care

    @mcdorli That is actuall up to many things, such as hardware capabilities, load etc. The point is to stay efficient ;)
    So on one side it is a couple ns or ms before the code executes fully, but yet the other solution would be faster.
     
  9. If you try to achieve effiency: try out booth possibilities and log how fast they are, then choose the faster one
     
  10. Offline

    mythbusterma

    @FisheyLP

    Or just don't bother. The amount of work he's doing is so minuscule so as not to matter.
     
  11. It's still a good idea to know what things to use and dont use for plugins. Maybe it doesn't matter in this particular plugin but in another?
     
  12. Offline

    mythbusterma

  13. Offline

    Swakiny


    I'm a little bit worried. I want to make it work faster as I can, if something can make server 0.0000000005x slower would be great to know why and if there's another method
     
  14. Offline

    mythbusterma

    @Swakiny

    All together now,

    Preemptive optimisation is the root of all evil.

    In all seriousness, the way you should do this is the way that makes the most sense to read, debug, etc. Your goal should not be performance at all costs, because that leads to unmaintainable code that is completely unreadable. Do it the way that is the most comfortable and readable and you will have a much better time.
     
  15. Offline

    Swakiny

    Oh, got it. Okay then.
     
  16. Offline

    Sir_Cam

    @mcdorli
    A nano-second is one-billionth of a second. I would imagine it would take a wee longer than that.

    @Swakiny
    If you have (which i would hope, just for the sake of making it easier) all the 'areas' in a config somewhere and then on reload have the plugin read all the LocationSets(X, Y ; X, Y). Then when it does run, loop through each Area set, then looping through each player to determine which area their coordinates are inside of. Followed by adding a number to a temporary ArrayList or HashMap. Then checking which has the most players would be as simple as going through each Area you have and comparing it to a temporary variable that, if greater than, would set the name of the area, or number of players in such area to that variable.

    This is merely the base of how to accomplish this, the code and how you personally want to do it is obviously left for you to figure out.
     
    Last edited: Mar 23, 2016
  17. Offline

    Swakiny


    Right now i'm using this:
    And isn't good at all, since the code will stop when a player has more than 1 nearby players, instead of keep looking for who player has more players nearby...

    Code:
     new BukkitRunnable()
                {
                    Player plz;
                    public Boolean canTerminate = false;
                    public void run()
                    {
                        Bukkit.broadcastMessage("test2");
                        loop:
                        for (Player p : worldnormal.getPlayers())
                        {
                                for(Entity le : p.getNearbyEntities(15,15,15))
                                {
                                    Bukkit.broadcastMessage("test3");
    
                                        if (le instanceof Player)
                                        {
                                            Bukkit.broadcastMessage("test4");
                                            if(!plist.contains(p.getName()))
                                            {
                                                plist.add(p.getName());
                                            }
                                        }
                                }
                            if(plist.size() > 0)
                            {
                                plz = p;
                                break loop;
                            }
                            else if(plist.size() == 0)
                            {
                                plist.clear();
                            }
    
                        }
                        if(plist.size() > 0 && plist.size() <= 8)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie = worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.GREEN + "Giant Level 1");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(1);
                            this.cancel();
                        }
                        else if(plist.size() > 8 && plist.size() <= 13)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie = worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.AQUA + "Giant Level 2");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(2);
                            this.cancel();
                        }
                        else if(plist.size() > 13 && plist.size() <= 20)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie =  worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.RED + "Giant Level 3");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(3);
                            this.cancel();
                        }
                        else if(plist.size() > 20)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie =  worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.GOLD + "Giant Level 4");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(4);
                            this.cancel();
                        }
                        else
                        {
                            Bukkit.broadcastMessage("Event down");
                            plist.clear();
                            this.cancel();
                        }
                    }
                }.runTaskTimer(Beat.pl, 100,100);

    @Edit hope it works
    I'm not breaking the loop anymore. I'm waiting till the code loop through all players then setting these booleans. Once the server finish the loop the code will only have 1 boolean set to true
    Code:
     new BukkitRunnable()
                {
                    Player plz;
                    public Boolean canTerminate = false;
                    public void run()
                    {
    
                        Bukkit.broadcastMessage("test2");
                        loop:
                        for (Player p : worldnormal.getPlayers())
                        {
                            List<String> plist = new ArrayList<String>();
                                for(Entity le : p.getNearbyEntities(15,15,15))
                                {
                                    Bukkit.broadcastMessage("test3");
    
                                        if (le instanceof Player)
                                        {
                                            Bukkit.broadcastMessage("test4");
                                            if(!plist.contains(p.getName()))
                                            {
                                                plist.add(p.getName());
                                            }
                                        }
                                }
                            if(plist.size() > 3 && plist.size() <= 8)
                            {
                                plz = p;
                                level1 = true;
                            }
                            else if(plist.size() > 8 && plist.size() <= 13)
                            {
                                plz = p;
                                level2 = true;
                                level1 = false;
                            }
                            else if(plist.size() > 13 && plist.size() <= 20)
                            {
                                plz = p;
                                level3 = true;
                                level2 = false;
                                level1 = false;
                            }
                            else if(plist.size() > 20)
                            {
                                plz = p;
                                level4 = true;
                                level3 = false;
                                level2 = false;
                                level1 = false;
                            }
    
                        }
                        if(level1 == true)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie = worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.GREEN + "Giant Level 1");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(1);
                            this.cancel();
                        }
                        else if(level2 == true)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie = worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.AQUA + "Giant Level 2");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(2);
                            this.cancel();
                        }
                        else if(level3 == true)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie =  worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.RED + "Giant Level 3");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(3);
                            this.cancel();
                        }
                        else if(level4 == true)
                        {
                            Location local = plz.getLocation();
                            Entity giantZombie =  worldnormal.spawnEntity(local, EntityType.GIANT);
                            giantZombie.setCustomName(ChatColor.GOLD + "Giant Level 4");
                            giantZombie.setCustomNameVisible(true);
                            giantEventLevel(4);
                            this.cancel();
                        }
                        else
                        {
                            Bukkit.broadcastMessage("Event down");
                            this.cancel();
                        }
                    }
                }.runTaskTimer(Beat.pl, 100,100);
     
    Last edited: Mar 24, 2016
Thread Status:
Not open for further replies.

Share This Page