Compare commits
No commits in common. "b809411faf8da8004434500ab0e05bd9176af6b4" and "ac8f6bdbb3f82d01225e4ba53f600bd879f5e9e2" have entirely different histories.
b809411faf
...
ac8f6bdbb3
@ -1,20 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,22 +1,12 @@
|
|||||||
package net.tomatentum.marinara.interaction.commands;
|
package net.tomatentum.marinara.interaction.commands;
|
||||||
|
|
||||||
import org.apache.logging.log4j.core.tools.picocli.CommandLine.Command;
|
public record CommandDefinition(String applicationCommand, String applicationCommandDescription, String[] subCommandGroups, String subCommand, String subCommandDescription) {
|
||||||
|
|
||||||
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 ExecutableCommandDefinition))
|
if (!(o instanceof CommandDefinition))
|
||||||
return false;
|
return false;
|
||||||
ExecutableCommandDefinition other = (ExecutableCommandDefinition) o;
|
CommandDefinition other = (CommandDefinition) 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);
|
||||||
@ -28,17 +18,16 @@ public record ExecutableCommandDefinition(
|
|||||||
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 ExecutableCommandDefinition build() {
|
public CommandDefinition build() {
|
||||||
if (applicationCommandName == null)
|
if (applicationCommandName == null)
|
||||||
throw new IllegalArgumentException("applicationCommandName cant be null");
|
throw new IllegalArgumentException("applicationCommandName cant be null");
|
||||||
|
|
||||||
return new ExecutableCommandDefinition(applicationCommandName, applicationCommandDescription, subCommandGroupNames, subCommandName, subCommandDescription, options);
|
return new CommandDefinition(applicationCommandName, applicationCommandDescription, subCommandGroupNames, subCommandName, subCommandDescription);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApplicationCommandName(String applicationCommandName) {
|
public void setApplicationCommandName(String applicationCommandName) {
|
||||||
@ -61,10 +50,6 @@ public record ExecutableCommandDefinition(
|
|||||||
this.subCommandDescription = subCommandDescription;
|
this.subCommandDescription = subCommandDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOptions(CommandOption[] options) {
|
|
||||||
this.options = options;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getApplicationCommandName() {
|
public String getApplicationCommandName() {
|
||||||
return applicationCommandName;
|
return applicationCommandName;
|
||||||
}
|
}
|
||||||
@ -84,9 +69,5 @@ public record ExecutableCommandDefinition(
|
|||||||
public String getSubCommandDescription() {
|
public String getSubCommandDescription() {
|
||||||
return subCommandDescription;
|
return subCommandDescription;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandOption[] getOptions() {
|
|
||||||
return options;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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.ExecutableCommandDefinition;
|
import net.tomatentum.marinara.interaction.commands.CommandDefinition;
|
||||||
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,7 +14,9 @@ import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
|||||||
|
|
||||||
public class CommandInteractionMethod extends InteractionMethod {
|
public class CommandInteractionMethod extends InteractionMethod {
|
||||||
|
|
||||||
private ExecutableCommandDefinition commandDefinition;
|
private CommandOption[] options;
|
||||||
|
|
||||||
|
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);
|
||||||
@ -23,12 +25,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, commandDefinition.options()[index].type(), commandDefinition.options()[index].name());
|
return wrapper.convertCommandOption(context, options[index].type(), options[index].name());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRun(Object context) {
|
public boolean canRun(Object context) {
|
||||||
ExecutableCommandDefinition other = wrapper.getCommandDefinition(context);
|
CommandDefinition other = wrapper.getCommandDefinition(context);
|
||||||
return commandDefinition.equals(other);
|
return commandDefinition.equals(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,18 +39,22 @@ public class CommandInteractionMethod extends InteractionMethod {
|
|||||||
return InteractionType.COMMAND;
|
return InteractionType.COMMAND;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecutableCommandDefinition getCommandDefinition() {
|
public CommandOption[] getOptions() {
|
||||||
|
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);
|
||||||
ExecutableCommandDefinition.Builder builder = new ExecutableCommandDefinition.Builder();
|
CommandDefinition.Builder builder = new CommandDefinition.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);
|
||||||
|
@ -1,15 +1,10 @@
|
|||||||
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;
|
||||||
|
|
||||||
@ -28,24 +23,6 @@ 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());
|
||||||
|
@ -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.ExecutableCommandDefinition;
|
import net.tomatentum.marinara.interaction.commands.CommandDefinition;
|
||||||
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 ExecutableCommandDefinition getCommandDefinition(Object context);
|
public abstract CommandDefinition getCommandDefinition(Object context);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user