Rework Command_health

This commit is contained in:
Steven Lawson 2013-08-10 12:39:50 -04:00
parent c10e0deb8b
commit f23c61d0b3
4 changed files with 64 additions and 63 deletions

View File

@ -1,5 +1,5 @@
#Fri, 09 Aug 2013 17:54:26 +0200
#Sat, 10 Aug 2013 12:37:32 -0400
program.VERSION=2.22
program.BUILDNUM=380
program.BUILDDATE=08/09/2013 05\:54 PM
program.BUILDNUM=386
program.BUILDDATE=08/10/2013 12\:37 PM

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Fri Aug 09 17:54:26 CEST 2013
build.number=381
#Sat Aug 10 12:37:32 EDT 2013
build.number=387

View File

@ -1,23 +1,34 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import java.text.DecimalFormat;
import java.util.concurrent.atomic.AtomicInteger;
import me.StevenLawson.TotalFreedomMod.TFM_Log;
import me.StevenLawson.TotalFreedomMod.TFM_TickMeter;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang.math.DoubleRange;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
@CommandPermissions(level = AdminLevel.SUPER, source = SourceType.BOTH)
@CommandParameters(description = "View ticks-per-second", usage = "/<command>")
public class Command_health extends TFM_Command
{
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);
@Override
public boolean run(final CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
Runtime runtime = Runtime.getRuntime();
long usedMem = runtime.totalMemory() - runtime.freeMemory();
playerMsg("Reserved Memory: " + (double) runtime.totalMemory() / (double) BYTES_PER_MB + "mb");
playerMsg("Used Memory: " + new DecimalFormat("#").format((double) usedMem / (double) BYTES_PER_MB) + "mb (" + new DecimalFormat("#").format(((double) usedMem / (double) runtime.totalMemory()) * 100.0) + "%)");
playerMsg("Max Memory: " + (double) runtime.maxMemory() / (double) BYTES_PER_MB + "mb");
playerMsg("Calculating ticks per second, please wait...");
new BukkitRunnable()
{
@ -26,36 +37,64 @@ public class Command_health extends TFM_Command
{
try
{
final TFM_TickMeter meter = new TFM_TickMeter(plugin);
meter.startTicking();
Thread.sleep(1000); // per second
meter.stopTicking();
final Runtime runtime = Runtime.getRuntime();
final int mb = 1048576; // 1024 * 1024
final float usedMem = runtime.totalMemory() - runtime.freeMemory();
TFM_TickMeter tickMeter = new TFM_TickMeter(plugin);
tickMeter.startTicking();
Thread.sleep(2500);
final double ticksPerSecond = tickMeter.stopTicking();
new BukkitRunnable()
{
@Override
public void run()
{
playerMsg("Reserved Memory: " + runtime.totalMemory() / mb + "mb");
playerMsg("Used Memory: " + new DecimalFormat("#").format(usedMem / mb) + "mb (" + new DecimalFormat("#").format((usedMem / runtime.totalMemory()) * 100) + "%)");
playerMsg("Max Memory: " + runtime.maxMemory() / mb + "mb");
playerMsg("Ticks per second: " + (meter.getTicks() == 20 ? ChatColor.GREEN : ChatColor.RED) + meter.getTicks());
playerMsg("Ticks per second: " + (TPS_RANGE.containsDouble(ticksPerSecond) ? ChatColor.GREEN : ChatColor.RED) + ticksPerSecond);
}
}.runTask(TotalFreedomMod.plugin);
}.runTask(plugin);
}
catch (Exception iex)
catch (Exception ex)
{
TFM_Log.warning("Exception in TFM_TickMeter: Thread was interupted in sleeping process.");
TFM_Log.warning(ExceptionUtils.getStackTrace(iex));
TFM_Log.severe(ex);
}
}
}.runTaskAsynchronously(TotalFreedomMod.plugin);
}.runTaskAsynchronously(plugin);
return true;
}
private class TFM_TickMeter
{
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);
}
}
}

View File

@ -1,38 +0,0 @@
package me.StevenLawson.TotalFreedomMod;
public class TFM_TickMeter
{
int ticks;
int taskId;
final TotalFreedomMod plugin;
public TFM_TickMeter(TotalFreedomMod plugin)
{
this.plugin = plugin;
}
public int startTicking()
{
int tId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
{
@Override
public void run()
{
ticks += 1;
}
}, 1L, 1L); // ticks (20 in 1 second)
taskId = tId;
return tId;
}
public void stopTicking()
{
plugin.getServer().getScheduler().cancelTask(taskId);
}
public int getTicks()
{
return ticks;
}
}