bukkit: Use WrappedBrigadierParser for modern ItemStack suggestions

This commit is contained in:
jmp 2021-04-30 00:12:19 -07:00 committed by Jason
parent 56f8b58489
commit 99040ca68a
8 changed files with 158 additions and 33 deletions

View file

@ -4,5 +4,5 @@
"https://checkstyle.org/dtds/suppressions_1_2.dtd"> "https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions> <suppressions>
<suppress checks="(?:(?:Member|Method)Name|DesignForExtension|Javadoc.*)" files=".*[\\/]mixin[\\/].*"/> <suppress checks="(?:(?:Member|Method)Name|DesignForExtension|Javadoc.*)" files=".*[\\/]mixin[\\/].*"/>
<suppress checks="(?:Javadoc.*)" files=".*[\\/]CraftBukkitReflection.java"/> <suppress checks="(?:Javadoc.*)" files=".*[\\/]bukkit[\\/]internal[\\/].*"/>
</suppressions> </suppressions>

View file

@ -23,9 +23,13 @@
// //
package cloud.commandframework.bukkit; package cloud.commandframework.bukkit;
import cloud.commandframework.brigadier.argument.WrappedBrigadierParser;
import cloud.commandframework.bukkit.internal.BukkitBackwardsBrigadierSenderMapper;
import cloud.commandframework.bukkit.internal.CraftBukkitReflection;
import cloud.commandframework.execution.preprocessor.CommandPreprocessingContext; import cloud.commandframework.execution.preprocessor.CommandPreprocessingContext;
import cloud.commandframework.execution.preprocessor.CommandPreprocessor; import cloud.commandframework.execution.preprocessor.CommandPreprocessor;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Set; import java.util.Set;
@ -39,6 +43,7 @@ final class BukkitCommandPreprocessor<C> implements CommandPreprocessor<C> {
private final BukkitCommandManager<C> commandManager; private final BukkitCommandManager<C> commandManager;
private final Set<CloudBukkitCapabilities> bukkitCapabilities; private final Set<CloudBukkitCapabilities> bukkitCapabilities;
private final @Nullable BukkitBackwardsBrigadierSenderMapper<C, ?> mapper;
/** /**
* The Bukkit Command Preprocessor for storing Bukkit-specific contexts in the command contexts * The Bukkit Command Preprocessor for storing Bukkit-specific contexts in the command contexts
@ -48,10 +53,25 @@ final class BukkitCommandPreprocessor<C> implements CommandPreprocessor<C> {
BukkitCommandPreprocessor(final @NonNull BukkitCommandManager<C> commandManager) { BukkitCommandPreprocessor(final @NonNull BukkitCommandManager<C> commandManager) {
this.commandManager = commandManager; this.commandManager = commandManager;
this.bukkitCapabilities = commandManager.queryCapabilities(); this.bukkitCapabilities = commandManager.queryCapabilities();
if (this.bukkitCapabilities.contains(CloudBukkitCapabilities.BRIGADIER) && CraftBukkitReflection.craftBukkit()) {
this.mapper = new BukkitBackwardsBrigadierSenderMapper<>(this.commandManager);
} else {
this.mapper = null;
}
} }
@Override @Override
public void accept(final @NonNull CommandPreprocessingContext<C> context) { public void accept(final @NonNull CommandPreprocessingContext<C> context) {
if (this.mapper != null) {
// If the server is Brigadier capable but the Brigadier manager has not been registered, store the native
// sender in context manually so that getting suggestions from WrappedBrigadierParser works like expected.
if (!context.getCommandContext().contains(WrappedBrigadierParser.COMMAND_CONTEXT_BRIGADIER_NATIVE_SENDER)) {
context.getCommandContext().store(
WrappedBrigadierParser.COMMAND_CONTEXT_BRIGADIER_NATIVE_SENDER,
this.mapper.apply(context.getCommandContext().getSender())
);
}
}
context.getCommandContext().store( context.getCommandContext().store(
BukkitCommandContextKeys.BUKKIT_COMMAND_SENDER, BukkitCommandContextKeys.BUKKIT_COMMAND_SENDER,
this.commandManager.getBackwardsCommandSenderMapper().apply(context.getCommandContext().getSender()) this.commandManager.getBackwardsCommandSenderMapper().apply(context.getCommandContext().getSender())

View file

@ -25,6 +25,7 @@ package cloud.commandframework.bukkit;
import cloud.commandframework.Command; import cloud.commandframework.Command;
import cloud.commandframework.brigadier.CloudBrigadierManager; import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.internal.BukkitBackwardsBrigadierSenderMapper;
import cloud.commandframework.bukkit.internal.CraftBukkitReflection; import cloud.commandframework.bukkit.internal.CraftBukkitReflection;
import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.CommandContext;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
@ -36,7 +37,6 @@ import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.lang.reflect.Method;
import java.util.Collections; import java.util.Collections;
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
@ -64,19 +64,9 @@ class CloudCommodoreManager<C> extends BukkitPluginRegistrationHandler<C> {
new BukkitBrigadierMapper<>(this.commandManager, this.brigadierManager); new BukkitBrigadierMapper<>(this.commandManager, this.brigadierManager);
if (!CraftBukkitReflection.craftBukkit()) { if (CraftBukkitReflection.craftBukkit()) {
return; this.brigadierManager.backwardsBrigadierSenderMapper(new BukkitBackwardsBrigadierSenderMapper<>(this.commandManager));
} }
final Class<?> vanillaCommandWrapperClass = CraftBukkitReflection.needOBCClass("command.VanillaCommandWrapper");
final Method getListenerMethod = CraftBukkitReflection.needMethod(
vanillaCommandWrapperClass, "getListener", CommandSender.class);
this.brigadierManager.backwardsBrigadierSenderMapper(cloud -> {
try {
return getListenerMethod.invoke(null, this.commandManager.getBackwardsCommandSenderMapper().apply(cloud));
} catch (final ReflectiveOperationException e) {
throw new RuntimeException(e);
}
});
} }
@Override @Override

View file

@ -0,0 +1,61 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.bukkit.internal;
import cloud.commandframework.bukkit.BukkitCommandManager;
import com.google.common.annotations.Beta;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.lang.reflect.Method;
import java.util.function.Function;
/**
* This is not API, and as such, may break, change, or be removed without any notice.
*/
@Beta
public final class BukkitBackwardsBrigadierSenderMapper<C, S> implements Function<C, S> {
private static final Class<?> VANILLA_COMMAND_WRAPPER_CLASS =
CraftBukkitReflection.needOBCClass("command.VanillaCommandWrapper");
private static final Method GET_LISTENER_METHOD =
CraftBukkitReflection.needMethod(VANILLA_COMMAND_WRAPPER_CLASS, "getListener", CommandSender.class);
private final BukkitCommandManager<C> commandManager;
public BukkitBackwardsBrigadierSenderMapper(final @NonNull BukkitCommandManager<C> commandManager) {
this.commandManager = commandManager;
}
@SuppressWarnings("unchecked")
@Override
public S apply(final @NonNull C cloud) {
try {
return (S) GET_LISTENER_METHOD.invoke(null, this.commandManager.getBackwardsCommandSenderMapper().apply(cloud));
} catch (final ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,29 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
/**
* Internal classes for the cloud-bukkit implementation. Classes in this package
* are not part of the API, and as such, may break, change, or be removed without
* any notice.
*/
package cloud.commandframework.bukkit.internal;

View file

@ -173,10 +173,7 @@ public final class ItemStackArgument<C> extends CommandArgument<C, ProtoItemStac
final @NonNull CommandContext<C> commandContext, final @NonNull CommandContext<C> commandContext,
final @NonNull String input final @NonNull String input
) { ) {
return Arrays.stream(Material.values()) return this.parser.suggestions(commandContext, input);
.filter(Material::isItem)
.map(value -> value.name().toLowerCase(Locale.ROOT))
.collect(Collectors.toList());
} }
} }
@ -228,6 +225,14 @@ public final class ItemStackArgument<C> extends CommandArgument<C, ProtoItemStac
return this.parser.parse(commandContext, inputQueue); return this.parser.parse(commandContext, inputQueue);
} }
@Override
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return this.parser.suggestions(commandContext, input);
}
private static final class ModernProtoItemStack implements ProtoItemStack { private static final class ModernProtoItemStack implements ProtoItemStack {
private final Object itemInput; private final Object itemInput;
@ -294,6 +299,17 @@ public final class ItemStackArgument<C> extends CommandArgument<C, ProtoItemStac
return this.parser.parse(commandContext, inputQueue); return this.parser.parse(commandContext, inputQueue);
} }
@Override
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return Arrays.stream(Material.values())
.filter(Material::isItem)
.map(value -> value.name().toLowerCase(Locale.ROOT))
.collect(Collectors.toList());
}
private static final class LegacyProtoItemStack implements ProtoItemStack { private static final class LegacyProtoItemStack implements ProtoItemStack {
private final Material material; private final Material material;

View file

@ -27,18 +27,17 @@ import cloud.commandframework.CommandTree;
import cloud.commandframework.arguments.CommandArgument; import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.brigadier.CloudBrigadierManager; import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.BukkitBrigadierMapper; import cloud.commandframework.bukkit.BukkitBrigadierMapper;
import cloud.commandframework.bukkit.internal.BukkitBackwardsBrigadierSenderMapper;
import cloud.commandframework.bukkit.internal.CraftBukkitReflection; import cloud.commandframework.bukkit.internal.CraftBukkitReflection;
import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.CommandContext;
import cloud.commandframework.permission.CommandPermission; import cloud.commandframework.permission.CommandPermission;
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand; import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.lang.reflect.Method;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -59,20 +58,10 @@ class PaperBrigadierListener<C> implements Listener {
new BukkitBrigadierMapper<>(this.paperCommandManager, this.brigadierManager); new BukkitBrigadierMapper<>(this.paperCommandManager, this.brigadierManager);
if (!CraftBukkitReflection.craftBukkit()) { if (CraftBukkitReflection.craftBukkit()) {
return; this.brigadierManager
.backwardsBrigadierSenderMapper(new BukkitBackwardsBrigadierSenderMapper<>(this.paperCommandManager));
} }
final Class<?> vanillaCommandWrapperClass = CraftBukkitReflection.needOBCClass("command.VanillaCommandWrapper");
final Method getListenerMethod = CraftBukkitReflection.needMethod(
vanillaCommandWrapperClass, "getListener", CommandSender.class);
this.brigadierManager.backwardsBrigadierSenderMapper(cloud -> {
try {
return (BukkitBrigadierCommandSource) getListenerMethod
.invoke(null, this.paperCommandManager.getBackwardsCommandSenderMapper().apply(cloud));
} catch (final ReflectiveOperationException e) {
throw new RuntimeException(e);
}
});
} }
protected @NonNull CloudBrigadierManager<C, BukkitBrigadierCommandSource> brigadierManager() { protected @NonNull CloudBrigadierManager<C, BukkitBrigadierCommandSource> brigadierManager() {

View file

@ -26,8 +26,11 @@ package cloud.commandframework.examples.bukkit;
import cloud.commandframework.ArgumentDescription; import cloud.commandframework.ArgumentDescription;
import cloud.commandframework.Command; import cloud.commandframework.Command;
import cloud.commandframework.CommandTree; import cloud.commandframework.CommandTree;
import cloud.commandframework.arguments.compound.ArgumentPair;
import cloud.commandframework.bukkit.data.ProtoItemStack;
import cloud.commandframework.keys.SimpleCloudKey; import cloud.commandframework.keys.SimpleCloudKey;
import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler; import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.MinecraftHelp; import cloud.commandframework.minecraft.extras.MinecraftHelp;
import cloud.commandframework.annotations.AnnotationParser; import cloud.commandframework.annotations.AnnotationParser;
import cloud.commandframework.annotations.Argument; import cloud.commandframework.annotations.Argument;
@ -62,6 +65,7 @@ import cloud.commandframework.minecraft.extras.TextColorArgument;
import cloud.commandframework.paper.PaperCommandManager; import cloud.commandframework.paper.PaperCommandManager;
import cloud.commandframework.permission.PredicatePermission; import cloud.commandframework.permission.PredicatePermission;
import cloud.commandframework.tasks.TaskConsumer; import cloud.commandframework.tasks.TaskConsumer;
import cloud.commandframework.types.tuples.Pair;
import cloud.commandframework.types.tuples.Triplet; import cloud.commandframework.types.tuples.Triplet;
import io.leangen.geantyref.TypeToken; import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.identity.Identity; import net.kyori.adventure.identity.Identity;
@ -410,6 +414,22 @@ public final class ExamplePlugin extends JavaPlugin {
(sender, key) -> "'{input}' is not very cash money of you" (sender, key) -> "'{input}' is not very cash money of you"
); );
} }
// vanilla-like give command
this.manager.command(this.manager.commandBuilder("givetest")
.senderType(Player.class)
.argument(ArgumentPair.of(
this.manager,
"itemstack",
Pair.of("item", "amount"),
Pair.of(ProtoItemStack.class, Integer.class)
).withMapper(ItemStack.class, (sender, pair) -> {
final ProtoItemStack proto = pair.getFirst();
final int amount = pair.getSecond();
return proto.createItemStack(amount, true);
}), ArgumentDescription.of("The ItemStack to give"))
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, text("Vanilla-like give command"))
.handler(ctx -> ((Player) ctx.getSender()).getInventory().addItem(ctx.get("itemstack"))));
} }
@CommandMethod("example help [query]") @CommandMethod("example help [query]")