Skip to content

Commit

Permalink
Handle merge commands classes, improve builder API and 2.3.3 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Jul 7, 2022
1 parent a1ebccf commit 956918a
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 40 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Helpful links:
- [Support Discord](https://discord.gg/6cUhkj6uZJ)
- [GitHub issues](https://github.com/Rollczi/LiteCommands/issues)
- [Example (Modern 2.3.2)](https://github.com/Rollczi/LiteCommands/tree/master/examples/bukkit)
- [Example (Modern 2.3.3)](https://github.com/Rollczi/LiteCommands/tree/master/examples/bukkit)
- [Docs (Legacy 1.7.2)](https://docs.rollczi.dev/)

### Panda Repository (Maven or Gradle) ❤️
Expand All @@ -25,11 +25,11 @@ Framework Core
<dependency>
<groupId>dev.rollczi.litecommands</groupId>
<artifactId>core</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
```
```groovy
implementation 'dev.rollczi.litecommands:core:2.3.2'
implementation 'dev.rollczi.litecommands:core:2.3.3'
```

### First Simple Command
Expand Down Expand Up @@ -64,11 +64,11 @@ Add this to your dependencies if you want use ready-made implementation for velo
<dependency>
<groupId>dev.rollczi.litecommands</groupId>
<artifactId>velocity</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
```
```groovy
implementation 'dev.rollczi.litecommands:velocity:2.3.2'
implementation 'dev.rollczi.litecommands:velocity:2.3.3'
```

#### All extensions:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {

allprojects {
group = "dev.rollczi.litecommands"
version = "2.3.2"
version = "2.3.3"

apply(plugin = "java-library")
apply(plugin = "maven-publish")
Expand Down
2 changes: 1 addition & 1 deletion examples/bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repositories {

dependencies {
compileOnly("org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT")
// implementation("dev.rollczi.litecommands:bukkit:2.3.2") // <-- uncomment in your project
// implementation("dev.rollczi.litecommands:bukkit:2.3.3") // <-- uncomment in your project
implementation(project(":litecommands-bukkit")) // don't use this line in your build.gradle

testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
Expand Down
4 changes: 2 additions & 2 deletions litecommands-bukkit-adventure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Maven
<dependency>
<groupId>dev.rollczi.litecommands</groupId>
<artifactId>bukkit-adventure</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
```
Gradle
```groovy
implementation 'dev.rollczi.litecommands:bukkit-adventure:2.3.2'
implementation 'dev.rollczi.litecommands:bukkit-adventure:2.3.3'
```
4 changes: 2 additions & 2 deletions litecommands-bukkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Maven
<dependency>
<groupId>dev.rollczi.litecommands</groupId>
<artifactId>bukkit</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
```
Gradle
```groovy
implementation 'dev.rollczi.litecommands:bukkit:2.3.2'
implementation 'dev.rollczi.litecommands:bukkit:2.3.3'
```

#### Examples:
Expand Down
4 changes: 2 additions & 2 deletions litecommands-bungee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Maven
<dependency>
<groupId>dev.rollczi.litecommands</groupId>
<artifactId>bungee</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
```
Gradle
```groovy
implementation 'dev.rollczi.litecommands:bungee:2.3.2'
implementation 'dev.rollczi.litecommands:bungee:2.3.3'
```

#### Examples:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public interface LiteCommandsBuilder<SENDER> {

Class<SENDER> getSenderType();

LiteCommandsBuilder<SENDER> beforeRegister(LiteCommandsProcess<SENDER> preProcess);
LiteCommandsBuilder<SENDER> beforeRegister(LiteCommandsPreProcess<SENDER> preProcess);

LiteCommandsBuilder<SENDER> afterRegister(LiteCommandsProcess<SENDER> postProcess);
LiteCommandsBuilder<SENDER> afterRegister(LiteCommandsPostProcess<SENDER> postProcess);

LiteCommands<SENDER> register();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.rollczi.litecommands;

import dev.rollczi.litecommands.command.CommandService;
import dev.rollczi.litecommands.injector.Injector;
import dev.rollczi.litecommands.platform.RegistryPlatform;

public interface LiteCommandsPostProcess<SENDER> {

void process(LiteCommandsBuilder<SENDER> builder, RegistryPlatform<SENDER> platform, Injector<SENDER> injector, CommandService<SENDER> commandService);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.rollczi.litecommands.injector.Injector;
import dev.rollczi.litecommands.platform.RegistryPlatform;

public interface LiteCommandsProcess<SENDER> {
public interface LiteCommandsPreProcess<SENDER> {

void process(LiteCommandsBuilder<SENDER> builder, RegistryPlatform<SENDER> platform, Injector<SENDER> injector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ public void register(CommandSection<SENDER> section) {
);
}

public RegistryPlatform<SENDER> getPlatform() {
return platform;
}

public ExecuteResultHandler<SENDER> getHandler() {
return handler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

String[] aliases() default {};

int priority() default -1;

int required() default -1;

FactoryAnnotationResolver<Section> RESOLVER = new SectionAnnotationResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import dev.rollczi.litecommands.LiteCommands;
import dev.rollczi.litecommands.LiteCommandsBuilder;
import dev.rollczi.litecommands.LiteCommandsProcess;
import dev.rollczi.litecommands.LiteCommandsPostProcess;
import dev.rollczi.litecommands.LiteCommandsPreProcess;
import dev.rollczi.litecommands.argument.Arg;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.simple.MultilevelArgument;
Expand Down Expand Up @@ -33,8 +34,10 @@
import panda.std.Option;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -49,8 +52,8 @@ class LiteCommandsBuilderImpl<SENDER> implements LiteCommandsBuilder<SENDER> {
private RegistryPlatform<SENDER> registryPlatform;
private CommandStateFactory<SENDER> commandStateFactory;

private LiteCommandsProcess<SENDER> preProcess = (builder, platform, injector) -> {};
private LiteCommandsProcess<SENDER> postProcess = (builder, platform, injector) -> {};
private LiteCommandsPreProcess<SENDER> preProcess = (builder, platform, injector) -> {};
private LiteCommandsPostProcess<SENDER> postProcess = (builder, platform, injector, commandService) -> {};

private final Set<Consumer<CommandStateFactory<SENDER>>> commandStateFactoryEditors = new HashSet<>();
private final CommandEditorRegistry editorRegistry = new CommandEditorRegistry();
Expand Down Expand Up @@ -216,13 +219,13 @@ public Class<SENDER> getSenderType() {
}

@Override
public LiteCommandsBuilder<SENDER> beforeRegister(LiteCommandsProcess<SENDER> preProcess) {
public LiteCommandsBuilder<SENDER> beforeRegister(LiteCommandsPreProcess<SENDER> preProcess) {
this.preProcess = preProcess;
return this;
}

@Override
public LiteCommandsBuilder<SENDER> afterRegister(LiteCommandsProcess<SENDER> postProcess) {
public LiteCommandsBuilder<SENDER> afterRegister(LiteCommandsPostProcess<SENDER> postProcess) {
this.postProcess = postProcess;
return this;
}
Expand All @@ -233,10 +236,12 @@ public LiteCommands<SENDER> register() {
throw new IllegalStateException("Registry platform is not set");
}


this.preProcess.process(this, this.registryPlatform, this.injectorSettings.create());

CommandService<SENDER> commandService = new CommandService<>(this.registryPlatform, this.executeResultHandler);
Injector<SENDER> injector = this.injectorSettings.create();
LiteCommands<SENDER> commands = new LiteCommandsImpl<>(senderType, registryPlatform, executeResultHandler, injector);
LiteCommands<SENDER> liteCommands = new LiteCommandsImpl<>(commandService, senderType, injector);

if (this.commandStateFactory == null) {
this.commandStateFactory = new LiteCommandFactory<>(injector, this.argumentsRegistry, this.editorRegistry);
Expand All @@ -252,25 +257,39 @@ public LiteCommands<SENDER> register() {

this.commandsInstances.add(command);
}
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
} catch (Exception exception) {
throw new RuntimeException(exception);
}

CommandService<SENDER> service = commands.getCommandService();
List<CommandSection<SENDER>> commandSections = new ArrayList<>();

root:
for (Object instance : commandsInstances) {
Option<CommandSection<SENDER>> option = this.commandStateFactory.create(instance);

if (option.isEmpty()) {
continue;
}

service.register(option.get());
CommandSection<SENDER> currentCommand = option.get();

for (CommandSection<SENDER> commandSection : commandSections) {
if (commandSection.isSimilar(currentCommand.getName())) {
commandSection.mergeSection(currentCommand);
continue root;
}
}

commandSections.add(currentCommand);
}

for (CommandSection<SENDER> commandSection : commandSections) {
commandService.register(commandSection);
}

this.postProcess.process(this, this.registryPlatform, injector);
this.postProcess.process(this, this.registryPlatform, injector, commandService);

return commands;
return liteCommands;
}

public static <T> LiteCommandsBuilder<T> builder(Class<T> senderType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
class LiteCommandsImpl<SENDER> implements LiteCommands<SENDER> {

private final CommandService<SENDER> commandService;
private final RegistryPlatform<SENDER> platform;
private final Injector<SENDER> injector;
private final Class<SENDER> senderType;
private final ExecuteResultHandler<SENDER> executeResultHandler;

LiteCommandsImpl(Class<SENDER> senderType, RegistryPlatform<SENDER> platform, ExecuteResultHandler<SENDER> handler, Injector<SENDER> injector) {
LiteCommandsImpl(CommandService<SENDER> commandService, Class<SENDER> senderType,Injector<SENDER> injector) {
this.commandService = commandService;
this.senderType = senderType;
this.injector = injector;
this.commandService = new CommandService<>(platform, handler);
this.platform = platform;
this.executeResultHandler = handler;
}

@Override
Expand All @@ -29,7 +25,7 @@ public CommandService<SENDER> getCommandService() {

@Override
public RegistryPlatform<SENDER> getPlatform() {
return this.platform;
return this.commandService.getPlatform();
}

@Override
Expand All @@ -44,7 +40,7 @@ public Class<SENDER> getSenderType() {

@Override
public ExecuteResultHandler<SENDER> getExecuteResultHandler() {
return executeResultHandler;
return this.commandService.getHandler();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.rollczi.litecommands.implementation;

import dev.rollczi.litecommands.TestFactory;
import dev.rollczi.litecommands.TestPlatform;
import dev.rollczi.litecommands.argument.Arg;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.section.Section;
import org.junit.jupiter.api.Test;

class MergeCommandClassesTest {

TestPlatform platform = TestFactory.withCommands(FirstCommandClass.class, SecondCommandClass.class);

@Section(route = "test")
static class FirstCommandClass {
@Execute(route = "set") public static String set(@Arg String name, @Arg String value) {
return name + " -> " + value;
}
}

@Section(route = "test")
static class SecondCommandClass {
@Execute(route = "unset") public static String get(@Arg String name) {
return name + " value";
}
}

@Test
void test() {
platform.execute("test", "unset", "Rollczi").assertResult("Rollczi value");
platform.execute("test", "set", "Rollczi", "vip").assertResult("Rollczi -> vip");
}

}
4 changes: 2 additions & 2 deletions litecommands-velocity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Maven
<dependency>
<groupId>dev.rollczi.litecommands</groupId>
<artifactId>velocity</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
</dependency>
```
Gradle
```groovy
implementation 'dev.rollczi.litecommands:velocity:2.3.2'
implementation 'dev.rollczi.litecommands:velocity:2.3.3'
```

#### Examples:
Expand Down

0 comments on commit 956918a

Please sign in to comment.