3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-10-03 08:41:05 +02:00

Credits to @SanderGielisse, change how netty information is stored so it's not constant and is now attached to ConnectionInfo

Dieser Commit ist enthalten in:
Myles 2016-03-02 10:12:43 +00:00
Ursprung 6199e1d8ce
Commit 153a68bf06
7 geänderte Dateien mit 31 neuen und 48 gelöschten Zeilen

Datei anzeigen

@ -1,5 +1,6 @@
package us.myles.ViaVersion; package us.myles.ViaVersion;
import io.netty.channel.socket.SocketChannel;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
@ -7,12 +8,17 @@ import us.myles.ViaVersion.packets.State;
import java.util.UUID; import java.util.UUID;
public class ConnectionInfo { public class ConnectionInfo {
private final SocketChannel channel;
private int protocol = 0; private int protocol = 0;
private State state = State.HANDSHAKE; private State state = State.HANDSHAKE;
private int compression = 0; private int compression = 0;
private Object lastPacket; private Object lastPacket;
private java.util.UUID UUID; private java.util.UUID UUID;
public ConnectionInfo(SocketChannel socketChannel) {
this.channel = socketChannel;
}
public int getProtocol() { public int getProtocol() {
return protocol; return protocol;
} }
@ -56,4 +62,8 @@ public class ConnectionInfo {
public Player getPlayer() { public Player getPlayer() {
return UUID == null ? null : Bukkit.getPlayer(UUID); return UUID == null ? null : Bukkit.getPlayer(UUID);
} }
public SocketChannel getChannel() {
return channel;
}
} }

Datei anzeigen

@ -13,11 +13,9 @@ import us.myles.ViaVersion.transformers.IncomingTransformer;
@ChannelHandler.Sharable @ChannelHandler.Sharable
public class ViaInboundHandler extends ChannelInboundHandlerAdapter { public class ViaInboundHandler extends ChannelInboundHandlerAdapter {
private final IncomingTransformer incomingTransformer; private final IncomingTransformer incomingTransformer;
private final ViaVersionInitializer init;
public ViaInboundHandler(Channel c, ConnectionInfo info, ViaVersionInitializer init) { public ViaInboundHandler(ConnectionInfo info) {
this.init = init; this.incomingTransformer = new IncomingTransformer(info);
this.incomingTransformer = new IncomingTransformer(c, info, init);
} }
@Override @Override

Datei anzeigen

@ -10,11 +10,11 @@ import us.myles.ViaVersion.transformers.OutgoingTransformer;
@ChannelHandler.Sharable @ChannelHandler.Sharable
public class ViaOutboundHandler extends ChannelOutboundHandlerAdapter { public class ViaOutboundHandler extends ChannelOutboundHandlerAdapter {
private final OutgoingTransformer outgoingTransformer; private final OutgoingTransformer outgoingTransformer;
private final ViaVersionInitializer init; private final ConnectionInfo info;
public ViaOutboundHandler(Channel c, ConnectionInfo info, ViaVersionInitializer init) { public ViaOutboundHandler(ConnectionInfo info) {
this.init = init; this.info = info;
this.outgoingTransformer = new OutgoingTransformer(c, info, init); this.outgoingTransformer = new OutgoingTransformer(info);
} }
@Override @Override

Datei anzeigen

@ -11,7 +11,7 @@ import java.lang.reflect.Constructor;
public class ViaOutboundPacketHandler extends ChannelOutboundHandlerAdapter { public class ViaOutboundPacketHandler extends ChannelOutboundHandlerAdapter {
private final ConnectionInfo info; private final ConnectionInfo info;
public ViaOutboundPacketHandler(Channel c, ConnectionInfo info) { public ViaOutboundPacketHandler(ConnectionInfo info) {
this.info = info; this.info = info;
} }

Datei anzeigen

@ -10,11 +10,6 @@ import java.lang.reflect.Method;
public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> { public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
private final ChannelInitializer<SocketChannel> oldInit; private final ChannelInitializer<SocketChannel> oldInit;
private Method method; private Method method;
private ConnectionInfo info;
private ViaInboundHandler inbound;
private ViaOutboundHandler outbound;
private SocketChannel socketChannel;
private ViaOutboundPacketHandler outbound2;
public ViaVersionInitializer(ChannelInitializer<SocketChannel> oldInit) { public ViaVersionInitializer(ChannelInitializer<SocketChannel> oldInit) {
this.oldInit = oldInit; this.oldInit = oldInit;
@ -28,24 +23,16 @@ public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
@Override @Override
protected void initChannel(SocketChannel socketChannel) throws Exception { protected void initChannel(SocketChannel socketChannel) throws Exception {
info = new ConnectionInfo(); ConnectionInfo info = new ConnectionInfo(socketChannel);
// Add originals // Add originals
this.method.invoke(this.oldInit, socketChannel); this.method.invoke(this.oldInit, socketChannel);
// Add our transformers // Add our transformers
this.socketChannel = socketChannel; ViaInboundHandler inbound = new ViaInboundHandler(info);
this.inbound = new ViaInboundHandler(socketChannel, info, this); ViaOutboundHandler outbound = new ViaOutboundHandler(info);
this.outbound = new ViaOutboundHandler(socketChannel, info, this); ViaOutboundPacketHandler outbound2 = new ViaOutboundPacketHandler(info);
this.outbound2 = new ViaOutboundPacketHandler(socketChannel, info); socketChannel.pipeline().addBefore("decoder", "via_incoming", inbound);
socketChannel.pipeline().addBefore("decoder", "via_incoming", this.inbound); socketChannel.pipeline().addBefore("packet_handler", "via_outgoing2", outbound2);
socketChannel.pipeline().addBefore("packet_handler", "via_outgoing2", this.outbound2); socketChannel.pipeline().addBefore("encoder", "via_outgoing", outbound);
socketChannel.pipeline().addBefore("encoder", "via_outgoing", this.outbound);
} }
public void remove(){
socketChannel.pipeline().remove("via_incoming");
socketChannel.pipeline().remove("via_outgoing");
socketChannel.pipeline().remove("via_outgoing2");
}
} }

Datei anzeigen

@ -1,13 +1,10 @@
package us.myles.ViaVersion.transformers; package us.myles.ViaVersion.transformers;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import us.myles.ViaVersion.*; import us.myles.ViaVersion.*;
import us.myles.ViaVersion.handlers.ViaVersionInitializer;
import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.PacketType;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.util.PacketUtil; import us.myles.ViaVersion.util.PacketUtil;
@ -17,14 +14,10 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class IncomingTransformer { public class IncomingTransformer {
private final Channel channel;
private final ConnectionInfo info; private final ConnectionInfo info;
private final ViaVersionInitializer init;
public IncomingTransformer(Channel channel, ConnectionInfo info, ViaVersionInitializer init) { public IncomingTransformer(ConnectionInfo info) {
this.channel = channel;
this.info = info; this.info = info;
this.init = init;
} }
public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException { public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException {
@ -51,8 +44,10 @@ public class IncomingTransformer {
PacketUtil.writeVarInt(protVer <= 102 ? protVer : 47, output); // pretend to be older PacketUtil.writeVarInt(protVer <= 102 ? protVer : 47, output); // pretend to be older
if (protVer <= 102) { if (protVer <= 102) {
// Not 1.9 remove pipes // not 1.9, remove pipes
this.init.remove(); info.getChannel().pipeline().remove("via_incoming");
info.getChannel().pipeline().remove("via_outgoing");
info.getChannel().pipeline().remove("via_outgoing2");
} }
String serverAddress = PacketUtil.readString(input); String serverAddress = PacketUtil.readString(input);
PacketUtil.writeString(serverAddress, output); PacketUtil.writeString(serverAddress, output);
@ -110,7 +105,7 @@ public class IncomingTransformer {
try { try {
Class<?> setSlot = ReflectionUtil.nms("PacketPlayOutSetSlot"); Class<?> setSlot = ReflectionUtil.nms("PacketPlayOutSetSlot");
Object setSlotPacket = setSlot.getConstructors()[1].newInstance(windowID, slot, null); Object setSlotPacket = setSlot.getConstructors()[1].newInstance(windowID, slot, null);
channel.writeAndFlush(setSlotPacket); // slot is empty info.getChannel().writeAndFlush(setSlotPacket); // slot is empty
slot = -999; // we're evil, they'll throw item on the ground slot = -999; // we're evil, they'll throw item on the ground
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();

Datei anzeigen

@ -2,9 +2,7 @@ package us.myles.ViaVersion.transformers;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.spacehq.mc.protocol.data.game.chunk.Column; import org.spacehq.mc.protocol.data.game.chunk.Column;
import org.spacehq.mc.protocol.util.NetUtil; import org.spacehq.mc.protocol.util.NetUtil;
@ -12,7 +10,6 @@ import us.myles.ViaVersion.CancelException;
import us.myles.ViaVersion.ConnectionInfo; import us.myles.ViaVersion.ConnectionInfo;
import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.handlers.ViaVersionInitializer;
import us.myles.ViaVersion.metadata.MetaIndex; import us.myles.ViaVersion.metadata.MetaIndex;
import us.myles.ViaVersion.metadata.NewType; import us.myles.ViaVersion.metadata.NewType;
import us.myles.ViaVersion.metadata.Type; import us.myles.ViaVersion.metadata.Type;
@ -31,18 +28,14 @@ import static us.myles.ViaVersion.util.PacketUtil.*;
public class OutgoingTransformer { public class OutgoingTransformer {
private static Gson gson = new Gson(); private static Gson gson = new Gson();
private final Channel channel;
private final ConnectionInfo info; private final ConnectionInfo info;
private final ViaVersionInitializer init;
private final ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance(); private final ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance();
private boolean cancel = false; private boolean cancel = false;
private Map<Integer, UUID> uuidMap = new HashMap<Integer, UUID>(); private Map<Integer, UUID> uuidMap = new HashMap<Integer, UUID>();
private Map<Integer, EntityType> clientEntityTypes = new HashMap<Integer, EntityType>(); private Map<Integer, EntityType> clientEntityTypes = new HashMap<Integer, EntityType>();
public OutgoingTransformer(Channel channel, ConnectionInfo info, ViaVersionInitializer init) { public OutgoingTransformer(ConnectionInfo info) {
this.channel = channel;
this.info = info; this.info = info;
this.init = init;
} }
public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException { public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException {