Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Java 8 #110

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions brigadier/src/main/java/revxrsal/commands/brigadier/BNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ private BNode(CommandNode<S> node) {
this.node = node;
}

public static <S> @NotNull BNode<S> of(@NotNull CommandNode<S> node) {
notNull(node, "node");
return new BNode<>(node);
}

public static <S> @NotNull BNode<S> literal(@NotNull String name) {
return of(LiteralArgumentBuilder.<S>literal(name).build());
}

public @NotNull BNode<S> executes(Command<S> command) {
Nodes.setCommand(node, command);
return this;
Expand All @@ -54,8 +63,10 @@ private BNode(CommandNode<S> node) {
}

public @NotNull BNode<S> suggests(SuggestionProvider<S> suggestionProvider) {
if (node instanceof ArgumentCommandNode<S, ?> argument)
if (node instanceof ArgumentCommandNode) {
ArgumentCommandNode<S, ?> argument = (ArgumentCommandNode<S, ?>) node;
Nodes.setSuggestionProvider(argument, suggestionProvider);
}
return this;
}

Expand All @@ -74,15 +85,6 @@ private BNode(CommandNode<S> node) {
return node;
}

public static <S> @NotNull BNode<S> of(@NotNull CommandNode<S> node) {
notNull(node, "node");
return new BNode<>(node);
}

public static <S> @NotNull BNode<S> literal(@NotNull String name) {
return of(LiteralArgumentBuilder.<S>literal(name).build());
}

@NotNull BNode<S> nextChild() {
return of(node.getChildren().iterator().next());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
import revxrsal.commands.stream.StringStream;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static revxrsal.commands.autocomplete.SuggestionProvider.empty;
import static revxrsal.commands.util.Strings.stripNamespace;
Expand Down Expand Up @@ -78,9 +81,10 @@ public final class BrigadierAdapter {
ParameterNode<A, ?> parameter,
BrigadierConverter<A, S> converter
) {
var suggestions = parameter.suggestions();
revxrsal.commands.autocomplete.SuggestionProvider<A> suggestions = parameter.suggestions();
if (suggestions.equals(empty())) {
if (parameter.parameterType() instanceof BrigadierParameterType<?, ?> brigadierParameterType) {
if (parameter.parameterType() instanceof BrigadierParameterType<?, ?>) {
BrigadierParameterType<?, ?> brigadierParameterType = (BrigadierParameterType<?, ?>) parameter.parameterType();
return brigadierParameterType.argumentType::listSuggestions;
}
return null;
Expand All @@ -102,12 +106,12 @@ public final class BrigadierAdapter {
return provideAsyncCompletions((AsyncSuggestionProvider<A>) suggestions, builder, test.context(), tooltip);
}

var values = suggestions.getSuggestions(test.context())
List<@NotNull Suggestion> values = suggestions.getSuggestions(test.context())
.stream()
.sorted(String.CASE_INSENSITIVE_ORDER)
.distinct()
.map(s -> toSuggestion(s, builder, tooltip))
.toList();
.collect(Collectors.toList());
return CompletableFuture.completedFuture(Suggestions.create(builder.getInput(), values));
};
}
Expand All @@ -126,7 +130,7 @@ public final class BrigadierAdapter {
.sorted(String.CASE_INSENSITIVE_ORDER)
.distinct()
.map(v -> toSuggestion(v, builder, tooltip))
.toList());
.collect(Collectors.toList()));
});
}

Expand Down Expand Up @@ -163,17 +167,20 @@ public final class BrigadierAdapter {

/**
* A {@link ParameterType} that wraps a Brigadier {@link ArgumentType}
*
* @param argumentType The argument to wrap
* @param <A> The actor type
* @param <T> The parameter type
*/
private record BrigadierParameterType<A extends CommandActor, T>(
ArgumentType<T> argumentType
) implements ParameterType<A, T> {
private static final class BrigadierParameterType<A extends CommandActor, T> implements ParameterType<A, T> {
private final ArgumentType<T> argumentType;

/**
* @param argumentType The argument to wrap
*/
private BrigadierParameterType(
ArgumentType<T> argumentType
) {this.argumentType = argumentType;}

@Override public boolean isGreedy() {
if (argumentType instanceof StringArgumentType sat) {
if (argumentType instanceof StringArgumentType) {
StringArgumentType sat = (StringArgumentType) argumentType;
return sat.getType() == StringArgumentType.StringType.GREEDY_PHRASE;
}
return false;
Expand All @@ -187,5 +194,28 @@ private record BrigadierParameterType<A extends CommandActor, T>(
input.setPosition(reader.getCursor());
return result;
}

public ArgumentType<T> argumentType() {return argumentType;}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
//noinspection unchecked
BrigadierParameterType<A, ?> that = (BrigadierParameterType<A, ?>) obj;
return Objects.equals(this.argumentType, that.argumentType);
}

@Override
public int hashCode() {
return Objects.hash(argumentType);
}

@Override
public String toString() {
return "BrigadierParameterType[" +
"argumentType=" + argumentType + ']';
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public BrigadierParser(@NotNull BrigadierConverter<A, S> converter) {
BNode<S> elementNode;
if (node.isLiteral()) {
elementNode = BNode.literal(node.name());
} else if (node instanceof ParameterNode<A, ?> parameter) {
} else if (node instanceof ParameterNode) {
ParameterNode<A, ?> parameter = (ParameterNode<A, ?>) node;
if (parameter.isSwitch() || parameter.isFlag())
break;
elementNode = BNode.of(ofParameter(parameter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.node.ParameterNode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;

import static revxrsal.commands.util.Preconditions.notNull;

Expand All @@ -50,7 +48,7 @@ public final class ArgumentTypes<A extends CommandActor> {
* <p>
* These also will not be included in {@link #toBuilder()}.
*/
private static final List<ArgumentTypeFactory<?>> HIGHEST_PRIORITY = List.of(
private static final List<ArgumentTypeFactory<?>> HIGHEST_PRIORITY = Collections.singletonList(
BTypeFactory.INSTANCE
);

Expand All @@ -60,7 +58,7 @@ public final class ArgumentTypes<A extends CommandActor> {
* <p>
* These also will not be included in {@link #toBuilder()}.
*/
private static final List<ArgumentTypeFactory<?>> DEFAULT_FACTORIES = List.of(
private static final List<ArgumentTypeFactory<?>> DEFAULT_FACTORIES = Arrays.asList(
DefaultTypeFactories.LONG,
DefaultTypeFactories.INTEGER,
DefaultTypeFactories.SHORT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ enum BTypeFactory implements ArgumentTypeFactory<CommandActor> {
INSTANCE;

@Override
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
public @Nullable ArgumentType<?> getArgumentType(@NotNull ParameterNode<CommandActor, ?> parameter) {
BType b = parameter.annotations().get(BType.class);
if (b == null)
return null;
Object v = InstanceCreator.create(b.value());
if (v instanceof ArgumentTypeFactory<?> factory)
//noinspection rawtypes
if (v instanceof ArgumentTypeFactory<?>) {
ArgumentTypeFactory<?> factory = (ArgumentTypeFactory<?>) v;
return factory.getArgumentType(((ParameterNode) parameter));
if (v instanceof ArgumentType<?> type)
return type;
}
if (v instanceof ArgumentType<?>) {
return (ArgumentType<?>) v;
}
throw new IllegalArgumentException("Don't know how to create an ArgumentType from @BType(" + b.value().getName() + ".class)");
}
}
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ version = "4.0.0-beta.16"

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
languageVersion.set(JavaLanguageVersion.of(8))
}
}

Expand All @@ -39,7 +39,7 @@ subprojects {

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
languageVersion.set(JavaLanguageVersion.of(8))
}
}

Expand Down
8 changes: 4 additions & 4 deletions bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ dependencies {
exclude(module = "spigot-api")
exclude(module = "brigadier")
}
compileOnly("com.destroystokyo.paper:paper-api:1.13.2-R0.1-SNAPSHOT") {
exclude(module = "spigot-api")
exclude(module = "adventure")
}
compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT")
compileOnly("com.mojang:brigadier:1.0.18")
compileOnly("io.papermc.paper:paper-mojangapi:1.19.1-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.19.1-R0.1-SNAPSHOT")

compileOnly("net.kyori:adventure-platform-bukkit:4.3.4")
}

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public final class BukkitVisitors {
@NotNull ActorFactory<A> actorFactory
) {
if (BukkitVersion.supportsAsyncCompletion()) {
return new LampBuilderVisitor<>() {
return new LampBuilderVisitor<A>() {
private boolean registered = false;

@Override public void visit(Lamp.@NotNull Builder<A> builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@
import revxrsal.commands.process.MessageSender;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

record BasicBukkitActor(
CommandSender sender,
Plugin plugin,
Optional<BukkitAudiences> audiences,
MessageSender<BukkitCommandActor, ComponentLike> messageSender,
Lamp<BukkitCommandActor> lamp
) implements BukkitCommandActor {
final class BasicBukkitActor implements BukkitCommandActor {

private static final UUID CONSOLE_UUID = new UUID(0, 0);
private final CommandSender sender;
private final Plugin plugin;
private final Optional<BukkitAudiences> audiences;
private final MessageSender<BukkitCommandActor, ComponentLike> messageSender;
private final Lamp<BukkitCommandActor> lamp;

BasicBukkitActor(
CommandSender sender,
Plugin plugin,
Optional<BukkitAudiences> audiences,
MessageSender<BukkitCommandActor, ComponentLike> messageSender,
Lamp<BukkitCommandActor> lamp
) {
this.sender = sender;
this.plugin = plugin;
this.audiences = audiences;
this.messageSender = messageSender;
this.lamp = lamp;
}

@Override public @NotNull CommandSender sender() {
return sender;
Expand All @@ -38,8 +52,8 @@ record BasicBukkitActor(
@Override public @NotNull Optional<Audience> audience() {
//noinspection DataFlowIssue
if (sender instanceof Audience)
return Optional.of(sender);
if (audiences.isEmpty())
return Optional.of((Audience) sender);
if (!audiences.isPresent())
return Optional.empty();
BukkitAudiences bukkitAudiences = audiences.get();
return Optional.of(bukkitAudiences.sender(sender()));
Expand All @@ -57,4 +71,38 @@ else if (isConsole())
@Override public Lamp<BukkitCommandActor> lamp() {
return lamp;
}

public Plugin plugin() {return plugin;}

public Optional<BukkitAudiences> audiences() {return audiences;}

public MessageSender<BukkitCommandActor, ComponentLike> messageSender() {return messageSender;}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
BasicBukkitActor that = (BasicBukkitActor) obj;
return Objects.equals(this.sender, that.sender) &&
Objects.equals(this.plugin, that.plugin) &&
Objects.equals(this.audiences, that.audiences) &&
Objects.equals(this.messageSender, that.messageSender) &&
Objects.equals(this.lamp, that.lamp);
}

@Override
public int hashCode() {
return Objects.hash(sender, plugin, audiences, messageSender, lamp);
}

@Override
public String toString() {
return "BasicBukkitActor[" +
"sender=" + sender + ", " +
"plugin=" + plugin + ", " +
"audiences=" + audiences + ", " +
"messageSender=" + messageSender + ", " +
"lamp=" + lamp + ']';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ public final class CommandRegisterListener implements Listener {
@EventHandler
@SuppressWarnings("deprecation")
public void onCommandRegistered(CommandRegisteredEvent<?> event) {
if (!(event.getCommand() instanceof PluginCommand pCommand)) {
if (!(event.getCommand() instanceof PluginCommand)) {
return;
}
PluginCommand pCommand = (PluginCommand) event.getCommand();
if (!(pCommand.getExecutor() instanceof LampCommandExecutor<?>)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ public void onEmptyEntitySelector(EmptyEntitySelectorException e, BukkitCommandA

@Override public void onInputParse(@NotNull InputParseException e, @NotNull BukkitCommandActor actor) {
switch (e.cause()) {
case INVALID_ESCAPE_CHARACTER ->
actor.error(legacyColorize("&cInvalid input. Use &e\\\\ &cto include a backslash."));
case UNCLOSED_QUOTE -> actor.error(legacyColorize("&cUnclosed quote. Make sure to close all quotes."));
case EXPECTED_WHITESPACE ->
actor.error(legacyColorize("&cExpected whitespace to end one argument, but found trailing data."));
case INVALID_ESCAPE_CHARACTER:
actor.error(legacyColorize("&cInvalid input. Use &e\\\\ &cto include a backslash."));
break;
case UNCLOSED_QUOTE:
actor.error(legacyColorize("&cUnclosed quote. Make sure to close all quotes."));
break;
case EXPECTED_WHITESPACE:
actor.error(legacyColorize("&cExpected whitespace to end one argument, but found trailing data."));
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public PlayerParameterType(boolean brigadierEnabled) {
if (entityList.size() != 1)
throw new MoreThanOneEntityException(selector);
Entity entity = entityList.get(0);
if (!(entity instanceof Player player))
if (!(entity instanceof Player))
throw new NonPlayerEntitiesException(selector);
Player player = (Player) entity;
return player;
} catch (IllegalArgumentException e) {
throw new MalformedEntitySelectorException(selector, e.getCause().getMessage());
Expand Down
Loading
Loading