Merge branch 'modular' of https://github.com/MylesIsCool/ViaVersion into modular

This commit is contained in:
Myles 2016-09-30 23:55:21 +01:00
commit 178b625ca0
7 changed files with 153 additions and 2 deletions

View File

@ -1,10 +1,12 @@
package us.myles.ViaVersion; package us.myles.ViaVersion;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent; import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
@ -19,6 +21,8 @@ import us.myles.ViaVersion.bungee.commands.BungeeCommand;
import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler; import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler;
import us.myles.ViaVersion.bungee.commands.BungeeCommandSender; import us.myles.ViaVersion.bungee.commands.BungeeCommandSender;
import us.myles.ViaVersion.bungee.platform.*; 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.dump.PluginInfo;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
@ -44,7 +48,7 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener {
Via.init(ViaManager.builder() Via.init(ViaManager.builder()
.platform(this) .platform(this)
.injector(new BungeeViaInjector()) .injector(new BungeeViaInjector())
.loader(new BungeeViaLoader()) .loader(new BungeeViaLoader(this))
.commandHandler(commandHandler) .commandHandler(commandHandler)
.build()); .build());
@ -159,4 +163,17 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener {
Via.getManager().removePortedClient(e.getPlayer().getUniqueId()); Via.getManager().removePortedClient(e.getPlayer().getUniqueId());
} }
// 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);
}
} }

View File

@ -4,14 +4,24 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder; 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.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.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.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.bungee.util.BungeePipelineUtil;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import us.myles.ViaVersion.util.ReflectionUtil;
import java.util.List; import java.util.List;
@ -47,6 +57,7 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
} }
// Increment sent // Increment sent
info.incrementSent(); info.incrementSent();
if (info.isActive()) { if (info.isActive()) {
// Handle ID // Handle ID
int id = Type.VAR_INT.read(bytebuf); int id = Type.VAR_INT.read(bytebuf);
@ -71,6 +82,8 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
bytebuf = BungeePipelineUtil.compress(ctx, bytebuf); bytebuf = BungeePipelineUtil.compress(ctx, bytebuf);
} }
out.add(bytebuf.retain()); out.add(bytebuf.retain());
checkServerChange();
} }
@Override @Override
@ -79,4 +92,45 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
super.exceptionCaught(ctx, cause); 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

@ -5,9 +5,11 @@ import io.netty.channel.ChannelInitializer;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaInjector;
import us.myles.ViaVersion.bungee.handlers.BungeeChannelInitializer; import us.myles.ViaVersion.bungee.handlers.BungeeChannelInitializer;
import us.myles.ViaVersion.util.ReflectionUtil;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.List;
public class BungeeViaInjector implements ViaInjector { public class BungeeViaInjector implements ViaInjector {
@Override @Override
@ -42,7 +44,7 @@ public class BungeeViaInjector implements ViaInjector {
@Override @Override
public int getServerProtocolVersion() throws Exception { public int getServerProtocolVersion() throws Exception {
return 47; // TODO Config Option return (int) ReflectionUtil.getStatic(Class.forName("net.md_5.bungee.protocol.ProtocolConstants"), "SUPPORTED_VERSION_IDS", List.class).get(0);
} }
@Override @Override

View File

@ -1,13 +1,23 @@
package us.myles.ViaVersion.bungee.platform; package us.myles.ViaVersion.bungee.platform;
import lombok.AllArgsConstructor;
import us.myles.ViaVersion.BungeePlugin;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.platform.ViaPlatformLoader; import us.myles.ViaVersion.api.platform.ViaPlatformLoader;
import us.myles.ViaVersion.bungee.providers.BungeeMovementTransmitter; import us.myles.ViaVersion.bungee.providers.BungeeMovementTransmitter;
import us.myles.ViaVersion.bungee.service.ProtocolDetectorService;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider;
import java.util.concurrent.TimeUnit;
@AllArgsConstructor
public class BungeeViaLoader implements ViaPlatformLoader { public class BungeeViaLoader implements ViaPlatformLoader {
private BungeePlugin plugin;
@Override @Override
public void load() { public void load() {
Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BungeeMovementTransmitter()); Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BungeeMovementTransmitter());
plugin.getProxy().getScheduler().schedule(plugin, new ProtocolDetectorService(plugin), 0, 1, TimeUnit.MINUTES);
} }
} }

View File

@ -0,0 +1,43 @@
package us.myles.ViaVersion.bungee.service;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.config.ServerInfo;
import us.myles.ViaVersion.BungeePlugin;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ProtocolDetectorService implements Runnable {
private static final Map<String, Integer> protocolIds = new ConcurrentHashMap<>();
private BungeePlugin plugin;
public ProtocolDetectorService(BungeePlugin plugin) {
this.plugin = plugin;
}
public static Integer getProtocolId(String serverName) {
if (!hasProtocolId(serverName))
return -1;
return protocolIds.get(serverName);
}
public static boolean hasProtocolId(String serverName) {
return protocolIds.containsKey(serverName);
}
@Override
public void run() {
System.out.println("Checking protocol ids"); // TODO remove message after confirming that it works
for (final Map.Entry<String, ServerInfo> lists : plugin.getProxy().getServers().entrySet()) {
lists.getValue().ping(new Callback<ServerPing>() {
@Override
public void done(ServerPing serverPing, Throwable throwable) {
if (throwable == null)
protocolIds.put(lists.getKey(), serverPing.getVersion().getProtocol());
}
});
}
}
}

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() { public List<Protocol> pipes() {
return protocolList; return protocolList;
} }
public void cleanPipes() {
pipes().clear();
registerPackets();
}
} }