Add Brigadier support.

This commit is contained in:
Alexander Söderberg 2020-09-14 22:36:58 +02:00
parent e1c17a4906
commit 7148e76bcd
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
25 changed files with 822 additions and 61 deletions

View file

@ -0,0 +1,263 @@
//
// 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.brigadier;
import com.google.common.collect.Maps;
import com.google.common.reflect.TypeToken;
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.sender.CommandSender;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import javax.annotation.Nonnull;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Function;
/**
* Manager used to map cloud {@link com.intellectualsites.commands.Command}
* <p>
* The structure of this class is largely inspired by
* <a href="https://github.com/aikar/commands/blob/master/brigadier/src/main/java/co.aikar.commands/ACFBrigadierManager.java">
* ACFBrigadiermanager</a> in the ACF project, which was originally written by MiniDigger and licensed under the MIT license.
*
* @param <C> Command sender type
* @param <S> Brigadier sender type
*/
public final class CloudBrigadierManager<C extends CommandSender, S> {
private final Map<Class<?>, Function<? extends CommandComponent<C, ?>,
? extends ArgumentType<?>>> mappers;
/**
* Create a new cloud brigadier manager
*/
public CloudBrigadierManager() {
this.mappers = Maps.newHashMap();
this.registerInternalMappings();
}
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;
if (hasMin) {
return IntegerArgumentType.integer(component.getMin(), component.getMax());
} else if (hasMax) {
return IntegerArgumentType.integer(Byte.MIN_VALUE, component.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;
if (hasMin) {
return IntegerArgumentType.integer(component.getMin(), component.getMax());
} else if (hasMax) {
return IntegerArgumentType.integer(Short.MIN_VALUE, component.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;
System.out.println("Constructing new IntegerArgumentType with min " + hasMin + " | max " + hasMax);
if (hasMin) {
return IntegerArgumentType.integer(component.getMin(), component.getMax());
} else if (hasMax) {
return IntegerArgumentType.integer(Integer.MIN_VALUE, component.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;
if (hasMin) {
return FloatArgumentType.floatArg(component.getMin(), component.getMax());
} else if (hasMax) {
return FloatArgumentType.floatArg(Float.MIN_VALUE, component.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;
if (hasMin) {
return DoubleArgumentType.doubleArg(component.getMin(), component.getMax());
} else if (hasMax) {
return DoubleArgumentType.doubleArg(Double.MIN_VALUE, component.getMax());
} else {
return DoubleArgumentType.doubleArg();
}
});
/* Map boolean to BoolArgumentType */
this.registerMapping(new TypeToken<BooleanComponent<C>>() {
}, component -> BoolArgumentType.bool());
/* Map String properly to StringArgumentType */
this.registerMapping(new TypeToken<StringComponent<C>>() {
}, component -> {
switch (component.getStringMode()) {
case SINGLE:
return StringArgumentType.word();
case QUOTED:
return StringArgumentType.string();
case GREEDY:
return StringArgumentType.greedyString();
default:
return StringArgumentType.word();
}
});
}
/**
* Register a cloud-Brigadier mapping
*
* @param componentType cloud component type
* @param mapper mapper function
* @param <T> cloud component value type
* @param <K> cloud component 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,
? extends ArgumentType<O>> mapper) {
this.mappers.put(componentType.getRawType(), mapper);
}
/**
* Get a Brigadier {@link ArgumentType} from a cloud {@link CommandComponent}
*
* @param componentType cloud component type
* @param component cloud component
* @param <T> cloud component value type (generic)
* @param <K> cloud component type (generic)
* @return Brigadier argument type
*/
@Nonnull
@SuppressWarnings("all")
public <T, K extends CommandComponent<?, ?>> ArgumentType<?> getArgument(@Nonnull final TypeToken<T> componentType,
@Nonnull final K component) {
final CommandComponent<C, ?> commandComponent = (CommandComponent<C, ?>) component;
final Function function = this.mappers.getOrDefault(componentType.getRawType(), t ->
createDefaultMapper((CommandComponent<C, T>) component));
return (ArgumentType<?>) function.apply(commandComponent);
}
@Nonnull
private <T, K extends CommandComponent<C, T>> ArgumentType<?> createDefaultMapper(@Nonnull final CommandComponent<C, T>
component) {
return StringArgumentType.string();
}
/**
* Create a literal command from Brigadier command info, and a cloud command instance
*
* @param cloudCommand Cloud root command
* @param root Brigadier root command
* @param suggestionProvider Brigadier suggestions provider
* @param executor Brigadier command executor
* @param permissionChecker Permission checker
* @return Constructed literal command node
*/
@Nonnull
public LiteralCommandNode<S> createLiteralCommandNode(@Nonnull final CommandTree.Node<CommandComponent<C, ?>> cloudCommand,
@Nonnull final LiteralCommandNode<S> root,
@Nonnull final SuggestionProvider<S> suggestionProvider,
@Nonnull final com.mojang.brigadier.Command<S> executor,
@Nonnull final BiPredicate<S, String> permissionChecker) {
final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder.<S>literal(root.getLiteral())
.requires(sender -> permissionChecker.test(sender, cloudCommand.getNodeMeta().getOrDefault("permission", "")));
if (cloudCommand.isLeaf() && cloudCommand.getValue() != null && cloudCommand.getValue().getOwningCommand() != null) {
literalArgumentBuilder.executes(executor);
}
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
for (final CommandTree.Node<CommandComponent<C, ?>> child : cloudCommand.getChildren()) {
constructedRoot.addChild(this.constructCommandNode(child, permissionChecker, executor, suggestionProvider));
}
return constructedRoot;
}
private CommandNode<S> constructCommandNode(@Nonnull final CommandTree.Node<CommandComponent<C, ?>> root,
@Nonnull final BiPredicate<S, String> permissionChecker,
@Nonnull final com.mojang.brigadier.Command<S> executor,
@Nonnull final SuggestionProvider<S> suggestionProvider) {
CommandNode<S> commandNode;
if (root.getValue() instanceof StaticComponent) {
final LiteralArgumentBuilder<S> argumentBuilder = LiteralArgumentBuilder.<S>literal(root.getValue().getName())
.requires(sender -> permissionChecker.test(sender, root.getNodeMeta().getOrDefault("permission", "")));
if (root.isLeaf()) {
argumentBuilder.executes(executor);
}
commandNode = argumentBuilder.build();
} else {
@SuppressWarnings("unchecked") final RequiredArgumentBuilder<S, Object> builder = RequiredArgumentBuilder
.<S, Object>argument(root.getValue().getName(),
(ArgumentType<Object>) getArgument(TypeToken.of(root.getValue().getClass()),
root.getValue()))
.suggests(suggestionProvider)
.requires(sender -> permissionChecker.test(sender, root.getNodeMeta().getOrDefault("permission", "")));
if (root.isLeaf() || !root.getValue().isRequired()) {
builder.executes(executor);
}
commandNode = builder.build();
}
for (final CommandTree.Node<CommandComponent<C, ?>> node : root.getChildren()) {
commandNode.addChild(constructCommandNode(node, permissionChecker, executor, suggestionProvider));
}
return commandNode;
}
}

View file

@ -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.
//
/**
* Brigadier mappings
*/
package com.intellectualsites.commands.brigadier;

View file

@ -62,7 +62,7 @@
<dependencies>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-bukkit</artifactId>
<artifactId>cloud-paper</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>

View file

@ -25,6 +25,7 @@ package com.intellectualsites.commands;
import com.intellectualsites.commands.components.StaticComponent;
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 org.bukkit.Bukkit;
@ -39,32 +40,57 @@ import java.util.stream.Collectors;
public final class BukkitTest extends JavaPlugin {
private static final int PERC_MIN = 0;
private static final int PERC_MAX = 100;
@Override
public void onLoad() {
public void onEnable() {
try {
final BukkitCommandManager commandManager = new BukkitCommandManager(this,
CommandExecutionCoordinator.simpleCoordinator());
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())
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());
} catch (final Exception e) {
e.printStackTrace();

View file

@ -24,6 +24,7 @@
package com.intellectualsites.commands;
import com.intellectualsites.commands.components.CommandComponent;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
@ -56,6 +57,7 @@ final class BukkitCommand extends org.bukkit.command.Command implements PluginId
this.bukkitCommandManager.executeCommand(BukkitCommandSender.of(commandSender), builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
commandSender.sendMessage(ChatColor.RED + throwable.getCause().getMessage());
throwable.printStackTrace();
} else {
// Do something...

View file

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~
~ 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.
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud</artifactId>
<groupId>com.intellectualsites</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-paper</artifactId>
<repositories>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-bukkit</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-brigadier</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-mojangapi</artifactId>
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>clean package</defaultGoal>
<finalName>CommandStuff-${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,59 @@
//
// 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;
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 org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import javax.annotation.Nonnull;
class PaperBrigadierListener implements Listener {
private final CloudBrigadierManager<BukkitCommandSender, BukkitBrigadierCommandSource> brigadierManager;
private final PaperCommandManager paperCommandManager;
PaperBrigadierListener(@Nonnull final PaperCommandManager paperCommandManager) throws Exception {
this.paperCommandManager = paperCommandManager;
this.brigadierManager = new CloudBrigadierManager<>();
}
@EventHandler
public void onCommandRegister(@Nonnull final CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
final CommandTree<BukkitCommandSender, BukkitCommandMeta> commandTree = this.paperCommandManager.getCommandTree();
final CommandTree.Node<CommandComponent<BukkitCommandSender, ?>> node = commandTree.getNamedNode(event.getCommandLabel());
if (node == null) {
return;
}
event.setLiteral(this.brigadierManager.createLiteralCommandNode(node,
event.getLiteral(),
event.getBrigadierCommand(),
event.getBrigadierCommand(),
(s, p) -> s.getBukkitSender().hasPermission(p)));
}
}

View file

@ -0,0 +1,65 @@
//
// 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;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull;
import java.util.function.Function;
/**
* Paper command manager that extends {@link BukkitCommandManager}
*/
public class PaperCommandManager extends BukkitCommandManager {
/**
* Construct a new Paper command manager
*
* @param owningPlugin Plugin that is constructing the manager
* @param commandExecutionCoordinator Coordinator provider
* @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
Exception {
super(owningPlugin, commandExecutionCoordinator);
}
/**
* Register the brigadier listener
*/
public void registerBrigadier() {
try {
Bukkit.getPluginManager().registerEvents(new PaperBrigadierListener(this),
this.getOwningPlugin());
} catch (final Exception e) {
this.getOwningPlugin().getLogger().severe("Failed to register Brigadier listener");
e.printStackTrace();
}
}
}

View file

@ -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.
//
/**
* Paper specific implementation that extends the Bukkit implementation
*/
package com.intellectualsites.commands;