Skip to content

Commit d2ecd81

Browse files
author
games647
authored
Merge pull request #15 from Alijkaz/main
Add Velocity Support
2 parents cf4e838 + 66ff2c6 commit d2ecd81

File tree

4 files changed

+178
-1
lines changed

4 files changed

+178
-1
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<url>https://www.spigotmc.org/resources/</url>
1414
<description>
15-
Forwards commands from Bukkit to BungeeCord to execute it there
15+
Forwards commands from Bukkit to BungeeCord (Or Velocity) to execute it there
1616
</description>
1717

1818
<properties>
@@ -25,6 +25,7 @@
2525
<modules>
2626
<module>bukkit</module>
2727
<module>bungee</module>
28+
<module>velocity</module>
2829
<module>universal</module>
2930
</modules>
3031

velocity/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.github.games647</groupId>
7+
<artifactId>commandforward</artifactId>
8+
<version>0.4.0</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<!--This have to be in lowercase because it's used by plugin.yml-->
13+
<artifactId>commandforward.velocity</artifactId>
14+
<packaging>jar</packaging>
15+
16+
<!--Represents the main plugin-->
17+
<name>CommandForwardVelocity</name>
18+
19+
<repositories>
20+
<!--Velocity-API -->
21+
<repository>
22+
<id>velocitypowered-repo</id>
23+
<url>https://nexus.velocitypowered.com/repository/maven-public/</url>
24+
</repository>
25+
</repositories>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.velocitypowered</groupId>
30+
<artifactId>velocity-api</artifactId>
31+
<version>3.1.0</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
</dependencies>
35+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.github.games647.commandforward.velocity;
2+
3+
import com.google.inject.Inject;
4+
import com.velocitypowered.api.command.CommandSource;
5+
import com.velocitypowered.api.event.Subscribe;
6+
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
7+
import com.velocitypowered.api.plugin.Plugin;
8+
import com.velocitypowered.api.plugin.annotation.DataDirectory;
9+
import com.velocitypowered.api.proxy.ProxyServer;
10+
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
11+
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
12+
import com.velocitypowered.api.proxy.server.RegisteredServer;
13+
import net.kyori.adventure.text.Component;
14+
import net.kyori.adventure.text.format.NamedTextColor;
15+
import org.slf4j.Logger;
16+
17+
import java.nio.file.Path;
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Optional;
21+
22+
/**
23+
*
24+
* Velocity support for CommandForward plugin
25+
* Since Velocity supports BungeeCord plugin-
26+
* messaging channels now
27+
*
28+
* @author Alijk
29+
* @since 2022-02-13
30+
*
31+
*/
32+
@Plugin(
33+
id = "commandforward",
34+
name = "CommandForward",
35+
version = "0.4.0",
36+
description = "Forwards commands from Bukkit to BungeeCord (Or Velocity) to execute it there",
37+
authors = {"games647", "https://github.com/games647/CommandForward/graphs/contributors"}
38+
)
39+
public class CommandForwardVelocity {
40+
41+
private final ChannelIdentifier MESSAGE_CHANNEL = MinecraftChannelIdentifier.from("commandforward:cmd");
42+
43+
private static Optional<CommandForwardVelocity> instance;
44+
private final ProxyServer proxyServer;
45+
private final Logger logger;
46+
47+
private final List<RegisteredServer> lobbies = new ArrayList<>();
48+
private final List<RegisteredServer> bedwars = new ArrayList<>();
49+
50+
@Inject
51+
public CommandForwardVelocity(ProxyServer proxyServer, Logger logger, @DataDirectory Path dataDirectory) {
52+
instance = Optional.of(this);
53+
this.proxyServer = proxyServer;
54+
this.logger = logger;
55+
}
56+
57+
@Subscribe
58+
public void onProxyInit(ProxyInitializeEvent event) {
59+
// Register the custom messaging channel
60+
proxyServer.getChannelRegistrar().register(MESSAGE_CHANNEL);
61+
// Register an event handler to catch messages for it
62+
proxyServer.getEventManager().register(this, new MessageListener(MESSAGE_CHANNEL));
63+
64+
}
65+
66+
/**
67+
* Print an error message
68+
*
69+
* @param source Sender that execute the current command
70+
* @param message Message to send to command sender
71+
*/
72+
private void sendErrorMessage(CommandSource source, String message) {
73+
Component textComponent = Component.text(String.format("[%s] %s", "CommandForward", message), NamedTextColor.RED);
74+
source.sendMessage(textComponent);
75+
}
76+
77+
public static CommandForwardVelocity getInstance() {
78+
return instance.orElseThrow(IllegalAccessError::new);
79+
}
80+
81+
public ProxyServer getProxyServer() {
82+
return proxyServer;
83+
}
84+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.github.games647.commandforward.velocity;
2+
3+
import com.google.common.io.ByteArrayDataInput;
4+
import com.google.common.io.ByteStreams;
5+
import com.velocitypowered.api.command.CommandManager;
6+
import com.velocitypowered.api.command.CommandSource;
7+
import com.velocitypowered.api.event.Subscribe;
8+
import com.velocitypowered.api.event.connection.PluginMessageEvent;
9+
import com.velocitypowered.api.plugin.PluginManager;
10+
import com.velocitypowered.api.proxy.Player;
11+
import com.velocitypowered.api.proxy.ServerConnection;
12+
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
13+
14+
public class MessageListener {
15+
private final ChannelIdentifier identifier;
16+
17+
public MessageListener(ChannelIdentifier identifier){
18+
this.identifier = identifier;
19+
}
20+
21+
@Subscribe
22+
public void onPluginMessageEvent(PluginMessageEvent event){
23+
// Received plugin message, check channel identifier matches
24+
if(event.getIdentifier().equals(identifier)){
25+
// Since this message was meant for this listener set it to handled
26+
// We do this so the message doesn't get routed through.
27+
event.setResult(PluginMessageEvent.ForwardResult.handled());
28+
29+
if(event.getSource() instanceof ServerConnection){
30+
// Read the data written to the message
31+
Player p = ((ServerConnection) event.getSource()).getPlayer();
32+
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
33+
parseMessage(p, in);
34+
}
35+
}
36+
}
37+
38+
private void parseMessage(CommandSource source, ByteArrayDataInput dataInput) {
39+
final boolean isPlayer = dataInput.readBoolean();
40+
final String command = dataInput.readUTF();
41+
final String arguments = dataInput.readUTF();
42+
final CommandSource invoker = (isPlayer) ? source : CommandForwardVelocity.getInstance().getProxyServer().getConsoleCommandSource();
43+
44+
invokeCommand(invoker, dataInput.readBoolean(), command, arguments);
45+
}
46+
47+
private void invokeCommand(CommandSource invoker, boolean isOp, String command, String arguments) {
48+
PluginManager pluginManager = CommandForwardVelocity.getInstance().getProxyServer().getPluginManager();
49+
CommandManager commandManager = CommandForwardVelocity.getInstance().getProxyServer().getCommandManager();
50+
51+
// TODO implement isOp handle progress
52+
53+
commandManager.executeAsync(invoker, command + " " + arguments);
54+
}
55+
56+
57+
}

0 commit comments

Comments
 (0)