diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:23 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:23 +0100 |
commit | 44587e49ff1840219d9bc44844d4a3a6cd8ac5de (patch) | |
tree | 276ae9625795e9d79fc7db8592dbcb3a1af60928 /dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java | |
parent | 5e168847c2624b767deb9da310ecfdf169e0f43c (diff) |
Add dicore3-command
Diffstat (limited to 'dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java')
-rw-r--r-- | dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java new file mode 100644 index 0000000..b5346d0 --- /dev/null +++ b/dicore3/command/src/main/java/io/dico/dicore/command/registration/BukkitCommand.java @@ -0,0 +1,122 @@ +package io.dico.dicore.command.registration; + +import io.dico.dicore.command.ICommandAddress; +import io.dico.dicore.command.ICommandDispatcher; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; + +/** + * This class extends the bukkit's command class. + * Instances are injected into the command map. + */ +public class BukkitCommand extends Command { + private ICommandDispatcher dispatcher; + private ICommandAddress origin; + + public BukkitCommand(ICommandAddress address) { + super(validateTree(address).getNames().get(0), "", "", address.getNames().subList(1, address.getNames().size())); + this.dispatcher = address.getDispatcherForTree(); + this.origin = address; + + setTimingsIfNecessary(this); + } + + private static ICommandAddress validateTree(ICommandAddress tree) { + if (!tree.hasParent()) { + throw new IllegalArgumentException(); + } + if (tree.getNames().isEmpty()) { + throw new IllegalArgumentException(); + } + return tree; + } + + public ICommandAddress getOrigin() { + return origin; + } + + @Override + public boolean execute(CommandSender sender, String label, String[] args) { + if (!dispatcher.dispatchCommand(sender, label, args)) { + //System.out.println("failed to dispatch command"); + // target command not found, send a message in the future TODO + } + return true; + } + + @Override + public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { + return this.tabComplete(sender, alias, args, null); + } + + //@Override + public List<String> tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException { + return dispatcher.getTabCompletions(sender, alias, location, args); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + BukkitCommand that = (BukkitCommand) o; + + return getName().equals(that.getName()) && dispatcher == that.dispatcher; + } + + @Override + public int hashCode() { + return dispatcher.hashCode() | getName().hashCode(); + } + + private static void setTimingsIfNecessary(Command object) { + // with paper spigot, the timings are not set by super constructor but by CommandMap.register(), which is not invoked for this system + // I use reflection so that the project does not require paper spigot to build + try { + // public field + Field field = Command.class.getDeclaredField("timings"); + if (field.get(object) != null) return; + Class<?> clazz = Class.forName("co.aikar.timings.TimingsManager"); + // public method + Method method = clazz.getDeclaredMethod("getCommandTiming", String.class, Command.class); + Object timings = method.invoke(null, "", object); + field.set(object, timings); + } catch (Throwable ignored) { + } + } + + /* + public static void registerToMap(ICommandAddress tree, Map<String, Command> map) { + BukkitCommand command = new BukkitCommand(tree); + Iterator<String> iterator = tree.getNames().iterator(); + map.put(iterator.next(), command); + while (iterator.hasNext()) { + map.putIfAbsent(iterator.next(), command); + } + } + + public static void unregisterFromMap(ICommandAddress tree, Map<String, Command> map) { + map.values().remove(new BukkitCommand(tree)); + } + + public static void registerChildrenToMap(ICommandAddress tree, Map<String, Command> map) { + for (Map.Entry<String, ? extends ICommandAddress> entry : tree.getChildren().entrySet()) { + ICommandAddress child = entry.getValue(); + registerToMap(child, map); + } + } + + public static void unregisterChildenFromMap(ICommandAddress tree, Map<String, Command> map) { + for (Map.Entry<String, ? extends ICommandAddress> entry : tree.getChildren().entrySet()) { + ICommandAddress child = entry.getValue(); + unregisterFromMap(child, map); + } + } + */ + +} |