🚚 Switch namespace
This commit is contained in:
parent
0064093dbf
commit
c74cda3a0f
207 changed files with 2689 additions and 611 deletions
|
|
@ -0,0 +1,62 @@
|
|||
//
|
||||
// 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 cloud.commandframework.paper;
|
||||
|
||||
import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class AsyncCommandSuggestionsListener<C> implements Listener {
|
||||
|
||||
private static final long CACHE_EXPIRATION_TIME = 30L;
|
||||
|
||||
private final PaperCommandManager<C> paperCommandManager;
|
||||
|
||||
AsyncCommandSuggestionsListener(@Nonnull final PaperCommandManager<C> paperCommandManager) {
|
||||
this.paperCommandManager = paperCommandManager;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
void onTabCompletion(@Nonnull final AsyncTabCompleteEvent event) throws Exception {
|
||||
if (event.getBuffer().isEmpty() || !event.getBuffer().startsWith("/")) {
|
||||
return;
|
||||
}
|
||||
final String[] arguments = event.getBuffer().substring(1).split(" ");
|
||||
if (paperCommandManager.getCommandTree().getNamedNode(arguments[0]) == null) {
|
||||
return;
|
||||
}
|
||||
final CommandSender sender = event.getSender();
|
||||
final C cloudSender = this.paperCommandManager.getCommandSenderMapper().apply(sender);
|
||||
final List<String> suggestions = new ArrayList<>(this.paperCommandManager.suggest(cloudSender,
|
||||
event.getBuffer().substring(1)));
|
||||
event.setCompletions(suggestions);
|
||||
event.setHandled(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
//
|
||||
// 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 cloud.commandframework.paper;
|
||||
|
||||
import cloud.commandframework.brigadier.CloudBrigadierManager;
|
||||
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
|
||||
import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
|
||||
import cloud.commandframework.CommandTree;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import cloud.commandframework.permission.CommandPermission;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
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;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.logging.Level;
|
||||
|
||||
class PaperBrigadierListener<C> implements Listener {
|
||||
|
||||
private static final int UUID_ARGUMENT_VERSION = 16;
|
||||
|
||||
private final CloudBrigadierManager<C, BukkitBrigadierCommandSource> brigadierManager;
|
||||
private final PaperCommandManager<C> paperCommandManager;
|
||||
private final String nmsVersion;
|
||||
|
||||
PaperBrigadierListener(@Nonnull final PaperCommandManager<C> paperCommandManager) throws Exception {
|
||||
this.paperCommandManager = paperCommandManager;
|
||||
this.brigadierManager = new CloudBrigadierManager<>(this.paperCommandManager,
|
||||
() -> new CommandContext<>(
|
||||
this.paperCommandManager.getCommandSenderMapper()
|
||||
.apply(Bukkit.getConsoleSender())));
|
||||
/* Register default mappings */
|
||||
final String version = Bukkit.getServer().getClass().getPackage().getName();
|
||||
this.nmsVersion = version.substring(version.lastIndexOf(".") + 1);
|
||||
final int majorMinecraftVersion = Integer.parseInt(this.nmsVersion.split("_")[1]);
|
||||
try {
|
||||
/* UUID nms argument is a 1.16+ feature */
|
||||
if (majorMinecraftVersion >= UUID_ARGUMENT_VERSION) {
|
||||
/* Map UUID */
|
||||
this.mapSimpleNMS(UUID.class, this.getNMSArgument("UUID").getConstructor());
|
||||
}
|
||||
/* Map Enchantment */
|
||||
this.mapSimpleNMS(Enchantment.class, this.getNMSArgument("Enchantment").getConstructor());
|
||||
/* Map EntityType */
|
||||
this.mapSimpleNMS(EntityType.class, this.getNMSArgument("EntitySummon").getConstructor());
|
||||
/* Map Material */
|
||||
this.mapSimpleNMS(Material.class, this.getNMSArgument("ItemStack").getConstructor());
|
||||
} catch (final Exception e) {
|
||||
this.paperCommandManager.getOwningPlugin()
|
||||
.getLogger()
|
||||
.log(Level.WARNING, "Failed to map Bukkit types to NMS argument types", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to retrieve an NMS argument type
|
||||
*
|
||||
* @param argument Argument type name
|
||||
* @return Argument class
|
||||
* @throws Exception If the type cannot be retrieved
|
||||
*/
|
||||
@Nonnull
|
||||
private Class<?> getNMSArgument(@Nonnull final String argument) throws Exception {
|
||||
return Class.forName(String.format("net.minecraft.server.%s.Argument%s", this.nmsVersion, argument));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to register a mapping between a type and a NMS argument type
|
||||
*
|
||||
* @param type Type to map
|
||||
* @param constructor Constructor that construct the NMS argument type
|
||||
*/
|
||||
public 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
|
||||
public void onCommandRegister(@Nonnull final CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
|
||||
final CommandTree<C> commandTree = this.paperCommandManager.getCommandTree();
|
||||
final CommandTree.Node<CommandArgument<C, ?>> node = commandTree.getNamedNode(event.getCommandLabel());
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
final BiPredicate<BukkitBrigadierCommandSource, CommandPermission> permissionChecker = (s, p) -> {
|
||||
final C sender = paperCommandManager.getCommandSenderMapper().apply(s.getBukkitSender());
|
||||
return paperCommandManager.hasPermission(sender, p);
|
||||
};
|
||||
event.setLiteral(this.brigadierManager.createLiteralCommandNode(node,
|
||||
event.getLiteral(),
|
||||
event.getBrigadierCommand(),
|
||||
event.getBrigadierCommand(),
|
||||
permissionChecker));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
//
|
||||
// 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 cloud.commandframework.paper;
|
||||
|
||||
import cloud.commandframework.CommandTree;
|
||||
import cloud.commandframework.bukkit.BukkitCommandManager;
|
||||
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Paper command manager that extends {@link BukkitCommandManager}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public class PaperCommandManager<C> extends BukkitCommandManager<C> {
|
||||
|
||||
/**
|
||||
* Construct a new Paper command manager
|
||||
*
|
||||
* @param owningPlugin Plugin that is constructing the manager
|
||||
* @param commandExecutionCoordinator Coordinator provider
|
||||
* @param commandSenderMapper Function that maps {@link CommandSender} to the command sender type
|
||||
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSender}
|
||||
* @throws Exception If the construction of the manager fails
|
||||
*/
|
||||
public PaperCommandManager(@Nonnull final Plugin owningPlugin,
|
||||
@Nonnull final Function<CommandTree<C>,
|
||||
CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
||||
@Nonnull final Function<CommandSender, C> commandSenderMapper,
|
||||
@Nonnull final Function<C, CommandSender> backwardsCommandSenderMapper) throws
|
||||
Exception {
|
||||
super(owningPlugin, commandExecutionCoordinator, commandSenderMapper, backwardsCommandSenderMapper);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Brigadier mappings using the native paper events
|
||||
*
|
||||
* @throws BrigadierFailureException Exception thrown if the mappings cannot be registered
|
||||
*/
|
||||
@Override
|
||||
public void registerBrigadier() throws BrigadierFailureException {
|
||||
this.checkBrigadierCompatibility();
|
||||
if (!this.queryCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
|
||||
super.registerBrigadier();
|
||||
} else {
|
||||
try {
|
||||
final PaperBrigadierListener<C> brigadierListener = new PaperBrigadierListener<>(this);
|
||||
Bukkit.getPluginManager().registerEvents(brigadierListener,
|
||||
this.getOwningPlugin());
|
||||
this.setSplitAliases(true);
|
||||
} catch (final Throwable e) {
|
||||
throw new BrigadierFailureException(BrigadierFailureReason.PAPER_BRIGADIER_INITIALIZATION_FAILURE, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register asynchronous completions. This requires all argument parsers to be thread safe, and it
|
||||
* is up to the caller to guarantee that such is the case
|
||||
*/
|
||||
public void registerAsynchronousCompletions() {
|
||||
Bukkit.getServer().getPluginManager().registerEvents(new AsyncCommandSuggestionsListener<>(this), this.getOwningPlugin());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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 cloud.commandframework.paper;
|
||||
Loading…
Add table
Add a link
Reference in a new issue