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 project.ext.fullVersion = project.ext.majorVersion + '.' + project.ext.minorVersion + '.' + project.ext.patchVersion
repositories { repositories {
mavenLocal() //mavenLocal()
mavenCentral() mavenCentral()
maven { 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.api.messenger.message.OutgoingMessage;
import me.lucko.luckperms.bungee.LPBungeePlugin; 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.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.api.event.PluginMessageEvent;
@ -57,13 +58,22 @@ public class PluginMessageMessenger implements Messenger, Listener {
} }
public void init() { public void init() {
this.plugin.getBootstrap().getProxy().getPluginManager().registerListener(this.plugin.getBootstrap(), this); ProxyServer proxy = this.plugin.getBootstrap().getProxy();
this.plugin.getBootstrap().getProxy().registerChannel(CHANNEL); proxy.getPluginManager().registerListener(this.plugin.getBootstrap(), this);
proxy.registerChannel(CHANNEL);
} }
@Override @Override
public void close() { 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 @Override
@ -71,11 +81,8 @@ public class PluginMessageMessenger implements Messenger, Listener {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(outgoingMessage.asEncodedString()); out.writeUTF(outgoingMessage.asEncodedString());
byte[] data = out.toByteArray(); byte[] message = out.toByteArray();
dispatchMessage(message);
for (ServerInfo server : this.plugin.getBootstrap().getProxy().getServers().values()) {
server.sendData(CHANNEL, data, false);
}
} }
@EventHandler @EventHandler
@ -97,11 +104,7 @@ public class PluginMessageMessenger implements Messenger, Listener {
if (this.consumer.consumeIncomingMessageAsString(msg)) { if (this.consumer.consumeIncomingMessageAsString(msg)) {
// Forward to other servers // Forward to other servers
this.plugin.getBootstrap().getScheduler().executeAsync(() -> { this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(data));
for (ServerInfo server : this.plugin.getBootstrap().getProxy().getServers().values()) {
server.sendData(CHANNEL, data, false);
}
});
} }
} }
} }

View File

@ -28,13 +28,15 @@ package me.lucko.luckperms.velocity.messaging;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; 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.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier; 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.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import me.lucko.luckperms.api.messenger.IncomingMessageConsumer; import me.lucko.luckperms.api.messenger.IncomingMessageConsumer;
import me.lucko.luckperms.api.messenger.Messenger; 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 org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Optional;
/** /**
* An implementation of {@link Messenger} using the plugin messaging channels. * 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 static final ChannelIdentifier CHANNEL = MinecraftChannelIdentifier.create("luckperms", "update");
private final LPVelocityPlugin plugin; private final LPVelocityPlugin plugin;
@ -60,24 +60,22 @@ public class PluginMessageMessenger implements Messenger, MessageHandler {
} }
public void init() { public void init() {
ChannelRegistrar channelRegistrar = this.plugin.getBootstrap().getProxy().getChannelRegistrar(); ProxyServer proxy = this.plugin.getBootstrap().getProxy();
channelRegistrar.register(this, CHANNEL); proxy.getChannelRegistrar().register(CHANNEL);
proxy.getEventManager().register(this.plugin.getBootstrap(), this);
} }
@Override @Override
public void close() { 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) { private void dispatchMessage(byte[] message) {
this.plugin.getBootstrap().getScheduler().executeAsync(() -> { for (RegisteredServer server : this.plugin.getBootstrap().getProxy().getAllServers()) {
this.plugin.getBootstrap().getProxy().getAllPlayers().stream() server.sendPluginMessage(CHANNEL, message);
.map(Player::getCurrentServer) }
.filter(Optional::isPresent)
.map(Optional::get)
.distinct()
.forEach(server -> server.sendPluginMessage(CHANNEL, data));
});
} }
@Override @Override
@ -85,25 +83,31 @@ public class PluginMessageMessenger implements Messenger, MessageHandler {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(outgoingMessage.asEncodedString()); out.writeUTF(outgoingMessage.asEncodedString());
byte[] data = out.toByteArray(); byte[] message = out.toByteArray();
dispatchMessage(data); dispatchMessage(message);
} }
@Override @Subscribe
public ForwardStatus handle(ChannelMessageSource source, ChannelSide side, ChannelIdentifier channel, byte[] data) { public void onPluginMessage(PluginMessageEvent e) {
if (side == ChannelSide.FROM_CLIENT) { // compare the underlying text representation of the channel
return ForwardStatus.HANDLED; // 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(); String msg = in.readUTF();
if (this.consumer.consumeIncomingMessageAsString(msg)) { if (this.consumer.consumeIncomingMessageAsString(msg)) {
// Forward to other servers // Forward to other servers
this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(data)); this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(e.getData()));
} }
return ForwardStatus.HANDLED;
} }
} }