Merge branch 'ver/6.5.0' into dev/7.0-2

This commit is contained in:
Zoe Gidiere
2024-09-17 17:46:33 -06:00
68 changed files with 452 additions and 323 deletions

View File

@@ -1,18 +1,10 @@
package com.dfsek.terra.api.command.arguments;
import cloud.commandframework.ArgumentDescription;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.context.CommandContext;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.function.BiFunction;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -21,40 +13,33 @@ import com.dfsek.terra.api.registry.exception.NoSuchEntryException;
import com.dfsek.terra.api.registry.key.RegistryKey;
import com.dfsek.terra.api.util.reflection.TypeKey;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.component.DefaultValue;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.context.CommandInput;
import org.incendo.cloud.parser.ArgumentParseResult;
import org.incendo.cloud.parser.ArgumentParser;
import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;
public class RegistryArgument<T, R> extends CommandArgument<T, R> {
private RegistryArgument(
boolean required,
@NonNull String name,
Function<CommandContext<T>, Registry<R>> registryFunction,
TypeToken<R> typeToken,
@NonNull String defaultValue,
@Nullable BiFunction<CommandContext<T>, String, List<String>> suggestionsProvider,
@NonNull ArgumentDescription description
) {
super(required,
name,
new RegistryArgumentParser<>(registryFunction),
defaultValue,
typeToken,
suggestionsProvider,
description);
}
public class RegistryArgument {
public static <T, R> Builder<T, R> builder(String name, Registry<R> registry) {
return new Builder<>(name, registry);
}
public static <T, R> CommandArgument<T, R> of(String name, Registry<R> registry) {
public static <T, R> CommandComponent<T> of(String name, Registry<R> registry) {
return RegistryArgument.<T, R>builder(name, registry).build();
}
public static <T, R> CommandArgument<T, R> optional(String name, Registry<R> registry) {
return RegistryArgument.<T, R>builder(name, registry).asOptional().build();
public static <T, R> CommandComponent<T> optional(String name, Registry<R> registry) {
return RegistryArgument.<T, R>builder(name, registry).optional().build();
}
public static <T, R> CommandArgument<T, R> optional(String name, Registry<R> registry, String defaultKey) {
return RegistryArgument.<T, R>builder(name, registry).asOptionalWithDefault(defaultKey).build();
public static <T, R> CommandComponent<T> optional(String name, Registry<R> registry, DefaultValue<T, R> defaultKey) {
return RegistryArgument.<T, R>builder(name, registry).optional(defaultKey).build();
}
@SuppressWarnings("unchecked")
@@ -63,49 +48,36 @@ public class RegistryArgument<T, R> extends CommandArgument<T, R> {
return new Builder<>(name, registryFunction, (TypeToken<R>) TypeToken.get(registryType.getType()));
}
public static <T, R> CommandArgument<T, R> of(String name, Function<CommandContext<T>, Registry<R>> registryFunction,
public static <T, R> CommandComponent<T> of(String name, Function<CommandContext<T>, Registry<R>> registryFunction,
TypeKey<R> registryType) {
return RegistryArgument.<T, R>builder(name, registryFunction, registryType).build();
}
public static <T, R> CommandArgument<T, R> optional(String name, Function<CommandContext<T>, Registry<R>> registryFunction,
public static <T, R> CommandComponent<T> optional(String name, Function<CommandContext<T>, Registry<R>> registryFunction,
TypeKey<R> registryType) {
return RegistryArgument.builder(name, registryFunction, registryType).asOptional().build();
return RegistryArgument.builder(name, registryFunction, registryType).optional().build();
}
public static <T, R> CommandArgument<T, R> optional(String name, Function<CommandContext<T>, Registry<R>> registryFunction,
TypeKey<R> registryType, String defaultKey) {
return RegistryArgument.builder(name, registryFunction, registryType).asOptionalWithDefault(defaultKey).build();
public static <T, R> CommandComponent<T> optional(String name, Function<CommandContext<T>, Registry<R>> registryFunction,
TypeKey<R> registryType, DefaultValue<T, R> defaultKey) {
return RegistryArgument.builder(name, registryFunction, registryType).optional(defaultKey).build();
}
public static final class Builder<T, R> extends CommandArgument.Builder<T, R> {
private final Function<CommandContext<T>, Registry<R>> registryFunction;
private final TypeToken<R> typeToken;
public static final class Builder<T, R> extends CommandComponent.Builder<T, R> {
@SuppressWarnings("unchecked")
private Builder(@NonNull String name, Registry<R> registry) {
super((TypeToken<R>) TypeToken.get(registry.getType().getType()), name);
this.registryFunction = commandContext -> registry;
this.typeToken = (TypeToken<R>) TypeToken.get(registry.getType().getType());
super();
this.name(name);
this.parser(ParserDescriptor.of(
new RegistryArgumentParser<>(commandContext -> registry),
(TypeToken<R>) TypeToken.get(registry.getType().getType())));
}
private Builder(@NonNull String name, Function<CommandContext<T>, Registry<R>> registryFunction, TypeToken<R> typeToken) {
super(typeToken, name);
this.typeToken = typeToken;
this.registryFunction = registryFunction;
}
@Override
public @NonNull RegistryArgument<T, R> build() {
return new RegistryArgument<>(
isRequired(),
getName(),
registryFunction,
typeToken,
getDefaultValue(),
getSuggestionsProvider(),
getDefaultDescription()
);
super();
this.name(name);
this.parser(ParserDescriptor.of(new RegistryArgumentParser<>(registryFunction), typeToken));
}
}
@@ -119,12 +91,12 @@ public class RegistryArgument<T, R> extends CommandArgument<T, R> {
@Override
public @NonNull ArgumentParseResult<@NonNull R> parse(@NonNull CommandContext<@NonNull T> commandContext,
@NonNull Queue<@NonNull String> inputQueue) {
String input = inputQueue.remove();
String next = inputQueue.peek();
if(next != null && next.equals(":")) {
input += inputQueue.remove();
input += inputQueue.remove();
@NonNull CommandInput commandInput) {
String input = commandInput.readString();
String next = commandInput.peekString();
if(next.equals(":")) {
input += commandInput.readString();
input += commandInput.readString();
}
Registry<R> registry = registryFunction.apply(commandContext);
@@ -146,8 +118,17 @@ public class RegistryArgument<T, R> extends CommandArgument<T, R> {
}
@Override
public @NonNull List<@NonNull String> suggestions(@NonNull CommandContext<T> commandContext, @NonNull String input) {
return registryFunction.apply(commandContext).keys().stream().map(RegistryKey::toString).sorted().collect(Collectors.toList());
public @NonNull SuggestionProvider<T> suggestionProvider() {
return new SuggestionProvider<>() {
@Override
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(
@NonNull CommandContext<T> context, @NonNull CommandInput input) {
// TODO: Verify whether this is correct
return CompletableFuture.completedFuture(registryFunction.apply(context).keys().stream().map(
registryKey -> Suggestion.suggestion(registryKey.toString())).sorted().collect(Collectors.toList()));
}
};
}
}
}

View File

@@ -1,10 +1,10 @@
package com.dfsek.terra.api.event.events.platform;
import cloud.commandframework.CommandManager;
import com.dfsek.terra.api.command.CommandSender;
import com.dfsek.terra.api.event.events.Event;
import org.incendo.cloud.CommandManager;
public class CommandRegistrationEvent implements Event {
private final CommandManager<CommandSender> commandManager;

View File

@@ -8,6 +8,7 @@
package com.dfsek.terra.api.util.reflection;
import org.jetbrains.annotations.NotNull;
import sun.misc.Unsafe;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
@@ -26,6 +27,18 @@ import java.util.stream.Stream;
public final class ReflectionUtil {
private static final Unsafe UNSAFE;
static {
try{
final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
UNSAFE = (Unsafe) unsafeField.get(null);
} catch(NoSuchFieldException | IllegalAccessException e){
throw new RuntimeException(e);
}
}
public static Field[] getFields(@NotNull Class<?> type) {
Field[] result = type.getDeclaredFields();
Class<?> parentClass = type.getSuperclass();
@@ -35,6 +48,14 @@ public final class ReflectionUtil {
return result;
}
public static void setFinalField(Object obj, String fieldName, Object value) throws NoSuchFieldException {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
long fieldOffset = UNSAFE.objectFieldOffset(field);
UNSAFE.putObject(obj, fieldOffset, value);
}
public static Method[] getMethods(@NotNull Class<?> type) {
Method[] result = type.getDeclaredMethods();
Class<?> parentClass = type.getSuperclass();