Store component type in the component. Add mappings to native (NMS) Brigadier types. Shorten builder names. Make the Bukkit command manager take in a generic command sender type.
This commit is contained in:
parent
b8db1d3cb7
commit
d144c3ea8c
29 changed files with 524 additions and 158 deletions
|
|
@ -52,6 +52,7 @@ import javax.annotation.Nonnull;
|
|||
import java.util.Map;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Manager used to map cloud {@link com.intellectualsites.commands.Command}
|
||||
|
|
@ -67,12 +68,14 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
|
||||
private final Map<Class<?>, Function<? extends CommandComponent<C, ?>,
|
||||
? extends ArgumentType<?>>> mappers;
|
||||
private final Map<Class<?>, Supplier<ArgumentType<?>>> defaultArgumentTypeSuppliers;
|
||||
|
||||
/**
|
||||
* Create a new cloud brigadier manager
|
||||
*/
|
||||
public CloudBrigadierManager() {
|
||||
this.mappers = Maps.newHashMap();
|
||||
this.defaultArgumentTypeSuppliers = Maps.newHashMap();
|
||||
this.registerInternalMappings();
|
||||
}
|
||||
|
||||
|
|
@ -106,9 +109,6 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Integer.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Integer.MAX_VALUE;
|
||||
|
||||
System.out.println("Constructing new IntegerArgumentType with min " + hasMin + " | max " + hasMax);
|
||||
|
||||
if (hasMin) {
|
||||
return IntegerArgumentType.integer(component.getMin(), component.getMax());
|
||||
} else if (hasMax) {
|
||||
|
|
@ -150,8 +150,6 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
this.registerMapping(new TypeToken<StringComponent<C>>() {
|
||||
}, component -> {
|
||||
switch (component.getStringMode()) {
|
||||
case SINGLE:
|
||||
return StringArgumentType.word();
|
||||
case QUOTED:
|
||||
return StringArgumentType.string();
|
||||
case GREEDY:
|
||||
|
|
@ -177,6 +175,17 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
this.mappers.put(componentType.getRawType(), mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a default mapping to between a class and a Brigadier argument type
|
||||
*
|
||||
* @param clazz Type to map
|
||||
* @param supplier Supplier that supplies the argument type
|
||||
*/
|
||||
public void registerDefaultArgumentTypeSupplier(@Nonnull final Class<?> clazz,
|
||||
@Nonnull final Supplier<ArgumentType<?>> supplier) {
|
||||
this.defaultArgumentTypeSuppliers.put(clazz, supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Brigadier {@link ArgumentType} from a cloud {@link CommandComponent}
|
||||
*
|
||||
|
|
@ -199,6 +208,11 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
@Nonnull
|
||||
private <T, K extends CommandComponent<C, T>> ArgumentType<?> createDefaultMapper(@Nonnull final CommandComponent<C, T>
|
||||
component) {
|
||||
final Supplier<ArgumentType<?>> argumentTypeSupplier = this.defaultArgumentTypeSuppliers.get(component.getValueType());
|
||||
if (argumentTypeSupplier != null) {
|
||||
return argumentTypeSupplier.get();
|
||||
}
|
||||
System.err.printf("Found not native mapping for '%s'\n", component.getValueType().getCanonicalName());
|
||||
return StringArgumentType.string();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,18 +24,24 @@
|
|||
package com.intellectualsites.commands;
|
||||
|
||||
import com.intellectualsites.commands.components.StaticComponent;
|
||||
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||
import com.intellectualsites.commands.components.standard.EnumComponent;
|
||||
import com.intellectualsites.commands.components.standard.IntegerComponent;
|
||||
import com.intellectualsites.commands.components.standard.StringComponent;
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import com.intellectualsites.commands.parsers.WorldComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class BukkitTest extends JavaPlugin {
|
||||
|
|
@ -46,52 +52,89 @@ public final class BukkitTest extends JavaPlugin {
|
|||
@Override
|
||||
public void onEnable() {
|
||||
try {
|
||||
final PaperCommandManager commandManager = new PaperCommandManager(this,
|
||||
CommandExecutionCoordinator.simpleCoordinator());
|
||||
commandManager.registerBrigadier();
|
||||
commandManager.registerCommand(commandManager.commandBuilder("gamemode",
|
||||
Collections.singleton("gajmöde"),
|
||||
BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Your ugli")
|
||||
.build())
|
||||
.withComponent(EnumComponent.required(GameMode.class, "gamemode"))
|
||||
.withComponent(StringComponent.<BukkitCommandSender>newBuilder("player")
|
||||
.withSuggestionsProvider((v1, v2) -> {
|
||||
final List<String> suggestions =
|
||||
new ArrayList<>(
|
||||
Bukkit.getOnlinePlayers()
|
||||
.stream()
|
||||
.map(Player::getName)
|
||||
.collect(Collectors.toList()));
|
||||
suggestions.add("dog");
|
||||
suggestions.add("cat");
|
||||
return suggestions;
|
||||
}).build())
|
||||
.withHandler(c -> c.getCommandSender()
|
||||
.asPlayer()
|
||||
.setGameMode(c.<GameMode>get("gamemode")
|
||||
.orElse(GameMode.SURVIVAL)))
|
||||
.build())
|
||||
.registerCommand(commandManager.commandBuilder("kenny")
|
||||
.withComponent(StaticComponent.required("sux"))
|
||||
.withComponent(IntegerComponent
|
||||
.<BukkitCommandSender>newBuilder("perc")
|
||||
.withMin(PERC_MIN).withMax(PERC_MAX).build())
|
||||
.withHandler(context -> {
|
||||
context.getCommandSender().asPlayer().sendMessage(String.format(
|
||||
"Kenny sux %d%%",
|
||||
context.<Integer>get("perc").orElse(PERC_MIN)
|
||||
));
|
||||
})
|
||||
.build())
|
||||
.registerCommand(commandManager.commandBuilder("test")
|
||||
.withComponent(StaticComponent.required("one"))
|
||||
.withHandler(c -> c.getCommandSender().sendMessage("One!"))
|
||||
.build())
|
||||
.registerCommand(commandManager.commandBuilder("test")
|
||||
.withComponent(StaticComponent.required("two"))
|
||||
.withHandler(c -> c.getCommandSender().sendMessage("Two!"))
|
||||
.build());
|
||||
final PaperCommandManager<BukkitCommandSender> mgr = new PaperCommandManager<>(this,
|
||||
CommandExecutionCoordinator.simpleCoordinator());
|
||||
mgr.registerBrigadier();
|
||||
mgr.command(mgr.commandBuilder("gamemode",
|
||||
Collections.singleton("gajmöde"),
|
||||
BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Your ugli")
|
||||
.build())
|
||||
.component(EnumComponent.required(GameMode.class, "gamemode"))
|
||||
.component(StringComponent.<BukkitCommandSender>newBuilder("player")
|
||||
.withSuggestionsProvider((v1, v2) -> {
|
||||
final List<String> suggestions =
|
||||
new ArrayList<>(
|
||||
Bukkit.getOnlinePlayers()
|
||||
.stream()
|
||||
.map(Player::getName)
|
||||
.collect(Collectors.toList()));
|
||||
suggestions.add("dog");
|
||||
suggestions.add("cat");
|
||||
return suggestions;
|
||||
}).build())
|
||||
.handler(c -> c.getSender()
|
||||
.asPlayer()
|
||||
.setGameMode(c.<GameMode>get("gamemode")
|
||||
.orElse(GameMode.SURVIVAL)))
|
||||
.build())
|
||||
.command(mgr.commandBuilder("kenny")
|
||||
.component(StaticComponent.required("sux"))
|
||||
.component(IntegerComponent
|
||||
.<BukkitCommandSender>newBuilder("perc")
|
||||
.withMin(PERC_MIN).withMax(PERC_MAX).build())
|
||||
.handler(context -> {
|
||||
context.getSender().asPlayer().sendMessage(String.format(
|
||||
"Kenny sux %d%%",
|
||||
context.<Integer>get("perc").orElse(PERC_MIN)
|
||||
));
|
||||
})
|
||||
.build())
|
||||
.command(mgr.commandBuilder("test")
|
||||
.component(StaticComponent.required("one"))
|
||||
.handler(c -> c.getSender().sendMessage("One!"))
|
||||
.build())
|
||||
.command(mgr.commandBuilder("test")
|
||||
.component(StaticComponent.required("two"))
|
||||
.handler(c -> c.getSender().sendMessage("Two!"))
|
||||
.build())
|
||||
.command(mgr.commandBuilder("uuidtest")
|
||||
.component(UUID.class, "uuid", builder -> builder
|
||||
.asRequired()
|
||||
.withParser((c, i) -> {
|
||||
final String string = i.peek();
|
||||
try {
|
||||
final UUID uuid = UUID.fromString(string);
|
||||
i.remove();
|
||||
return ComponentParseResult.success(uuid);
|
||||
} catch (final Exception e) {
|
||||
return ComponentParseResult.failure(e);
|
||||
}
|
||||
}).build())
|
||||
.handler(c -> c.getSender()
|
||||
.sendMessage(String.format("UUID: %s\n", c.<UUID>get("uuid").orElse(null))))
|
||||
.build())
|
||||
.command(mgr.commandBuilder("give")
|
||||
.component(EnumComponent.required(Material.class, "material"))
|
||||
.component(IntegerComponent.required("amount"))
|
||||
.handler(c -> {
|
||||
final Material material = c.getRequired("material");
|
||||
final int amount = c.getRequired("amount");
|
||||
final ItemStack itemStack = new ItemStack(material, amount);
|
||||
c.getSender().asPlayer().getInventory().addItem(itemStack);
|
||||
c.getSender().sendMessage("You've been given stuff, bro.");
|
||||
})
|
||||
.build())
|
||||
.command(mgr.commandBuilder("worldtp", BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Teleport to a world")
|
||||
.build())
|
||||
.component(WorldComponent.required("world"))
|
||||
.handler(c -> {
|
||||
final World world = c.getRequired("world");
|
||||
c.getSender().asPlayer().teleport(world.getSpawnLocation());
|
||||
c.getSender().sendMessage("Teleported.");
|
||||
})
|
||||
.build());
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,15 +32,15 @@ import org.bukkit.plugin.Plugin;
|
|||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
|
||||
final class BukkitCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand {
|
||||
final class BukkitCommand<C extends BukkitCommandSender> extends org.bukkit.command.Command implements PluginIdentifiableCommand {
|
||||
|
||||
private final CommandComponent<BukkitCommandSender, ?> command;
|
||||
private final BukkitCommandManager bukkitCommandManager;
|
||||
private final com.intellectualsites.commands.Command<BukkitCommandSender, BukkitCommandMeta> cloudCommand;
|
||||
private final CommandComponent<C, ?> command;
|
||||
private final BukkitCommandManager<C> bukkitCommandManager;
|
||||
private final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand;
|
||||
|
||||
BukkitCommand(@Nonnull final com.intellectualsites.commands.Command<BukkitCommandSender, BukkitCommandMeta> cloudCommand,
|
||||
@Nonnull final CommandComponent<BukkitCommandSender, ?> command,
|
||||
@Nonnull final BukkitCommandManager bukkitCommandManager) {
|
||||
BukkitCommand(@Nonnull final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand,
|
||||
@Nonnull final CommandComponent<C, ?> command,
|
||||
@Nonnull final BukkitCommandManager<C> bukkitCommandManager) {
|
||||
super(command.getName());
|
||||
this.command = command;
|
||||
this.bukkitCommandManager = bukkitCommandManager;
|
||||
|
|
@ -54,7 +54,7 @@ final class BukkitCommand extends org.bukkit.command.Command implements PluginId
|
|||
for (final String string : strings) {
|
||||
builder.append(" ").append(string);
|
||||
}
|
||||
this.bukkitCommandManager.executeCommand(BukkitCommandSender.of(commandSender), builder.toString())
|
||||
this.bukkitCommandManager.executeCommand((C) BukkitCommandSender.of(commandSender), builder.toString())
|
||||
.whenComplete(((commandResult, throwable) -> {
|
||||
if (throwable != null) {
|
||||
commandSender.sendMessage(ChatColor.RED + throwable.getCause().getMessage());
|
||||
|
|
@ -79,7 +79,7 @@ final class BukkitCommand extends org.bukkit.command.Command implements PluginId
|
|||
for (final String string : args) {
|
||||
builder.append(" ").append(string);
|
||||
}
|
||||
return this.bukkitCommandManager.suggest(BukkitCommandSender.of(sender), builder.toString());
|
||||
return this.bukkitCommandManager.suggest((C) BukkitCommandSender.of(sender), builder.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -23,7 +23,10 @@
|
|||
//
|
||||
package com.intellectualsites.commands;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import com.intellectualsites.commands.parsers.WorldComponent;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
|
@ -33,7 +36,7 @@ import java.util.function.Function;
|
|||
* Command manager for the Bukkit platform, using {@link BukkitCommandSender} as the
|
||||
* command sender type
|
||||
*/
|
||||
public class BukkitCommandManager extends CommandManager<BukkitCommandSender, BukkitCommandMeta> {
|
||||
public class BukkitCommandManager<C extends BukkitCommandSender> extends CommandManager<C, BukkitCommandMeta> {
|
||||
|
||||
private final Plugin owningPlugin;
|
||||
|
||||
|
|
@ -45,12 +48,15 @@ public class BukkitCommandManager extends CommandManager<BukkitCommandSender, Bu
|
|||
* @throws Exception If the construction of the manager fails
|
||||
*/
|
||||
public BukkitCommandManager(@Nonnull final Plugin owningPlugin,
|
||||
@Nonnull final Function<CommandTree<BukkitCommandSender, BukkitCommandMeta>,
|
||||
CommandExecutionCoordinator<BukkitCommandSender, BukkitCommandMeta>> commandExecutionCoordinator)
|
||||
@Nonnull final Function<CommandTree<C, BukkitCommandMeta>,
|
||||
CommandExecutionCoordinator<C, BukkitCommandMeta>> commandExecutionCoordinator)
|
||||
throws Exception {
|
||||
super(commandExecutionCoordinator, new BukkitPluginRegistrationHandler());
|
||||
((BukkitPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this);
|
||||
this.owningPlugin = owningPlugin;
|
||||
|
||||
/* Register Bukkit parsers */
|
||||
this.getParserRegistry().registerParserSupplier(TypeToken.of(World.class), params -> new WorldComponent.WorldParser<>());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -37,7 +37,12 @@ public abstract class BukkitCommandSender implements CommandSender {
|
|||
|
||||
private final org.bukkit.command.CommandSender internalSender;
|
||||
|
||||
BukkitCommandSender(@Nonnull final org.bukkit.command.CommandSender internalSender) {
|
||||
/**
|
||||
* Create a new command sender from a Bukkit {@link CommandSender}
|
||||
*
|
||||
* @param internalSender Bukkit command sender
|
||||
*/
|
||||
public BukkitCommandSender(@Nonnull final org.bukkit.command.CommandSender internalSender) {
|
||||
this.internalSender = internalSender;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,174 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// 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 com.intellectualsites.commands.parsers;
|
||||
|
||||
import com.intellectualsites.commands.BukkitCommandSender;
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||
import com.intellectualsites.commands.components.parser.ComponentParser;
|
||||
import com.intellectualsites.commands.context.CommandContext;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* cloud component type that parses Bukkit {@link org.bukkit.World worlds}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public class WorldComponent<C extends BukkitCommandSender> extends CommandComponent<C, World> {
|
||||
|
||||
protected WorldComponent(final boolean required,
|
||||
@Nonnull final String name,
|
||||
@Nonnull final String defaultValue) {
|
||||
super(required, name, new WorldParser<>(), defaultValue, World.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder
|
||||
*
|
||||
* @param name Name of the component
|
||||
* @param <C> Command sender type
|
||||
* @return Created builder
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends BukkitCommandSender> CommandComponent.Builder<C, World> newBuilder(@Nonnull final String name) {
|
||||
return new WorldComponent.Builder<>(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new required component
|
||||
*
|
||||
* @param name Component name
|
||||
* @param <C> Command sender type
|
||||
* @return Created component
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends BukkitCommandSender> CommandComponent<C, World> required(@Nonnull final String name) {
|
||||
return WorldComponent.<C>newBuilder(name).asRequired().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional component
|
||||
*
|
||||
* @param name Component name
|
||||
* @param <C> Command sender type
|
||||
* @return Created component
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends BukkitCommandSender> CommandComponent<C, World> optional(@Nonnull final String name) {
|
||||
return WorldComponent.<C>newBuilder(name).asOptional().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional component with a default value
|
||||
*
|
||||
* @param name Component name
|
||||
* @param defaultValue Default value
|
||||
* @param <C> Command sender type
|
||||
* @return Created component
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends BukkitCommandSender> CommandComponent<C, World> optional(@Nonnull final String name,
|
||||
@Nonnull final String defaultValue) {
|
||||
return WorldComponent.<C>newBuilder(name).asOptionalWithDefault(defaultValue).build();
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder<C extends BukkitCommandSender> extends CommandComponent.Builder<C, World> {
|
||||
|
||||
protected Builder(@Nonnull final String name) {
|
||||
super(World.class, name);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CommandComponent<C, World> build() {
|
||||
return new WorldComponent<>(this.isRequired(), this.getName(), this.getDefaultValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final class WorldParser<C extends BukkitCommandSender> implements ComponentParser<C, World> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ComponentParseResult<World> parse(@Nonnull final CommandContext<C> commandContext,
|
||||
@Nonnull final Queue<String> inputQueue) {
|
||||
final String input = inputQueue.peek();
|
||||
if (input == null) {
|
||||
return ComponentParseResult.failure(new NullPointerException("No input was provided"));
|
||||
}
|
||||
|
||||
final World world = Bukkit.getWorld(input);
|
||||
if (world == null) {
|
||||
return ComponentParseResult.failure(new WorldParseException(input));
|
||||
}
|
||||
|
||||
inputQueue.remove();
|
||||
return ComponentParseResult.success(world);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext, @Nonnull final String input) {
|
||||
return Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static final class WorldParseException extends IllegalArgumentException {
|
||||
|
||||
private final String input;
|
||||
|
||||
/**
|
||||
* Construct a new WorldParseException
|
||||
*
|
||||
* @param input Input
|
||||
*/
|
||||
public WorldParseException(@Nonnull final String input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the input provided by the sender
|
||||
*
|
||||
* @return Input
|
||||
*/
|
||||
public String getInput() {
|
||||
return this.input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("'%s' is not a valid Minecraft world", this.input);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
/**
|
||||
* Bukkit specific command components
|
||||
*/
|
||||
package com.intellectualsites.commands.parsers;
|
||||
|
|
@ -27,10 +27,19 @@ import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
|
|||
import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
|
||||
import com.intellectualsites.commands.brigadier.CloudBrigadierManager;
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.UUID;
|
||||
|
||||
class PaperBrigadierListener implements Listener {
|
||||
|
||||
|
|
@ -40,6 +49,49 @@ class PaperBrigadierListener implements Listener {
|
|||
PaperBrigadierListener(@Nonnull final PaperCommandManager paperCommandManager) throws Exception {
|
||||
this.paperCommandManager = paperCommandManager;
|
||||
this.brigadierManager = new CloudBrigadierManager<>();
|
||||
/* Register default mappings */
|
||||
final String version = Bukkit.getServer().getClass().getPackage().getName();
|
||||
final String nms = version.substring(version.lastIndexOf(".") + 1);
|
||||
try {
|
||||
/* Map UUID */
|
||||
this.mapSimpleNMS(UUID.class, this.getNMSArgument("UUID", nms).getConstructor());
|
||||
/* Map World */
|
||||
this.mapSimpleNMS(World.class, this.getNMSArgument("Dimension", nms).getConstructor());
|
||||
/* Map Enchantment */
|
||||
this.mapSimpleNMS(Enchantment.class, this.getNMSArgument("Enchantment", nms).getConstructor());
|
||||
/* Map EntityType */
|
||||
this.mapSimpleNMS(EntityType.class, this.getNMSArgument("EntitySummon", nms).getConstructor());
|
||||
/* Map Material */
|
||||
this.mapSimpleNMS(Material.class, this.getNMSArgument("ItemStack", nms).getConstructor());
|
||||
} catch (final Exception e) {
|
||||
this.paperCommandManager.getOwningPlugin()
|
||||
.getLogger()
|
||||
.warning("Failed to map Bukkit types to NMS argument types");
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private Class<?> getNMSArgument(@Nonnull final String argument, @Nonnull final String nms) throws Exception {
|
||||
return Class.forName(String.format("net.minecraft.server.%s.Argument%s", nms, argument));
|
||||
}
|
||||
|
||||
private void mapSimpleNMS(@Nonnull final Class<?> type,
|
||||
@Nonnull final Constructor<?> constructor) {
|
||||
try {
|
||||
this.brigadierManager.registerDefaultArgumentTypeSupplier(type, () -> {
|
||||
try {
|
||||
return (ArgumentType<?>) constructor.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
} catch (final Exception e) {
|
||||
this.paperCommandManager.getOwningPlugin()
|
||||
.getLogger()
|
||||
.warning(String.format("Failed to map '%s' to a Mojang serializable argument type",
|
||||
type.getCanonicalName()));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ import java.util.function.Function;
|
|||
/**
|
||||
* Paper command manager that extends {@link BukkitCommandManager}
|
||||
*/
|
||||
public class PaperCommandManager extends BukkitCommandManager {
|
||||
public class PaperCommandManager<C extends BukkitCommandSender> extends BukkitCommandManager<C> {
|
||||
|
||||
/**
|
||||
* Construct a new Paper command manager
|
||||
|
|
@ -43,8 +43,8 @@ public class PaperCommandManager extends BukkitCommandManager {
|
|||
* @throws Exception If the construction of the manager fails
|
||||
*/
|
||||
public PaperCommandManager(@Nonnull final Plugin owningPlugin,
|
||||
@Nonnull final Function<CommandTree<BukkitCommandSender, BukkitCommandMeta>,
|
||||
CommandExecutionCoordinator<BukkitCommandSender, BukkitCommandMeta>> commandExecutionCoordinator) throws
|
||||
@Nonnull final Function<CommandTree<C, BukkitCommandMeta>,
|
||||
CommandExecutionCoordinator<C, BukkitCommandMeta>> commandExecutionCoordinator) throws
|
||||
Exception {
|
||||
super(owningPlugin, commandExecutionCoordinator);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue