TPS Monitor

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 that shows the tps, servername, and if its online or not and when i do /monitor the tps has tons of numbers for example "TPS: 19.80982567353407" as the tps. How do i shorten the tps to like "TPS: 19.80". Also i there a way to get the servers status? Like "Status: Online/Orffline" or something?

    Main 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.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("§e§l(!)§r §eServer §7: §8(§7" + Bukkit.getServerName() + "§8)");
                player.sendMessage("§e§l(!)§r §eTPS §7: " + TPS.getTPS());
                player.sendMessage("§e§l(!)§r §eServer §7: §8(§aOnline§8)");
                player.sendMessage("");
                player.sendMessage("§8§m-----------------------");
                    }
                }
            return false;
        }
    }
    TPS Code:
    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("[AntiLag] 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;
      }
    }
     
    Last edited: Aug 20, 2016
    jyhsu likes this.
  2. Offline

    I Al Istannen

    @GodzillaFlame42
    Create a DecimalFormat with the pattern "#.##"
    The pattern is a lot more configurable than that, it is just a quick example.
    Then just use DecimalFormat#format(double).

    @AlvinB ninjad me.

    And what is that??
    "new long['»'];"
    Why not use the number directly? That is one hell of a magic number.

    "Bukkit.getLogger().severe("[AntiLag] An error occured whilst retrieving the TPS");"
    Consider using your plugin's logger.

    "TICK_COUNT" is no constant. Don't format it as one. Just name it "tickCount". Same for the others.

    Why is everything static? You can also use an instance.
     
  3. Offline

    I Al Istannen

    @GodzillaFlame42
    Look at the link Alvin posted. It is explained quite well, but I will repeat the import section:
    Symbols:
    • # A number. May be there or not.
    • 0 A number. Will be displayed, even if it is zero.
    • . The decimal point
    So we have the number "200.00000".
    Patterns:
    • "#.##"
      • "200"
    • "0.00"
      • "200.00"
    If he have "200.22"
    Patterns:
    • "#.##"
      • "200.22"
    • "0.00"
      • "200.22"
    If we have "200.226"
    Patterns:
    • "#.##"
      • "200.23"
    • "0.00"
      • "200.23"
     
  4. @I Al Istannen I honestly do not understand how to do it still with the help of you showing me this.
     
  5. Offline

    I Al Istannen

    @GodzillaFlame42
    You want it to show "19.80982567353407" as "19.80".
    Use this pattern: "#.##"
     
  6. @I Al Istannen I cant figure out where to put that would i put it in the TPS class or the Main

    Main 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.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("§e§l(!)§r §eServer §7: §8(§7" + Bukkit.getServerName() + "§8)");
                player.sendMessage("§e§l(!)§r §eTPS §7: " + TPS.getTPS() + "#.##");
                player.sendMessage("§e§l(!)§r §eServer §7: §8(§aOnline§8)");
                player.sendMessage("");
                player.sendMessage("§8§m-----------------------");
                    }
                }
            return false;
        }
    }
     
  7. Offline

    I Al Istannen

    @GodzillaFlame42
    Return true if the command was executed correctly.

    Either change the package or rename the main class. Otherwise not two of your plugins will be compatible with each other, as they have the same name for their classes.

    Don't use the color char "§" directly. Make a utility method "color" which colors a String and use "&" as color char.

    Apply it in the message in the onCommand.
    Define a formatter as a class variable (field). Set it's pattern to "#.##".
    Replace TPS.getTPS() with the formatted one [formatter#format(TPS.getTPS())].
     
  8. @I Al Istannen i think im too newby at java to understand this :( i dont know what you mean by "Define a formatter as a class variable (field). Set it's pattern to "#.##"."
     
  9. Offline

    I Al Istannen

    @GodzillaFlame42
    That could actually be the case. I would recommend learning a bit more Java, if you don't know what a field (class variable) is.

    It should look this way:

    "DecimalFormat formatter = new DecimalFormat("#.##");

    public void onEnable() {"
    and so on

    Then replace "TPS.getTPS()" with "formatter.format(TPS.getTPS())".

    Though this is spoonfeeding and not really wanted in this forum as most people don't learn from it.

    This is a field. A variable declared in a class.

    And I hope you fixed the other things I mentioned :)
     
  10. @I Al Istannen ok Thank you so much! But one more thing... How would i add a timer where it sends them this message every 5 minutes?
     
  11. Offline

    InstanceofDeath

    A field is a array declaraded in a class .. Such a variable is a class variable or a just simplyfied a static variable . I know that eclipse says something other
     
Thread Status:
Not open for further replies.

Share This Page