Skip to content

Commit

Permalink
Update for velocity API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Sep 19, 2018
1 parent 75f0f40 commit 9fd2028
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ subprojects {
project.ext.fullVersion = project.ext.majorVersion + '.' + project.ext.minorVersion + '.' + project.ext.patchVersion

repositories {
mavenLocal()
//mavenLocal()
mavenCentral()

maven {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import me.lucko.luckperms.api.messenger.message.OutgoingMessage;
import me.lucko.luckperms.bungee.LPBungeePlugin;

import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PluginMessageEvent;
Expand All @@ -57,25 +58,31 @@ public PluginMessageMessenger(LPBungeePlugin plugin, IncomingMessageConsumer con
}

public void init() {
this.plugin.getBootstrap().getProxy().getPluginManager().registerListener(this.plugin.getBootstrap(), this);
this.plugin.getBootstrap().getProxy().registerChannel(CHANNEL);
ProxyServer proxy = this.plugin.getBootstrap().getProxy();
proxy.getPluginManager().registerListener(this.plugin.getBootstrap(), this);
proxy.registerChannel(CHANNEL);
}

@Override
public void close() {
this.plugin.getBootstrap().getProxy().unregisterChannel(CHANNEL);
ProxyServer proxy = this.plugin.getBootstrap().getProxy();
proxy.unregisterChannel(CHANNEL);
proxy.getPluginManager().unregisterListener(this);
}

private void dispatchMessage(byte[] message) {
for (ServerInfo server : this.plugin.getBootstrap().getProxy().getServers().values()) {
server.sendData(CHANNEL, message, false);
}
}

@Override
public void sendOutgoingMessage(@NonNull OutgoingMessage outgoingMessage) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(outgoingMessage.asEncodedString());

byte[] data = out.toByteArray();

for (ServerInfo server : this.plugin.getBootstrap().getProxy().getServers().values()) {
server.sendData(CHANNEL, data, false);
}
byte[] message = out.toByteArray();
dispatchMessage(message);
}

@EventHandler
Expand All @@ -97,11 +104,7 @@ public void onPluginMessage(PluginMessageEvent e) {

if (this.consumer.consumeIncomingMessageAsString(msg)) {
// Forward to other servers
this.plugin.getBootstrap().getScheduler().executeAsync(() -> {
for (ServerInfo server : this.plugin.getBootstrap().getProxy().getServers().values()) {
server.sendData(CHANNEL, data, false);
}
});
this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(data));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.event.connection.PluginMessageEvent.ForwardResult;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.ChannelMessageSource;
import com.velocitypowered.api.proxy.messages.ChannelRegistrar;
import com.velocitypowered.api.proxy.messages.ChannelSide;
import com.velocitypowered.api.proxy.messages.MessageHandler;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.proxy.server.RegisteredServer;

import me.lucko.luckperms.api.messenger.IncomingMessageConsumer;
import me.lucko.luckperms.api.messenger.Messenger;
Expand All @@ -43,12 +45,10 @@

import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Optional;

/**
* An implementation of {@link Messenger} using the plugin messaging channels.
*/
public class PluginMessageMessenger implements Messenger, MessageHandler {
public class PluginMessageMessenger implements Messenger {
private static final ChannelIdentifier CHANNEL = MinecraftChannelIdentifier.create("luckperms", "update");

private final LPVelocityPlugin plugin;
Expand All @@ -60,50 +60,54 @@ public PluginMessageMessenger(LPVelocityPlugin plugin, IncomingMessageConsumer c
}

public void init() {
ChannelRegistrar channelRegistrar = this.plugin.getBootstrap().getProxy().getChannelRegistrar();
channelRegistrar.register(this, CHANNEL);
ProxyServer proxy = this.plugin.getBootstrap().getProxy();
proxy.getChannelRegistrar().register(CHANNEL);
proxy.getEventManager().register(this.plugin.getBootstrap(), this);
}

@Override
public void close() {
// TODO: no way to unregister an individual MessageHandler?
ProxyServer proxy = this.plugin.getBootstrap().getProxy();
proxy.getChannelRegistrar().unregister(CHANNEL);
proxy.getEventManager().unregisterListener(this.plugin.getBootstrap(), this);
}

private void dispatchMessage(byte[] data) {
this.plugin.getBootstrap().getScheduler().executeAsync(() -> {
this.plugin.getBootstrap().getProxy().getAllPlayers().stream()
.map(Player::getCurrentServer)
.filter(Optional::isPresent)
.map(Optional::get)
.distinct()
.forEach(server -> server.sendPluginMessage(CHANNEL, data));
});
private void dispatchMessage(byte[] message) {
for (RegisteredServer server : this.plugin.getBootstrap().getProxy().getAllServers()) {
server.sendPluginMessage(CHANNEL, message);
}
}

@Override
public void sendOutgoingMessage(@NonNull OutgoingMessage outgoingMessage) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(outgoingMessage.asEncodedString());

byte[] data = out.toByteArray();
dispatchMessage(data);
byte[] message = out.toByteArray();
dispatchMessage(message);
}

@Override
public ForwardStatus handle(ChannelMessageSource source, ChannelSide side, ChannelIdentifier channel, byte[] data) {
if (side == ChannelSide.FROM_CLIENT) {
return ForwardStatus.HANDLED;
@Subscribe
public void onPluginMessage(PluginMessageEvent e) {
// compare the underlying text representation of the channel
// the namespaced representation is used by legacy servers too, so we
// are able to support both. :)
if (!e.getIdentifier().getId().equals(CHANNEL.getId())) {
return;
}

e.setResult(ForwardResult.handled());

if (e.getSource() instanceof Player) {
return;
}

ByteArrayDataInput in = ByteStreams.newDataInput(data);
ByteArrayDataInput in = e.dataAsDataStream();
String msg = in.readUTF();

if (this.consumer.consumeIncomingMessageAsString(msg)) {
// Forward to other servers
this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(data));
this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(e.getData()));
}

return ForwardStatus.HANDLED;
}

}

0 comments on commit 9fd2028

Please sign in to comment.