From 0cf8030f72baef336e98230d4bd7b32222aefa5b Mon Sep 17 00:00:00 2001 From: Jason <11360596+jpenilla@users.noreply.github.com> Date: Thu, 16 Sep 2021 19:46:18 -0500 Subject: [PATCH] bukkit: Make KeyedWorldArgument fallback to normal WorldParser when World does not implement Keyed (#298) --- .../paper/argument/KeyedWorldArgument.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cloud-minecraft/cloud-paper/src/main/java/cloud/commandframework/paper/argument/KeyedWorldArgument.java b/cloud-minecraft/cloud-paper/src/main/java/cloud/commandframework/paper/argument/KeyedWorldArgument.java index 25ef4642..0f83bac5 100644 --- a/cloud-minecraft/cloud-paper/src/main/java/cloud/commandframework/paper/argument/KeyedWorldArgument.java +++ b/cloud-minecraft/cloud-paper/src/main/java/cloud/commandframework/paper/argument/KeyedWorldArgument.java @@ -27,6 +27,7 @@ import cloud.commandframework.ArgumentDescription; import cloud.commandframework.arguments.CommandArgument; import cloud.commandframework.arguments.parser.ArgumentParseResult; import cloud.commandframework.arguments.parser.ArgumentParser; +import cloud.commandframework.bukkit.internal.CraftBukkitReflection; import cloud.commandframework.bukkit.parsers.WorldArgument; import cloud.commandframework.context.CommandContext; import cloud.commandframework.exceptions.parsing.NoInputProvidedException; @@ -44,6 +45,9 @@ import java.util.function.BiFunction; /** * Argument type that parses Bukkit {@link World worlds} from a {@link NamespacedKey}. * + *

Falls back to parsing by name, using the {@link WorldArgument.WorldParser} on server implementations where {@link World} + * does not implement {@link org.bukkit.Keyed}.

+ * * @param Command sender type * @since 1.6.0 */ @@ -162,6 +166,20 @@ public final class KeyedWorldArgument extends CommandArgument { */ public static final class Parser implements ArgumentParser { + private final ArgumentParser parser; + + /** + * Create a new {@link Parser}. + */ + public Parser() { + final Class keyed = CraftBukkitReflection.findClass("org.bukkit.Keyed"); + if (keyed != null && keyed.isAssignableFrom(World.class)) { + this.parser = null; + } else { + this.parser = new WorldArgument.WorldParser<>(); + } + } + @Override public @NonNull ArgumentParseResult<@NonNull World> parse( @NonNull final CommandContext<@NonNull C> commandContext, @@ -175,6 +193,10 @@ public final class KeyedWorldArgument extends CommandArgument { )); } + if (this.parser != null) { + return this.parser.parse(commandContext, inputQueue); + } + final NamespacedKey key = NamespacedKey.fromString(input); if (key == null) { return ArgumentParseResult.failure(new WorldArgument.WorldParseException(input, commandContext)); @@ -194,6 +216,10 @@ public final class KeyedWorldArgument extends CommandArgument { final @NonNull CommandContext commandContext, final @NonNull String input ) { + if (this.parser != null) { + return this.parser.suggestions(commandContext, input); + } + final List completions = new ArrayList<>(); for (final World world : Bukkit.getWorlds()) { if (world.getKey().getNamespace().equals(NamespacedKey.MINECRAFT)) {