From e4f5850ad6922b6a0c848a8e562b2ffdb915bb8d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 22 Feb 2019 00:40:11 +0100 Subject: [PATCH] Component stuff, command stuff Made the components return correctly-typed plugins Added support for showing commands Moved the subcommands and paramconverters to the base class --- BuildConfigUpdater/BuildConfigUpdater.iml | 2 ++ .../buttondevteam/core/ComponentCommand.java | 2 +- .../lib/TBMCCommandPreprocessEvent.java | 2 +- .../lib/architecture/Component.java | 14 +++++----- .../java/buttondevteam/lib/chat/Command2.java | 26 ++++++++++++------- .../buttondevteam/lib/chat/Command2MC.java | 13 ++-------- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/BuildConfigUpdater/BuildConfigUpdater.iml b/BuildConfigUpdater/BuildConfigUpdater.iml index 274b3de..72a504c 100644 --- a/BuildConfigUpdater/BuildConfigUpdater.iml +++ b/BuildConfigUpdater/BuildConfigUpdater.iml @@ -12,6 +12,8 @@ + + diff --git a/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java b/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java index a0e18f7..4e4a998 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java +++ b/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java @@ -54,7 +54,7 @@ public class ComponentCommand extends ICommand2MC { return true; } - private Optional getComponentOrError(Plugin plugin, String arg, CommandSender sender) { + private Optional> getComponentOrError(Plugin plugin, String arg, CommandSender sender) { val oc = Component.getComponents().values().stream() .filter(c -> plugin.getName().equals(c.getPlugin().getName())) .filter(c -> c.getClass().getSimpleName().equalsIgnoreCase(arg)).findAny(); diff --git a/ButtonCore/src/main/java/buttondevteam/lib/TBMCCommandPreprocessEvent.java b/ButtonCore/src/main/java/buttondevteam/lib/TBMCCommandPreprocessEvent.java index a72c0b7..d7cd103 100755 --- a/ButtonCore/src/main/java/buttondevteam/lib/TBMCCommandPreprocessEvent.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/TBMCCommandPreprocessEvent.java @@ -25,7 +25,7 @@ public class TBMCCommandPreprocessEvent extends Event implements Cancellable { public TBMCCommandPreprocessEvent(CommandSender sender, String message) { this.sender = sender; - this.message = message; //TODO: Actually call from Discord as well + this.message = message; } @Override diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java index 5494c78..4338179 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java @@ -20,14 +20,14 @@ import java.util.Map; /** * Configuration is based on class name */ -public abstract class Component { - private static HashMap, Component> components = new HashMap<>(); +public abstract class Component { + private static HashMap, Component> components = new HashMap<>(); @Getter private boolean enabled = false; @Getter @NonNull - private JavaPlugin plugin; + private TP plugin; @NonNull private @Getter IHaveConfig config; @@ -46,7 +46,7 @@ public abstract class Component { * @param component The component to register * @return Whether the component is registered successfully (it may have failed to enable) */ - public static boolean registerComponent(JavaPlugin plugin, Component component) { + public static boolean registerComponent(T plugin, Component component) { return registerUnregisterComponent(plugin, component, true); } @@ -58,11 +58,11 @@ public abstract class Component { * @param component The component to unregister * @return Whether the component is unregistered successfully (it also got disabled) */ - public static boolean unregisterComponent(JavaPlugin plugin, Component component) { + public static boolean unregisterComponent(T plugin, Component component) { return registerUnregisterComponent(plugin, component, false); } - public static boolean registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) { + public static boolean registerUnregisterComponent(T plugin, Component component, boolean register) { try { val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class); if (metaAnn != null) { @@ -153,7 +153,7 @@ public abstract class Component { * * @return The currently registered components */ - public static Map, Component> getComponents() { + public static Map, Component> getComponents() { return Collections.unmodifiableMap(components); } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java index 9b8ad31..c6c0de0 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java @@ -25,6 +25,7 @@ import java.util.function.Function; */ public abstract class Command2 { protected Command2() { + commandHelp.add("§6---- Commands ----"); } /** @@ -65,6 +66,11 @@ public abstract class Command2 public final String errormsg; } + private HashMap> subcommands = new HashMap<>(); + private HashMap, ParamConverter> paramConverters = new HashMap<>(); + + private ArrayList commandHelp = new ArrayList<>(); //Mainly needed by Discord + /** * Adds a param converter that obtains a specific object from a string parameter. * The converter may return null. @@ -73,17 +79,11 @@ public abstract class Command2 * @param converter The converter to use * @param The type of the result */ - public abstract void addParamConverter(Class cl, Function converter, String errormsg); - - protected void addParamConverter(Class cl, Function converter, String errormsg, HashMap, ParamConverter> map) { - map.put(cl, new ParamConverter<>(converter, errormsg)); + public void addParamConverter(Class cl, Function converter, String errormsg) { + paramConverters.put(cl, new ParamConverter<>(converter, errormsg)); } - public abstract boolean handleCommand(TP sender, String commandLine) throws Exception; - - protected boolean handleCommand(TP sender, String commandline, - HashMap> subcommands, - HashMap, ParamConverter> paramConverters) throws Exception { + public boolean handleCommand(TP sender, String commandline) throws Exception { for (int i = commandline.length(); i != -1; i = commandline.lastIndexOf(' ', i - 1)) { String subcommand = commandline.substring(0, i).toLowerCase(); SubcommandData sd = subcommands.get(subcommand); //O(1) @@ -161,7 +161,7 @@ public abstract class Command2 public abstract void registerCommand(TC command); - protected void registerCommand(TC command, HashMap> subcommands, char commandChar) { + protected void registerCommand(TC command, char commandChar) { val path = command.getCommandPath(); int x = path.indexOf(' '); val mainPath = commandChar + path.substring(0, x == -1 ? path.length() : x); @@ -177,6 +177,8 @@ public abstract class Command2 ht[0] = "§6---- " + ht[0] + " ----"; scmdHelpList.addAll(Arrays.asList(ht)); scmdHelpList.add("§6Subcommands:"); + if (!commandHelp.contains(mainPath)) + commandHelp.add(mainPath); } catch (Exception e) { TBMCCoreAPI.SendException("Could not register default handler for command /" + path, e); } @@ -232,4 +234,8 @@ public abstract class Command2 } public abstract boolean hasPermission(TP sender, TC command); + + public String[] getCommandsText() { + return commandHelp.toArray(new String[0]); + } } //TODO: Test support of Player instead of CommandSender diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java index 9e7e004..4a26622 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java @@ -2,21 +2,12 @@ package buttondevteam.lib.chat; import buttondevteam.core.MainPlugin; -import java.util.HashMap; import java.util.function.Function; public class Command2MC extends Command2 { - private HashMap> subcommands = new HashMap<>(); - private HashMap, ParamConverter> paramConverters = new HashMap<>(); - - @Override - public boolean handleCommand(Command2MCSender sender, String commandLine) throws Exception { - return handleCommand(sender, commandLine, subcommands, paramConverters); - } - @Override public void registerCommand(ICommand2MC command) { - registerCommand(command, subcommands, '/'); + super.registerCommand(command, '/'); } @Override @@ -30,6 +21,6 @@ public class Command2MC extends Command2 { */ @Override public void addParamConverter(Class cl, Function converter, String errormsg) { - addParamConverter(cl, converter, "§c" + errormsg, paramConverters); + super.addParamConverter(cl, converter, "§c" + errormsg); } }