New command system improvements, broadcast toggles, config fixes #62

Merged
NorbiPeti merged 23 commits from dev into master 2019-03-17 01:27:43 +00:00
6 changed files with 29 additions and 30 deletions
Showing only changes of commit e4f5850ad6 - Show all commits

View file

@ -12,6 +12,8 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />

View file

@ -54,7 +54,7 @@ public class ComponentCommand extends ICommand2MC {
return true;
}
private Optional<Component> getComponentOrError(Plugin plugin, String arg, CommandSender sender) {
private Optional<Component<?>> 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();

View file

@ -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

View file

@ -20,14 +20,14 @@ import java.util.Map;
/**
* Configuration is based on class name
*/
public abstract class Component {
private static HashMap<Class<? extends Component>, Component> components = new HashMap<>();
public abstract class Component<TP extends JavaPlugin> {
private static HashMap<Class<? extends Component>, Component<? extends JavaPlugin>> 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 <T extends JavaPlugin> boolean registerComponent(T plugin, Component<T> 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 <T extends JavaPlugin> boolean unregisterComponent(T plugin, Component<T> component) {
return registerUnregisterComponent(plugin, component, false);
}
public static boolean registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) {
public static <T extends JavaPlugin> boolean registerUnregisterComponent(T plugin, Component<T> 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<Class<? extends Component>, Component> getComponents() {
public static Map<Class<? extends Component>, Component<? extends JavaPlugin>> getComponents() {
return Collections.unmodifiableMap(components);
}

View file

@ -25,6 +25,7 @@ import java.util.function.Function;
*/
public abstract class Command2<TC extends ICommand2, TP extends Command2Sender> {
protected Command2() {
commandHelp.add("§6---- Commands ----");
}
/**
@ -65,6 +66,11 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
public final String errormsg;
}
private HashMap<String, SubcommandData<TC>> subcommands = new HashMap<>();
private HashMap<Class<?>, ParamConverter<?>> paramConverters = new HashMap<>();
private ArrayList<String> 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<TC extends ICommand2, TP extends Command2Sender>
* @param converter The converter to use
* @param <T> The type of the result
*/
public abstract <T> void addParamConverter(Class<T> cl, Function<String, T> converter, String errormsg);
protected <T> void addParamConverter(Class<T> cl, Function<String, T> converter, String errormsg, HashMap<Class<?>, ParamConverter<?>> map) {
map.put(cl, new ParamConverter<>(converter, errormsg));
public <T> void addParamConverter(Class<T> cl, Function<String, T> 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<String, SubcommandData<TC>> subcommands,
HashMap<Class<?>, 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<TC> sd = subcommands.get(subcommand); //O(1)
@ -161,7 +161,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
public abstract void registerCommand(TC command);
protected void registerCommand(TC command, HashMap<String, SubcommandData<TC>> 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<TC extends ICommand2, TP extends Command2Sender>
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<TC extends ICommand2, TP extends Command2Sender>
}
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

View file

@ -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<ICommand2MC, Command2MCSender> {
private HashMap<String, SubcommandData<ICommand2MC>> subcommands = new HashMap<>();
private HashMap<Class<?>, 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<ICommand2MC, Command2MCSender> {
*/
@Override
public <T> void addParamConverter(Class<T> cl, Function<String, T> converter, String errormsg) {
addParamConverter(cl, converter, "§c" + errormsg, paramConverters);
super.addParamConverter(cl, converter, "§c" + errormsg);
}
}