Fix Brigadier suggestions for KeyedWorldArgument
This commit is contained in:
parent
4447ab451f
commit
b4492e9bc5
3 changed files with 48 additions and 15 deletions
|
|
@ -169,6 +169,24 @@ public final class BukkitBrigadierMapper<C> {
|
||||||
public <T extends ArgumentParser<C, ?>> void mapSimpleNMS(
|
public <T extends ArgumentParser<C, ?>> void mapSimpleNMS(
|
||||||
final @NonNull TypeToken<T> type,
|
final @NonNull TypeToken<T> type,
|
||||||
final @NonNull String argumentId
|
final @NonNull String argumentId
|
||||||
|
) {
|
||||||
|
this.mapSimpleNMS(type, argumentId, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to register a mapping between a cloud argument parser type and an NMS brigadier argument type which
|
||||||
|
* has a no-args constructor.
|
||||||
|
*
|
||||||
|
* @param type Type to map
|
||||||
|
* @param <T> argument parser type
|
||||||
|
* @param argumentId network id of argument type
|
||||||
|
* @param useCloudSuggestions whether to use cloud suggestions
|
||||||
|
* @since 1.6.0
|
||||||
|
*/
|
||||||
|
public <T extends ArgumentParser<C, ?>> void mapSimpleNMS(
|
||||||
|
final @NonNull TypeToken<T> type,
|
||||||
|
final @NonNull String argumentId,
|
||||||
|
final boolean useCloudSuggestions
|
||||||
) {
|
) {
|
||||||
final Constructor<?> constructor;
|
final Constructor<?> constructor;
|
||||||
try {
|
try {
|
||||||
|
|
@ -182,21 +200,26 @@ public final class BukkitBrigadierMapper<C> {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.brigadierManager.registerMapping(type, builder -> builder.to(argument -> {
|
this.brigadierManager.registerMapping(type, builder -> {
|
||||||
try {
|
builder.to(argument -> {
|
||||||
return (ArgumentType<?>) constructor.newInstance();
|
try {
|
||||||
} catch (final ReflectiveOperationException e) {
|
return (ArgumentType<?>) constructor.newInstance();
|
||||||
this.commandManager.getOwningPlugin().getLogger().log(
|
} catch (final ReflectiveOperationException e) {
|
||||||
Level.WARNING,
|
this.commandManager.getOwningPlugin().getLogger().log(
|
||||||
String.format(
|
Level.WARNING,
|
||||||
"Failed to create instance of brigadier argument type '%s'.",
|
String.format(
|
||||||
GenericTypeReflector.erase(type.getType()).getCanonicalName()
|
"Failed to create instance of brigadier argument type '%s'.",
|
||||||
),
|
GenericTypeReflector.erase(type.getType()).getCanonicalName()
|
||||||
e
|
),
|
||||||
);
|
e
|
||||||
return fallbackType();
|
);
|
||||||
|
return fallbackType();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (useCloudSuggestions) {
|
||||||
|
builder.cloudSuggestions();
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ final class PaperBrigadierMapper<C> {
|
||||||
final Class<?> keyed = CraftBukkitReflection.findClass("org.bukkit.Keyed");
|
final Class<?> keyed = CraftBukkitReflection.findClass("org.bukkit.Keyed");
|
||||||
if (keyed != null && keyed.isAssignableFrom(World.class)) {
|
if (keyed != null && keyed.isAssignableFrom(World.class)) {
|
||||||
mapper.mapSimpleNMS(new TypeToken<KeyedWorldArgument.Parser<C>>() {
|
mapper.mapSimpleNMS(new TypeToken<KeyedWorldArgument.Parser<C>>() {
|
||||||
}, "resource_location");
|
}, "resource_location", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ import cloud.commandframework.minecraft.extras.MinecraftHelp;
|
||||||
import cloud.commandframework.minecraft.extras.RichDescription;
|
import cloud.commandframework.minecraft.extras.RichDescription;
|
||||||
import cloud.commandframework.minecraft.extras.TextColorArgument;
|
import cloud.commandframework.minecraft.extras.TextColorArgument;
|
||||||
import cloud.commandframework.paper.PaperCommandManager;
|
import cloud.commandframework.paper.PaperCommandManager;
|
||||||
|
import cloud.commandframework.paper.argument.KeyedWorldArgument;
|
||||||
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.Pair;
|
||||||
|
|
@ -478,6 +479,15 @@ public final class ExamplePlugin extends JavaPlugin {
|
||||||
final ItemStack stack = ctx.get("itemstack");
|
final ItemStack stack = ctx.get("itemstack");
|
||||||
((Player) ctx.getSender()).getInventory().addItem(stack);
|
((Player) ctx.getSender()).getInventory().addItem(stack);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
this.manager.command(builder.literal("keyed_world")
|
||||||
|
.argument(KeyedWorldArgument.of("world"))
|
||||||
|
.senderType(Player.class)
|
||||||
|
.handler(ctx -> {
|
||||||
|
final World world = ctx.get("world");
|
||||||
|
final Player sender = (Player) ctx.getSender();
|
||||||
|
this.getServer().getScheduler().runTask(this, () -> sender.teleport(world.getSpawnLocation()));
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@CommandMethod("example help [query]")
|
@CommandMethod("example help [query]")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue