From fd52f8652e20ada7038ac4ab50485e6f5a716487 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 17 May 2012 20:25:02 +1000 Subject: [PATCH] Add a LagMeter (ticks per second) to the /gc command. This command can now be accessed with /lag as well. Also added a simple /vanish command, with the extra node essentials.vanish.see If you require a more advanced vanish solution checkout VanishNoPacket from mbaxter. --- .../com/earth2me/essentials/Essentials.java | 16 +++++++ .../com/earth2me/essentials/IEssentials.java | 1 + .../src/com/earth2me/essentials/LagMeter.java | 44 +++++++++++++++++++ .../src/com/earth2me/essentials/User.java | 11 +++++ .../essentials/commands/Commandgc.java | 16 +++++++ .../essentials/commands/Commandvanish.java | 40 +++++++++++++++++ Essentials/src/messages.properties | 4 ++ Essentials/src/plugin.yml | 8 +++- 8 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/LagMeter.java create mode 100644 Essentials/src/com/earth2me/essentials/commands/Commandvanish.java diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 6197f5edd..11ee95511 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -84,6 +84,7 @@ public class Essentials extends JavaPlugin implements IEssentials private transient ExecuteTimer execTimer; private transient I18n i18n; private transient Metrics metrics; + private transient LagMeter lagMeter; @Override public ISettings getSettings() @@ -240,6 +241,10 @@ public class Essentials extends JavaPlugin implements IEssentials final EssentialsTimer timer = new EssentialsTimer(this); getScheduler().scheduleSyncRepeatingTask(this, timer, 1, 100); + + lagMeter = new LagMeter(); + getScheduler().scheduleSyncRepeatingTask(this, lagMeter, 0, 40); + Economy.setEss(this); execTimer.mark("RegListeners"); @@ -264,6 +269,13 @@ public class Essentials extends JavaPlugin implements IEssentials @Override public void onDisable() { + for (Player p : getServer().getOnlinePlayers()) + { + if (getUser(p).isVanished()) + { + p.sendMessage(ChatColor.RED + _("unvanishedReload")); + } + } i18n.onDisable(); Economy.setEss(null); Trade.closeLog(); @@ -614,6 +626,10 @@ public class Essentials extends JavaPlugin implements IEssentials return i18n; } + public LagMeter getLagMeter() + { + return lagMeter; + } private static class EssentialsWorldListener implements Listener, Runnable { diff --git a/Essentials/src/com/earth2me/essentials/IEssentials.java b/Essentials/src/com/earth2me/essentials/IEssentials.java index 83c2e7325..85fc8165c 100644 --- a/Essentials/src/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/IEssentials.java @@ -71,4 +71,5 @@ public interface IEssentials extends Plugin void setMetrics(Metrics metrics); + LagMeter getLagMeter(); } diff --git a/Essentials/src/com/earth2me/essentials/LagMeter.java b/Essentials/src/com/earth2me/essentials/LagMeter.java new file mode 100644 index 000000000..1c50755b8 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/LagMeter.java @@ -0,0 +1,44 @@ +package com.earth2me.essentials; + +import java.util.LinkedList; + + +public class LagMeter implements Runnable +{ + private transient long lastPoll = System.currentTimeMillis() - 3000; + private final transient LinkedList history = new LinkedList(); + + @Override + public void run() + { + long now = System.currentTimeMillis(); + long timeSpent = (now - lastPoll) / 1000; + if (timeSpent == 0) + { + timeSpent = 1; + } + if (history.size() > 10) + { + history.remove(); + } + float tps = 40f / timeSpent; + if (tps <= 20) + { + history.add(tps); + } + lastPoll = now; + } + + public float getAverageTPS() + { + float avg = 0; + for (Float f : history) + { + if (f != null) + { + avg += f; + } + } + return avg / history.size(); + } +} diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 14660d8b8..c6ce3a569 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -17,6 +17,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser private CommandSender replyTo = null; private transient User teleportRequester; private transient boolean teleportRequestHere; + private transient boolean vanished; private transient final Teleport teleport; private transient long teleportRequestTime; private transient long lastOnlineActivity; @@ -640,4 +641,14 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { return teleportInvulnerabilityTimestamp != 0 && teleportInvulnerabilityTimestamp >= System.currentTimeMillis(); } + + public boolean isVanished() + { + return vanished; + } + + public void toggleVanished() + { + vanished = !vanished; + } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgc.java b/Essentials/src/com/earth2me/essentials/commands/Commandgc.java index 9429bc5a8..46f6999bd 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgc.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgc.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; +import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.command.CommandSender; @@ -16,6 +17,21 @@ public class Commandgc extends EssentialsCommand @Override protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception { + float tps = ess.getLagMeter().getAverageTPS(); + ChatColor color; + if (tps >= 18) + { + color = ChatColor.GREEN; + } + else if (tps >= 15) + { + color = ChatColor.YELLOW; + } + else + { + color = ChatColor.RED; + } + sender.sendMessage(_("tps", "" + color + tps)); sender.sendMessage(_("gcmax", (Runtime.getRuntime().maxMemory() / 1024 / 1024))); sender.sendMessage(_("gctotal", (Runtime.getRuntime().totalMemory() / 1024 / 1024))); sender.sendMessage(_("gcfree", (Runtime.getRuntime().freeMemory() / 1024 / 1024))); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandvanish.java b/Essentials/src/com/earth2me/essentials/commands/Commandvanish.java new file mode 100644 index 000000000..96cf95e36 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/Commandvanish.java @@ -0,0 +1,40 @@ +package com.earth2me.essentials.commands; + +import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.User; +import org.bukkit.ChatColor; +import org.bukkit.Server; +import org.bukkit.entity.Player; + + +public class Commandvanish extends EssentialsCommand +{ + public Commandvanish() + { + super("vanish"); + } + + @Override + protected void run(Server server, User user, String commandLabel, String[] args) throws Exception + { + if (user.isVanished()) + { + for (Player p : server.getOnlinePlayers()) + { + p.showPlayer(user); + } + user.sendMessage(ChatColor.GREEN + _("vanished")); + } + else + { + for (Player p : server.getOnlinePlayers()) + { + if (!ess.getUser(p).isAuthorized("essentials.vanish.see")) + { + p.hidePlayer(user); + } + user.sendMessage(ChatColor.GREEN + _("unvanished")); + } + } + } +} diff --git a/Essentials/src/messages.properties b/Essentials/src/messages.properties index 73ce00bee..11ed0d74b 100644 --- a/Essentials/src/messages.properties +++ b/Essentials/src/messages.properties @@ -364,6 +364,7 @@ tradeSignEmptyOwner=There is nothing to collect from this trade sign. treeFailure=\u00a7cTree generation failure. Try again on grass or dirt. treeSpawned=\u00a77Tree spawned. true=true +tps=Current TPS = {0} typeTpaccept=\u00a77To teleport, type \u00a7c/tpaccept\u00a77. typeTpdeny=\u00a77To deny this request, type \u00a7c/tpdeny\u00a77. typeWorldName=\u00a77You can also type the name of a specific world. @@ -377,6 +378,8 @@ unknownItemName=Unknown item name: {0} unlimitedItemPermission=\u00a7cNo permission for unlimited item {0}. unlimitedItems=Unlimited items: unmutedPlayer=Player {0} unmuted. +unvanished=You are once again visible. +unvanishedReload=A reload has forced you to become visible. upgradingFilesError=Error while upgrading the files userDoesNotExist=The user {0} does not exist. userIsAway={0} is now AFK @@ -386,6 +389,7 @@ userUsedPortal={0} used an existing exit portal. userdataMoveBackError=Failed to move userdata/{0}.tmp to userdata/{1} userdataMoveError=Failed to move userdata/{0} to userdata/{1}.tmp usingTempFolderForTesting=Using temp folder for testing: +vanished=You have now been vanished. versionMismatch=Version mismatch! Please update {0} to the same version. versionMismatchAll=Version mismatch! Please update all Essentials jars to the same version. voiceSilenced=\u00a77Your voice has been silenced diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index 3e1dc52c0..d9d4179be 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -119,9 +119,9 @@ commands: usage: / [player] aliases: [coords,egetpos,position,eposition,whereami,ewhereami] gc: - description: Reports garbage collection info; useful to developers. + description: Reports garbage collection and tick info; useful to developers. usage: / - aliases: [mem,memory,egc,emem,ememory] + aliases: [elag,lag,mem,memory,egc,emem,ememory] give: description: Give a player an item. usage: / [amount ...] @@ -405,6 +405,10 @@ commands: description: Allows the unlimited placing of items. usage: / [player] aliases: [eunlimited,ul,unl,eul,eunl] + vanish: + description: Hide yourself from other players. + usage: / + aliases: [evanish] warp: description: List all warps or warp to the specified location. usage: / [player]