Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 17
java-version: 21
- uses: gradle/actions/setup-gradle@v3
- name: Build
run: ./gradlew build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@

import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.ParsedCommandNode;
import com.mojang.brigadier.context.StringRange;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.brigadier.parser.WrappedBrigadierParser;
import org.incendo.cloud.type.tuple.Pair;

/**
* Brigadier {@link Command} implementation that delegates to cloud.
Expand Down Expand Up @@ -79,14 +86,60 @@ public CloudBrigadierCommand(
@Override
public int run(final @NonNull CommandContext<S> ctx) {
final S source = ctx.getSource();
final String input = ctx.getInput().substring(ctx.getLastChild().getNodes().get(0).getRange().getStart());
final String input = this.inputMapper.apply(
ctx.getInput().substring(parsedNodes(ctx.getLastChild()).get(0).second().getStart())
);
final C sender = this.brigadierManager.senderMapper().map(source);

this.commandManager.commandExecutor().executeCommand(
sender,
this.inputMapper.apply(input),
input,
cloudContext -> cloudContext.store(WrappedBrigadierParser.COMMAND_CONTEXT_BRIGADIER_NATIVE_SENDER, source)
);
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
}

/**
* Return type changed at some point, but information is essentially the same. This code works for both versions of the
* method.
*
* @param commandContext command context
* @param <S> source type
* @return parsed nodes
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <S> List<Pair<com.mojang.brigadier.tree.CommandNode<S>, StringRange>> parsedNodes(
final com.mojang.brigadier.context.CommandContext<S> commandContext
) {
try {
final Method getNodesMethod = commandContext.getClass().getDeclaredMethod("getNodes");
final Object nodes = getNodesMethod.invoke(commandContext);
if (nodes instanceof List) {
return ParsedCommandNodeHandler.toPairList((List) nodes);
} else if (nodes instanceof Map) {
return ((Map<com.mojang.brigadier.tree.CommandNode<S>, StringRange>) nodes).entrySet().stream()
.map(entry -> Pair.of(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
} else {
throw new IllegalStateException();
}
} catch (final ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
}


// Inner class to prevent attempting to load ParsedCommandNode when it doesn't exist
@SuppressWarnings("unchecked")
private static final class ParsedCommandNodeHandler {

private ParsedCommandNodeHandler() {
}

private static <S> List<Pair<com.mojang.brigadier.tree.CommandNode<S>, StringRange>> toPairList(final List<?> nodes) {
return ((List<ParsedCommandNode<S>>) nodes).stream()
.map(n -> Pair.of(n.getNode(), n.getRange()))
.collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@
//
package org.incendo.cloud.brigadier.suggestion;

import com.mojang.brigadier.context.ParsedCommandNode;
import com.mojang.brigadier.context.StringRange;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.tree.CommandNode;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand All @@ -41,10 +36,12 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.brigadier.CloudBrigadierManager;
import org.incendo.cloud.brigadier.parser.WrappedBrigadierParser;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.suggestion.SuggestionFactory;
import org.incendo.cloud.type.tuple.Pair;

import static org.incendo.cloud.brigadier.CloudBrigadierCommand.parsedNodes;

/**
* Produces Brigadier suggestions by invoking the Cloud suggestion provider.
Expand Down Expand Up @@ -82,14 +79,12 @@ public BrigadierSuggestionFactory(
*
* @param senderContext the brigadier context
* @param parentNode the parent command node
* @param component the command component to generate suggestions for
* @param builder the suggestion builder to generate suggestions with
* @return future that completes with the suggestions
*/
public @NonNull CompletableFuture<@NonNull Suggestions> buildSuggestions(
final com.mojang.brigadier.context.@NonNull CommandContext<S> senderContext,
final org.incendo.cloud.internal.@Nullable CommandNode<C> parentNode,
final @NonNull CommandComponent<C> component,
final @NonNull SuggestionsBuilder builder
) {
final C cloudSender = this.cloudBrigadierManager.senderMapper().map(senderContext.getSource());
Expand All @@ -98,7 +93,9 @@ public BrigadierSuggestionFactory(
cloudSender,
this.commandManager
);
String command = builder.getInput().substring(getNodes(senderContext.getLastChild()).get(0).second().getStart());
commandContext.store(WrappedBrigadierParser.COMMAND_CONTEXT_BRIGADIER_NATIVE_SENDER, senderContext.getSource());
String command = builder.getInput()
.substring(parsedNodes(senderContext.getLastChild()).get(0).second().getStart());

/* Remove namespace */
final String leading = command.split(" ")[0];
Expand Down Expand Up @@ -135,48 +132,4 @@ public BrigadierSuggestionFactory(
return suggestionsBuilder.build();
});
}

/**
* Return type changed at some point, but information is essentially the same. This code works for both versions of the
* method.
*
* @param commandContext command context
* @param <S> source type
* @return parsed nodes
*/
@SuppressWarnings("unchecked")
private static <S> List<Pair<CommandNode<S>, StringRange>> getNodes(
final com.mojang.brigadier.context.CommandContext<S> commandContext
) {
try {
final Method getNodesMethod = commandContext.getClass().getDeclaredMethod("getNodes");
final Object nodes = getNodesMethod.invoke(commandContext);
if (nodes instanceof List) {
return ParsedCommandNodeHandler.toPairList((List) nodes);
} else if (nodes instanceof Map) {
return ((Map<CommandNode<S>, StringRange>) nodes).entrySet().stream()
.map(entry -> Pair.of(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
} else {
throw new IllegalStateException();
}
} catch (final ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
}


// Inner class to prevent attempting to load ParsedCommandNode when it doesn't exist
@SuppressWarnings("unchecked")
private static final class ParsedCommandNodeHandler {

private ParsedCommandNodeHandler() {
}

private static <S> List<Pair<CommandNode<S>, StringRange>> toPairList(final List<?> nodes) {
return ((List<ParsedCommandNode<S>>) nodes).stream()
.map(n -> Pair.of(n.getNode(), n.getRange()))
.collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public CloudDelegatingSuggestionProvider(
return this.brigadierSuggestionFactory.buildSuggestions(
context,
this.node.parent(),
this.node.component(),
builder
);
}
Expand Down
4 changes: 4 additions & 0 deletions cloud-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ dependencies {
}
}

configurations.javadocLinksSources {
exclude("io.papermc.paper")
}

spotless {
java {
targetExclude(file("src/main/java/cloud/commandframework/bukkit/internal/MinecraftArgumentTypes.java"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.apiguardian.api.API;
Expand All @@ -39,8 +38,8 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.incendo.cloud.Command;
import org.incendo.cloud.bukkit.internal.BukkitHelper;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.description.CommandDescription;
import org.incendo.cloud.internal.CommandNode;
import org.incendo.cloud.permission.Permission;
import org.incendo.cloud.suggestion.Suggestion;
Expand All @@ -57,20 +56,6 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi

private boolean disabled;

private static @NonNull String description(final @NonNull Command<?> command) {
final Optional<String> bukkitDescription = command.commandMeta().optional(BukkitCommandMeta.BUKKIT_DESCRIPTION);
if (bukkitDescription.isPresent()) {
return bukkitDescription.get();
}

final CommandDescription description = command.commandDescription();
if (!description.isEmpty()) {
return description.description().textDescription();
}

return command.rootComponent().description().textDescription();
}

BukkitCommand(
final @NonNull String label,
final @NonNull List<@NonNull String> aliases,
Expand All @@ -80,7 +65,7 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
) {
super(
label,
description(cloudCommand),
BukkitHelper.description(cloudCommand),
"",
aliases
);
Expand Down Expand Up @@ -129,7 +114,7 @@ public boolean execute(

@Override
public @NonNull String getDescription() {
return description(this.cloudCommand);
return BukkitHelper.description(this.cloudCommand);
}

@Override
Expand Down
Loading