Skip to content

Commit

Permalink
feat!: Updating velocity plugin support to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekwah committed May 19, 2021
1 parent 9fe0275 commit af7a598
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .versionrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let versionRegex = /(\nversion:\s)([0-9.-]+)/;
let velocityVersionRegex = /(\sversion\s=\s")([0-9.-]+)("\))/;
let velocityVersionRegex = /(\sversion\s=\s")([0-9.-]+)(",)/;


const ymlUpdater = {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ dependencies {
implementation "org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT"
implementation "net.md-5:bungeecord-api:1.15-SNAPSHOT"

implementation "com.velocitypowered:velocity-api:1.1.0-SNAPSHOT"
annotationProcessor "com.velocitypowered:velocity-api:1.1.0-SNAPSHOT"
implementation "com.velocitypowered:velocity-api:2.0.0-SNAPSHOT"
annotationProcessor "com.velocitypowered:velocity-annotation-processor:2.0.0-SNAPSHOT"

implementation "io.netty:netty-all:4.0.4.Final"
compileOnly 'com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import com.sekwah.advancedportals.bungee.BungeeMessages;
import com.velocitypowered.api.event.Subscribe;
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.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier;
import com.velocitypowered.api.proxy.connection.ServerConnection;
import com.velocitypowered.api.proxy.messages.PluginChannelId;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import net.kyori.adventure.key.Key;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;
Expand All @@ -23,14 +26,20 @@
*/
@Plugin(id = "advancedportals", name = "Advanced Portals",
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 HashMap<String, String[]> PlayerDestiMap = new HashMap<>();

private static final Key ADVANCED_PORTALS = Key.key("advancedportals", "events");

private final Logger logger;
private final ProxyServer proxy;
private LegacyChannelIdentifier AP_CHANNEL;
private PluginChannelId advancedPortalsChannelId;

@Inject
public AdvancedPortalsPlugin(ProxyServer proxy, Logger logger) {
Expand All @@ -46,17 +55,17 @@ public AdvancedPortalsPlugin(ProxyServer proxy, Logger logger) {
public void onProxyInitialize(ProxyInitializeEvent event) {

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
public void onPluginMessage(PluginMessageEvent event) {
if(event.getIdentifier().equals(AP_CHANNEL)) {
if(event.getSource() instanceof ServerConnection) {
if(event.channel().equals(advancedPortalsChannelId)) {
if(event.source() instanceof ServerConnection) {

ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
ByteArrayDataInput in = ByteStreams.newDataInput(event.rawMessage());

String subChannel = in.readUTF();

Expand All @@ -67,47 +76,47 @@ public void onPluginMessage(PluginMessageEvent event) {

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();
}
else if (subChannel.equalsIgnoreCase(BungeeMessages.BUNGEE_COMMAND)) {
String command = in.readUTF();
ServerConnection connection = (ServerConnection) event.getSource();
if(connection.getPlayer() != null) {
proxy.getCommandManager().executeAsync(connection.getPlayer(), command);
ServerConnection connection = (ServerConnection) event.source();
if(connection.player() != null) {
proxy.commandManager().execute(connection.player(), command);
}

}
}
// 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
public void postJoinEvent(ServerPostConnectEvent event) {
String uuid = event.getPlayer().getUniqueId().toString();
String uuid = event.player().id().toString();

String[] val = PlayerDestiMap.get(uuid);

if (val != null) {
// key: UUID (string)
// value: [0] targetServer, [1] targetDestination, [2] onlineUUID

event.getPlayer().getCurrentServer().ifPresent(serverConnection -> {

if (serverConnection.getServerInfo().getName().equalsIgnoreCase(val[0])) {
@Nullable ServerConnection serverConnection = event.player().connectedServer();
if(serverConnection == null) {
if (serverConnection.serverInfo().name().equalsIgnoreCase(val[0])) {

ByteArrayDataOutput out = ByteStreams.newDataOutput();

out.writeUTF(BungeeMessages.SERVER_DESTI);
out.writeUTF(val[1]);
out.writeUTF(val[2]);

serverConnection.sendPluginMessage(AP_CHANNEL, out.toByteArray());
serverConnection.sendPluginMessage(advancedPortalsChannelId, out.toByteArray());

}
});
}
}
}

Expand Down

0 comments on commit af7a598

Please sign in to comment.