Get bungee protocol version detection working. Still some odd issues.

This commit is contained in:
Matsv 2016-10-01 00:53:50 +02:00
parent ca246f9d95
commit 152993f749
No known key found for this signature in database
GPG Key ID: 97CEC2A2EA31350F
4 changed files with 85 additions and 26 deletions

View File

@ -22,6 +22,7 @@ import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler;
import us.myles.ViaVersion.bungee.commands.BungeeCommandSender;
import us.myles.ViaVersion.bungee.platform.*;
import us.myles.ViaVersion.bungee.service.ProtocolDetectorService;
import us.myles.ViaVersion.bungee.storage.BungeeStorage;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.util.GsonUtil;
@ -165,35 +166,14 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener {
// Set the handshake version every time someone connects to any server TODO reflection
@EventHandler
public void onServerConnect(ServerConnectEvent e) throws NoSuchFieldException, IllegalAccessException {
us.myles.ViaVersion.api.data.UserConnection user = Via.getManager().getConnection(e.getPlayer().getUniqueId());
if (!user.has(BungeeStorage.class)) {
user.put(new BungeeStorage(user, e.getPlayer()));
}
int protocolId = ProtocolDetectorService.getProtocolId(e.getTarget().getName());
UserConnection connection = (UserConnection) e.getPlayer();
connection.getPendingConnection().getHandshake().setProtocolVersion(protocolId);
}
/*
TODO: Change when connected
System.out.println("Switching servers..");
if (!ProtocolDetectorService.hasProtocolId(e.getServer().getInfo().getName())) {
getLogger().severe("Could not find the protocol id for server " + e.getServer());
return;
}
int protocolId = ProtocolDetectorService.getProtocolId(e.getServer().getInfo().getName());
UserConnection connection = (UserConnection) e.getPlayer();
ChannelWrapper wrapper = ReflectionUtil.get(connection, "ch", ChannelWrapper.class);
wrapper.setVersion(protocolId);
us.myles.ViaVersion.api.data.UserConnection viaConnection = Via.getManager().getConnection(e.getPlayer().getUniqueId());
ProtocolInfo info = viaConnection.get(ProtocolInfo.class);
// Choose the pipe
List<Pair<Integer, Protocol>> protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocolId);
ProtocolPipeline pipeline = viaConnection.get(ProtocolInfo.class).getPipeline();
if (protocols != null) {
pipeline.pipes().clear();
for (Pair<Integer, Protocol> prot : protocols) {
pipeline.add(prot.getValue());
}
}*/
}

View File

@ -4,14 +4,24 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.netty.ChannelWrapper;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.bungee.service.ProtocolDetectorService;
import us.myles.ViaVersion.bungee.storage.BungeeStorage;
import us.myles.ViaVersion.bungee.util.BungeePipelineUtil;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil;
import us.myles.ViaVersion.util.ReflectionUtil;
import java.util.List;
@ -47,6 +57,7 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
}
// Increment sent
info.incrementSent();
if (info.isActive()) {
// Handle ID
int id = Type.VAR_INT.read(bytebuf);
@ -71,6 +82,8 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
bytebuf = BungeePipelineUtil.compress(ctx, bytebuf);
}
out.add(bytebuf.retain());
checkServerChange();
}
@Override
@ -79,4 +92,45 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
super.exceptionCaught(ctx, cause);
}
// TODO reflection
public void checkServerChange() throws NoSuchFieldException, IllegalAccessException {
if (info.has(BungeeStorage.class)) {
BungeeStorage storage = info.get(BungeeStorage.class);
ProxiedPlayer player = storage.getPlayer();
if (player.getServer() != null) {
if (player.getServer() != null && !player.getServer().getInfo().getName().equals(storage.getCurrentServer())) {
String serverName = player.getServer().getInfo().getName();
storage.setCurrentServer(serverName);
// TODO HANDLE
if (!ProtocolDetectorService.hasProtocolId(serverName)) {
Via.getPlatform().getLogger().severe("Could not find the protocol id for server " + serverName);
return;
}
int protocolId = ProtocolDetectorService.getProtocolId(serverName);
net.md_5.bungee.UserConnection connection = (net.md_5.bungee.UserConnection) player;
ChannelWrapper wrapper = ReflectionUtil.get(connection, "ch", ChannelWrapper.class);
wrapper.setVersion(protocolId);
us.myles.ViaVersion.api.data.UserConnection viaConnection = Via.getManager().getConnection(player.getUniqueId());
ProtocolInfo info = viaConnection.get(ProtocolInfo.class);
// Refresh the pipes
List<Pair<Integer, Protocol>> protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocolId);
ProtocolPipeline pipeline = viaConnection.get(ProtocolInfo.class).getPipeline();
if (protocols != null) {
pipeline.cleanPipes();
for (Pair<Integer, Protocol> prot : protocols) {
pipeline.add(prot.getValue());
}
}
connection.init();
}
}
}
}
}

View File

@ -0,0 +1,20 @@
package us.myles.ViaVersion.bungee.storage;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
@Data
@EqualsAndHashCode(callSuper = true)
public class BungeeStorage extends StoredObject {
private ProxiedPlayer player;
private String currentServer;
public BungeeStorage(UserConnection user, ProxiedPlayer player) {
super(user);
this.player = player;
this.currentServer = "";
}
}

View File

@ -162,4 +162,9 @@ public class ProtocolPipeline extends Protocol {
public List<Protocol> pipes() {
return protocolList;
}
public void cleanPipes() {
pipes().clear();
registerPackets();
}
}