TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_health.java

106 lines
3.5 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2012-12-27 23:27:59 +00:00
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
import java.text.DecimalFormat;
2013-08-10 16:39:50 +00:00
import java.util.concurrent.atomic.AtomicInteger;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
2013-08-10 16:39:50 +00:00
import org.apache.commons.lang.math.DoubleRange;
import org.bukkit.ChatColor;
2012-12-27 23:27:59 +00:00
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
2013-08-10 16:39:50 +00:00
import org.bukkit.scheduler.BukkitTask;
2012-12-27 23:27:59 +00:00
@CommandPermissions(level = PlayerRank.SUPER_ADMIN, source = SourceType.BOTH)
@CommandParameters(description = "View ticks-per-second", usage = "/<command>")
public class Command_health extends FreedomCommand
2012-12-27 23:27:59 +00:00
{
2013-08-10 16:39:50 +00:00
private static final int BYTES_PER_MB = 1024 * 1024;
private static final DoubleRange TPS_RANGE = new DoubleRange(20.0 - 0.1, 20.0 + 0.1);
2012-12-27 23:27:59 +00:00
@Override
public boolean run(final CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
2012-12-27 23:27:59 +00:00
{
2013-08-10 16:39:50 +00:00
Runtime runtime = Runtime.getRuntime();
long usedMem = runtime.totalMemory() - runtime.freeMemory();
msg("Reserved Memory: " + (double) runtime.totalMemory() / (double) BYTES_PER_MB + "mb");
msg("Used Memory: " + new DecimalFormat("#").format((double) usedMem / (double) BYTES_PER_MB)
+ "mb (" + new DecimalFormat("#").format(((double) usedMem / (double) runtime.totalMemory()) * 100.0) + "%)");
msg("Max Memory: " + (double) runtime.maxMemory() / (double) BYTES_PER_MB + "mb");
msg("Calculating ticks per second, please wait...");
new BukkitRunnable()
{
@Override
public void run()
{
try
{
2013-08-10 16:39:50 +00:00
TFM_TickMeter tickMeter = new TFM_TickMeter(plugin);
tickMeter.startTicking();
Thread.sleep(2500);
final double ticksPerSecond = tickMeter.stopTicking();
new BukkitRunnable()
{
@Override
public void run()
{
msg("Ticks per second: " + (TPS_RANGE.containsDouble(ticksPerSecond) ? ChatColor.GREEN : ChatColor.RED) + ticksPerSecond);
}
2013-08-10 16:39:50 +00:00
}.runTask(plugin);
}
catch (Exception ex)
{
FLog.severe(ex);
}
}
2013-08-10 16:39:50 +00:00
}.runTaskAsynchronously(plugin);
2012-12-27 23:27:59 +00:00
return true;
}
2013-08-10 16:39:50 +00:00
private class TFM_TickMeter
{
2013-08-10 16:39:50 +00:00
private final AtomicInteger ticks = new AtomicInteger();
private final TotalFreedomMod plugin;
private long startTime;
private BukkitTask task;
public TFM_TickMeter(TotalFreedomMod plugin)
{
this.plugin = plugin;
}
public void startTicking()
{
startTime = System.currentTimeMillis();
ticks.set(0);
task = new BukkitRunnable()
{
@Override
public void run()
{
ticks.incrementAndGet();
}
}.runTaskTimer(plugin, 0L, 1L);
}
public double stopTicking()
{
task.cancel();
long elapsed = System.currentTimeMillis() - startTime;
int tickCount = ticks.get();
return (double) tickCount / ((double) elapsed / 1000.0);
}
}
2012-12-27 23:27:59 +00:00
}