Skip to content

Commit

Permalink
Update to Velocity 2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed May 16, 2021
1 parent 2b39a98 commit f5036eb
Show file tree
Hide file tree
Showing 32 changed files with 371 additions and 355 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Snap!

This is the Seriously Necessary Adapter Plugin to enable plugins written against the
BungeeCord or Waterfall API to load and (kinda) run on [Velocity](https://velocitypowered.com/). 👀
BungeeCord or Waterfall API to load and (kinda) run on [Velocity 2](https://velocitypowered.com/). 👀

## How?

Expand All @@ -19,12 +19,12 @@ Originally I wanted to document the Velocity equivalents to Bungee events, metho

Seeing as the proxies don't have too much logic that seems to have worked although
it is definitely a lot more inefficient than just running native Velocity plugins
due lots of classes being in need of getting translated on the fly.
due to lots of classes being in need of getting translated on the fly.

Technically this could be made in a way that is a lot more efficient by directly
modifying the Velocity or BungeeCord source code to extend the respective other
classes but in practice that massively increases the work required to get this
plugin running, and that's all I wanted to do for now.
plugin running, and that's all I wanted to do. (for now)

## What works?

Expand All @@ -51,7 +51,7 @@ If you are sure that the plugin will work fine otherwise then you can have it re
suited for a plugin. The related methods will return `null` or set nothing. Instead
of erroring.
- **Scoreboards.** Velocity doesn't have API for them and I'm not going to create a
packet based one. Maybe there will be a way to integrate in some plugin or Velocity
packet based one. Maybe there will be a way to hook into some plugin or Velocity
adds support in the future.
- Some **ProxyConfig** settings don't exist on Velocity or aren't exposed in the API so
they return some sensible defaults which should reflect the proxy's state.
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

<groupId>de.themoep</groupId>
<artifactId>snap</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<build.number>${buildNumber}</build.number>
<minecraft.plugin.version>${project.version} ${buildDescription}</minecraft.plugin.version>
</properties>
Expand All @@ -32,7 +32,7 @@
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>1.1.4-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
43 changes: 20 additions & 23 deletions src/main/java/de/themoep/snap/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import com.typesafe.config.ConfigParseOptions;
import com.typesafe.config.ConfigRenderOptions;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.serialize.SerializationException;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -51,9 +50,8 @@ public PluginConfig(Snap plugin, Path configFile, String defaultFile) {
this.configFile = configFile;
this.defaultFile = defaultFile;
configLoader = HoconConfigurationLoader.builder()
.setPath(configFile)
.setParseOptions(ConfigParseOptions.defaults())
.setRenderOptions(ConfigRenderOptions.defaults())
.path(configFile)
.defaultOptions(ConfigurationOptions.defaults())
.build();
}

Expand All @@ -62,13 +60,12 @@ public boolean load() {
config = configLoader.load();
if (defaultFile != null && plugin.getClass().getClassLoader().getResource(defaultFile) != null) {
defaultConfig = HoconConfigurationLoader.builder()
.setPath(configFile)
.setParseOptions(ConfigParseOptions.defaults())
.setRenderOptions(ConfigRenderOptions.defaults())
.setSource(() -> new BufferedReader(new InputStreamReader(plugin.getClass().getClassLoader().getResourceAsStream(defaultFile))))
.path(configFile)
.defaultOptions(ConfigurationOptions.defaults())
.source(() -> new BufferedReader(new InputStreamReader(plugin.getClass().getClassLoader().getResourceAsStream(defaultFile))))
.build()
.load();
if (config.isEmpty()) {
if (config.empty()) {
config = defaultConfig.copy();
}
}
Expand Down Expand Up @@ -112,32 +109,32 @@ public void save() {
}
}

public Object set(String path, Object value) {
ConfigurationNode node = config.getNode(splitPath(path));
Object prev = node.getValue();
node.setValue(value);
public Object set(String path, Object value) throws SerializationException {
ConfigurationNode node = config.node(splitPath(path));
Object prev = node.raw();
node.set(value);
return prev;
}

public ConfigurationNode remove(String path) {
ConfigurationNode node = config.getNode(splitPath(path));
return node.isVirtual() ? node : node.setValue(null);
public ConfigurationNode remove(String path) throws SerializationException {
ConfigurationNode node = config.node(splitPath(path));
return node.virtual() ? node : node.set(null);
}

public ConfigurationNode getRawConfig() {
return config;
}

public ConfigurationNode getRawConfig(String path) {
return getRawConfig().getNode(splitPath(path));
return getRawConfig().node(splitPath(path));
}

public boolean has(String path) {
return !getRawConfig(path).isVirtual();
return !getRawConfig(path).virtual();
}

public boolean isSection(String path) {
return getRawConfig(path).hasMapChildren();
return getRawConfig(path).isMap();
}

public int getInt(String path) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/de/themoep/snap/Snap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.google.common.cache.CacheBuilder;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.lifecycle.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import de.themoep.snap.forwarding.SnapPlayer;
import de.themoep.snap.forwarding.SnapServerInfo;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) throws NoSuchMetho
bungeeAdapter = new SnapBungeeAdapter(this);
bungeeAdapter.registerEvents();
bungeeAdapter.loadPlugins();
getProxy().getEventManager().register(this, new SnapListener(this));
getProxy().eventManager().register(this, new SnapListener(this));
} else {
getLogger().error("Unable to load config! Plugin will not enable.");
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public Map<String, SnapPlayer> getPlayerNames() {
}

public SnapPlayer getPlayer(Player player) {
SnapPlayer p = players.computeIfAbsent(player.getUniqueId(), u -> new SnapPlayer(this, player));
SnapPlayer p = players.computeIfAbsent(player.id(), u -> new SnapPlayer(this, player));
playerNames.putIfAbsent(p.getName(), p);
return p;
}
Expand All @@ -119,7 +119,7 @@ public SnapServerInfo getServerInfo(RegisteredServer server) {
if (server == null) {
return null;
}
return servers.computeIfAbsent(server.getServerInfo().getName(), u -> new SnapServerInfo(this, server));
return servers.computeIfAbsent(server.serverInfo().name(), u -> new SnapServerInfo(this, server));
}

public Map<String, SnapServerInfo> getServers() {
Expand Down
50 changes: 26 additions & 24 deletions src/main/java/de/themoep/snap/SnapBungeeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.connection.Player;
import de.themoep.snap.forwarding.SnapCommandSender;
import de.themoep.snap.forwarding.SnapProxyServer;
import de.themoep.snap.forwarding.listener.ChatListener;
Expand All @@ -45,6 +45,7 @@
import de.themoep.snap.forwarding.listener.SettingsChangedListener;
import de.themoep.snap.forwarding.listener.TabCompleteResponseListener;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.config.ListenerInfo;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.plugin.PluginManager;
Expand All @@ -56,6 +57,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Handler;
Expand Down Expand Up @@ -92,39 +94,39 @@ public class SnapBungeeAdapter {

void registerEvents() {
// Register forwarding events
snap.getProxy().getEventManager().register(snap, new ChatListener(snap));
snap.getProxy().getEventManager().register(snap, new ClientConnectListener(snap));
snap.getProxy().getEventManager().register(snap, new ConnectionInitListener(snap));
snap.getProxy().getEventManager().register(snap, new LoginListener(snap));
snap.getProxy().getEventManager().register(snap, new PlayerDisconnectListener(snap));
snap.getProxy().getEventManager().register(snap, new PlayerHandshakeListener(snap));
snap.getProxy().getEventManager().register(snap, new PluginMessageListener(snap));
snap.getProxy().getEventManager().register(snap, new PostLoginListener(snap));
snap.getProxy().getEventManager().register(snap, new PreLoginListener(snap));
// TODO snap.getProxy().getEventManager().register(snap, new ProxyDefineCommandListener(snap)); hard to convert :S
snap.getProxy().getEventManager().register(snap, new ProxyPingListener(snap));
snap.getProxy().getEventManager().register(snap, new ProxyQueryListener(snap));
snap.getProxy().getEventManager().register(snap, new ProxyReloadListener(snap));
snap.getProxy().getEventManager().register(snap, new ServerConnectedListener(snap));
snap.getProxy().getEventManager().register(snap, new ServerConnectListener(snap));
snap.getProxy().getEventManager().register(snap, new ServerDisconnectListener(snap));
snap.getProxy().getEventManager().register(snap, new ServerKickListener(snap));
snap.getProxy().getEventManager().register(snap, new ServerSwitchListener(snap));
snap.getProxy().getEventManager().register(snap, new SettingsChangedListener(snap));
// TODO snap.getProxy().getEventManager().register(snap, new TabCompleteListener(snap)); no real Velocity equivalent
snap.getProxy().getEventManager().register(snap, new TabCompleteResponseListener(snap));
snap.getProxy().eventManager().register(snap, new ChatListener(snap));
snap.getProxy().eventManager().register(snap, new ClientConnectListener(snap));
snap.getProxy().eventManager().register(snap, new ConnectionInitListener(snap));
snap.getProxy().eventManager().register(snap, new LoginListener(snap));
snap.getProxy().eventManager().register(snap, new PlayerDisconnectListener(snap));
snap.getProxy().eventManager().register(snap, new PlayerHandshakeListener(snap));
snap.getProxy().eventManager().register(snap, new PluginMessageListener(snap));
snap.getProxy().eventManager().register(snap, new PostLoginListener(snap));
snap.getProxy().eventManager().register(snap, new PreLoginListener(snap));
// TODO snap.getProxy().eventManager().register(snap, new ProxyDefineCommandListener(snap)); hard to convert :S
snap.getProxy().eventManager().register(snap, new ProxyPingListener(snap));
snap.getProxy().eventManager().register(snap, new ProxyQueryListener(snap));
snap.getProxy().eventManager().register(snap, new ProxyReloadListener(snap));
snap.getProxy().eventManager().register(snap, new ServerConnectedListener(snap));
snap.getProxy().eventManager().register(snap, new ServerConnectListener(snap));
snap.getProxy().eventManager().register(snap, new ServerDisconnectListener(snap));
snap.getProxy().eventManager().register(snap, new ServerKickListener(snap));
snap.getProxy().eventManager().register(snap, new ServerSwitchListener(snap));
snap.getProxy().eventManager().register(snap, new SettingsChangedListener(snap));
// TODO snap.getProxy().eventManager().register(snap, new TabCompleteListener(snap)); no real Velocity equivalent
snap.getProxy().eventManager().register(snap, new TabCompleteResponseListener(snap));
}

void loadPlugins() {
pluginManager.detectPlugins(pluginsFolder);
pluginManager.loadPlugins();
pluginManager.enablePlugins();

CommandManager cm = snap.getProxy().getCommandManager();
CommandManager cm = snap.getProxy().commandManager();
for (Map.Entry<String, Command> e : pluginManager.getCommands()) {
Command command = e.getValue();
cm.register(
cm.metaBuilder(command.getName()).aliases(command.getAliases()).build(),
cm.createMetaBuilder(command.getName()).aliases(command.getAliases()).build(),
new SimpleCommand() {

@Override
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/de/themoep/snap/SnapListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.lifecycle.ProxyShutdownEvent;
import com.velocitypowered.api.event.player.DisconnectEvent;
import com.velocitypowered.api.event.player.GameProfileRequestEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.event.player.LoginEvent;
import com.velocitypowered.api.util.GameProfile;

import java.util.UUID;
Expand All @@ -40,23 +40,23 @@ public SnapListener(Snap snap) {

@Subscribe(order = PostOrder.FIRST)
public void onPlayerConnect(LoginEvent event) {
if (event.getResult().isAllowed()) {
snap.getPlayer(event.getPlayer());
if (event.result().isAllowed()) {
snap.getPlayer(event.player());
}
}

@Subscribe(order = PostOrder.LAST)
public void onPlayerConnectLast(LoginEvent event) {
if (!event.getResult().isAllowed()) {
snap.getPlayers().remove(event.getPlayer().getUniqueId());
snap.getPlayerNames().remove(event.getPlayer().getUsername());
if (!event.result().isAllowed()) {
snap.getPlayers().remove(event.player().id());
snap.getPlayerNames().remove(event.player().username());
}
}

@Subscribe(order = PostOrder.LAST)
public void onPlayerQuit(DisconnectEvent event) {
snap.getPlayers().remove(event.getPlayer().getUniqueId());
snap.getPlayerNames().remove(event.getPlayer().getUsername());
snap.getPlayers().remove(event.player().id());
snap.getPlayerNames().remove(event.player().username());
}

@Subscribe
Expand All @@ -66,9 +66,9 @@ public void onShutdown(ProxyShutdownEvent event) {

@Subscribe
public void onGameprofileRequest(GameProfileRequestEvent event) {
UUID playerId = snap.pullCachedUuidForUsername(event.getUsername());
if (playerId != null && !event.isOnlineMode() && !event.getGameProfile().getId().equals(playerId)) {
event.setGameProfile(new GameProfile(playerId, event.getGameProfile().getName(), event.getGameProfile().getProperties()));
UUID playerId = snap.pullCachedUuidForUsername(event.username());
if (playerId != null && !event.isOnlineMode() && !event.gameProfile().uuid().equals(playerId)) {
event.setGameProfile(new GameProfile(playerId, event.gameProfile().name(), event.gameProfile().properties()));
}
}
}
Loading

0 comments on commit f5036eb

Please sign in to comment.