From 6f396fb13c3a84bc20e81d7fee29ec6ffa4313a9 Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 7 Jan 2017 17:03:49 -0500 Subject: [PATCH] Timings support for commands --- README.md | 4 ++ .../java/co/aikar/commands/AikarTiming.java | 31 +++++++++++++ .../java/co/aikar/commands/BaseCommand.java | 3 ++ .../java/co/aikar/commands/CommandTiming.java | 36 +++++++++++++++ .../java/co/aikar/commands/CommandUtil.java | 23 ++++++++++ .../java/co/aikar/commands/EmptyTiming.java | 40 +++++++++++++++++ .../co/aikar/commands/RegisteredCommand.java | 2 + .../java/co/aikar/commands/SpigotTiming.java | 45 +++++++++++++++++++ 8 files changed, 184 insertions(+) create mode 100644 src/main/java/co/aikar/commands/AikarTiming.java create mode 100644 src/main/java/co/aikar/commands/CommandTiming.java create mode 100644 src/main/java/co/aikar/commands/EmptyTiming.java create mode 100644 src/main/java/co/aikar/commands/SpigotTiming.java diff --git a/README.md b/README.md index 7f879244..286207ed 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ This is a rough import of my command framework to be a standalone Library. This We haven't even decided a name yet! +## Targetted Platforms +Requires CraftBukkit, Spigot or Paper. +Sponge maybe in future if we can + ## Purpose This is the Framework used on [Empire Minecraft](https://ref.emc.gs/Aikar?gac=commands.github). diff --git a/src/main/java/co/aikar/commands/AikarTiming.java b/src/main/java/co/aikar/commands/AikarTiming.java new file mode 100644 index 00000000..b2ec6fff --- /dev/null +++ b/src/main/java/co/aikar/commands/AikarTiming.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands; + +public class AikarTiming extends SpigotTiming {// TODO: Extend CommandTiming once migrated to v2 + public AikarTiming(BaseCommand command, String name) { + super(command, name); + // TODO: Take plugin from BaseCommand and use Timing.of(plugin, name) and use Timing class + } +} diff --git a/src/main/java/co/aikar/commands/BaseCommand.java b/src/main/java/co/aikar/commands/BaseCommand.java index b7591b14..a5a722b1 100644 --- a/src/main/java/co/aikar/commands/BaseCommand.java +++ b/src/main/java/co/aikar/commands/BaseCommand.java @@ -75,6 +75,7 @@ public abstract class BaseCommand extends Command { setName(cmd); setLabel(cmd); } + this.description = cmd + " commands"; this.usageMessage = "/" + cmd; @@ -277,7 +278,9 @@ public abstract class BaseCommand extends Command { private static void executeCommand(CommandSender sender, String[] args, RegisteredCommand cmd) { if (cmd.hasPermission(sender)) { List sargs = Lists.newArrayList(args); + cmd.timing.startTiming(); cmd.invoke(sender, sargs); + cmd.timing.stopTiming(); } else { CommandUtil.sendMsg(sender, "&cI'm sorry, but you do not have permission to perform this command."); } diff --git a/src/main/java/co/aikar/commands/CommandTiming.java b/src/main/java/co/aikar/commands/CommandTiming.java new file mode 100644 index 00000000..90e336e2 --- /dev/null +++ b/src/main/java/co/aikar/commands/CommandTiming.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands; + +public abstract class CommandTiming { + private final BaseCommand command; + + CommandTiming(BaseCommand command) { + + this.command = command; + } + + public abstract void startTiming(); + public abstract void stopTiming(); +} diff --git a/src/main/java/co/aikar/commands/CommandUtil.java b/src/main/java/co/aikar/commands/CommandUtil.java index 9a076cd4..7d30f27a 100644 --- a/src/main/java/co/aikar/commands/CommandUtil.java +++ b/src/main/java/co/aikar/commands/CommandUtil.java @@ -860,6 +860,29 @@ public final class CommandUtil { private static T superSneaky(Throwable t) throws T { + //noinspection ConstantConditions throw (T) t; } + + static int hasTimings = -1; + public static synchronized CommandTiming getTiming(BaseCommand cmd, String command) { + if (hasTimings == -1) { + try { + Class.forName("co.aikar.timings.Timing"); + hasTimings = 1; + } catch (ClassNotFoundException ignored1) { + try { + Class.forName("org.spigotmc.CustomTimingsHandler"); + hasTimings = 2; + } catch (ClassNotFoundException ignored2) { + hasTimings = 0; + } + } + } + if (hasTimings > 0) { + final String name = "Command: " + command; + return hasTimings == 1 ? new AikarTiming(cmd, name): new SpigotTiming(cmd, name); + } + return new EmptyTiming(cmd); + } } diff --git a/src/main/java/co/aikar/commands/EmptyTiming.java b/src/main/java/co/aikar/commands/EmptyTiming.java new file mode 100644 index 00000000..8dc56929 --- /dev/null +++ b/src/main/java/co/aikar/commands/EmptyTiming.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands; + +public class EmptyTiming extends CommandTiming { + EmptyTiming(BaseCommand command) { + super(command); + } + + @Override + public final void startTiming() { + + } + + @Override + public final void stopTiming() { + + } +} diff --git a/src/main/java/co/aikar/commands/RegisteredCommand.java b/src/main/java/co/aikar/commands/RegisteredCommand.java index fd50f72b..bd11c5c4 100644 --- a/src/main/java/co/aikar/commands/RegisteredCommand.java +++ b/src/main/java/co/aikar/commands/RegisteredCommand.java @@ -62,6 +62,7 @@ public class RegisteredCommand { public final CommandCompletion complete; public final int nonSenderAwareResolvers; public final int optionalResolvers; + CommandTiming timing; RegisteredCommand(BaseCommand scope, String command, Method method, String prefSubCommand) { this.scope = scope; @@ -69,6 +70,7 @@ public class RegisteredCommand { prefSubCommand = ""; } this.command = command + (method.getAnnotation(CommandAlias.class) == null && !prefSubCommand.isEmpty() ? prefSubCommand : ""); + this.timing = CommandUtil.getTiming(scope, command);; this.method = method; this.prefSubCommand = prefSubCommand; this.perm = method.getAnnotation(CommandPermission.class); diff --git a/src/main/java/co/aikar/commands/SpigotTiming.java b/src/main/java/co/aikar/commands/SpigotTiming.java new file mode 100644 index 00000000..6f1faed6 --- /dev/null +++ b/src/main/java/co/aikar/commands/SpigotTiming.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands; + +import org.spigotmc.CustomTimingsHandler; + +public class SpigotTiming extends CommandTiming { + private final CustomTimingsHandler timing; + + SpigotTiming(BaseCommand command, String name) { + super(command); + this.timing = new CustomTimingsHandler(name); + } + + @Override + public void startTiming() { + timing.startTiming(); + } + + @Override + public void stopTiming() { + timing.stopTiming(); + } +}