Monitor Plugin Reapeating Timer

Discussion in 'Plugin Development' started by GodzillaFlame42, Aug 20, 2016.

Thread Status:
Not open for further replies.
  1. So i am coding a monitor plugin which just like gives the tps and other stuff but i want it to send it to whoever is op every 5 minutes. And its not working. Someone please help me. I also dont know how to get the servers memory cause i wanted to add that too on the line.

    Code:
    Code:
    package me.godzilla;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        public void onEnable() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, (Runnable) new Main(), 20, 200);
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, (Runnable) new Main(), 100L, 1L);
        }
       
        public static int TICK_COUNT= 0;
        public static long[] TICKS= new long[600];
        public static long LAST_TICK= 0L;
    
        public static double getTPS()
        {
          return getTPS(100);
        }
    
        public static double getTPS(int ticks)
        {
          if (TICK_COUNT< ticks) {
            return 20.0D;
          }
          int target = (TICK_COUNT- 1 - ticks) % TICKS.length;
          long elapsed = System.currentTimeMillis() - TICKS[target];
    
          return ticks / (elapsed / 1000.0D);
        }
    
        public static long getElapsed(int tickID)
        {
          if (TICK_COUNT- tickID >= TICKS.length)
          {
          }
    
          long time = TICKS[(tickID % TICKS.length)];
          return System.currentTimeMillis() - time;
        }
    
        public void run()
        {
          TICKS[(TICK_COUNT% TICKS.length)] = System.currentTimeMillis();
    
          TICK_COUNT+= 1;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("monitor")){
                if(sender instanceof Player) {
                Player player = (Player) sender;
                if(player.isOp());
                ChatColor.translateAlternateColorCodes('&', "§");
                player.sendMessage("§8§m-----------------------");
                player.sendMessage("");
                player.sendMessage("§8» §e§l(!) §eServer: §7Factions");
                player.sendMessage("§8» §e§l(!) §eTPS: §7" + Main.getTPS());
                player.sendMessage("§8» §e§l(!) §eStatus: §aOnline");
                player.sendMessage("");
                player.sendMessage("§8§m-----------------------");
               
                }
        }
            return false;
        }
    }
     
  2. Offline

    Tecno_Wizard

  3. How would i do a timer that sends a server check up to them every 5 minutes?

    Main Class:
    Code:
    package me.godzilla;
    
    import java.text.DecimalFormat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        DecimalFormat formatter = new DecimalFormat("##.##");
       
        public void onEnable() {
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new TPS(), 100L, 1L);
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("monitor")){
                if(sender instanceof Player) {
                Player player = (Player) sender;
                if(player.isOp());
                ChatColor.translateAlternateColorCodes('&', "§");
                player.sendMessage("§8§m-----------------------");
                player.sendMessage("");
                player.sendMessage("§b§l(!)§r §bServer §7: §8(§7" + Bukkit.getServerName() + "§8)");
                player.sendMessage("§b§l(!)§r §bTPS §7: " + formatter.format(TPS.getTPS()));
                player.sendMessage("§b§l(!)§r §bServer §7: §8(§aOnline§8)");
                player.sendMessage("");
                player.sendMessage("§8§m-----------------------");
                return true;
                    }
                }
            return false;
        }
    }
    TPS Class:
    Code:
    package me.godzilla;
    
    import org.bukkit.Bukkit;
    
    public class TPS
      implements Runnable
    {
      public static int TICK_COUNT = 0;
      public static long[] TICKS = new long['»'];
      public static long LAST_TICK = 0L;
     
      public static double getTPS()
      {
        return getTPS(100);
      }
     
      public static double getTPS(int ticks)
      {
        try
        {
          if (TICK_COUNT < ticks) {
            return 20.0D;
          }
          int target = (TICK_COUNT - 1 - ticks) % TICKS.length;
          long elapsed = System.currentTimeMillis() - TICKS[target];
         
          return ticks / (elapsed / 1000.0D);
        }
        catch (Exception e)
        {
          if ((e instanceof ArrayIndexOutOfBoundsException)) {
            return 20.0D;
          }
          Bukkit.getLogger().severe("An error occured whilst retrieving the TPS");
        }
        return 20.0D;
      }
     
      public static long getElapsed(int tickID)
      {
       
        long time = TICKS[(tickID % TICKS.length)];
        return System.currentTimeMillis() - time;
      }
     
      public void run()
      {
        TICKS[(TICK_COUNT % TICKS.length)] = System.currentTimeMillis();
       
        TICK_COUNT += 1;
      }
    }
     
  4. Offline

    InstanceofDeath

    The java.util.date class gives you access to an object with the local time .. java stands for the standard libery, javax is the other option. util classes have normally something to do with time, random and useful functions. If you asked for something other ... reply
     
  5. Offline

    InstanceofDeath

    sry, but your code doesnt look like your a newbie .. It looks nearly professionel

    So what do you want exactly? Every 5 minutes there gets a normal message get broadcasted out or the date ?? I am just confused

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  6. @InstanceofDeath Well i wanted it to send that message to all ops every 5 minutes. Also that you for the compliment :)
     
  7. Offline

    InstanceofDeath

    Then just normally use a bukkit scheduler .. There are many tutorials out there and in that scheduler you write something like this
    <syntax>
    Date date = new Date()
    for(Player op : Bukkit.getOnlinePlayers) {
    if(op.isOp()) {
    op.sendMessage(Date);
    }
    }

    </syntax>
     
  8. Offline

    Zombie_Striker

    @InstanceofDeath
    There is a far simpler way to achieve this:

    @GodzillaFlame42
    Using Schedulers (scroll down to the bottom for an example), create a repeating task that repeats every 5 minutes. Every time it repeats, check up on the server.
     
  9. Offline

    InstanceofDeath

    Ähm what I just wrote? :€
     
  10. Offline

    timtower Administrator Administrator Moderator

    Merged threads
     
  11. @GodzillaFlame42
    Instead of handling exceptions by catching all exceptions and instanceof checking for your exception, put the exception you want to catch in the catch statement. Also, why are you even catching an ArrayIndexOutOfBoundsException? Fix where the exception would get thrown, instead of catching it.
     
Thread Status:
Not open for further replies.

Share This Page