Skip to content

Commit

Permalink
Release 3.0.0-BETA-pre17. Add compile time variables. Remove deprecat…
Browse files Browse the repository at this point in the history
…ed LiteResult and LegacyContextProvider.
  • Loading branch information
Rollczi committed Oct 3, 2023
1 parent 35a34c4 commit 6df0a55
Show file tree
Hide file tree
Showing 38 changed files with 154 additions and 127 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Framework Core
<dependency>
<groupId>dev.rollczi</groupId>
<artifactId>litecommands-core</artifactId>
<version>3.0.0-BETA-pre16</version>
<version>3.0.0-BETA-pre17</version>
</dependency>
```

```groovy
implementation 'dev.rollczi:litecommands-core:3.0.0-BETA-pre16'
implementation 'dev.rollczi:litecommands-core:3.0.0-BETA-pre17'
```

### First Simple Command
Expand Down Expand Up @@ -85,12 +85,12 @@ Add this to your dependencies if you want to use ready-made implementation for v
<dependency>
<groupId>dev.rollczi</groupId>
<artifactId>litecommands-velocity</artifactId>
<version>3.0.0-BETA-pre16</version>
<version>3.0.0-BETA-pre17</version>
</dependency>
```

```groovy
implementation 'dev.rollczi:litecommands-velocity:3.0.0-BETA-pre16'
implementation 'dev.rollczi:litecommands-velocity:3.0.0-BETA-pre17'
```

#### Add -parameters to your compiler to use all features of LiteCommands
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ repositories {

dependencies {
implementation("me.champeau.jmh:jmh-gradle-plugin:0.7.1")
implementation("net.kyori:blossom:1.3.1")
implementation("net.kyori:indra-git:3.1.3")
}

sourceSets {
Expand Down
25 changes: 25 additions & 0 deletions buildSrc/src/main/kotlin/litecommands-compile-variables.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id("java-library")
id("net.kyori.blossom")
id("net.kyori.indra.git")
}

blossom {
indraGit {
if (!isPresent) {
throw IllegalStateException("indra-git is not present")
}

val litecommandsVariables = "src/dev/rollczi/litecommands/LiteCommandsVariables.java"

val version = project.version.toString()
val branchName = branchName() ?: throw IllegalStateException("branch name is null")

val commit = commit() ?: throw IllegalStateException("commit is null")
val commitHash = commit.name

replaceToken("{litecommands-version}", version, litecommandsVariables)
replaceToken("{litecommands-branch}", branchName, litecommandsVariables)
replaceToken("{litecommands-commit}", commitHash, litecommandsVariables)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("java-library")

id("me.champeau.jmh")
}

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/litecommands-publish.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "dev.rollczi"
version = "3.0.0-BETA-pre16"
version = "3.0.0-BETA-pre17"

java {
withSourcesJar()
Expand Down
4 changes: 2 additions & 2 deletions examples/bukkit-adventure-platform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")

// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre16") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-adventure-platform:3.0.0-BETA-pre16") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre17") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-adventure-platform:3.0.0-BETA-pre17") // <-- uncomment in your project
implementation("net.kyori:adventure-platform-bukkit:4.3.0")
implementation("net.kyori:adventure-text-minimessage:4.14.0")

Expand Down
4 changes: 2 additions & 2 deletions examples/bukkit-chatgpt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")

// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre16") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-chatgpt:3.0.0-BETA-pre16") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre17") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-chatgpt:3.0.0-BETA-pre17") // <-- uncomment in your project
implementation(project(":litecommands-bukkit")) // don't use this line in your build.gradle
implementation(project(":litecommands-chatgpt")) // don't use this line in your build.gradle
}
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 @@ -18,7 +18,7 @@ repositories {
dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")

// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre16") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-bukkit:3.0.0-BETA-pre17") // <-- uncomment in your project
implementation(project(":litecommands-bukkit")) // don't use this line in your build.gradle
}

Expand Down
2 changes: 1 addition & 1 deletion examples/velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {
compileOnly("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")
annotationProcessor("com.velocitypowered:velocity-api:3.2.0-SNAPSHOT")

// implementation("dev.rollczi:litecommands-velocity:3.0.0-BETA-pre16") // <-- uncomment in your project
// implementation("dev.rollczi:litecommands-velocity:3.0.0-BETA-pre17") // <-- uncomment in your project
implementation(project(":litecommands-velocity")) // don't use this line in your build.gradle
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import dev.rollczi.litecommands.context.ContextProvider;
import dev.rollczi.litecommands.context.ContextResult;
import dev.rollczi.litecommands.context.LegacyContextProvider;
import dev.rollczi.litecommands.invocation.Invocation;
import net.kyori.adventure.audience.Audience;
import panda.std.Result;

class AdventureAudienceContextual<SENDER> implements ContextProvider<SENDER, Audience> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<CommandBuilder<SENDER>> provide(LiteCommandsInternalBuilderApi<SENDE
WrapperRegistry wrapperRegistry = builder.getWrapperRegistry();

InstanceSourceProcessor<SENDER> processor = new InstanceSourceProcessor<>(annotationProcessorService, wrapperRegistry);
Injector<SENDER> injector = new Injector<>(builder.getBindRegistry());
Injector injector = new Injector(builder.getBindRegistry());

List<Object> instances = new ArrayList<>(this.commandInstances);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import dev.rollczi.litecommands.reflect.ReflectFormatUtil;
import dev.rollczi.litecommands.wrapper.WrapFormat;
import dev.rollczi.litecommands.wrapper.WrapperRegistry;
import panda.std.Option;

import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;

final class MethodParameterUtil {

Expand All @@ -18,9 +18,9 @@ private MethodParameterUtil() {
Class<?> outParameterType = parameter.getType();

if (wrapperRegistry.isWrapper(outParameterType)) {
Option<Class<?>> optionGenericType = extractFirstType(parameter);
Optional<Class<?>> optionGenericType = extractFirstType(parameter);

if (optionGenericType.isEmpty()) {
if (!optionGenericType.isPresent()) {
throw new IllegalArgumentException("Cannot extract expected type from parameter " + ReflectFormatUtil.parameter(parameter));
}

Expand All @@ -30,27 +30,27 @@ private MethodParameterUtil() {
return WrapFormat.notWrapped(outParameterType);
}

private static Option<Class<?>> extractFirstType(Parameter parameter) {
private static Optional<Class<?>> extractFirstType(Parameter parameter) {
Type parameterizedType = parameter.getParameterizedType();

if (parameterizedType instanceof ParameterizedType) {
ParameterizedType parameterized = (ParameterizedType) parameterizedType;
Type[] arguments = parameterized.getActualTypeArguments();

if (arguments.length == 0) {
return Option.none();
return Optional.empty();
}

Type type = arguments[0];

if (!(type instanceof Class)) {
return Option.none();
return Optional.empty();
}

return Option.of((Class<?>) type);
return Optional.of((Class<?>) type);
}

return Option.none();
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.rollczi.litecommands.bind.BindRegistry;
import dev.rollczi.litecommands.LiteCommandsException;
import dev.rollczi.litecommands.reflect.LiteCommandsReflectInvocationException;
import panda.std.Result;

import java.lang.reflect.Constructor;
import java.lang.reflect.Parameter;
Expand All @@ -11,7 +12,7 @@
import java.util.List;
import java.util.stream.Collectors;

public class Injector<SENDER> {
public class Injector {

private final BindRegistry registry;

Expand All @@ -38,14 +39,14 @@ public <T> T createInstance(Class<T> type) {
for (int i = 0; i < constructor.getParameterCount(); i++) {
Parameter parameter = constructor.getParameters()[i];
Class<?> parameterType = parameter.getType();
Object valueToInject = this.registry.getInstance(parameterType);
Result<?, String> result = this.registry.getInstance(parameterType);

if (valueToInject == null) {
exceptions.add(new LiteCommandsReflectInvocationException(constructor, parameter, "Cannot inject parameter " + parameterType.getName()));
if (result.isErr()) {
exceptions.add(new LiteCommandsReflectInvocationException(constructor, parameter, "Cannot inject parameter " + parameterType.getName() + " error: " + result.getError()));
continue;
}

parameters[i] = valueToInject;
parameters[i] = result.get();
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.junit.jupiter.api.Test;
import panda.std.Option;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import dev.rollczi.litecommands.scheduler.Scheduler;
import dev.rollczi.litecommands.scheduler.SchedulerPoll;
import dev.rollczi.litecommands.shared.ThrowingSupplier;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
import panda.std.function.ThrowingSupplier;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package dev.rollczi.litecommands.bungee.tools;

import dev.rollczi.litecommands.context.LegacyContextProvider;
import dev.rollczi.litecommands.context.ContextProvider;
import dev.rollczi.litecommands.context.ContextResult;
import dev.rollczi.litecommands.invocation.Invocation;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import panda.std.Option;
import panda.std.Result;

import java.util.function.Supplier;

public class BungeeOnlyPlayerContextual<MESSAGE> implements LegacyContextProvider<CommandSender, ProxiedPlayer> {
public class BungeeOnlyPlayerContextual<MESSAGE> implements ContextProvider<CommandSender, ProxiedPlayer> {

private final Supplier<MESSAGE> onlyPlayerMessage;

Expand All @@ -22,10 +21,12 @@ public BungeeOnlyPlayerContextual(MESSAGE onlyPlayerMessage) {
}

@Override
public Result<ProxiedPlayer, Object> provideLegacy(Invocation<CommandSender> invocation) {
return Option.of(invocation.sender())
.is(ProxiedPlayer.class)
.toResult(onlyPlayerMessage.get());
public ContextResult<ProxiedPlayer> provide(Invocation<CommandSender> invocation) {
if (invocation.sender() instanceof ProxiedPlayer) {
return ContextResult.ok(() -> (ProxiedPlayer) invocation.sender());
}

return ContextResult.error(onlyPlayerMessage.get());
}

}
1 change: 1 addition & 0 deletions litecommands-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
`litecommands-java-unit-test`
`litecommands-repositories`
`litecommands-publish`
`litecommands-compile-variables`
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

public interface LiteCommands<SENDER> {

String VERSION = "3.0.0-BETA-pre16";
@Deprecated
String VERSION = LiteCommandsVariables.VERSION;

CommandManager<SENDER> getCommandManager();

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

/**
* Holds the version of LiteCommands.
* Replaced by Gradle during build, see buildSrc/src/main/kotlin/litecommands-compile-variables.gradle.kts
* see also <a href="https://github.com/KyoriPowered/blossom">Blossom</a>
*/
public final class LiteCommandsVariables {

public static final String VERSION = "{litecommands-version}";
public static final String BRANCH = "{litecommands-branch}";
public static final String COMMIT = "{litecommands-commit}";

private LiteCommandsVariables() {
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.rollczi.litecommands.bind;

import dev.rollczi.litecommands.util.MapUtil;
import panda.std.Option;
import panda.std.Result;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

public class BindRegistry {
Expand All @@ -21,8 +21,8 @@ public void bindInstanceUnsafe(Class<?> on, Supplier<?> bind) {
}

@SuppressWarnings("unchecked")
public <T> Result<T, Object> getInstance(Class<T> clazz) {
Option<Supplier<?>> option = MapUtil.findByInstanceOf(clazz, this.instanceBindings);
public <T> Result<T, String> getInstance(Class<T> clazz) {
Optional<Supplier<?>> option = MapUtil.findByInstanceOf(clazz, this.instanceBindings);

if (option.isPresent()) {
Supplier<T> supplier = (Supplier<T>) option.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import panda.std.Option;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -129,11 +128,16 @@ public boolean hasSimilarNames(CommandBuilder<SENDER> context) {

@Override
public @NotNull CommandBuilder<SENDER> appendChild(CommandBuilder<SENDER> context) {
CommandBuilder<SENDER> newContext = Option.ofOptional(this.getChild(context.name()))
.peek(current -> current.meagre(context))
.orElseGet(context);
Optional<CommandBuilder<SENDER>> childOption = this.getChild(context.name());

this.children.put(newContext.name(), newContext);
if (childOption.isPresent()) {
CommandBuilder<SENDER> child = childOption.get();
child.meagre(context);

context = child;
}

this.children.put(context.name(), context);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private <PARSED> RequirementResult<PARSED> matchContext(ContextRequirement<PARSE

private <PARSED> RequirementResult<?> matchBind(BindRequirement<PARSED> bindRequirement, Invocation<SENDER> invocation) {
WrapFormat<PARSED, ?> wrapFormat = bindRequirement.getWrapperFormat();
Result<PARSED, Object> instance = bindRegistry.getInstance(wrapFormat.getParsedType());
Result<PARSED, String> instance = bindRegistry.getInstance(wrapFormat.getParsedType());
Wrapper wrapper = wrapperRegistry.getWrappedExpectedFactory(wrapFormat);

if (instance.isOk()) {
Expand Down
Loading

0 comments on commit 6df0a55

Please sign in to comment.