Inject into Bungee, (does not get past login from when i've tested)

Also add commands
This commit is contained in:
Myles 2016-09-26 14:48:46 +01:00
parent 42edffb309
commit cdab5b3f5b
9 changed files with 151 additions and 24 deletions

View File

@ -1,6 +1,7 @@
package us.myles.ViaVersion;
import com.google.gson.JsonObject;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin;
@ -10,6 +11,8 @@ import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.bungee.command.BungeeCommand;
import us.myles.ViaVersion.bungee.command.BungeeCommandHandler;
import us.myles.ViaVersion.bungee.BungeeViaAPI;
import us.myles.ViaVersion.bungee.BungeeViaInjector;
import us.myles.ViaVersion.bungee.BungeeViaLoader;
@ -23,16 +26,20 @@ public class Bungee extends Plugin implements ViaPlatform {
private BungeeViaAPI api;
private BungeeConfigProvider config;
private BungeeCommandHandler commandHandler;
@Override
public void onLoad() {
api = new BungeeViaAPI();
config = new BungeeConfigProvider();
commandHandler = new BungeeCommandHandler();
ProxyServer.getInstance().getPluginManager().registerCommand(this, new BungeeCommand(commandHandler));
// Init platform
Via.init(ViaManager.builder()
.platform(this)
.injector(new BungeeViaInjector())
.loader(new BungeeViaLoader())
.commandHandler(commandHandler)
.build());
}

View File

@ -0,0 +1,19 @@
package us.myles.ViaVersion.bungee.command;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;
public class BungeeCommand extends Command {
private final BungeeCommandHandler handler;
public BungeeCommand(BungeeCommandHandler handler) {
super("viaversion"); // TODO PERMS HERE
this.handler = handler;
}
@Override
public void execute(CommandSender commandSender, String[] strings) {
handler.onCommand(new BungeeCommandSender(commandSender), strings);
}
}

View File

@ -0,0 +1,7 @@
package us.myles.ViaVersion.bungee.command;
import us.myles.ViaVersion.commands.ViaCommandHandler;
public class BungeeCommandHandler extends ViaCommandHandler {
}

View File

@ -3,6 +3,7 @@ package us.myles.ViaVersion.bungee.config;
import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -145,7 +146,7 @@ public class BungeeConfigProvider implements ViaVersionConfig, ConfigurationProv
@Override
public List<Integer> getBlockedProtocols() {
return null;
return Arrays.asList();
}
@Override

View File

@ -3,9 +3,14 @@ package us.myles.ViaVersion.bungee.handlers;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.Protocol;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
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;
@ -14,12 +19,13 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class ViaDecodeHandler extends ByteToMessageDecoder {
public class ViaDecodeHandler extends MinecraftDecoder {
private final ByteToMessageDecoder minecraftDecoder;
private final MinecraftDecoder minecraftDecoder;
private final UserConnection info;
public ViaDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) {
public ViaDecodeHandler(UserConnection info, MinecraftDecoder minecraftDecoder) {
super(Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion());
this.info = info;
this.minecraftDecoder = minecraftDecoder;
}
@ -68,7 +74,7 @@ public class ViaDecodeHandler extends ByteToMessageDecoder {
// call minecraft decoder
try {
list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf));
list.addAll(BungeePipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf));
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
@ -86,4 +92,14 @@ public class ViaDecodeHandler extends ByteToMessageDecoder {
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
super.exceptionCaught(ctx, cause);
}
@Override
public void setProtocol(Protocol protocol) {
this.minecraftDecoder.setProtocol(protocol);
}
@Override
public void setProtocolVersion(int protocolVersion) {
this.minecraftDecoder.setProtocolVersion(protocolVersion);
}
}

View File

@ -3,9 +3,14 @@ package us.myles.ViaVersion.bungee.handlers;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.MinecraftEncoder;
import net.md_5.bungee.protocol.Protocol;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type;
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;
@ -13,27 +18,25 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException;
public class ViaEncodeHandler extends MessageToByteEncoder {
public class ViaEncodeHandler extends MinecraftEncoder {
private final UserConnection info;
private final MessageToByteEncoder minecraftEncoder;
private final MinecraftEncoder minecraftEncoder;
public ViaEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
public ViaEncodeHandler(UserConnection info, MinecraftEncoder minecraftEncoder) {
super(Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion());
this.info = info;
this.minecraftEncoder = minecraftEncoder;
}
@Override
protected void encode(final ChannelHandlerContext ctx, Object o, final ByteBuf bytebuf) throws Exception {
// handle the packet type
if (!(o instanceof ByteBuf)) {
// call minecraft encoder
try {
PipelineUtil.callEncode(this.minecraftEncoder, ctx, o, bytebuf);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
protected void encode(final ChannelHandlerContext ctx, DefinedPacket o, final ByteBuf bytebuf) throws Exception {
// call minecraft encoder
try {
BungeePipelineUtil.callEncode(this.minecraftEncoder, ctx, o, bytebuf);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
}
if (bytebuf.readableBytes() == 0) {
@ -67,4 +70,14 @@ public class ViaEncodeHandler extends MessageToByteEncoder {
if (PipelineUtil.containsCause(cause, CancelException.class)) return;
super.exceptionCaught(ctx, cause);
}
@Override
public void setProtocol(Protocol protocol) {
this.minecraftEncoder.setProtocol(protocol);
}
@Override
public void setProtocolVersion(int protocolVersion) {
this.minecraftEncoder.setProtocolVersion(protocolVersion);
}
}

View File

@ -5,6 +5,9 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.MinecraftEncoder;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
@ -37,12 +40,12 @@ public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
// Add originals
this.method.invoke(this.original, socketChannel);
// Add our transformers
MessageToByteEncoder encoder = new ViaEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder"));
ByteToMessageDecoder decoder = new ViaDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder"));
ViaPacketHandler chunkHandler = new ViaPacketHandler(info);
ViaEncodeHandler encoder = new ViaEncodeHandler(info, (MinecraftEncoder) socketChannel.pipeline().get("packet-encoder"));
ViaDecodeHandler decoder = new ViaDecodeHandler(info, (MinecraftDecoder) socketChannel.pipeline().get("packet-decoder"));
// ViaPacketHandler chunkHandler = new ViaPacketHandler(info);
socketChannel.pipeline().replace("encoder", "encoder", encoder);
socketChannel.pipeline().replace("decoder", "decoder", decoder);
socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
socketChannel.pipeline().replace("packet-encoder", "packet-encoder", encoder);
socketChannel.pipeline().replace("packet-decoder", "packet-decoder", decoder);
// socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
}
}

View File

@ -0,0 +1,56 @@
package us.myles.ViaVersion.bungee.util;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.MinecraftEncoder;
import us.myles.ViaVersion.Bungee;
import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class BungeePipelineUtil {
private static Method DECODE_METHOD;
private static Method ENCODE_METHOD;
static {
try {
DECODE_METHOD = MinecraftDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, ByteBuf.class, List.class);
DECODE_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
System.out.println("Netty issue?");
}
try {
ENCODE_METHOD = MinecraftEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, DefinedPacket.class, ByteBuf.class);
ENCODE_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
System.out.println("Netty issue?");
}
}
public static List<Object> callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) throws InvocationTargetException {
List<Object> output = new ArrayList<>();
try {
BungeePipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return output;
}
public static void callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, Object msg, ByteBuf output) throws InvocationTargetException {
try {
BungeePipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, msg, output);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

View File

@ -5,6 +5,7 @@ import com.google.common.collect.Maps;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@ -30,6 +31,10 @@ public class ReflectionUtil {
public static void setStatic(Class<?> clazz, String f, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(f);
field.setAccessible(true);
// Remove any final stuff
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, value);
}