Skip to content

Commit

Permalink
v1.0.7 is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed May 18, 2023
1 parent 8c34f52 commit beda43b
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 69 deletions.
2 changes: 1 addition & 1 deletion bungee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'io.github.mqzn'
version '1.0.6'
version '1.0.7'

repositories {
mavenCentral()
Expand Down
3 changes: 1 addition & 2 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ version '1.0.6'

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}

dependencies {
Expand All @@ -33,11 +32,11 @@ dependencies {
testImplementation 'org.jetbrains:annotations:23.0.0'
testImplementation "net.kyori:adventure-api:4.13.1"


compileOnly 'org.jetbrains:annotations:24.0.1'
compileOnly "net.kyori:adventure-api:4.13.1"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compileOnly 'com.github.Revxrsal:asm-invoke-util:1.0'
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import io.github.mqzn.commands.utilities.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import revxrsal.asm.BoundMethodCaller;
import revxrsal.asm.MethodCaller;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.Arrays;
Expand All @@ -31,7 +28,6 @@

public final class AnnotationParser<S> {


@NotNull
public final static String SUB_COMMAND_EXECUTE_METHOD = "execute";

Expand All @@ -58,7 +54,39 @@ public AnnotationParser(@NotNull CommandManager<?, S> manager) {
@SuppressWarnings("unchecked")
public <E extends Enum<E>, C, CO> void parse(CO annotatedCommand) {

if (!checkAnnotation(annotatedCommand)) return;
//load nested classes
if(annotatedCommand.getClass().isAnnotationPresent(CommandsGroup.class)) {
for (Class<?> innerClass : annotatedCommand.getClass().getDeclaredClasses()) {
boolean isStatic = Modifier.isStatic(innerClass.getModifiers());
if(!isStatic) {
throw new IllegalStateException(
String.format("Found a member class `%s` which is NOT static",
innerClass.getName())
);
}
try {
Constructor<?> constructor = innerClass.getConstructor();
if (!constructor.canAccess(null)) {
constructor.setAccessible(true);
}
var innerClassObject = constructor.newInstance();
parse(innerClassObject);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
throw new RuntimeException(e);
}

}
}


if (!checkAnnotation(annotatedCommand)) {
throw new IllegalStateException(
String.format("Failed to load command class `%s`, the class is NOT annotated with `@Command`",
annotatedCommand.getClass().getName())
);
}

Command cmdAnnotation = annotatedCommand.getClass().getAnnotation(Command.class);
assert cmdAnnotation != null;

Expand Down Expand Up @@ -105,10 +133,9 @@ public <E extends Enum<E>, C, CO> void parse(CO annotatedCommand) {
if (method.getParameters().length == 1 && method.isAnnotationPresent(Default.class)) {

//default Execution
builder.defaultExecutor((sender, context) -> {
BoundMethodCaller caller = MethodCaller.wrap(method).bindTo(annotatedCommand);
caller.call(sender);
});
builder
.defaultExecutor(
(sender, context) -> invokeMethod(annotatedCommand,method,sender));
}

continue;
Expand Down Expand Up @@ -138,8 +165,7 @@ public <E extends Enum<E>, C, CO> void parse(CO annotatedCommand) {
.flags(flags)
.execute((sender, context) -> {
Object[] valuesToUse = readValues(method, sender, context);
BoundMethodCaller caller = MethodCaller.wrap(method).bindTo(annotatedCommand);
caller.call(valuesToUse);
invokeMethod(annotatedCommand, method, valuesToUse);
});

builder.syntax(syntaxBuilder.build());
Expand Down Expand Up @@ -232,10 +258,9 @@ private <C> SubCommandBuilder<S, C> loadSubCommandBuilder(Command cmd,
"has some redundant parameters although it needs only 1 parameter for the command sender", defaultExecutionMethod.getName()));
}

subBuilder = subBuilder.defaultExecution((sender, context) -> {
BoundMethodCaller caller = MethodCaller.wrap(defaultExecutionMethod).bindTo(subCommandInstance);
caller.call(sender);
});
subBuilder = subBuilder
.defaultExecution(
(sender, context) -> invokeMethod(subCommandInstance, defaultExecutionMethod, sender));
}

Method executeMethod = Arrays.stream(subClass.getDeclaredMethods())
Expand Down Expand Up @@ -270,8 +295,7 @@ private <C> SubCommandBuilder<S, C> loadSubCommandBuilder(Command cmd,

subBuilder = subBuilder.execute((sender, context) -> {
Object[] valuesToUse = readValues(executeMethod, sender, context);
BoundMethodCaller caller = MethodCaller.wrap(executeMethod).bindTo(subCommandInstance);
caller.call(valuesToUse);
invokeMethod(subCommandInstance,executeMethod, valuesToUse);
});
}

Expand Down Expand Up @@ -788,6 +812,13 @@ private String annotationNotPresent(Class<?> subClass, Class<? extends Annotatio
return String.format("Subcommand class '%s' is NOT annotated with @%s", subClass.getName(), annotation.getSimpleName());
}

private <T> void invokeMethod(T instance, Method method, Object... objects) {
try {
method.invoke(instance,objects);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

private record ResolvedSubCommandMethod(Argument<?>[] arguments,
Argument<?>[] actualArguments,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.mqzn.commands.annotations.base;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CommandsGroup {



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import io.github.mqzn.commands.annotations.AnnotationParser;
import io.github.mqzn.commands.test.annotations.TestAnnotatedCommand;
import org.jetbrains.annotations.TestOnly;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Objects;

@TestOnly
public final class TestBootstrap {

Expand All @@ -23,53 +22,16 @@ public TestBootstrap() {

@Test
public void firstTest() {


/*var sub1 = SubCommandBuilder.<ClientSender, ClientSender>genericBuilder(ClientSender.class,"test","sub1")
.argument(Argument.word("username"))
.children("sub2")
.execute((sender, context)-> {
System.out.println("Executing context for sub1 !");
})
.defaultExecution((sender, context)-> {
System.out.println("Default sub1 execution");
})
.build();
var sub2 = SubCommandBuilder.<ClientSender, ClientSender>genericBuilder(ClientSender.class,"test","sub2")
.argument(Argument.integer("num"))
.parent("sub1")
.children("sub3")
.execute((sender, context)-> {
System.out.println("Executing context for sub2 !");
})
.build();
var sub3 = SubCommandBuilder.<ClientSender, ClientSender>genericBuilder(ClientSender.class,"test","sub3")
.argument(Argument.word("address"))
.parent("sub2")
.execute((sender, context)-> {
System.out.println("Executing context for sub3 !");
})
.build();
var cmd = Command.builder(commandManager, "test")
.syntax(sub1, sub2, sub3)
.build();
commandManager.registerCommand(cmd);*/

parser.parse(new TestAnnotatedCommand());

// /test sub1
String[] args = new String[]{
"sub1",
"first",
"2",
"third"
};

commandManager.executeCommand(Objects.requireNonNull(commandManager.getCommand("testa")), sender, args);
var cmd = commandManager.getCommand("testinner2");
Assertions.assertNotNull(cmd);

commandManager.executeCommand(cmd, sender, args);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.github.mqzn.commands.test.annotations;

import io.github.mqzn.commands.annotations.base.Command;
import io.github.mqzn.commands.annotations.base.CommandsGroup;
import io.github.mqzn.commands.annotations.base.Default;
import io.github.mqzn.commands.annotations.subcommands.SubCommand;
import io.github.mqzn.commands.test.ClientSender;

@CommandsGroup
@Command(name = "testa")
@SubCommand(value = TestSub1.class)
//@SubCommand(value = TestSub2.class)
Expand All @@ -11,5 +15,37 @@
//@SubCommand(value = TestRoot2.class)
public class TestAnnotatedCommand {


@CommandsGroup
@Command(name = "testinner1")
public static class TestInnerCommand {

@Default
public void exec(ClientSender sender) {
System.out.println("Executing default for " + this.getClass().getSimpleName());
}

@CommandsGroup
@Command(name = "testinner2")
public static class TestInner2Command {

@Default
public void exec(ClientSender sender) {
System.out.println("Executing default for " + this.getClass().getSimpleName());
}

@Command(name = "testinner3")
public static class TestInner3Command {

@Default
public void exec(ClientSender sender) {
System.out.println("Executing default for " + this.getClass().getSimpleName());
}
}

}

}


}
2 changes: 1 addition & 1 deletion jcord/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'io.github.mqzn'
version '1.0.6'
version '1.0.7'

repositories {
mavenCentral()
Expand Down
4 changes: 1 addition & 3 deletions spigot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'io.github.mqzn'
version '1.0.6'
version '1.0.7'

repositories {
mavenCentral()
Expand All @@ -27,8 +27,6 @@ dependencies {

compileOnly "net.kyori:adventure-api:4.13.1"
compileOnly "net.kyori:adventure-platform-bukkit:4.3.0"

implementation 'com.github.Revxrsal:asm-invoke-util:1.0'
}

def targetJavaVersion = 17
Expand Down
2 changes: 1 addition & 1 deletion velocity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'io.github.mqzn'
version '1.0.6'
version '1.0.7'

repositories {
mavenCentral()
Expand Down

0 comments on commit beda43b

Please sign in to comment.