🚚 Switch namespace
This commit is contained in:
parent
0064093dbf
commit
c74cda3a0f
207 changed files with 2689 additions and 611 deletions
|
|
@ -0,0 +1,163 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import cloud.commandframework.Command;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.exceptions.ArgumentParseException;
|
||||
import cloud.commandframework.exceptions.InvalidCommandSenderException;
|
||||
import cloud.commandframework.exceptions.InvalidSyntaxException;
|
||||
import cloud.commandframework.exceptions.NoPermissionException;
|
||||
import cloud.commandframework.exceptions.NoSuchCommandException;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginIdentifiableCommand;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
final class BukkitCommand<C> extends org.bukkit.command.Command implements PluginIdentifiableCommand {
|
||||
|
||||
private static final String MESSAGE_NO_PERMS = ChatColor.RED
|
||||
+ "I'm sorry, but you do not have permission to perform this command. "
|
||||
+ "Please contact the server administrators if you believe that this is in error.";
|
||||
private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command. Type \"/help\" for help.";
|
||||
|
||||
private final CommandArgument<C, ?> command;
|
||||
private final BukkitCommandManager<C> manager;
|
||||
private final Command<C> cloudCommand;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
BukkitCommand(@Nonnull final String label,
|
||||
@Nonnull final List<String> aliases,
|
||||
@Nonnull final Command<C> cloudCommand,
|
||||
@Nonnull final CommandArgument<C, ?> command,
|
||||
@Nonnull final BukkitCommandManager<C> manager) {
|
||||
super(label,
|
||||
cloudCommand.getCommandMeta().getOrDefault("description", ""),
|
||||
"",
|
||||
aliases);
|
||||
this.command = command;
|
||||
this.manager = manager;
|
||||
this.cloudCommand = cloudCommand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(final CommandSender commandSender, final String s, final String[] strings) {
|
||||
/* Join input */
|
||||
final StringBuilder builder = new StringBuilder(this.command.getName());
|
||||
for (final String string : strings) {
|
||||
builder.append(" ").append(string);
|
||||
}
|
||||
final C sender = this.manager.getCommandSenderMapper().apply(commandSender);
|
||||
this.manager.executeCommand(sender,
|
||||
builder.toString())
|
||||
.whenComplete(((commandResult, throwable) -> {
|
||||
if (throwable != null) {
|
||||
if (throwable instanceof CompletionException) {
|
||||
throwable = throwable.getCause();
|
||||
}
|
||||
final Throwable finalThrowable = throwable;
|
||||
if (throwable instanceof InvalidSyntaxException) {
|
||||
this.manager.handleException(sender,
|
||||
InvalidSyntaxException.class,
|
||||
(InvalidSyntaxException) throwable, (c, e) ->
|
||||
commandSender.sendMessage(
|
||||
ChatColor.RED + "Invalid Command Syntax. "
|
||||
+ "Correct command syntax is: "
|
||||
+ ChatColor.GRAY + "/"
|
||||
+ ((InvalidSyntaxException) finalThrowable)
|
||||
.getCorrectSyntax())
|
||||
);
|
||||
} else if (throwable instanceof InvalidCommandSenderException) {
|
||||
this.manager.handleException(sender,
|
||||
InvalidCommandSenderException.class,
|
||||
(InvalidCommandSenderException) throwable, (c, e) ->
|
||||
commandSender.sendMessage(
|
||||
ChatColor.RED + finalThrowable.getMessage())
|
||||
);
|
||||
} else if (throwable instanceof NoPermissionException) {
|
||||
this.manager.handleException(sender,
|
||||
NoPermissionException.class,
|
||||
(NoPermissionException) throwable, (c, e) ->
|
||||
commandSender.sendMessage(MESSAGE_NO_PERMS)
|
||||
);
|
||||
} else if (throwable instanceof NoSuchCommandException) {
|
||||
this.manager.handleException(sender,
|
||||
NoSuchCommandException.class,
|
||||
(NoSuchCommandException) throwable, (c, e) ->
|
||||
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND)
|
||||
);
|
||||
} else if (throwable instanceof ArgumentParseException) {
|
||||
this.manager.handleException(sender,
|
||||
ArgumentParseException.class,
|
||||
(ArgumentParseException) throwable, (c, e) ->
|
||||
commandSender.sendMessage(
|
||||
ChatColor.RED + "Invalid Command Argument: "
|
||||
+ ChatColor.GRAY + finalThrowable.getCause()
|
||||
.getMessage())
|
||||
);
|
||||
} else {
|
||||
commandSender.sendMessage(throwable.getMessage());
|
||||
throwable.printStackTrace();
|
||||
}
|
||||
}
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.cloudCommand.getCommandMeta().getOrDefault("description", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(final CommandSender sender, final String alias, final String[] args) throws
|
||||
IllegalArgumentException {
|
||||
final StringBuilder builder = new StringBuilder(this.command.getName());
|
||||
for (final String string : args) {
|
||||
builder.append(" ").append(string);
|
||||
}
|
||||
return this.manager.suggest(this.manager.getCommandSenderMapper().apply(sender),
|
||||
builder.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getPlugin() {
|
||||
return this.manager.getOwningPlugin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPermission() {
|
||||
return this.cloudCommand.getCommandPermission().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return this.manager.getCommandSyntaxFormatter().apply(this.cloudCommand.getArguments(), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,285 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.CommandTree;
|
||||
import cloud.commandframework.bukkit.parsers.MaterialArgument;
|
||||
import cloud.commandframework.bukkit.parsers.WorldArgument;
|
||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Command manager for the Bukkit platform
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public class BukkitCommandManager<C> extends CommandManager<C> {
|
||||
|
||||
private static final int VERSION_RADIX = 10;
|
||||
private static final int BRIGADIER_MINIMAL_VERSION = 13;
|
||||
private static final int PAPER_BRIGADIER_VERSION = 15;
|
||||
|
||||
private final Plugin owningPlugin;
|
||||
private final int minecraftVersion;
|
||||
private final boolean paper;
|
||||
|
||||
private final Function<CommandSender, C> commandSenderMapper;
|
||||
private final Function<C, CommandSender> backwardsCommandSenderMapper;
|
||||
|
||||
private boolean splitAliases = false;
|
||||
|
||||
/**
|
||||
* Construct a new Bukkit 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 BukkitCommandManager(@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(commandExecutionCoordinator, new BukkitPluginRegistrationHandler<>());
|
||||
((BukkitPluginRegistrationHandler<C>) this.getCommandRegistrationHandler()).initialize(this);
|
||||
this.owningPlugin = owningPlugin;
|
||||
this.commandSenderMapper = commandSenderMapper;
|
||||
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
|
||||
|
||||
/* Register Bukkit parsers */
|
||||
this.getParserRegistry().registerParserSupplier(TypeToken.of(World.class), params -> new WorldArgument.WorldParser<>());
|
||||
this.getParserRegistry().registerParserSupplier(TypeToken.of(Material.class),
|
||||
params -> new MaterialArgument.MaterialParser<>());
|
||||
|
||||
/* Try to determine the Minecraft version */
|
||||
int version = -1;
|
||||
try {
|
||||
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)")
|
||||
.matcher(Bukkit.getVersion());
|
||||
if (matcher.find()) {
|
||||
version = Integer.parseInt(matcher.toMatchResult().group(2),
|
||||
VERSION_RADIX);
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
this.owningPlugin.getLogger().severe("Failed to determine Minecraft version "
|
||||
+ "for cloud Bukkit capability detection");
|
||||
}
|
||||
this.minecraftVersion = version;
|
||||
|
||||
boolean paper = false;
|
||||
try {
|
||||
Class.forName("com.destroystokyo.paper.PaperConfig");
|
||||
paper = true;
|
||||
} catch (final Exception ignored) {
|
||||
}
|
||||
this.paper = paper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the plugin that owns the manager
|
||||
*
|
||||
* @return Owning plugin
|
||||
*/
|
||||
@Nonnull
|
||||
public Plugin getOwningPlugin() {
|
||||
return this.owningPlugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create default command meta data
|
||||
*
|
||||
* @return Meta data
|
||||
*/
|
||||
@Nonnull
|
||||
@Override
|
||||
public BukkitCommandMeta createDefaultCommandMeta() {
|
||||
return BukkitCommandMetaBuilder.builder().withDescription("").build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command sender mapper
|
||||
*
|
||||
* @return Command sender mapper
|
||||
*/
|
||||
@Nonnull
|
||||
public final Function<CommandSender, C> getCommandSenderMapper() {
|
||||
return this.commandSenderMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean hasPermission(@Nonnull final C sender, @Nonnull final String permission) {
|
||||
if (permission.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
|
||||
}
|
||||
|
||||
protected final void setSplitAliases(final boolean value) {
|
||||
this.splitAliases = value;
|
||||
}
|
||||
|
||||
protected final boolean getSplitAliases() {
|
||||
return this.splitAliases;
|
||||
}
|
||||
|
||||
protected final void checkBrigadierCompatibility() throws BrigadierFailureException {
|
||||
if (!this.queryCapability(CloudBukkitCapabilities.BRIGADIER)) {
|
||||
throw new BrigadierFailureException(BrigadierFailureReason.VERSION_TOO_LOW,
|
||||
new IllegalArgumentException("Version: " + this.minecraftVersion));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Query for a specific capability
|
||||
*
|
||||
* @param capability Capability
|
||||
* @return {@code true} if the manager has the given capability, else {@code false}
|
||||
*/
|
||||
public final boolean queryCapability(@Nonnull final CloudBukkitCapabilities capability) {
|
||||
return this.queryCapabilities().contains(capability);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for the platform capabilities
|
||||
*
|
||||
* @return A set containing all capabilities of the instance
|
||||
*/
|
||||
public final Set<CloudBukkitCapabilities> queryCapabilities() {
|
||||
if (this.paper) {
|
||||
if (this.minecraftVersion >= BRIGADIER_MINIMAL_VERSION) {
|
||||
if (this.minecraftVersion >= PAPER_BRIGADIER_VERSION) {
|
||||
return EnumSet.of(CloudBukkitCapabilities.NATIVE_BRIGADIER,
|
||||
CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION,
|
||||
CloudBukkitCapabilities.BRIGADIER);
|
||||
} else {
|
||||
return EnumSet.of(CloudBukkitCapabilities.COMMODORE_BRIGADIER,
|
||||
CloudBukkitCapabilities.BRIGADIER);
|
||||
}
|
||||
} else {
|
||||
return EnumSet.of(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION);
|
||||
}
|
||||
} else {
|
||||
if (this.minecraftVersion >= BRIGADIER_MINIMAL_VERSION) {
|
||||
return EnumSet.of(CloudBukkitCapabilities.COMMODORE_BRIGADIER,
|
||||
CloudBukkitCapabilities.BRIGADIER);
|
||||
}
|
||||
}
|
||||
return EnumSet.noneOf(CloudBukkitCapabilities.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to register the Brigadier mapper, and return it.
|
||||
*
|
||||
* @throws BrigadierFailureException If Brigadier isn't
|
||||
* supported by the platform
|
||||
*/
|
||||
public void registerBrigadier() throws BrigadierFailureException {
|
||||
this.checkBrigadierCompatibility();
|
||||
try {
|
||||
final CloudCommodoreManager<C> cloudCommodoreManager = new CloudCommodoreManager<>(this);
|
||||
cloudCommodoreManager.initialize(this);
|
||||
this.setCommandRegistrationHandler(cloudCommodoreManager);
|
||||
} catch (final Throwable e) {
|
||||
throw new BrigadierFailureException(BrigadierFailureReason.COMMODORE_NOT_PRESENT, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the backwards command sender plugin
|
||||
*
|
||||
* @return The backwards command sender mapper
|
||||
*/
|
||||
@Nonnull
|
||||
public final Function<C, CommandSender> getBackwardsCommandSenderMapper() {
|
||||
return this.backwardsCommandSenderMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reasons to explain why Brigadier failed to initialize
|
||||
*/
|
||||
public enum BrigadierFailureReason {
|
||||
COMMODORE_NOT_PRESENT, VERSION_TOO_LOW, PAPER_BRIGADIER_INITIALIZATION_FAILURE
|
||||
}
|
||||
|
||||
|
||||
public static final class BrigadierFailureException extends IllegalStateException {
|
||||
|
||||
private final BrigadierFailureReason reason;
|
||||
|
||||
/**
|
||||
* Initialize a new Brigadier failure exception
|
||||
*
|
||||
* @param reason Reason
|
||||
*/
|
||||
public BrigadierFailureException(@Nonnull final BrigadierFailureReason reason) {
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a new Brigadier failure exception
|
||||
*
|
||||
* @param reason Reason
|
||||
* @param cause Cause
|
||||
*/
|
||||
public BrigadierFailureException(@Nonnull final BrigadierFailureReason reason, @Nonnull final Throwable cause) {
|
||||
super(cause);
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reason for the exception
|
||||
*
|
||||
* @return Reason
|
||||
*/
|
||||
@Nonnull
|
||||
public BrigadierFailureReason getReason() {
|
||||
return this.reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format("Could not initialize Brigadier mappings. Reason: %s (%s)",
|
||||
this.reason.name().toLowerCase().replace("_", " "),
|
||||
this.getCause() == null ? "" : this.getCause().getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import cloud.commandframework.meta.SimpleCommandMeta;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class BukkitCommandMeta extends SimpleCommandMeta {
|
||||
|
||||
/**
|
||||
* Bukkit command meta data
|
||||
*
|
||||
* @param simpleCommandMeta Simple command meta data instance that gets mirrored
|
||||
*/
|
||||
public BukkitCommandMeta(@Nonnull final SimpleCommandMeta simpleCommandMeta) {
|
||||
super(simpleCommandMeta.getAll());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import cloud.commandframework.meta.CommandMeta;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public final class BukkitCommandMetaBuilder {
|
||||
|
||||
private BukkitCommandMetaBuilder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder stage 1
|
||||
*
|
||||
* @return Builder instance
|
||||
*/
|
||||
@Nonnull
|
||||
public static BuilderStage1 builder() {
|
||||
return new BuilderStage1();
|
||||
}
|
||||
|
||||
|
||||
public static final class BuilderStage1 {
|
||||
|
||||
private BuilderStage1() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the command description
|
||||
*
|
||||
* @param description Command description
|
||||
* @return Builder instance
|
||||
*/
|
||||
@Nonnull
|
||||
public BuilderStage2 withDescription(@Nonnull final String description) {
|
||||
return new BuilderStage2(description);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static final class BuilderStage2 {
|
||||
|
||||
private final String description;
|
||||
|
||||
private BuilderStage2(@Nonnull final String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the command meta instance
|
||||
*
|
||||
* @return Meta instance
|
||||
*/
|
||||
@Nonnull
|
||||
public BukkitCommandMeta build() {
|
||||
return new BukkitCommandMeta(CommandMeta.simple().with("description", this.description).build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Command sender that proxies {@link org.bukkit.command.CommandSender}
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public abstract class BukkitCommandSender {
|
||||
|
||||
private final org.bukkit.command.CommandSender internalSender;
|
||||
|
||||
/**
|
||||
* Create a new command sender from a Bukkit {@link org.bukkit.command.CommandSender}
|
||||
*
|
||||
* @param internalSender Bukkit command sender
|
||||
*/
|
||||
public BukkitCommandSender(@Nonnull final org.bukkit.command.CommandSender internalSender) {
|
||||
this.internalSender = internalSender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new {@link BukkitCommandSender} for a {@link Player}
|
||||
*
|
||||
* @param player Player instance
|
||||
* @return Constructed command sender
|
||||
*/
|
||||
@Nonnull
|
||||
public static BukkitCommandSender player(@Nonnull final Player player) {
|
||||
return new BukkitPlayerSender(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new {@link BukkitCommandSender} for the Bukkit console
|
||||
*
|
||||
* @return Constructed command sender
|
||||
*/
|
||||
@Nonnull
|
||||
public static BukkitCommandSender console() {
|
||||
return new BukkitConsoleSender();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new {@link BukkitCommandSender} from a Bukkit {@link org.bukkit.command.CommandSender}
|
||||
*
|
||||
* @param sender Bukkit command sender
|
||||
* @return Constructed command sender
|
||||
*/
|
||||
@Nonnull
|
||||
public static BukkitCommandSender of(@Nonnull final org.bukkit.command.CommandSender sender) {
|
||||
if (sender instanceof Player) {
|
||||
return player((Player) sender);
|
||||
}
|
||||
return console();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(final Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final BukkitCommandSender that = (BukkitCommandSender) o;
|
||||
return Objects.equal(internalSender, that.internalSender);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return Objects.hashCode(internalSender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the proxied {@link org.bukkit.command.CommandSender}
|
||||
*
|
||||
* @return Proxied command sneder
|
||||
*/
|
||||
@Nonnull
|
||||
public org.bukkit.command.CommandSender getInternalSender() {
|
||||
return this.internalSender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this sender represents a player
|
||||
*
|
||||
* @return {@code true} if this sender represents a player, {@code false} if not
|
||||
*/
|
||||
public abstract boolean isPlayer();
|
||||
|
||||
/**
|
||||
* Get this sender as a player. This can only safely be done if {@link #isPlayer()}}
|
||||
* returns {@code true}
|
||||
*
|
||||
* @return Player object
|
||||
*/
|
||||
@Nonnull
|
||||
public abstract Player asPlayer();
|
||||
|
||||
/**
|
||||
* Send a message to the command sender
|
||||
*
|
||||
* @param message Message to send
|
||||
*/
|
||||
public void sendMessage(@Nonnull final String message) {
|
||||
this.internalSender.sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
final class BukkitConsoleSender extends BukkitCommandSender {
|
||||
|
||||
BukkitConsoleSender() {
|
||||
super(Bukkit.getConsoleSender());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayer() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Player asPlayer() {
|
||||
throw new UnsupportedOperationException("Cannot convert console to player");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
final class BukkitPlayerSender extends BukkitCommandSender {
|
||||
|
||||
BukkitPlayerSender(@Nonnull final Player player) {
|
||||
super(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPlayer() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Player asPlayer() {
|
||||
return (Player) this.getInternalSender();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import cloud.commandframework.Command;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.arguments.StaticArgument;
|
||||
import cloud.commandframework.internal.CommandRegistrationHandler;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.help.GenericCommandHelpTopic;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHandler {
|
||||
|
||||
private final Map<CommandArgument<?, ?>, org.bukkit.command.Command> registeredCommands = new HashMap<>();
|
||||
|
||||
private Map<String, org.bukkit.command.Command> bukkitCommands;
|
||||
private BukkitCommandManager<C> bukkitCommandManager;
|
||||
private CommandMap commandMap;
|
||||
|
||||
BukkitPluginRegistrationHandler() {
|
||||
}
|
||||
|
||||
void initialize(@Nonnull final BukkitCommandManager<C> bukkitCommandManager) throws Exception {
|
||||
final Method getCommandMap = Bukkit.getServer().getClass().getDeclaredMethod("getCommandMap");
|
||||
getCommandMap.setAccessible(true);
|
||||
this.commandMap = (CommandMap) getCommandMap.invoke(Bukkit.getServer());
|
||||
final Field knownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
knownCommands.setAccessible(true);
|
||||
@SuppressWarnings("ALL") final Map<String, org.bukkit.command.Command> bukkitCommands =
|
||||
(Map<String, org.bukkit.command.Command>) knownCommands.get(commandMap);
|
||||
this.bukkitCommands = bukkitCommands;
|
||||
this.bukkitCommandManager = bukkitCommandManager;
|
||||
Bukkit.getHelpMap().registerHelpTopicFactory(BukkitCommand.class, GenericCommandHelpTopic::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean registerCommand(@Nonnull final Command<?> command) {
|
||||
/* 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(commandArgument.getName())) {
|
||||
label = String.format("%s:%s", this.bukkitCommandManager.getOwningPlugin().getName(), commandArgument.getName());
|
||||
} else {
|
||||
label = commandArgument.getName();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") final List<String> aliases = ((StaticArgument<C>) commandArgument).getAlternativeAliases();
|
||||
|
||||
@SuppressWarnings("unchecked") final BukkitCommand<C> bukkitCommand = new BukkitCommand<>(
|
||||
commandArgument.getName(),
|
||||
(this.bukkitCommandManager.getSplitAliases() ? Collections.<String>emptyList() : aliases),
|
||||
(Command<C>) command,
|
||||
(CommandArgument<C, ?>) commandArgument,
|
||||
this.bukkitCommandManager);
|
||||
this.registeredCommands.put(commandArgument, bukkitCommand);
|
||||
this.commandMap.register(commandArgument.getName(), this.bukkitCommandManager.getOwningPlugin().getName().toLowerCase(),
|
||||
bukkitCommand);
|
||||
this.registerExternal(commandArgument.getName(), command, bukkitCommand);
|
||||
|
||||
if (this.bukkitCommandManager.getSplitAliases()) {
|
||||
for (final String alias : aliases) {
|
||||
if (!this.bukkitCommands.containsKey(alias)) {
|
||||
@SuppressWarnings("unchecked") final BukkitCommand<C> aliasCommand = new BukkitCommand<>(
|
||||
alias,
|
||||
Collections.emptyList(),
|
||||
(Command<C>) command,
|
||||
(CommandArgument<C, ?>) commandArgument,
|
||||
this.bukkitCommandManager);
|
||||
this.commandMap.register(alias, this.bukkitCommandManager.getOwningPlugin()
|
||||
.getName().toLowerCase(),
|
||||
bukkitCommand);
|
||||
this.registerExternal(alias, command, aliasCommand);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void registerExternal(@Nonnull final String label,
|
||||
@Nonnull final Command<?> command,
|
||||
@Nonnull final BukkitCommand<C> bukkitCommand) {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
/**
|
||||
* Capabilities for the Bukkit module
|
||||
*/
|
||||
public enum CloudBukkitCapabilities {
|
||||
BRIGADIER, COMMODORE_BRIGADIER, NATIVE_BRIGADIER, ASYNCHRONOUS_COMPLETION
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
//
|
||||
// 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.bukkit;
|
||||
|
||||
import cloud.commandframework.Command;
|
||||
import cloud.commandframework.brigadier.CloudBrigadierManager;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
import me.lucko.commodore.Commodore;
|
||||
import me.lucko.commodore.CommodoreProvider;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
class CloudCommodoreManager<C> extends BukkitPluginRegistrationHandler<C> {
|
||||
|
||||
private final BukkitCommandManager<C> commandManager;
|
||||
private final CloudBrigadierManager brigadierManager;
|
||||
private final Commodore commodore;
|
||||
|
||||
CloudCommodoreManager(@Nonnull final BukkitCommandManager<C> commandManager)
|
||||
throws BukkitCommandManager.BrigadierFailureException {
|
||||
if (!CommodoreProvider.isSupported()) {
|
||||
throw new BukkitCommandManager.BrigadierFailureException(BukkitCommandManager
|
||||
.BrigadierFailureReason.COMMODORE_NOT_PRESENT);
|
||||
}
|
||||
this.commandManager = commandManager;
|
||||
this.commodore = CommodoreProvider.getCommodore(commandManager.getOwningPlugin());
|
||||
this.brigadierManager = new CloudBrigadierManager<>(commandManager, () ->
|
||||
new CommandContext<>(commandManager.getCommandSenderMapper().apply(Bukkit.getConsoleSender())));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerExternal(@Nonnull final String label,
|
||||
@Nonnull final Command<?> command,
|
||||
@Nonnull final BukkitCommand<C> bukkitCommand) {
|
||||
final com.mojang.brigadier.Command<?> cmd = o -> 1;
|
||||
final LiteralCommandNode<?> literalCommandNode = this.brigadierManager
|
||||
.createLiteralCommandNode(label, command, (o, p) -> true, cmd);
|
||||
this.commodore.register(bukkitCommand, literalCommandNode, p ->
|
||||
this.commandManager.hasPermission(commandManager.getCommandSenderMapper().apply(p),
|
||||
command.getCommandPermission()));
|
||||
}
|
||||
}
|
||||
|
|
@ -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.
|
||||
//
|
||||
|
||||
/**
|
||||
* cloud implementation for Bukkit 1.8-1.16
|
||||
*/
|
||||
package cloud.commandframework.bukkit;
|
||||
|
|
@ -0,0 +1,166 @@
|
|||
//
|
||||
// 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.bukkit.parsers;
|
||||
|
||||
import cloud.commandframework.arguments.parser.ArgumentParser;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import org.bukkit.Material;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* cloud argument type that parses Bukkit {@link Material materials}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public class MaterialArgument<C> extends CommandArgument<C, Material> {
|
||||
|
||||
protected MaterialArgument(final boolean required,
|
||||
@Nonnull final String name,
|
||||
@Nonnull final String defaultValue,
|
||||
@Nullable final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
|
||||
super(required, name, new MaterialParser<>(), defaultValue, Material.class, suggestionsProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder
|
||||
*
|
||||
* @param name Name of the argument
|
||||
* @param <C> Command sender type
|
||||
* @return Created builder
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> MaterialArgument.Builder<C> newBuilder(@Nonnull final String name) {
|
||||
return new MaterialArgument.Builder<>(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new required argument
|
||||
*
|
||||
* @param name Argument name
|
||||
* @param <C> Command sender type
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> CommandArgument<C, Material> required(@Nonnull final String name) {
|
||||
return MaterialArgument.<C>newBuilder(name).asRequired().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional argument
|
||||
*
|
||||
* @param name Argument name
|
||||
* @param <C> Command sender type
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> CommandArgument<C, Material> optional(@Nonnull final String name) {
|
||||
return MaterialArgument.<C>newBuilder(name).asOptional().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional argument with a default value
|
||||
*
|
||||
* @param name Argument name
|
||||
* @param material Default value
|
||||
* @param <C> Command sender type
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> CommandArgument<C, Material> optional(@Nonnull final String name,
|
||||
@Nonnull final Material material) {
|
||||
return MaterialArgument.<C>newBuilder(name).asOptionalWithDefault(material.name().toLowerCase()).build();
|
||||
}
|
||||
|
||||
public static final class Builder<C> extends CommandArgument.Builder<C, Material> {
|
||||
|
||||
protected Builder(@Nonnull final String name) {
|
||||
super(Material.class, name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static final class MaterialParser<C> implements ArgumentParser<C, Material> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ArgumentParseResult<Material> parse(@Nonnull final CommandContext<C> commandContext,
|
||||
@Nonnull final Queue<String> inputQueue) {
|
||||
final String input = inputQueue.peek();
|
||||
if (input == null) {
|
||||
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
|
||||
}
|
||||
|
||||
try {
|
||||
final Material material = Material.valueOf(input.replace("minecraft:", "").toUpperCase());
|
||||
inputQueue.remove();
|
||||
return ArgumentParseResult.success(material);
|
||||
} catch (final IllegalArgumentException exception) {
|
||||
return ArgumentParseResult.failure(new MaterialParseException(input));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static final class MaterialParseException extends IllegalArgumentException {
|
||||
|
||||
private final String input;
|
||||
|
||||
/**
|
||||
* Construct a new MaterialParseException
|
||||
*
|
||||
* @param input Input
|
||||
*/
|
||||
public MaterialParseException(@Nonnull final String input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the input
|
||||
*
|
||||
* @return Input
|
||||
*/
|
||||
@Nonnull
|
||||
public String getInput() {
|
||||
return this.input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return EnumSet.allOf(Material.class).stream().map(Material::name).map(String::toLowerCase)
|
||||
.collect(Collectors.joining(", "));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,176 @@
|
|||
//
|
||||
// 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.bukkit.parsers;
|
||||
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParser;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* cloud argument type that parses Bukkit {@link org.bukkit.World worlds}
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public class WorldArgument<C> extends CommandArgument<C, World> {
|
||||
|
||||
protected WorldArgument(final boolean required,
|
||||
@Nonnull final String name,
|
||||
@Nonnull final String defaultValue,
|
||||
@Nullable final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
|
||||
super(required, name, new WorldParser<>(), defaultValue, World.class, suggestionsProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder
|
||||
*
|
||||
* @param name Name of the argument
|
||||
* @param <C> Command sender type
|
||||
* @return Created builder
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> CommandArgument.Builder<C, World> newBuilder(@Nonnull final String name) {
|
||||
return new WorldArgument.Builder<>(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new required argument
|
||||
*
|
||||
* @param name Argument name
|
||||
* @param <C> Command sender type
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> CommandArgument<C, World> required(@Nonnull final String name) {
|
||||
return WorldArgument.<C>newBuilder(name).asRequired().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional argument
|
||||
*
|
||||
* @param name Argument name
|
||||
* @param <C> Command sender type
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> CommandArgument<C, World> optional(@Nonnull final String name) {
|
||||
return WorldArgument.<C>newBuilder(name).asOptional().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new optional argument with a default value
|
||||
*
|
||||
* @param name Argument name
|
||||
* @param defaultValue Default value
|
||||
* @param <C> Command sender type
|
||||
* @return Created argument
|
||||
*/
|
||||
@Nonnull
|
||||
public static <C> 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 CommandArgument.Builder<C, World> {
|
||||
|
||||
protected Builder(@Nonnull final String name) {
|
||||
super(World.class, name);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CommandArgument<C, World> build() {
|
||||
return new WorldArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final class WorldParser<C> implements ArgumentParser<C, World> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ArgumentParseResult<World> parse(@Nonnull final CommandContext<C> commandContext,
|
||||
@Nonnull final Queue<String> inputQueue) {
|
||||
final String input = inputQueue.peek();
|
||||
if (input == null) {
|
||||
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
|
||||
}
|
||||
|
||||
final World world = Bukkit.getWorld(input);
|
||||
if (world == null) {
|
||||
return ArgumentParseResult.failure(new WorldParseException(input));
|
||||
}
|
||||
|
||||
inputQueue.remove();
|
||||
return ArgumentParseResult.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 arguments
|
||||
*/
|
||||
package cloud.commandframework.bukkit.parsers;
|
||||
Loading…
Add table
Add a link
Reference in a new issue