Add Bungee implementation
This commit is contained in:
parent
04a6919c6a
commit
1a85251fc6
29 changed files with 420 additions and 57 deletions
|
|
@ -0,0 +1,122 @@
|
|||
//
|
||||
// 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.bungee;
|
||||
|
||||
import com.intellectualsites.commands.arguments.CommandArgument;
|
||||
import com.intellectualsites.commands.arguments.StaticArgument;
|
||||
import com.intellectualsites.commands.exceptions.ArgumentParseException;
|
||||
import com.intellectualsites.commands.exceptions.InvalidCommandSenderException;
|
||||
import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
|
||||
import com.intellectualsites.commands.exceptions.NoPermissionException;
|
||||
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
|
||||
import com.intellectualsites.commands.meta.SimpleCommandMeta;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
import net.md_5.bungee.api.plugin.TabExecutor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public final class BungeeCommand<C> extends Command implements TabExecutor {
|
||||
|
||||
private static final String MESSAGE_NO_PERMS =
|
||||
"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 BungeeCommandManager<C> bungeeCommandManager;
|
||||
private final CommandArgument<C, ?> command;
|
||||
private final com.intellectualsites.commands.Command<C, SimpleCommandMeta> cloudCommand;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
BungeeCommand(@Nonnull final com.intellectualsites.commands.Command<C, SimpleCommandMeta> cloudCommand,
|
||||
@Nonnull final CommandArgument<C, ?> command,
|
||||
@Nonnull final BungeeCommandManager<C> bungeeCommandManager) {
|
||||
super(command.getName(),
|
||||
cloudCommand.getCommandPermission(),
|
||||
((StaticArgument<C>) command).getAlternativeAliases().toArray(new String[0]));
|
||||
this.command = command;
|
||||
this.bungeeCommandManager = bungeeCommandManager;
|
||||
this.cloudCommand = cloudCommand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(final CommandSender commandSender, final String[] strings) {
|
||||
/* Join input */
|
||||
final StringBuilder builder = new StringBuilder(this.command.getName());
|
||||
for (final String string : strings) {
|
||||
builder.append(" ").append(string);
|
||||
}
|
||||
this.bungeeCommandManager.executeCommand(this.bungeeCommandManager.getCommandSenderMapper().apply(commandSender),
|
||||
builder.toString())
|
||||
.whenComplete(((commandResult, throwable) -> {
|
||||
if (throwable != null) {
|
||||
if (throwable instanceof InvalidSyntaxException) {
|
||||
commandSender.sendMessage(
|
||||
new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ")
|
||||
.color(ChatColor.RED)
|
||||
.append("/")
|
||||
.color(ChatColor.GRAY)
|
||||
.append(((InvalidSyntaxException) throwable).getCorrectSyntax())
|
||||
.color(ChatColor.GRAY)
|
||||
.create()
|
||||
);
|
||||
} else if (throwable instanceof InvalidCommandSenderException) {
|
||||
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage())
|
||||
.color(ChatColor.RED)
|
||||
.create());
|
||||
} else if (throwable instanceof NoPermissionException) {
|
||||
commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS)
|
||||
.color(ChatColor.WHITE)
|
||||
.create());
|
||||
} else if (throwable instanceof NoSuchCommandException) {
|
||||
commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND)
|
||||
.color(ChatColor.WHITE)
|
||||
.create());
|
||||
} else if (throwable instanceof ArgumentParseException) {
|
||||
commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ")
|
||||
.color(ChatColor.GRAY)
|
||||
.append(throwable.getCause().getMessage())
|
||||
.create());
|
||||
} else {
|
||||
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
|
||||
throwable.printStackTrace();
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<String> onTabComplete(final CommandSender sender,
|
||||
final String[] args) {
|
||||
final StringBuilder builder = new StringBuilder(this.command.getName());
|
||||
for (final String string : args) {
|
||||
builder.append(" ").append(string);
|
||||
}
|
||||
return this.bungeeCommandManager.suggest(this.bungeeCommandManager.getCommandSenderMapper().apply(sender),
|
||||
builder.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
//
|
||||
// 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.bungee;
|
||||
|
||||
import com.intellectualsites.commands.CommandManager;
|
||||
import com.intellectualsites.commands.CommandTree;
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import com.intellectualsites.commands.meta.SimpleCommandMeta;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class BungeeCommandManager<C> extends CommandManager<C, SimpleCommandMeta> {
|
||||
|
||||
private final Plugin owningPlugin;
|
||||
private final Function<CommandSender, C> commandSenderMapper;
|
||||
private final Function<C, CommandSender> backwardsCommandSenderMapper;
|
||||
|
||||
/**
|
||||
* 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 BungeeCommandManager(@Nonnull final Plugin owningPlugin,
|
||||
@Nonnull final Function<CommandTree<C, SimpleCommandMeta>,
|
||||
CommandExecutionCoordinator<C, SimpleCommandMeta>> commandExecutionCoordinator,
|
||||
@Nonnull final Function<CommandSender, C> commandSenderMapper,
|
||||
@Nonnull final Function<C, CommandSender> backwardsCommandSenderMapper)
|
||||
throws Exception {
|
||||
super(commandExecutionCoordinator, new BungeePluginRegistrationHandler<>());
|
||||
((BungeePluginRegistrationHandler<C>) this.getCommandRegistrationHandler()).initialize(this);
|
||||
this.owningPlugin = owningPlugin;
|
||||
this.commandSenderMapper = commandSenderMapper;
|
||||
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean hasPermission(@Nonnull final C sender,
|
||||
@Nonnull final String permission) {
|
||||
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public final SimpleCommandMeta createDefaultCommandMeta() {
|
||||
return SimpleCommandMeta.empty();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
final Function<CommandSender, C> getCommandSenderMapper() {
|
||||
return this.commandSenderMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the owning plugin
|
||||
*
|
||||
* @return Owning plugin
|
||||
*/
|
||||
@Nonnull
|
||||
public Plugin getOwningPlugin() {
|
||||
return this.owningPlugin;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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 com.intellectualsites.commands.bungee;
|
||||
|
||||
import com.intellectualsites.commands.Command;
|
||||
import com.intellectualsites.commands.arguments.CommandArgument;
|
||||
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
||||
import com.intellectualsites.commands.meta.SimpleCommandMeta;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
final class BungeePluginRegistrationHandler<C> implements
|
||||
CommandRegistrationHandler<SimpleCommandMeta> {
|
||||
|
||||
private final Map<CommandArgument<?, ?>, net.md_5.bungee.api.plugin.Command> registeredCommands = new HashMap<>();
|
||||
|
||||
private BungeeCommandManager<C> bungeeCommandManager;
|
||||
|
||||
BungeePluginRegistrationHandler() {
|
||||
}
|
||||
|
||||
void initialize(@Nonnull final BungeeCommandManager<C> bungeeCommandManager) {
|
||||
this.bungeeCommandManager = bungeeCommandManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean registerCommand(@Nonnull final Command<?, SimpleCommandMeta> command) {
|
||||
/* We only care about the root command argument */
|
||||
final CommandArgument<?, ?> commandArgument = command.getArguments().get(0);
|
||||
if (this.registeredCommands.containsKey(commandArgument)) {
|
||||
return false;
|
||||
}
|
||||
@SuppressWarnings("unchecked") final BungeeCommand<C> bungeeCommand = new BungeeCommand<>(
|
||||
(Command<C, SimpleCommandMeta>) command,
|
||||
(CommandArgument<C, ?>) commandArgument,
|
||||
this.bungeeCommandManager);
|
||||
this.registeredCommands.put(commandArgument, bungeeCommand);
|
||||
this.bungeeCommandManager.getOwningPlugin().getProxy().getPluginManager()
|
||||
.registerCommand(this.bungeeCommandManager.getOwningPlugin(), bungeeCommand);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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.
|
||||
//
|
||||
|
||||
/**
|
||||
* BungeeCord implementation of cloud
|
||||
*/
|
||||
package com.intellectualsites.commands.bungee;
|
||||
Loading…
Add table
Add a link
Reference in a new issue