Add Velocity module

This commit is contained in:
Alexander Söderberg 2020-09-19 15:50:44 +02:00
parent ccd0e8ae0e
commit f1d4e7865d
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
17 changed files with 566 additions and 22 deletions

View file

@ -0,0 +1,66 @@
<?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-velocity</artifactId>
<repositories>
<repository>
<id>velocity-snapshosts</id>
<url>https://repo.velocitypowered.com/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-core</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.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,89 @@
//
// 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.velocity;
import com.intellectualsites.commands.CommandManager;
import com.intellectualsites.commands.CommandTree;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.meta.CommandMeta;
import com.intellectualsites.commands.meta.SimpleCommandMeta;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.ProxyServer;
import javax.annotation.Nonnull;
import java.util.function.Function;
/**
* {@link CommandManager} implementation for Velocity
*
* @param <C> Command sender type
*/
public class VelocityCommandManager<C> extends CommandManager<C> {
private final ProxyServer proxyServer;
private final Function<CommandSource, C> commandSenderMapper;
private final Function<C, CommandSource> backwardsCommandSenderMapper;
/**
* Create a new command manager instance
*
* @param proxyServer ProxyServer instance
* @param commandExecutionCoordinator Coordinator provider
* @param commandSenderMapper Function that maps {@link CommandSource} to the command sender type
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSource}
*/
public VelocityCommandManager(
@Nonnull final ProxyServer proxyServer,
@Nonnull final Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
@Nonnull final Function<CommandSource, C> commandSenderMapper,
@Nonnull final Function<C, CommandSource> backwardsCommandSenderMapper) {
super(commandExecutionCoordinator, new VelocityPluginRegistrationHandler<>());
((VelocityPluginRegistrationHandler<C>) this.getCommandRegistrationHandler()).initialize(this);
this.proxyServer = proxyServer;
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 CommandMeta createDefaultCommandMeta() {
return SimpleCommandMeta.empty();
}
@Nonnull
final ProxyServer getProxyServer() {
return this.proxyServer;
}
@Nonnull
final Function<CommandSource, C> getCommandSenderMapper() {
return this.commandSenderMapper;
}
}

View file

@ -0,0 +1,86 @@
//
// 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.velocity;
import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.StaticArgument;
import com.intellectualsites.commands.brigadier.CloudBrigadierManager;
import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationHandler {
private final Map<CommandArgument<?, ?>, BrigadierCommand> registeredCommands = new HashMap<>();
private CloudBrigadierManager<C, CommandSource> brigadierManager;
private VelocityCommandManager<C> velocityCommandManager;
void initialize(@Nonnull final VelocityCommandManager<C> velocityCommandManager) {
this.velocityCommandManager = velocityCommandManager;
this.brigadierManager = new CloudBrigadierManager<>(velocityCommandManager,
() -> new CommandContext<>(
velocityCommandManager.getCommandSenderMapper()
.apply(velocityCommandManager.getProxyServer()
.getConsoleCommandSource()))
);
}
@Override
public boolean registerCommand(@Nonnull final Command<?> command) {
final CommandArgument<?, ?> argument = command.getArguments().get(0);
if (this.registeredCommands.containsKey(argument)) {
return false;
}
final List<String> aliases = ((StaticArgument<C>) argument).getAlternativeAliases();
final BrigadierCommand brigadierCommand = new BrigadierCommand(
this.brigadierManager.createLiteralCommandNode((Command<C>) command,
(c, p) -> this.velocityCommandManager.hasPermission(
this.velocityCommandManager.getCommandSenderMapper()
.apply(c), p),
commandContext -> {
final CommandSource source = commandContext.getSource();
final String input = commandContext.getInput();
this.velocityCommandManager.executeCommand(
this.velocityCommandManager.getCommandSenderMapper()
.apply(source), input);
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
})
);
final CommandMeta commandMeta = this.velocityCommandManager.getProxyServer().getCommandManager()
.metaBuilder(brigadierCommand)
.aliases(aliases.toArray(new String[0])).build();
this.velocityCommandManager.getProxyServer().getCommandManager().register(commandMeta, brigadierCommand);
this.registeredCommands.put(argument, brigadierCommand);
return true;
}
}

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.
//
/**
* Velocity implementation of cloud
*/
package com.intellectualsites.commands.velocity;