Skip to content

Commit

Permalink
GH-427 Support async parse result. (#435)
Browse files Browse the repository at this point in the history
* Support async parse result. WiP

* Create requirement match service (cleanup of CommandExecutorService). Support async context results.

* Remove typed resolvers. Remove wrappers. Add the argument profile system (APS) to replace typed resolvers. Implement "wrapper" solutions in argument resolvers.

* Use the argument profile system (APS) for quoted argument and nullable arguments. Add @sender. Add RequirementDefinition to easy declaration simple annotations. Clean up parsers.

* Fix quoted profile

* Enhance performance of SchedulerExecutorPoolImpl

* Make ArgumentProfile Prioritized. Expose ChatGTP's client. Rewrite ChatGPT argument to APS. Add unit tests.

* Make ArgumentProfile Prioritized. Expose ChatGTP's client. Rewrite ChatGPT argument to APS. Add unit tests. Add support for programmatic for ChatGPT.

* Add CompletableFutureResolver and related unit tests. Add missing ParserChained API.

* Skip modules without tests while running global tests.

* Fix for local tests.

* Add test for context's aliases.

* Add unit tests for programmatic API. Fix issue with async programmatic executor. Add api for async arguments, contexts, and binds (programmatic)

* Add ProfileNamespaces

* Limit async pool in tests.

* Add argument matcher subdomain.

* Remove annotation holder and RequirementProcessor

* Add more API for async context and parse result
  • Loading branch information
Rollczi authored Oct 5, 2024
1 parent 9cd6f9a commit 723a091
Show file tree
Hide file tree
Showing 280 changed files with 4,350 additions and 2,723 deletions.
89 changes: 21 additions & 68 deletions .idea/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
`java-library`
}

tasks.withType<Test> {
useJUnitPlatform()
maxParallelForks = Runtime.getRuntime().availableProcessors()

if (isTest()) {
gradle.startParameter.taskRequests.forEach {
if (it.args.contains("test") && !it.args.contains("--rerun")) {
it.args.add("--rerun")
}
}
}
}

fun isTest() = gradle.startParameter.taskNames.any { it.contains("test") }
19 changes: 5 additions & 14 deletions buildSrc/src/main/kotlin/litecommands-publish.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ plugins {
group = "dev.rollczi"
version = "3.7.0-SNAPSHOT"

java {
withSourcesJar()
withJavadocJar()
}

publishing {
java {
withSourcesJar()
withJavadocJar()
}

repositories {
mavenLocal()

Expand All @@ -32,15 +32,6 @@ publishing {
snapshots = true,
beta = true,
)

/* maven(
name ="minecodes",
url = "https://maven.minecodes.pl",
username = "MINE_CODES_MAVEN_USERNAME",
password = "MINE_CODES_MAVEN_PASSWORD",
snapshots = true,
beta = true,
)*/
}
}

Expand Down
10 changes: 10 additions & 0 deletions buildSrc/src/main/kotlin/litecommands-unit-test.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.add
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java-library")
id("org.jetbrains.kotlin.jvm")
Expand All @@ -17,6 +21,12 @@ dependencies {

tasks.getByName<Test>("test") {
useJUnitPlatform()
maxParallelForks = Runtime.getRuntime().availableProcessors()

tasks.withType<JavaCompile>().configureEach {
options.isFork = true
options.compilerArgs.add("-parameters")
}
}

sourceSets.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.rollczi.example.bukkit.chatgpt.command.BanCommand;
import dev.rollczi.example.bukkit.chatgpt.command.ChatGptCommand;
import dev.rollczi.litecommands.bukkit.LiteBukkitFactory;
import dev.rollczi.litecommands.chatgpt.ChatGptModel;
import dev.rollczi.litecommands.chatgpt.LiteChatGptExtension;
import dev.rollczi.litecommands.LiteCommands;
Expand All @@ -17,10 +18,10 @@ public class ExampleChatGptPlugin extends JavaPlugin {

@Override
public void onEnable() {
this.liteCommands = LiteCommandsBukkit.builder("my-chatgpt-plugin")
this.liteCommands = LiteBukkitFactory.builder("my-chatgpt-plugin")
.extension(new LiteChatGptExtension<>(), configuration -> configuration
.apiKey("OPENAI_API_KEY") // get your api key from https://platform.openai.com/account/api-keys
.model(ChatGptModel.GPT_4) // get model from https://platform.openai.com/docs/models/gpt-3-5
.model(ChatGptModel.GPT_4) // get model from https://platform.openai.com/docs/models/
.temperature(1.0) // see more https://platform.openai.com/docs/guides/gpt/how-should-i-set-the-temperature-parameter
.tokensLimit(2, 64) // min and max tokens
.cooldown(Duration.ofMillis(500)) // cooldown between suggestions per player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
import dev.rollczi.example.bukkit.command.currency.CurrencyBalanceCommand;
import dev.rollczi.example.bukkit.command.currency.CurrencyCommand;
import dev.rollczi.example.bukkit.command.currency.CurrencyService;
import dev.rollczi.example.bukkit.user.User;
import dev.rollczi.example.bukkit.user.UserArgumentResolver;
import dev.rollczi.example.bukkit.user.UserCommand;
import dev.rollczi.example.bukkit.user.UserService;
import dev.rollczi.example.bukkit.validator.IsNotOpValidator;
import dev.rollczi.example.bukkit.validator.IsNotOp;
import dev.rollczi.litecommands.argument.profile.ProfileNamespaces;
import dev.rollczi.litecommands.bukkit.LiteBukkitFactory;
import dev.rollczi.litecommands.bukkit.LiteBukkitMessages;
import dev.rollczi.example.bukkit.handler.ExampleInvalidUsageHandler;
import dev.rollczi.example.bukkit.handler.ExampleMissingPermissionsHandler;
import dev.rollczi.litecommands.LiteCommands;
import dev.rollczi.litecommands.join.JoinArgument;
import dev.rollczi.litecommands.programmatic.LiteCommand;
import dev.rollczi.litecommands.strict.StrictMode;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import dev.rollczi.litecommands.bukkit.LiteCommandsBukkit;
import dev.rollczi.litecommands.schematic.SchematicFormat;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
Expand All @@ -41,7 +45,7 @@ public void onEnable() {
// [!] you don't have to use this service, it's just for demonstration [!]
CurrencyService currencyService = new CurrencyService();

this.liteCommands = LiteCommandsBukkit.builder()
this.liteCommands = LiteBukkitFactory.builder()
// configure bukkit platform
.settings(settings -> settings
.fallbackPrefix("my-plugin") // fallback prefix - used by bukkit to identify command
Expand All @@ -62,7 +66,8 @@ public void onEnable() {
new RandomItemCommand(),
new NumberCommand(),
new CurrencyCommand(currencyService),
new CurrencyBalanceCommand(currencyService)
new CurrencyBalanceCommand(currencyService),
new UserCommand()
)

// Custom annotation validators
Expand All @@ -86,10 +91,11 @@ public void onEnable() {

// Arguments @Arg
.argument(GameMode.class, new GameModeArgument())
.argument(User.class, new UserArgumentResolver(new UserService()))

// Suggestions, if you want you can override default argument suggesters
.argumentSuggestion(Integer.class, SuggestionResult.of("1", "2", "3"))
.argumentSuggestion(String.class, JoinArgument.KEY, SuggestionResult.of("Simple suggestion", "Simple suggestion 2"))
.argumentSuggestion(String.class, ProfileNamespaces.JOIN, SuggestionResult.of("Simple suggestion", "Simple suggestion 2"))

.message(LiteBukkitMessages.PLAYER_ONLY, "&cOnly player can execute this command!")
.message(LiteBukkitMessages.PLAYER_NOT_FOUND, input -> "&cPlayer &7" + input + " &cnot found!")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.rollczi.example.bukkit.user;

import java.util.UUID;

public class User {

private final UUID uuid;
private final String name;

User(UUID uuid, String name) {
this.uuid = uuid;
this.name = name;
}

User(String name) {
this(UUID.randomUUID(), name);
}

public UUID getUuid() {
return uuid;
}

public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.rollczi.example.bukkit.user;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import java.util.regex.Pattern;
import org.bukkit.command.CommandSender;

public class UserArgumentResolver extends ArgumentResolver<CommandSender, User> {

private final Pattern VALID_USER_PATTERN = Pattern.compile("^[a-zA-Z0-9_]{3,16}$");
private final UserService userService;

public UserArgumentResolver(UserService userService) {
this.userService = userService;
}

@Override
protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
return ParseResult.completableFuture(userService.getUser(argument), user -> {
if (user == null) {
return ParseResult.failure("User not found.");
}

return ParseResult.success(user);
});
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<User> argument, SuggestionContext context) {
return userService.getCachedUsers().stream().map(user -> user.getName())
.collect(SuggestionResult.collector());
}

@Override
public boolean match(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
return VALID_USER_PATTERN.matcher(argument).matches();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.rollczi.example.bukkit.user;

import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import org.bukkit.command.CommandSender;

@Command(name = "user")
public class UserCommand {

@Execute(name = "show-async")
void show(@Context CommandSender sender, @Arg User user) {
sender.sendMessage("User: " + user.getUuid() + " " + user.getName());
}

}
Loading

0 comments on commit 723a091

Please sign in to comment.