Better way to do this?

Discussion in 'Plugin Development' started by Seegee, Mar 17, 2013.

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


    Hey guy, so what I need to do is be constantly checking if a player is in an area, and if they are, add "points" to a variable. This is an example:
        int blue1points = 0;
        int blue2points = 0;
        int red1points = 0;
        int red2points = 0;
        public void onPlayerMove(PlayerMoveEvent event){
            final Player player = event.getPlayer();
            final Location blue1 = new Location(player.getWorld(), 234,63,339);
            final Location blue2 = new Location(player.getWorld(), 207,63,377);
            final Location red1 = new Location(player.getWorld(), 139,63,308);
            final Location red2 = new Location(player.getWorld(), 162.6,63,269);
            player.getServer().getScheduler().scheduleAsyncRepeatingTask(new Plugin(), new Runnable() {
                  public void run() {
                        if(player.getLocation().distance(blue1) < 7) {
                        if(player.getLocation().distance(blue2) < 7) {
                        if(player.getLocation().distance(red1) < 7) {
                        if(player.getLocation().distance(red2) < 7) {
                }, 10, 10);
    This works but the points go up EXTREMELY fast and it crashed my server running 1gb of ram. I assumed that it would add a point every .5 seconds, because 10 tick, which is the interval, is .5 seconds, but I think that everytime a player moves, it will start a new "thread" of this runnable. What would be an alternative way to do this? I know that this is not the right way...
  2. Offline


    Maybe start it onEnable()?
  3. Offline


    How would I get stuff like the player.getLocation()? Would I have to check every player in the game?
  4. Offline


    I would suggest using a HashMap with the Player as the keys and BukkitTask as the value then whenever you get an onMove event do an if statement to check if there is a task already associated with that player.
  5. Please do not use the Player as a key, but only the players name. As long as people don't know how to handle it correctly, it can case memory leaks.
    LudicrousYoshi likes this.
  6. Offline


    Ah my bad! I typed out Player but in my head i actually meant the player's name haha

    thanks for catching that hapm you probably saved Seegee a lot of possible trouble in the future because of me :p
  7. Offline


    LudicrousYoshi I would also suggest to just use one scheduler in onEnable, (which btw should be sync instead of async, Seegee ), and just run over Bukkit.getOnlinePlayers() (or the set of players you want to run over)
Thread Status:
Not open for further replies.

Share This Page