Update for velocity API changes

This commit is contained in:
Luck 2018-09-19 21:47:22 +01:00
parent 75f0f40d70
commit 9fd2028d9f
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 51 additions and 44 deletions

View File

@ -41,7 +41,7 @@ subprojects {
project.ext.fullVersion = project.ext.majorVersion + '.' + project.ext.minorVersion + '.' + project.ext.patchVersion
repositories {
mavenLocal()
//mavenLocal()
mavenCentral()
maven {

View File

@ -34,6 +34,7 @@ import me.lucko.luckperms.api.messenger.Messenger;
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;
@ -57,13 +58,22 @@ public class PluginMessageMessenger implements Messenger, Listener {
}
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
@ -71,11 +81,8 @@ public class PluginMessageMessenger implements Messenger, Listener {
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
@ -97,11 +104,7 @@ public class PluginMessageMessenger implements Messenger, Listener {
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));
}
}
}

View File

@ -28,13 +28,15 @@ package me.lucko.luckperms.velocity.messaging;
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;
@ -43,12 +45,10 @@ import me.lucko.luckperms.velocity.LPVelocityPlugin;
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;
@ -60,24 +60,22 @@ public class PluginMessageMessenger implements Messenger, MessageHandler {
}
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
@ -85,25 +83,31 @@ public class PluginMessageMessenger implements Messenger, MessageHandler {
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;
}
ByteArrayDataInput in = ByteStreams.newDataInput(data);
e.setResult(ForwardResult.handled());
if (e.getSource() instanceof Player) {
return;
}
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;
}
}