feat!: Updating velocity plugin support to 2.0

This commit is contained in:
Sekwah 2021-05-20 00:43:14 +01:00
parent 9fe02751db
commit af7a5986f7
3 changed files with 33 additions and 24 deletions

View File

@ -1,5 +1,5 @@
let versionRegex = /(\nversion:\s)([0-9.-]+)/; let versionRegex = /(\nversion:\s)([0-9.-]+)/;
let velocityVersionRegex = /(\sversion\s=\s")([0-9.-]+)("\))/; let velocityVersionRegex = /(\sversion\s=\s")([0-9.-]+)(",)/;
const ymlUpdater = { const ymlUpdater = {

View File

@ -82,8 +82,8 @@ dependencies {
implementation "org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT" implementation "org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT"
implementation "net.md-5:bungeecord-api:1.15-SNAPSHOT" implementation "net.md-5:bungeecord-api:1.15-SNAPSHOT"
implementation "com.velocitypowered:velocity-api:1.1.0-SNAPSHOT" implementation "com.velocitypowered:velocity-api:2.0.0-SNAPSHOT"
annotationProcessor "com.velocitypowered:velocity-api:1.1.0-SNAPSHOT" annotationProcessor "com.velocitypowered:velocity-annotation-processor:2.0.0-SNAPSHOT"
implementation "io.netty:netty-all:4.0.4.Final" implementation "io.netty:netty-all:4.0.4.Final"
compileOnly 'com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT' compileOnly 'com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT'

View File

@ -7,13 +7,16 @@ import com.google.inject.Inject;
import com.sekwah.advancedportals.bungee.BungeeMessages; import com.sekwah.advancedportals.bungee.BungeeMessages;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.event.lifecycle.ProxyInitializeEvent;
import com.velocitypowered.api.event.player.ServerPostConnectEvent; import com.velocitypowered.api.event.player.ServerPostConnectEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.connection.ServerConnection;
import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier; import com.velocitypowered.api.proxy.messages.PluginChannelId;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import net.kyori.adventure.key.Key;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -23,14 +26,20 @@ import java.util.concurrent.TimeUnit;
*/ */
@Plugin(id = "advancedportals", name = "Advanced Portals", @Plugin(id = "advancedportals", name = "Advanced Portals",
url = "https://www.spigotmc.org/resources/advanced-portals.14356/", url = "https://www.spigotmc.org/resources/advanced-portals.14356/",
version = "0.6.0") version = "0.6.0",
description = "Customisable portal plugin",
dependencies = {
@Dependency(id = "velocity", version = "2.x.x")
})
public class AdvancedPortalsPlugin { public class AdvancedPortalsPlugin {
public HashMap<String, String[]> PlayerDestiMap = new HashMap<>(); public HashMap<String, String[]> PlayerDestiMap = new HashMap<>();
private static final Key ADVANCED_PORTALS = Key.key("advancedportals", "events");
private final Logger logger; private final Logger logger;
private final ProxyServer proxy; private final ProxyServer proxy;
private LegacyChannelIdentifier AP_CHANNEL; private PluginChannelId advancedPortalsChannelId;
@Inject @Inject
public AdvancedPortalsPlugin(ProxyServer proxy, Logger logger) { public AdvancedPortalsPlugin(ProxyServer proxy, Logger logger) {
@ -46,17 +55,17 @@ public class AdvancedPortalsPlugin {
public void onProxyInitialize(ProxyInitializeEvent event) { public void onProxyInitialize(ProxyInitializeEvent event) {
String[] splitChannel = BungeeMessages.CHANNEL_NAME.split(":"); String[] splitChannel = BungeeMessages.CHANNEL_NAME.split(":");
AP_CHANNEL = new LegacyChannelIdentifier(BungeeMessages.CHANNEL_NAME); advancedPortalsChannelId = PluginChannelId.withLegacy(BungeeMessages.CHANNEL_NAME, ADVANCED_PORTALS);
proxy.getChannelRegistrar().register(AP_CHANNEL); proxy.channelRegistrar().register(advancedPortalsChannelId);
} }
@Subscribe @Subscribe
public void onPluginMessage(PluginMessageEvent event) { public void onPluginMessage(PluginMessageEvent event) {
if(event.getIdentifier().equals(AP_CHANNEL)) { if(event.channel().equals(advancedPortalsChannelId)) {
if(event.getSource() instanceof ServerConnection) { if(event.source() instanceof ServerConnection) {
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); ByteArrayDataInput in = ByteStreams.newDataInput(event.rawMessage());
String subChannel = in.readUTF(); String subChannel = in.readUTF();
@ -67,26 +76,26 @@ public class AdvancedPortalsPlugin {
PlayerDestiMap.put(targetUUID, new String[]{targetServer, targetDestination, targetUUID}); PlayerDestiMap.put(targetUUID, new String[]{targetServer, targetDestination, targetUUID});
proxy.getScheduler().buildTask(this, () -> PlayerDestiMap.remove(targetUUID)) proxy.scheduler().buildTask(this, () -> PlayerDestiMap.remove(targetUUID))
.delay(10, TimeUnit.SECONDS).schedule(); .delay(10, TimeUnit.SECONDS).schedule();
} }
else if (subChannel.equalsIgnoreCase(BungeeMessages.BUNGEE_COMMAND)) { else if (subChannel.equalsIgnoreCase(BungeeMessages.BUNGEE_COMMAND)) {
String command = in.readUTF(); String command = in.readUTF();
ServerConnection connection = (ServerConnection) event.getSource(); ServerConnection connection = (ServerConnection) event.source();
if(connection.getPlayer() != null) { if(connection.player() != null) {
proxy.getCommandManager().executeAsync(connection.getPlayer(), command); proxy.commandManager().execute(connection.player(), command);
} }
} }
} }
// So that client packets don't make it through to the servers, always trigger on this channel. // So that client packets don't make it through to the servers, always trigger on this channel.
event.setResult(PluginMessageEvent.ForwardResult.handled()); event.setHandled(true);
} }
} }
@Subscribe @Subscribe
public void postJoinEvent(ServerPostConnectEvent event) { public void postJoinEvent(ServerPostConnectEvent event) {
String uuid = event.getPlayer().getUniqueId().toString(); String uuid = event.player().id().toString();
String[] val = PlayerDestiMap.get(uuid); String[] val = PlayerDestiMap.get(uuid);
@ -94,9 +103,9 @@ public class AdvancedPortalsPlugin {
// key: UUID (string) // key: UUID (string)
// value: [0] targetServer, [1] targetDestination, [2] onlineUUID // value: [0] targetServer, [1] targetDestination, [2] onlineUUID
event.getPlayer().getCurrentServer().ifPresent(serverConnection -> { @Nullable ServerConnection serverConnection = event.player().connectedServer();
if(serverConnection == null) {
if (serverConnection.getServerInfo().getName().equalsIgnoreCase(val[0])) { if (serverConnection.serverInfo().name().equalsIgnoreCase(val[0])) {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
@ -104,10 +113,10 @@ public class AdvancedPortalsPlugin {
out.writeUTF(val[1]); out.writeUTF(val[1]);
out.writeUTF(val[2]); out.writeUTF(val[2]);
serverConnection.sendPluginMessage(AP_CHANNEL, out.toByteArray()); serverConnection.sendPluginMessage(advancedPortalsChannelId, out.toByteArray());
} }
}); }
} }
} }