component->argument
This commit is contained in:
parent
b3d75496b5
commit
8b0a650b48
52 changed files with 1032 additions and 1024 deletions
|
|
@ -27,15 +27,15 @@ import com.google.common.collect.Maps;
|
|||
import com.google.common.reflect.TypeToken;
|
||||
import com.intellectualsites.commands.CommandManager;
|
||||
import com.intellectualsites.commands.CommandTree;
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.intellectualsites.commands.components.StaticComponent;
|
||||
import com.intellectualsites.commands.components.standard.BooleanComponent;
|
||||
import com.intellectualsites.commands.components.standard.ByteComponent;
|
||||
import com.intellectualsites.commands.components.standard.DoubleComponent;
|
||||
import com.intellectualsites.commands.components.standard.FloatComponent;
|
||||
import com.intellectualsites.commands.components.standard.IntegerComponent;
|
||||
import com.intellectualsites.commands.components.standard.ShortComponent;
|
||||
import com.intellectualsites.commands.components.standard.StringComponent;
|
||||
import com.intellectualsites.commands.arguments.CommandArgument;
|
||||
import com.intellectualsites.commands.arguments.StaticArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.BooleanArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.ByteArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.DoubleArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.FloatArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.IntegerArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.ShortArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.StringArgument;
|
||||
import com.intellectualsites.commands.execution.preprocessor.CommandPreprocessingContext;
|
||||
import com.intellectualsites.commands.sender.CommandSender;
|
||||
import com.mojang.brigadier.LiteralMessage;
|
||||
|
|
@ -77,7 +77,7 @@ import java.util.function.Supplier;
|
|||
*/
|
||||
public final class CloudBrigadierManager<C extends CommandSender, S> {
|
||||
|
||||
private final Map<Class<?>, Function<? extends CommandComponent<C, ?>,
|
||||
private final Map<Class<?>, Function<? extends CommandArgument<C, ?>,
|
||||
? extends ArgumentType<?>>> mappers;
|
||||
private final Map<Class<?>, Supplier<ArgumentType<?>>> defaultArgumentTypeSuppliers;
|
||||
private final Supplier<com.intellectualsites.commands.context.CommandContext<C>> dummyContextProvider;
|
||||
|
|
@ -101,75 +101,75 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
|
||||
private void registerInternalMappings() {
|
||||
/* Map byte, short and int to IntegerArgumentType */
|
||||
this.registerMapping(new TypeToken<ByteComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Byte.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Byte.MAX_VALUE;
|
||||
this.registerMapping(new TypeToken<ByteArgument<C>>() {
|
||||
}, argument -> {
|
||||
final boolean hasMin = argument.getMin() != Byte.MIN_VALUE;
|
||||
final boolean hasMax = argument.getMax() != Byte.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return IntegerArgumentType.integer(component.getMin(), component.getMax());
|
||||
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
||||
} else if (hasMax) {
|
||||
return IntegerArgumentType.integer(Byte.MIN_VALUE, component.getMax());
|
||||
return IntegerArgumentType.integer(Byte.MIN_VALUE, argument.getMax());
|
||||
} else {
|
||||
return IntegerArgumentType.integer();
|
||||
}
|
||||
});
|
||||
this.registerMapping(new TypeToken<ShortComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Short.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Short.MAX_VALUE;
|
||||
this.registerMapping(new TypeToken<ShortArgument<C>>() {
|
||||
}, argument -> {
|
||||
final boolean hasMin = argument.getMin() != Short.MIN_VALUE;
|
||||
final boolean hasMax = argument.getMax() != Short.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return IntegerArgumentType.integer(component.getMin(), component.getMax());
|
||||
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
||||
} else if (hasMax) {
|
||||
return IntegerArgumentType.integer(Short.MIN_VALUE, component.getMax());
|
||||
return IntegerArgumentType.integer(Short.MIN_VALUE, argument.getMax());
|
||||
} else {
|
||||
return IntegerArgumentType.integer();
|
||||
}
|
||||
});
|
||||
this.registerMapping(new TypeToken<IntegerComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Integer.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Integer.MAX_VALUE;
|
||||
this.registerMapping(new TypeToken<IntegerArgument<C>>() {
|
||||
}, argument -> {
|
||||
final boolean hasMin = argument.getMin() != Integer.MIN_VALUE;
|
||||
final boolean hasMax = argument.getMax() != Integer.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return IntegerArgumentType.integer(component.getMin(), component.getMax());
|
||||
return IntegerArgumentType.integer(argument.getMin(), argument.getMax());
|
||||
} else if (hasMax) {
|
||||
return IntegerArgumentType.integer(Integer.MIN_VALUE, component.getMax());
|
||||
return IntegerArgumentType.integer(Integer.MIN_VALUE, argument.getMax());
|
||||
} else {
|
||||
return IntegerArgumentType.integer();
|
||||
}
|
||||
});
|
||||
/* Map float to FloatArgumentType */
|
||||
this.registerMapping(new TypeToken<FloatComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Float.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Float.MAX_VALUE;
|
||||
this.registerMapping(new TypeToken<FloatArgument<C>>() {
|
||||
}, argument -> {
|
||||
final boolean hasMin = argument.getMin() != Float.MIN_VALUE;
|
||||
final boolean hasMax = argument.getMax() != Float.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return FloatArgumentType.floatArg(component.getMin(), component.getMax());
|
||||
return FloatArgumentType.floatArg(argument.getMin(), argument.getMax());
|
||||
} else if (hasMax) {
|
||||
return FloatArgumentType.floatArg(Float.MIN_VALUE, component.getMax());
|
||||
return FloatArgumentType.floatArg(Float.MIN_VALUE, argument.getMax());
|
||||
} else {
|
||||
return FloatArgumentType.floatArg();
|
||||
}
|
||||
});
|
||||
/* Map double to DoubleArgumentType */
|
||||
this.registerMapping(new TypeToken<DoubleComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Double.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Double.MAX_VALUE;
|
||||
this.registerMapping(new TypeToken<DoubleArgument<C>>() {
|
||||
}, argument -> {
|
||||
final boolean hasMin = argument.getMin() != Double.MIN_VALUE;
|
||||
final boolean hasMax = argument.getMax() != Double.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return DoubleArgumentType.doubleArg(component.getMin(), component.getMax());
|
||||
return DoubleArgumentType.doubleArg(argument.getMin(), argument.getMax());
|
||||
} else if (hasMax) {
|
||||
return DoubleArgumentType.doubleArg(Double.MIN_VALUE, component.getMax());
|
||||
return DoubleArgumentType.doubleArg(Double.MIN_VALUE, argument.getMax());
|
||||
} else {
|
||||
return DoubleArgumentType.doubleArg();
|
||||
}
|
||||
});
|
||||
/* Map boolean to BoolArgumentType */
|
||||
this.registerMapping(new TypeToken<BooleanComponent<C>>() {
|
||||
}, component -> BoolArgumentType.bool());
|
||||
this.registerMapping(new TypeToken<BooleanArgument<C>>() {
|
||||
}, argument -> BoolArgumentType.bool());
|
||||
/* Map String properly to StringArgumentType */
|
||||
this.registerMapping(new TypeToken<StringComponent<C>>() {
|
||||
}, component -> {
|
||||
switch (component.getStringMode()) {
|
||||
this.registerMapping(new TypeToken<StringArgument<C>>() {
|
||||
}, argument -> {
|
||||
switch (argument.getStringMode()) {
|
||||
case QUOTED:
|
||||
return StringArgumentType.string();
|
||||
case GREEDY:
|
||||
|
|
@ -183,16 +183,16 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
/**
|
||||
* Register a cloud-Brigadier mapping
|
||||
*
|
||||
* @param componentType cloud component type
|
||||
* @param argumentType cloud argument type
|
||||
* @param mapper mapper function
|
||||
* @param <T> cloud component value type
|
||||
* @param <K> cloud component type
|
||||
* @param <T> cloud argument value type
|
||||
* @param <K> cloud argument type
|
||||
* @param <O> Brigadier argument type value
|
||||
*/
|
||||
public <T, K extends CommandComponent<C, T>, O> void registerMapping(@Nonnull final TypeToken<K> componentType,
|
||||
@Nonnull final Function<? extends K,
|
||||
public <T, K extends CommandArgument<C, T>, O> void registerMapping(@Nonnull final TypeToken<K> argumentType,
|
||||
@Nonnull final Function<? extends K,
|
||||
? extends ArgumentType<O>> mapper) {
|
||||
this.mappers.put(componentType.getRawType(), mapper);
|
||||
this.mappers.put(argumentType.getRawType(), mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -207,36 +207,36 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a Brigadier {@link ArgumentType} from a cloud {@link CommandComponent}
|
||||
* Get a Brigadier {@link ArgumentType} from a cloud {@link CommandArgument}
|
||||
*
|
||||
* @param componentType cloud component type
|
||||
* @param component cloud component
|
||||
* @param <T> cloud component value type (generic)
|
||||
* @param <K> cloud component type (generic)
|
||||
* @param argumentType cloud argument type
|
||||
* @param argument cloud argument
|
||||
* @param <T> cloud argument value type (generic)
|
||||
* @param <K> cloud argument type (generic)
|
||||
* @return Brigadier argument type
|
||||
*/
|
||||
@Nullable
|
||||
@SuppressWarnings("all")
|
||||
private <T, K extends CommandComponent<?, ?>> Pair<ArgumentType<?>, Boolean> getArgument(
|
||||
@Nonnull final TypeToken<T> componentType,
|
||||
@Nonnull final K component) {
|
||||
final CommandComponent<C, ?> commandComponent = (CommandComponent<C, ?>) component;
|
||||
Function function = this.mappers.get(componentType.getRawType());
|
||||
private <T, K extends CommandArgument<?, ?>> Pair<ArgumentType<?>, Boolean> getArgument(
|
||||
@Nonnull final TypeToken<T> argumentType,
|
||||
@Nonnull final K argument) {
|
||||
final CommandArgument<C, ?> commandArgument = (CommandArgument<C, ?>) argument;
|
||||
Function function = this.mappers.get(argumentType.getRawType());
|
||||
if (function == null) {
|
||||
return this.createDefaultMapper(commandComponent);
|
||||
return this.createDefaultMapper(commandArgument);
|
||||
}
|
||||
return new Pair<>((ArgumentType<?>) function.apply(commandComponent), !component.getValueType().equals(String.class));
|
||||
return new Pair<>((ArgumentType<?>) function.apply(commandArgument), !argument.getValueType().equals(String.class));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private <T, K extends CommandComponent<C, T>> Pair<ArgumentType<?>, Boolean> createDefaultMapper(
|
||||
@Nonnull final CommandComponent<C, T>
|
||||
component) {
|
||||
final Supplier<ArgumentType<?>> argumentTypeSupplier = this.defaultArgumentTypeSuppliers.get(component.getValueType());
|
||||
private <T, K extends CommandArgument<C, T>> Pair<ArgumentType<?>, Boolean> createDefaultMapper(
|
||||
@Nonnull final CommandArgument<C, T>
|
||||
argument) {
|
||||
final Supplier<ArgumentType<?>> argumentTypeSupplier = this.defaultArgumentTypeSuppliers.get(argument.getValueType());
|
||||
if (argumentTypeSupplier != null) {
|
||||
return new Pair<>(argumentTypeSupplier.get(), true);
|
||||
}
|
||||
System.err.printf("Found not native mapping for '%s'\n", component.getValueType().getCanonicalName());
|
||||
System.err.printf("Found not native mapping for '%s'\n", argument.getValueType().getCanonicalName());
|
||||
return new Pair<>(StringArgumentType.string(), false);
|
||||
}
|
||||
|
||||
|
|
@ -251,7 +251,7 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
* @return Constructed literal command node
|
||||
*/
|
||||
@Nonnull
|
||||
public LiteralCommandNode<S> createLiteralCommandNode(@Nonnull final CommandTree.Node<CommandComponent<C, ?>> cloudCommand,
|
||||
public LiteralCommandNode<S> createLiteralCommandNode(@Nonnull final CommandTree.Node<CommandArgument<C, ?>> cloudCommand,
|
||||
@Nonnull final LiteralCommandNode<S> root,
|
||||
@Nonnull final SuggestionProvider<S> suggestionProvider,
|
||||
@Nonnull final com.mojang.brigadier.Command<S> executor,
|
||||
|
|
@ -262,19 +262,19 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
literalArgumentBuilder.executes(executor);
|
||||
}
|
||||
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
|
||||
for (final CommandTree.Node<CommandComponent<C, ?>> child : cloudCommand.getChildren()) {
|
||||
for (final CommandTree.Node<CommandArgument<C, ?>> child : cloudCommand.getChildren()) {
|
||||
constructedRoot.addChild(this.constructCommandNode(child, permissionChecker, executor, suggestionProvider).build());
|
||||
}
|
||||
return constructedRoot;
|
||||
}
|
||||
|
||||
private ArgumentBuilder<S, ?> constructCommandNode(@Nonnull final CommandTree.Node<CommandComponent<C, ?>> root,
|
||||
private ArgumentBuilder<S, ?> constructCommandNode(@Nonnull final CommandTree.Node<CommandArgument<C, ?>> root,
|
||||
@Nonnull final BiPredicate<S, String> permissionChecker,
|
||||
@Nonnull final com.mojang.brigadier.Command<S> executor,
|
||||
@Nonnull final SuggestionProvider<S> suggestionProvider) {
|
||||
|
||||
ArgumentBuilder<S, ?> argumentBuilder;
|
||||
if (root.getValue() instanceof StaticComponent) {
|
||||
if (root.getValue() instanceof StaticArgument) {
|
||||
argumentBuilder = LiteralArgumentBuilder.<S>literal(root.getValue().getName())
|
||||
.requires(sender -> permissionChecker.test(sender, root.getNodeMeta().getOrDefault("permission", "")))
|
||||
.executes(executor);
|
||||
|
|
@ -290,14 +290,14 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
if (root.isLeaf() || !root.getValue().isRequired()) {
|
||||
argumentBuilder.executes(executor);
|
||||
}
|
||||
for (final CommandTree.Node<CommandComponent<C, ?>> node : root.getChildren()) {
|
||||
for (final CommandTree.Node<CommandArgument<C, ?>> node : root.getChildren()) {
|
||||
argumentBuilder.then(constructCommandNode(node, permissionChecker, executor, suggestionProvider));
|
||||
}
|
||||
return argumentBuilder;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private CompletableFuture<Suggestions> buildSuggestions(@Nonnull final CommandComponent<C, ?> component,
|
||||
private CompletableFuture<Suggestions> buildSuggestions(@Nonnull final CommandArgument<C, ?> argument,
|
||||
@Nonnull final CommandContext<S> s,
|
||||
@Nonnull final SuggestionsBuilder builder) {
|
||||
final com.intellectualsites.commands.context.CommandContext<C> commandContext = this.dummyContextProvider.get();
|
||||
|
|
@ -314,7 +314,7 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
command = command.substring(1);
|
||||
}
|
||||
final List<String> suggestions = this.commandManager.suggest(commandContext.getSender(), command);
|
||||
/*component.getParser().suggestions(commandContext, builder.getInput());*/
|
||||
/*argument.getParser().suggestions(commandContext, builder.getInput());*/
|
||||
for (final String suggestion : suggestions) {
|
||||
System.out.printf("- %s\n", suggestion);
|
||||
}
|
||||
|
|
@ -327,9 +327,9 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
|
|||
System.out.printf("- %s\n", suggestion);
|
||||
}*/
|
||||
for (final String suggestion : suggestions) {
|
||||
String tooltip = component.getName();
|
||||
if (!(component instanceof StaticComponent)) {
|
||||
if (component.isRequired()) {
|
||||
String tooltip = argument.getName();
|
||||
if (!(argument instanceof StaticArgument)) {
|
||||
if (argument.isRequired()) {
|
||||
tooltip = '<' + tooltip + '>';
|
||||
} else {
|
||||
tooltip = '[' + tooltip + ']';
|
||||
|
|
|
|||
|
|
@ -23,16 +23,16 @@
|
|||
//
|
||||
package com.intellectualsites.commands;
|
||||
|
||||
import com.intellectualsites.commands.components.StaticComponent;
|
||||
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||
import com.intellectualsites.commands.components.standard.BooleanComponent;
|
||||
import com.intellectualsites.commands.components.standard.DoubleComponent;
|
||||
import com.intellectualsites.commands.components.standard.EnumComponent;
|
||||
import com.intellectualsites.commands.components.standard.FloatComponent;
|
||||
import com.intellectualsites.commands.components.standard.IntegerComponent;
|
||||
import com.intellectualsites.commands.components.standard.StringComponent;
|
||||
import com.intellectualsites.commands.arguments.StaticArgument;
|
||||
import com.intellectualsites.commands.arguments.parser.ArgumentParseResult;
|
||||
import com.intellectualsites.commands.arguments.standard.BooleanArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.DoubleArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.EnumArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.FloatArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.IntegerArgument;
|
||||
import com.intellectualsites.commands.arguments.standard.StringArgument;
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import com.intellectualsites.commands.parsers.WorldComponent;
|
||||
import com.intellectualsites.commands.parsers.WorldArgument;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
|
|
@ -65,8 +65,8 @@ public final class BukkitTest extends JavaPlugin {
|
|||
BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Your ugli")
|
||||
.build())
|
||||
.component(EnumComponent.required(GameMode.class, "gamemode"))
|
||||
.component(StringComponent.<BukkitCommandSender>newBuilder("player")
|
||||
.argument(EnumArgument.required(GameMode.class, "gamemode"))
|
||||
.argument(StringArgument.<BukkitCommandSender>newBuilder("player")
|
||||
.withSuggestionsProvider((v1, v2) -> {
|
||||
final List<String> suggestions =
|
||||
new ArrayList<>(
|
||||
|
|
@ -84,8 +84,8 @@ public final class BukkitTest extends JavaPlugin {
|
|||
.orElse(GameMode.SURVIVAL)))
|
||||
.build())
|
||||
.command(mgr.commandBuilder("kenny")
|
||||
.component(StaticComponent.required("sux"))
|
||||
.component(IntegerComponent
|
||||
.argument(StaticArgument.required("sux"))
|
||||
.argument(IntegerArgument
|
||||
.<BukkitCommandSender>newBuilder("perc")
|
||||
.withMin(PERC_MIN).withMax(PERC_MAX).build())
|
||||
.handler(context -> {
|
||||
|
|
@ -96,32 +96,32 @@ public final class BukkitTest extends JavaPlugin {
|
|||
})
|
||||
.build())
|
||||
.command(mgr.commandBuilder("test")
|
||||
.component(StaticComponent.required("one"))
|
||||
.argument(StaticArgument.required("one"))
|
||||
.handler(c -> c.getSender().sendMessage("One!"))
|
||||
.build())
|
||||
.command(mgr.commandBuilder("test")
|
||||
.component(StaticComponent.required("two"))
|
||||
.argument(StaticArgument.required("two"))
|
||||
.handler(c -> c.getSender().sendMessage("Two!"))
|
||||
.build())
|
||||
.command(mgr.commandBuilder("uuidtest")
|
||||
.component(UUID.class, "uuid", builder -> builder
|
||||
.argument(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);
|
||||
return ArgumentParseResult.success(uuid);
|
||||
} catch (final Exception e) {
|
||||
return ComponentParseResult.failure(e);
|
||||
return ArgumentParseResult.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"))
|
||||
.argument(EnumArgument.required(Material.class, "material"))
|
||||
.argument(IntegerArgument.required("amount"))
|
||||
.handler(c -> {
|
||||
final Material material = c.getRequired("material");
|
||||
final int amount = c.getRequired("amount");
|
||||
|
|
@ -133,7 +133,7 @@ public final class BukkitTest extends JavaPlugin {
|
|||
.command(mgr.commandBuilder("worldtp", BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Teleport to a world")
|
||||
.build())
|
||||
.component(WorldComponent.required("world"))
|
||||
.argument(WorldArgument.required("world"))
|
||||
.handler(c -> {
|
||||
final World world = c.getRequired("world");
|
||||
c.getSender().asPlayer().teleport(world.getSpawnLocation());
|
||||
|
|
@ -141,11 +141,11 @@ public final class BukkitTest extends JavaPlugin {
|
|||
})
|
||||
.build())
|
||||
.command(mgr.commandBuilder("brigadier")
|
||||
.component(FloatComponent.required("float"))
|
||||
.component(DoubleComponent.required("double"))
|
||||
.component(IntegerComponent.required("int"))
|
||||
.component(BooleanComponent.required("bool"))
|
||||
.component(StringComponent.required("string"))
|
||||
.argument(FloatArgument.required("float"))
|
||||
.argument(DoubleArgument.required("double"))
|
||||
.argument(IntegerArgument.required("int"))
|
||||
.argument(BooleanArgument.required("bool"))
|
||||
.argument(StringArgument.required("string"))
|
||||
.handler(c -> c.getSender().sendMessage("Executed the command"))
|
||||
.build());
|
||||
} catch (final Exception e) {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
//
|
||||
package com.intellectualsites.commands;
|
||||
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.intellectualsites.commands.arguments.CommandArgument;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginIdentifiableCommand;
|
||||
|
|
@ -35,12 +35,12 @@ import java.util.List;
|
|||
final class BukkitCommand<C extends com.intellectualsites.commands.sender.CommandSender>
|
||||
extends org.bukkit.command.Command implements PluginIdentifiableCommand {
|
||||
|
||||
private final CommandComponent<C, ?> command;
|
||||
private final CommandArgument<C, ?> command;
|
||||
private final BukkitCommandManager<C> bukkitCommandManager;
|
||||
private final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand;
|
||||
|
||||
BukkitCommand(@Nonnull final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand,
|
||||
@Nonnull final CommandComponent<C, ?> command,
|
||||
@Nonnull final CommandArgument<C, ?> command,
|
||||
@Nonnull final BukkitCommandManager<C> bukkitCommandManager) {
|
||||
super(command.getName());
|
||||
this.command = command;
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ package com.intellectualsites.commands;
|
|||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import com.intellectualsites.commands.parsers.WorldComponent;
|
||||
import com.intellectualsites.commands.parsers.WorldArgument;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
|
@ -64,7 +64,7 @@ public class BukkitCommandManager<C extends com.intellectualsites.commands.sende
|
|||
this.commandSenderMapper = commandSenderMapper;
|
||||
|
||||
/* Register Bukkit parsers */
|
||||
this.getParserRegistry().registerParserSupplier(TypeToken.of(World.class), params -> new WorldComponent.WorldParser<>());
|
||||
this.getParserRegistry().registerParserSupplier(TypeToken.of(World.class), params -> new WorldArgument.WorldParser<>());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
//
|
||||
package com.intellectualsites.commands;
|
||||
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.intellectualsites.commands.arguments.CommandArgument;
|
||||
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
||||
import com.intellectualsites.commands.sender.CommandSender;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
@ -39,7 +39,7 @@ import java.util.Map;
|
|||
|
||||
final class BukkitPluginRegistrationHandler<C extends CommandSender> implements CommandRegistrationHandler<BukkitCommandMeta> {
|
||||
|
||||
private final Map<CommandComponent<?, ?>, org.bukkit.command.Command> registeredCommands = new HashMap<>();
|
||||
private final Map<CommandArgument<?, ?>, org.bukkit.command.Command> registeredCommands = new HashMap<>();
|
||||
|
||||
private Map<String, org.bukkit.command.Command> bukkitCommands;
|
||||
private BukkitCommandManager<C> bukkitCommandManager;
|
||||
|
|
@ -63,23 +63,23 @@ final class BukkitPluginRegistrationHandler<C extends CommandSender> implements
|
|||
|
||||
@Override
|
||||
public boolean registerCommand(@Nonnull final Command<?, BukkitCommandMeta> command) {
|
||||
/* We only care about the root command component */
|
||||
final CommandComponent<?, ?> commandComponent = command.getComponents().get(0);
|
||||
if (this.registeredCommands.containsKey(commandComponent)) {
|
||||
/* We only care about the root command argument */
|
||||
final CommandArgument<?, ?> commandArgument = command.getArguments().get(0);
|
||||
if (this.registeredCommands.containsKey(commandArgument)) {
|
||||
return false;
|
||||
}
|
||||
final String label;
|
||||
if (bukkitCommands.containsKey(commandComponent.getName())) {
|
||||
label = String.format("%s:%s", this.bukkitCommandManager.getOwningPlugin().getName(), commandComponent.getName());
|
||||
if (bukkitCommands.containsKey(commandArgument.getName())) {
|
||||
label = String.format("%s:%s", this.bukkitCommandManager.getOwningPlugin().getName(), commandArgument.getName());
|
||||
} else {
|
||||
label = commandComponent.getName();
|
||||
label = commandArgument.getName();
|
||||
}
|
||||
@SuppressWarnings("unchecked") final BukkitCommand<C> bukkitCommand = new BukkitCommand<>(
|
||||
(Command<C, BukkitCommandMeta>) command,
|
||||
(CommandComponent<C, ?>) commandComponent,
|
||||
(CommandArgument<C, ?>) commandArgument,
|
||||
this.bukkitCommandManager);
|
||||
this.registeredCommands.put(commandComponent, bukkitCommand);
|
||||
this.commandMap.register(commandComponent.getName(), this.bukkitCommandManager.getOwningPlugin().getName().toLowerCase(),
|
||||
this.registeredCommands.put(commandArgument, bukkitCommand);
|
||||
this.commandMap.register(commandArgument.getName(), this.bukkitCommandManager.getOwningPlugin().getName().toLowerCase(),
|
||||
bukkitCommand);
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@
|
|||
//
|
||||
package com.intellectualsites.commands.parsers;
|
||||
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||
import com.intellectualsites.commands.components.parser.ComponentParser;
|
||||
import com.intellectualsites.commands.arguments.CommandArgument;
|
||||
import com.intellectualsites.commands.arguments.parser.ArgumentParseResult;
|
||||
import com.intellectualsites.commands.arguments.parser.ArgumentParser;
|
||||
import com.intellectualsites.commands.context.CommandContext;
|
||||
import com.intellectualsites.commands.sender.CommandSender;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
@ -37,70 +37,70 @@ import java.util.Queue;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* cloud component type that parses Bukkit {@link org.bukkit.World worlds}
|
||||
* cloud argument type that parses Bukkit {@link org.bukkit.World worlds}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public class WorldComponent<C extends CommandSender> extends CommandComponent<C, World> {
|
||||
public class WorldArgument<C extends CommandSender> extends CommandArgument<C, World> {
|
||||
|
||||
protected WorldComponent(final boolean required,
|
||||
@Nonnull final String name,
|
||||
@Nonnull final String defaultValue) {
|
||||
protected WorldArgument(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 name Name of the argument
|
||||
* @param <C> Command sender type
|
||||
* @return Created builder
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> CommandComponent.Builder<C, World> newBuilder(@Nonnull final String name) {
|
||||
return new WorldComponent.Builder<>(name);
|
||||
public static <C extends CommandSender> CommandArgument.Builder<C, World> newBuilder(@Nonnull final String name) {
|
||||
return new WorldArgument.Builder<>(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new required component
|
||||
* Create a new required argument
|
||||
*
|
||||
* @param name Component name
|
||||
* @param name Argument name
|
||||
* @param <C> Command sender type
|
||||
* @return Created component
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> CommandComponent<C, World> required(@Nonnull final String name) {
|
||||
return WorldComponent.<C>newBuilder(name).asRequired().build();
|
||||
public static <C extends CommandSender> CommandArgument<C, World> required(@Nonnull final String name) {
|
||||
return WorldArgument.<C>newBuilder(name).asRequired().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional component
|
||||
* Create a new optional argument
|
||||
*
|
||||
* @param name Component name
|
||||
* @param name Argument name
|
||||
* @param <C> Command sender type
|
||||
* @return Created component
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> CommandComponent<C, World> optional(@Nonnull final String name) {
|
||||
return WorldComponent.<C>newBuilder(name).asOptional().build();
|
||||
public static <C extends CommandSender> CommandArgument<C, World> optional(@Nonnull final String name) {
|
||||
return WorldArgument.<C>newBuilder(name).asOptional().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional component with a default value
|
||||
* Create a new optional argument with a default value
|
||||
*
|
||||
* @param name Component name
|
||||
* @param name Argument name
|
||||
* @param defaultValue Default value
|
||||
* @param <C> Command sender type
|
||||
* @return Created component
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> CommandComponent<C, World> optional(@Nonnull final String name,
|
||||
@Nonnull final String defaultValue) {
|
||||
return WorldComponent.<C>newBuilder(name).asOptionalWithDefault(defaultValue).build();
|
||||
public static <C extends CommandSender> CommandArgument<C, World> optional(@Nonnull final String name,
|
||||
@Nonnull final String defaultValue) {
|
||||
return WorldArgument.<C>newBuilder(name).asOptionalWithDefault(defaultValue).build();
|
||||
}
|
||||
|
||||
|
||||
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, World> {
|
||||
public static final class Builder<C extends CommandSender> extends CommandArgument.Builder<C, World> {
|
||||
|
||||
protected Builder(@Nonnull final String name) {
|
||||
super(World.class, name);
|
||||
|
|
@ -108,30 +108,30 @@ public class WorldComponent<C extends CommandSender> extends CommandComponent<C,
|
|||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CommandComponent<C, World> build() {
|
||||
return new WorldComponent<>(this.isRequired(), this.getName(), this.getDefaultValue());
|
||||
public CommandArgument<C, World> build() {
|
||||
return new WorldArgument<>(this.isRequired(), this.getName(), this.getDefaultValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final class WorldParser<C extends CommandSender> implements ComponentParser<C, World> {
|
||||
public static final class WorldParser<C extends CommandSender> implements ArgumentParser<C, World> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ComponentParseResult<World> parse(@Nonnull final CommandContext<C> commandContext,
|
||||
@Nonnull final Queue<String> inputQueue) {
|
||||
public ArgumentParseResult<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"));
|
||||
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
|
||||
}
|
||||
|
||||
final World world = Bukkit.getWorld(input);
|
||||
if (world == null) {
|
||||
return ComponentParseResult.failure(new WorldParseException(input));
|
||||
return ArgumentParseResult.failure(new WorldParseException(input));
|
||||
}
|
||||
|
||||
inputQueue.remove();
|
||||
return ComponentParseResult.success(world);
|
||||
return ArgumentParseResult.success(world);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
|
@ -23,6 +23,6 @@
|
|||
//
|
||||
|
||||
/**
|
||||
* Bukkit specific command components
|
||||
* Bukkit specific command arguments
|
||||
*/
|
||||
package com.intellectualsites.commands.parsers;
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ package com.intellectualsites.commands;
|
|||
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.intellectualsites.commands.arguments.CommandArgument;
|
||||
import com.intellectualsites.commands.context.CommandContext;
|
||||
import com.intellectualsites.commands.sender.CommandSender;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
|
|
@ -112,7 +112,7 @@ class PaperBrigadierListener<C extends CommandSender> implements Listener {
|
|||
@EventHandler
|
||||
public void onCommandRegister(@Nonnull final CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
|
||||
final CommandTree<C, BukkitCommandMeta> commandTree = this.paperCommandManager.getCommandTree();
|
||||
final CommandTree.Node<CommandComponent<C, ?>> node = commandTree.getNamedNode(event.getCommandLabel());
|
||||
final CommandTree.Node<CommandArgument<C, ?>> node = commandTree.getNamedNode(event.getCommandLabel());
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue