mirror of
https://github.com/aikar/commands.git
synced 2026-06-02 15:22:17 +00:00
Timings support for commands
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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.");
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -860,6 +860,29 @@ public final class CommandUtil {
|
||||
|
||||
private static <T extends Throwable> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user