Added lag meter to hub and arcade.

This commit is contained in:
Jonathan Williams 2013-09-03 23:31:29 -07:00
parent 37897037a3
commit d5ef0208a1
4 changed files with 121 additions and 54 deletions

View File

@ -0,0 +1,117 @@
package mineplex.core.monitor;
import java.util.HashSet;
import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class LagMeter extends MiniPlugin
{
private CoreClientManager _clientManager;
private long _lastRun = -1;
private int _count;
private double _ticksPerSecond;
private double _ticksPerSecondAverage;
private long _lastAverage;
private HashSet<Player> _monitoring = new HashSet<Player>();
public LagMeter(JavaPlugin plugin, CoreClientManager clientManager)
{
super("LagMeter", plugin);
_clientManager = clientManager;
_lastRun = System.currentTimeMillis();
_lastAverage = System.currentTimeMillis();
}
@EventHandler
public void onPlayerCommandPreProcess(PlayerCommandPreprocessEvent event)
{
if (_clientManager.Get(event.getPlayer()).GetRank().Has(Rank.MODERATOR))
{
if (event.getMessage().trim().equalsIgnoreCase("/lag"))
{
sendUpdate(event.getPlayer());
event.setCancelled(true);
}
else if (event.getMessage().trim().equalsIgnoreCase("/monitor"))
{
if (_monitoring.contains(event.getPlayer()))
_monitoring.remove(event.getPlayer());
else
_monitoring.add(event.getPlayer());
event.setCancelled(true);
}
}
}
@EventHandler
public void playerQuit(PlayerQuitEvent event)
{
_monitoring.remove(event.getPlayer());
}
@EventHandler
public void update(UpdateEvent event)
{
if (event.getType() != UpdateType.TICK)
return;
if (_count % 20 == 0)
{
_ticksPerSecond = (1000D / (System.currentTimeMillis() - _lastRun)) * 20D;
sendUpdates();
_lastRun = System.currentTimeMillis();
}
if (_count % 600 == 0)
{
_ticksPerSecondAverage = 30000D / (System.currentTimeMillis() - _lastAverage);
_ticksPerSecond = 0;
_lastAverage = System.currentTimeMillis();
}
_count++;
}
public double GetTicksPerSecond()
{
return _ticksPerSecond;
}
private void sendUpdates()
{
for (Player player : _monitoring)
{
sendUpdate(player);
}
}
private void sendUpdate(Player player)
{
player.sendMessage(" ");
player.sendMessage(" ");
player.sendMessage(" ");
player.sendMessage(" ");
player.sendMessage(" ");
player.sendMessage(F.main(GetName(), ChatColor.GRAY + "Live-------" + ChatColor.YELLOW + String.format("%.00f", _ticksPerSecond)));
player.sendMessage(F.main(GetName(), ChatColor.GRAY + "Avg--------" + ChatColor.YELLOW + String.format("%.00f", _ticksPerSecondAverage * 20)));
player.sendMessage(F.main(GetName(), ChatColor.YELLOW + "MEM"));
player.sendMessage(F.main(GetName(), ChatColor.GRAY + "Free-------" + ChatColor.YELLOW + (Runtime.getRuntime().freeMemory() / 1048576) + "MB"));
player.sendMessage(F.main(GetName(), ChatColor.GRAY + "Max--------" + ChatColor.YELLOW + (Runtime.getRuntime().maxMemory() / 1048576)) + "MB");
}
}

View File

@ -10,6 +10,7 @@ import mineplex.core.donation.DonationManager;
import mineplex.core.energy.Energy;
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.message.MessageManager;
import mineplex.core.monitor.LagMeter;
import mineplex.core.movement.Movement;
import mineplex.core.npc.NpcManager;
import mineplex.core.packethandler.PacketHandler;
@ -77,6 +78,7 @@ public class Hub extends JavaPlugin implements INautilusPlugin, IRelation
new ServerManager(this, clientManager, donationManager, portal);
new MemoryFix(this);
new FileUpdater(this, portal);
new LagMeter(this, clientManager);
CombatManager combatManager = new CombatManager(this);
BlockRestore blockRestore = new BlockRestore(this);

View File

@ -1,54 +0,0 @@
package nautilus.minecraft.core.utils;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class LagMeter implements Runnable, Listener
{
private long _lastRun = -1;
private int _count;
private double _ticksPerSecond;
private double _ticksPerSecondAverage;
public LagMeter(JavaPlugin plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, this, 0, 20);
}
@EventHandler
public void OnPlayerCommandPreProcess(PlayerCommandPreprocessEvent event)
{
if (event.getMessage().trim().equalsIgnoreCase("/lag"))
{
event.getPlayer().sendMessage(ChatColor.GRAY + "Server is running at " + String.format("%.00f", _ticksPerSecondAverage * 20) + " TPS (" + String.format("%.00f", _ticksPerSecondAverage) + "% Max speed)");
event.setCancelled(true);
}
}
public void run()
{
if (_lastRun != -1)
{
_ticksPerSecond += (double)1000 / (System.currentTimeMillis() - _lastRun);
if (_count % 5 == 0)
{
_ticksPerSecondAverage = _ticksPerSecond / 5;
_ticksPerSecond = 0;
}
_count++;
}
_lastRun = System.currentTimeMillis();
}
public double GetTicksPerSecond()
{
return _ticksPerSecond;
}
}

View File

@ -26,6 +26,7 @@ import me.chiss.Core.Modules.*;
import me.chiss.Core.Plugin.IPlugin;
import me.chiss.Core.Scheduler.Scheduler;
import mineplex.core.message.MessageManager;
import mineplex.core.monitor.LagMeter;
import mineplex.core.npc.NpcManager;
import mineplex.core.packethandler.PacketHandler;
import mineplex.core.pet.PetManager;
@ -109,6 +110,7 @@ public class Arcade extends JavaPlugin implements INautilusPlugin, IPlugin
GetSpawn();
GetTeleport();
new FileUpdater(this, new Portal(this));
new LagMeter(this, _clientManager);
PacketHandler packetHandler = new PacketHandler(this);
DisguiseManager disguiseManager = new DisguiseManager(this, packetHandler);