Async task without crash server, how?

Discussion in 'Plugin Development' started by Pr07o7yp3, Apr 24, 2013.

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

    Pr07o7yp3

    Hey, I need to start Async task because I need real time by TPS but I know it's not good idea to use Async and change something in the game. So, which is the best way to do that? I need to start Async repeat task between 20 TPS.
     
  2. Offline

    microgeek

  3. Offline

    LazyLemons

    Lag.java:
    Code:
    public class Lag
      implements Runnable
    {
      public static int TickCount = 0;
      public static long[] Ticks = new long[600];
      public static long LastTick = 0L;
     
      public static double getTPS()
      {
        return getTPS(100);
      }
     
      public static double getTPS(int ticks)
      {
        if (TickCount < ticks) {
          return 20.0D;
        }
        int target = (TickCount - 1 - ticks) % Ticks.length;
        long elapsed = System.currentTimeMillis() - Ticks[target];
     
        return ticks / (elapsed / 1000.0D);
      }
     
      public static long getElapsed(int tickID)
      {
        if (TickCount - tickID >= Ticks.length)
        {
        }
     
        long time = Ticks[(tickID % Ticks.length)];
        return System.currentTimeMillis() - time;
      }
     
      public void run()
      {
        Ticks[(TickCount % Ticks.length)] = System.currentTimeMillis();
     
        TickCount += 1;
      }
    }
    
    Put this in your onEnable() method:
    Code:
     Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Lag(), 100L, 1L);
    To get TPS :
    Code:
     Lag.getTPS() 
     
  4. Offline

    skipperguy12

    LazyLemons
    That is soo cool, you should really put that in the resources section, it's helpful!
     
  5. Offline

    Pr07o7yp3

    Thanks. :)
     
  6. Offline

    evilmidget38

    LazyLemons What's with the empty code block in Lag.getElapsed ? Also, why make everything static? And if you're going to make everything static, you should probably followed the naming conventions for static fields.
     
Thread Status:
Not open for further replies.

Share This Page