mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2025-02-16 19:51:35 +01:00
Credits to @SanderGielisse, change how netty information is stored so it's not constant and is now attached to ConnectionInfo
This commit is contained in:
parent
6199e1d8ce
commit
153a68bf06
@ -1,5 +1,6 @@
|
||||
package us.myles.ViaVersion;
|
||||
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.myles.ViaVersion.packets.State;
|
||||
@ -7,12 +8,17 @@ import us.myles.ViaVersion.packets.State;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ConnectionInfo {
|
||||
private final SocketChannel channel;
|
||||
private int protocol = 0;
|
||||
private State state = State.HANDSHAKE;
|
||||
private int compression = 0;
|
||||
private Object lastPacket;
|
||||
private java.util.UUID UUID;
|
||||
|
||||
public ConnectionInfo(SocketChannel socketChannel) {
|
||||
this.channel = socketChannel;
|
||||
}
|
||||
|
||||
public int getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
@ -56,4 +62,8 @@ public class ConnectionInfo {
|
||||
public Player getPlayer() {
|
||||
return UUID == null ? null : Bukkit.getPlayer(UUID);
|
||||
}
|
||||
|
||||
public SocketChannel getChannel() {
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
@ -13,11 +13,9 @@ import us.myles.ViaVersion.transformers.IncomingTransformer;
|
||||
@ChannelHandler.Sharable
|
||||
public class ViaInboundHandler extends ChannelInboundHandlerAdapter {
|
||||
private final IncomingTransformer incomingTransformer;
|
||||
private final ViaVersionInitializer init;
|
||||
|
||||
public ViaInboundHandler(Channel c, ConnectionInfo info, ViaVersionInitializer init) {
|
||||
this.init = init;
|
||||
this.incomingTransformer = new IncomingTransformer(c, info, init);
|
||||
public ViaInboundHandler(ConnectionInfo info) {
|
||||
this.incomingTransformer = new IncomingTransformer(info);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -10,11 +10,11 @@ import us.myles.ViaVersion.transformers.OutgoingTransformer;
|
||||
@ChannelHandler.Sharable
|
||||
public class ViaOutboundHandler extends ChannelOutboundHandlerAdapter {
|
||||
private final OutgoingTransformer outgoingTransformer;
|
||||
private final ViaVersionInitializer init;
|
||||
private final ConnectionInfo info;
|
||||
|
||||
public ViaOutboundHandler(Channel c, ConnectionInfo info, ViaVersionInitializer init) {
|
||||
this.init = init;
|
||||
this.outgoingTransformer = new OutgoingTransformer(c, info, init);
|
||||
public ViaOutboundHandler(ConnectionInfo info) {
|
||||
this.info = info;
|
||||
this.outgoingTransformer = new OutgoingTransformer(info);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -11,7 +11,7 @@ import java.lang.reflect.Constructor;
|
||||
public class ViaOutboundPacketHandler extends ChannelOutboundHandlerAdapter {
|
||||
private final ConnectionInfo info;
|
||||
|
||||
public ViaOutboundPacketHandler(Channel c, ConnectionInfo info) {
|
||||
public ViaOutboundPacketHandler(ConnectionInfo info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
|
@ -10,11 +10,6 @@ import java.lang.reflect.Method;
|
||||
public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
|
||||
private final ChannelInitializer<SocketChannel> oldInit;
|
||||
private Method method;
|
||||
private ConnectionInfo info;
|
||||
private ViaInboundHandler inbound;
|
||||
private ViaOutboundHandler outbound;
|
||||
private SocketChannel socketChannel;
|
||||
private ViaOutboundPacketHandler outbound2;
|
||||
|
||||
public ViaVersionInitializer(ChannelInitializer<SocketChannel> oldInit) {
|
||||
this.oldInit = oldInit;
|
||||
@ -28,24 +23,16 @@ public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||
info = new ConnectionInfo();
|
||||
ConnectionInfo info = new ConnectionInfo(socketChannel);
|
||||
// Add originals
|
||||
this.method.invoke(this.oldInit, socketChannel);
|
||||
// Add our transformers
|
||||
this.socketChannel = socketChannel;
|
||||
this.inbound = new ViaInboundHandler(socketChannel, info, this);
|
||||
this.outbound = new ViaOutboundHandler(socketChannel, info, this);
|
||||
this.outbound2 = new ViaOutboundPacketHandler(socketChannel, info);
|
||||
socketChannel.pipeline().addBefore("decoder", "via_incoming", this.inbound);
|
||||
socketChannel.pipeline().addBefore("packet_handler", "via_outgoing2", this.outbound2);
|
||||
socketChannel.pipeline().addBefore("encoder", "via_outgoing", this.outbound);
|
||||
ViaInboundHandler inbound = new ViaInboundHandler(info);
|
||||
ViaOutboundHandler outbound = new ViaOutboundHandler(info);
|
||||
ViaOutboundPacketHandler outbound2 = new ViaOutboundPacketHandler(info);
|
||||
socketChannel.pipeline().addBefore("decoder", "via_incoming", inbound);
|
||||
socketChannel.pipeline().addBefore("packet_handler", "via_outgoing2", outbound2);
|
||||
socketChannel.pipeline().addBefore("encoder", "via_outgoing", outbound);
|
||||
|
||||
}
|
||||
|
||||
public void remove(){
|
||||
socketChannel.pipeline().remove("via_incoming");
|
||||
socketChannel.pipeline().remove("via_outgoing");
|
||||
socketChannel.pipeline().remove("via_outgoing2");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,10 @@
|
||||
package us.myles.ViaVersion.transformers;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import us.myles.ViaVersion.*;
|
||||
import us.myles.ViaVersion.handlers.ViaVersionInitializer;
|
||||
import us.myles.ViaVersion.packets.PacketType;
|
||||
import us.myles.ViaVersion.packets.State;
|
||||
import us.myles.ViaVersion.util.PacketUtil;
|
||||
@ -17,14 +14,10 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class IncomingTransformer {
|
||||
private final Channel channel;
|
||||
private final ConnectionInfo info;
|
||||
private final ViaVersionInitializer init;
|
||||
|
||||
public IncomingTransformer(Channel channel, ConnectionInfo info, ViaVersionInitializer init) {
|
||||
this.channel = channel;
|
||||
public IncomingTransformer(ConnectionInfo info) {
|
||||
this.info = info;
|
||||
this.init = init;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
if (protVer <= 102) {
|
||||
// Not 1.9 remove pipes
|
||||
this.init.remove();
|
||||
// not 1.9, remove pipes
|
||||
info.getChannel().pipeline().remove("via_incoming");
|
||||
info.getChannel().pipeline().remove("via_outgoing");
|
||||
info.getChannel().pipeline().remove("via_outgoing2");
|
||||
}
|
||||
String serverAddress = PacketUtil.readString(input);
|
||||
PacketUtil.writeString(serverAddress, output);
|
||||
@ -110,7 +105,7 @@ public class IncomingTransformer {
|
||||
try {
|
||||
Class<?> setSlot = ReflectionUtil.nms("PacketPlayOutSetSlot");
|
||||
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
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -2,9 +2,7 @@ package us.myles.ViaVersion.transformers;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.spacehq.mc.protocol.data.game.chunk.Column;
|
||||
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.ViaVersionPlugin;
|
||||
import us.myles.ViaVersion.api.ViaVersion;
|
||||
import us.myles.ViaVersion.handlers.ViaVersionInitializer;
|
||||
import us.myles.ViaVersion.metadata.MetaIndex;
|
||||
import us.myles.ViaVersion.metadata.NewType;
|
||||
import us.myles.ViaVersion.metadata.Type;
|
||||
@ -31,18 +28,14 @@ import static us.myles.ViaVersion.util.PacketUtil.*;
|
||||
|
||||
public class OutgoingTransformer {
|
||||
private static Gson gson = new Gson();
|
||||
private final Channel channel;
|
||||
private final ConnectionInfo info;
|
||||
private final ViaVersionInitializer init;
|
||||
private final ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance();
|
||||
private boolean cancel = false;
|
||||
private Map<Integer, UUID> uuidMap = new HashMap<Integer, UUID>();
|
||||
private Map<Integer, EntityType> clientEntityTypes = new HashMap<Integer, EntityType>();
|
||||
|
||||
public OutgoingTransformer(Channel channel, ConnectionInfo info, ViaVersionInitializer init) {
|
||||
this.channel = channel;
|
||||
public OutgoingTransformer(ConnectionInfo info) {
|
||||
this.info = info;
|
||||
this.init = init;
|
||||
}
|
||||
|
||||
public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException {
|
||||
|
Loading…
Reference in New Issue
Block a user