1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 | import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import net.minecraft.server.v1_8_R3.PacketDataSerializer;
import net.minecraft.server.v1_8_R3.PacketPlayInCustomPayload;
import net.minecraft.server.v1_8_R3.PacketPlayInFlying;
import net.minecraft.server.v1_8_R3.PacketPlayInFlying.PacketPlayInLook;
import net.minecraft.server.v1_8_R3.PacketPlayInFlying.PacketPlayInPosition;
import net.minecraft.server.v1_8_R3.PacketPlayInFlying.PacketPlayInPositionLook;
public class PacketPlayerIn extends ChannelInboundHandlerAdapter {
@Override
public void channelRead( ChannelHandlerContext ctx, Object msg ) throws Exception {
// Is incoming packet a custom payload?
if ( msg instanceof PacketPlayInCustomPayload ) {
PacketPlayInCustomPayload packet = ( PacketPlayInCustomPayload ) msg;
// Is channel name labymod3:shadow?
String channel = packet.a();
if ( channel.equals( "labymod3:shadow" ) ) {
PacketDataSerializer data = packet.b();
// First byte is the shadow packet id
byte id = data.readByte();
switch ( id ) {
case 0x00: // PacketPlayInPosition
double x = data.readDouble();
double y = data.readDouble();
double z = data.readDouble();
boolean onGround = data.readBoolean();
PacketPlayInPosition packetPlayInPosition = new PacketPlayInPosition();
setValue( PacketPlayInFlying.class, packetPlayInPosition, "x", x );
setValue( PacketPlayInFlying.class, packetPlayInPosition, "y", y );
setValue( PacketPlayInFlying.class, packetPlayInPosition, "z", z );
setValue( PacketPlayInFlying.class, packetPlayInPosition, "f", onGround );
super.channelRead( ctx, packetPlayInPosition );
break;
case 0x01: // PacketPlayInLook
float yaw = data.readFloat();
float pitch = data.readFloat();
onGround = data.readBoolean();
PacketPlayInLook packetPlayInLook = new PacketPlayInLook();
setValue( PacketPlayInFlying.class, packetPlayInLook, "yaw", yaw );
setValue( PacketPlayInFlying.class, packetPlayInLook, "pitch", pitch );
setValue( PacketPlayInFlying.class, packetPlayInLook, "f", onGround );
super.channelRead( ctx, packetPlayInLook );
break;
case 0x02: // PacketPlayInPositionLook
x = data.readDouble();
y = data.readDouble();
z = data.readDouble();
yaw = data.readFloat();
pitch = data.readFloat();
onGround = data.readBoolean();
PacketPlayInPositionLook packetPlayInPositionLook = new PacketPlayInPositionLook();
setValue( PacketPlayInFlying.class, packetPlayInPositionLook, "x", x );
setValue( PacketPlayInFlying.class, packetPlayInPositionLook, "y", y );
setValue( PacketPlayInFlying.class, packetPlayInPositionLook, "z", z );
setValue( PacketPlayInFlying.class, packetPlayInPositionLook, "yaw", yaw );
setValue( PacketPlayInFlying.class, packetPlayInPositionLook, "pitch", pitch );
setValue( PacketPlayInFlying.class, packetPlayInPositionLook, "f", onGround );
super.channelRead( ctx, packetPlayInPositionLook );
break;
case 0x03: // PacketPlayInFlying
onGround = data.readBoolean();
PacketPlayInFlying packetPlayInFlying = new PacketPlayInFlying();
setValue( PacketPlayInFlying.class, packetPlayInFlying, "f", onGround );
super.channelRead( ctx, packetPlayInFlying );
break;
}
// Timestamp of client at this packet
long time = data.readLong();
// Read all movement inputs
double moveForward = data.readDouble();
double moveStrafe = data.readDouble();
boolean jump = data.readBoolean();
boolean sneak = data.readBoolean();
double x = data.readDouble();
double y = data.readDouble();
double z = data.readDouble();
float yaw = data.readFloat();
float pitch = data.readFloat();
boolean sprinting = data.readBoolean();
// Packet counter since join
int packetCounter = data.readInt();
// Handle data
handleInput( time, moveForward, moveStrafe, jump, sneak, x, y, z, yaw, pitch, sprinting, packetCounter );
// Cancel this packet
return;
}
}
// This packet can pass
super.channelRead( ctx, msg );
}
private void setValue( Class clazz, Object instance, String field, Object value ) throws Exception {
Field f = clazz.getDeclaredField( field );
f.setAccessible( true );
f.set( instance, value );
}
}
|