Compare commits

..

5 Commits

Author SHA1 Message Date
b809411faf
add registerCommands() method 2024-10-14 17:13:51 +02:00
42a1d86bf9
add ApplicationCommandDefinition prototype 2024-10-14 17:13:18 +02:00
1a4dfff8ec
+ rename and move options into definition 2024-10-14 17:12:44 +02:00
bde3c401fb
+ rename 2024-10-14 17:11:43 +02:00
533af43bea
rename of the Command data class 2024-10-14 17:11:14 +02:00
5 changed files with 76 additions and 20 deletions

View File

@ -0,0 +1,20 @@
package net.tomatentum.marinara.interaction.commands;
import java.util.ArrayList;
import java.util.List;
public record ApplicationCommandDefinition(
List<ExecutableCommandDefinition> executableDefinitons,
String applicationCommand
) {
public ApplicationCommandDefinition(String applicationCommand) {
this(new ArrayList<>(), applicationCommand);
}
public ApplicationCommandDefinition addExecutableCommand(ExecutableCommandDefinition def) {
executableDefinitons.add(def);
return this;
}
}

View File

@ -1,12 +1,22 @@
package net.tomatentum.marinara.interaction.commands; package net.tomatentum.marinara.interaction.commands;
public record CommandDefinition(String applicationCommand, String applicationCommandDescription, String[] subCommandGroups, String subCommand, String subCommandDescription) { import org.apache.logging.log4j.core.tools.picocli.CommandLine.Command;
import net.tomatentum.marinara.interaction.commands.annotation.CommandOption;
public record ExecutableCommandDefinition(
String applicationCommand,
String applicationCommandDescription,
String[] subCommandGroups,
String subCommand,
String subCommandDescription,
CommandOption[] options) {
@Override @Override
public final boolean equals(Object o) { public final boolean equals(Object o) {
if (!(o instanceof CommandDefinition)) if (!(o instanceof ExecutableCommandDefinition))
return false; return false;
CommandDefinition other = (CommandDefinition) o; ExecutableCommandDefinition other = (ExecutableCommandDefinition) o;
return other.applicationCommand.equals(this.applicationCommand) && return other.applicationCommand.equals(this.applicationCommand) &&
other.subCommandGroups.equals(this.subCommandGroups) && other.subCommandGroups.equals(this.subCommandGroups) &&
other.subCommand.equals(this.subCommand); other.subCommand.equals(this.subCommand);
@ -18,16 +28,17 @@ public record CommandDefinition(String applicationCommand, String applicationCom
private String[] subCommandGroupNames; private String[] subCommandGroupNames;
private String subCommandName; private String subCommandName;
private String subCommandDescription; private String subCommandDescription;
public CommandOption[] options;
public Builder() { public Builder() {
this.subCommandGroupNames = new String[0]; this.subCommandGroupNames = new String[0];
} }
public CommandDefinition build() { public ExecutableCommandDefinition build() {
if (applicationCommandName == null) if (applicationCommandName == null)
throw new IllegalArgumentException("applicationCommandName cant be null"); throw new IllegalArgumentException("applicationCommandName cant be null");
return new CommandDefinition(applicationCommandName, applicationCommandDescription, subCommandGroupNames, subCommandName, subCommandDescription); return new ExecutableCommandDefinition(applicationCommandName, applicationCommandDescription, subCommandGroupNames, subCommandName, subCommandDescription, options);
} }
public void setApplicationCommandName(String applicationCommandName) { public void setApplicationCommandName(String applicationCommandName) {
@ -50,6 +61,10 @@ public record CommandDefinition(String applicationCommand, String applicationCom
this.subCommandDescription = subCommandDescription; this.subCommandDescription = subCommandDescription;
} }
public void setOptions(CommandOption[] options) {
this.options = options;
}
public String getApplicationCommandName() { public String getApplicationCommandName() {
return applicationCommandName; return applicationCommandName;
} }
@ -69,5 +84,9 @@ public record CommandDefinition(String applicationCommand, String applicationCom
public String getSubCommandDescription() { public String getSubCommandDescription() {
return subCommandDescription; return subCommandDescription;
} }
public CommandOption[] getOptions() {
return options;
}
} }
} }

View File

@ -3,7 +3,7 @@ package net.tomatentum.marinara.interaction.methods;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.commands.CommandDefinition; import net.tomatentum.marinara.interaction.commands.ExecutableCommandDefinition;
import net.tomatentum.marinara.interaction.commands.annotation.ApplicationCommand; import net.tomatentum.marinara.interaction.commands.annotation.ApplicationCommand;
import net.tomatentum.marinara.interaction.commands.annotation.CommandOption; import net.tomatentum.marinara.interaction.commands.annotation.CommandOption;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
@ -14,9 +14,7 @@ import net.tomatentum.marinara.wrapper.LibraryWrapper;
public class CommandInteractionMethod extends InteractionMethod { public class CommandInteractionMethod extends InteractionMethod {
private CommandOption[] options; private ExecutableCommandDefinition commandDefinition;
private CommandDefinition commandDefinition;
CommandInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { CommandInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) {
super(method, handler, wrapper); super(method, handler, wrapper);
@ -25,12 +23,12 @@ public class CommandInteractionMethod extends InteractionMethod {
@Override @Override
public Object getParameter(Object context, int index) { public Object getParameter(Object context, int index) {
return wrapper.convertCommandOption(context, options[index].type(), options[index].name()); return wrapper.convertCommandOption(context, commandDefinition.options()[index].type(), commandDefinition.options()[index].name());
} }
@Override @Override
public boolean canRun(Object context) { public boolean canRun(Object context) {
CommandDefinition other = wrapper.getCommandDefinition(context); ExecutableCommandDefinition other = wrapper.getCommandDefinition(context);
return commandDefinition.equals(other); return commandDefinition.equals(other);
} }
@ -39,22 +37,18 @@ public class CommandInteractionMethod extends InteractionMethod {
return InteractionType.COMMAND; return InteractionType.COMMAND;
} }
public CommandOption[] getOptions() { public ExecutableCommandDefinition getCommandDefinition() {
return options;
}
public CommandDefinition getCommandDefinition() {
return commandDefinition; return commandDefinition;
} }
private void parseMethod() { private void parseMethod() {
ReflectionUtil.checkValidCommandMethod(method); ReflectionUtil.checkValidCommandMethod(method);
parseOptions();
ApplicationCommand cmd = ReflectionUtil.getAnnotation(method, ApplicationCommand.class); ApplicationCommand cmd = ReflectionUtil.getAnnotation(method, ApplicationCommand.class);
CommandDefinition.Builder builder = new CommandDefinition.Builder(); ExecutableCommandDefinition.Builder builder = new ExecutableCommandDefinition.Builder();
builder.setApplicationCommandName(cmd.name()); builder.setApplicationCommandName(cmd.name());
builder.setApplicationCommandDescription(cmd.description()); builder.setApplicationCommandDescription(cmd.description());
builder.setOptions(parseOptions());
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) { if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class); SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);

View File

@ -1,10 +1,15 @@
package net.tomatentum.marinara.registry; package net.tomatentum.marinara.registry;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import net.tomatentum.marinara.handler.InteractionHandler; import net.tomatentum.marinara.handler.InteractionHandler;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.commands.ApplicationCommandDefinition;
import net.tomatentum.marinara.interaction.commands.ExecutableCommandDefinition;
import net.tomatentum.marinara.interaction.methods.CommandInteractionMethod;
import net.tomatentum.marinara.interaction.methods.InteractionMethod; import net.tomatentum.marinara.interaction.methods.InteractionMethod;
import net.tomatentum.marinara.wrapper.LibraryWrapper; import net.tomatentum.marinara.wrapper.LibraryWrapper;
@ -23,6 +28,24 @@ public class InteractionRegistry {
} }
} }
public void registerCommands() {
List<ApplicationCommandDefinition> defs = new ArrayList<>();
List<ExecutableCommandDefinition> execDefs = interactionMethods.stream()
.filter((x) -> x.getClass().isAssignableFrom(CommandInteractionMethod.class))
.map((x) -> ((CommandInteractionMethod)x).getCommandDefinition())
.toList();
execDefs.forEach((def) -> {
Optional<ApplicationCommandDefinition> appDef = defs.stream()
.filter((x) -> x.applicationCommand().equals(def.applicationCommand()))
.findFirst();
if (appDef.isPresent())
appDef.get().addExecutableCommand(def);
else
defs.add(new ApplicationCommandDefinition(def.applicationCommand()).addExecutableCommand(def));
});
}
public void handle(Object context) { public void handle(Object context) {
interactionMethods.forEach((m) -> { interactionMethods.forEach((m) -> {
InteractionType type = wrapper.getInteractionType(context.getClass()); InteractionType type = wrapper.getInteractionType(context.getClass());

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import net.tomatentum.marinara.interaction.commands.CommandDefinition; import net.tomatentum.marinara.interaction.commands.ExecutableCommandDefinition;
import net.tomatentum.marinara.interaction.commands.option.OptionType; import net.tomatentum.marinara.interaction.commands.option.OptionType;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
@ -32,5 +32,5 @@ public abstract class LibraryWrapper {
public abstract InteractionType getInteractionType(Class<?> clazz); public abstract InteractionType getInteractionType(Class<?> clazz);
public abstract Object convertCommandOption(Object context, OptionType type, String optionName); public abstract Object convertCommandOption(Object context, OptionType type, String optionName);
public abstract CommandDefinition getCommandDefinition(Object context); public abstract ExecutableCommandDefinition getCommandDefinition(Object context);
} }