PacketEvents.java
8.57 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.mumfrey.liteloader.core;
import java.util.List;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.Packet;
import net.minecraft.network.login.server.S02PacketLoginSuccess;
import net.minecraft.network.play.client.C01PacketChatMessage;
import net.minecraft.network.play.client.C15PacketClientSettings;
import net.minecraft.network.play.client.C17PacketCustomPayload;
import net.minecraft.network.play.server.S01PacketJoinGame;
import net.minecraft.network.play.server.S02PacketChat;
import net.minecraft.network.play.server.S3FPacketCustomPayload;
import net.minecraft.util.IThreadListener;
import com.mumfrey.liteloader.PacketHandler;
import com.mumfrey.liteloader.ServerChatFilter;
import com.mumfrey.liteloader.api.InterfaceProvider;
import com.mumfrey.liteloader.api.Listener;
import com.mumfrey.liteloader.common.transformers.PacketEventInfo;
import com.mumfrey.liteloader.core.event.HandlerList;
import com.mumfrey.liteloader.core.event.HandlerList.ReturnLogicOp;
import com.mumfrey.liteloader.core.runtime.Packets;
import com.mumfrey.liteloader.interfaces.FastIterable;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
/**
* Packet event handling
*
* @author Adam Mummery-Smith
*/
public abstract class PacketEvents implements InterfaceProvider
{
protected static PacketEvents instance;
class PacketHandlerList extends HandlerList<PacketHandler>
{
private static final long serialVersionUID = 1L;
PacketHandlerList() { super(PacketHandler.class, ReturnLogicOp.AND_BREAK_ON_FALSE); }
}
/**
* Reference to the loader instance
*/
protected final LiteLoader loader;
private PacketHandlerList packetHandlers[] = new PacketHandlerList[Packets.count()];
private FastIterable<ServerChatFilter> serverChatFilters = new HandlerList<ServerChatFilter>(ServerChatFilter.class, ReturnLogicOp.AND_BREAK_ON_FALSE);
private final int loginSuccessPacketId = Packets.S02PacketLoginSuccess.getIndex();
private final int serverChatPacketId = Packets.S02PacketChat.getIndex();
private final int clientChatPacketId = Packets.C01PacketChatMessage.getIndex();
private final int joinGamePacketId = Packets.S01PacketJoinGame.getIndex();
private final int serverPayloadPacketId = Packets.S3FPacketCustomPayload.getIndex();
private final int clientPayloadPacketId = Packets.C17PacketCustomPayload.getIndex();
private final int clientSettingsPacketId = Packets.C15PacketClientSettings.getIndex();
public PacketEvents()
{
PacketEvents.instance = this;
this.loader = LiteLoader.getInstance();
}
@Override
public Class<? extends Listener> getListenerBaseType()
{
return Listener.class;
}
@Override
public void registerInterfaces(InterfaceRegistrationDelegate delegate)
{
delegate.registerInterface(PacketHandler.class);
delegate.registerInterface(ServerChatFilter.class);
}
@Override
public void initProvider()
{
}
/**
* @param serverChatFilter
*/
public void registerServerChatFilter(ServerChatFilter serverChatFilter)
{
this.serverChatFilters.add(serverChatFilter);
}
public void registerPacketHandler(PacketHandler handler)
{
List<Class<? extends Packet>> handledPackets = handler.getHandledPackets();
if (handledPackets != null)
{
for (Class<? extends Packet> packetClass : handledPackets)
{
String packetClassName = packetClass.getName();
int packetId = Packets.indexOf(packetClassName);
if (packetId == -1 || packetId >= this.packetHandlers.length)
{
LiteLoaderLogger.warning("PacketHandler %s attempted to register a handler for unupported packet class %s", handler.getName(), packetClassName);
continue;
}
if (this.packetHandlers[packetId] == null)
{
this.packetHandlers[packetId] = new PacketHandlerList();
}
this.packetHandlers[packetId].add(handler);
}
}
}
public static void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler)
{
PacketEvents.instance.handlePacket(e, netHandler, e.getPacketId());
}
private void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, int packetId)
{
Packets packetInfo = Packets.packets[e.getPacketId()];
IThreadListener threadListener = this.getPacketContextListener(packetInfo.getContext());
if (threadListener != null && !threadListener.isCallingFromMinecraftThread())
{
this.handleAsyncPacketEvent(e, netHandler, packetId);
return;
}
if (this.handlePacketEvent(e, netHandler, packetId) || this.packetHandlers[packetId] == null || e.isCancelled())
{
return;
}
if (this.packetHandlers[packetId].all().handlePacket(netHandler, e.getSource()))
{
return;
}
e.cancel();
}
/**
* @param context
*/
protected abstract IThreadListener getPacketContextListener(Packets.Context context);
/**
* @param e
* @param netHandler
* @param packetId
*/
protected void handleAsyncPacketEvent(PacketEventInfo<Packet> e, INetHandler netHandler, int packetId)
{
Packet packet = e.getSource();
if (packetId == this.loginSuccessPacketId)
{
this.handlePacket(e, netHandler, (S02PacketLoginSuccess)packet);
}
}
/**
* @param e
* @param netHandler
* @param packetId
*
* @return true if the packet was handled by a local handler and shouldn't be forwarded to later handlers
*/
protected boolean handlePacketEvent(PacketEventInfo<Packet> e, INetHandler netHandler, int packetId)
{
Packet packet = e.getSource();
if (packetId == this.serverChatPacketId)
{
this.handlePacket(e, netHandler, (S02PacketChat)packet);
return true;
}
if (packetId == this.clientChatPacketId)
{
this.handlePacket(e, netHandler, (C01PacketChatMessage)packet);
return true;
}
if (packetId == this.joinGamePacketId)
{
this.handlePacket(e, netHandler, (S01PacketJoinGame)packet);
return true;
}
if (packetId == this.serverPayloadPacketId)
{
this.handlePacket(e, netHandler, (S3FPacketCustomPayload)packet);
return true;
}
if (packetId == this.clientPayloadPacketId)
{
this.handlePacket(e, netHandler, (C17PacketCustomPayload)packet);
return true;
}
if (packetId == this.clientSettingsPacketId)
{
this.handlePacket(e, netHandler, (C15PacketClientSettings)packet);
return true;
}
return false;
}
/**
* @param e
* @param netHandler
* @param packet
*/
protected abstract void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, S02PacketLoginSuccess packet);
/**
* S02PacketChat::processPacket()
*
* @param netHandler
* @param packet
*/
protected abstract void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, S02PacketChat packet);
/**
* S02PacketChat::processPacket()
*
* @param netHandler
* @param packet
*/
protected void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, C01PacketChatMessage packet)
{
EntityPlayerMP player = netHandler instanceof NetHandlerPlayServer ? ((NetHandlerPlayServer)netHandler).playerEntity : null;
if (!this.serverChatFilters.all().onChat(player, packet, packet.getMessage()))
{
e.cancel();
}
}
/**
* S01PacketJoinGame::processPacket()
*
* @param netHandler
* @param packet
*/
protected void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, S01PacketJoinGame packet)
{
this.loader.onJoinGame(netHandler, packet);
}
/**
* S3FPacketCustomPayload::processPacket()
*
* @param netHandler
* @param packet
*/
protected void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, S3FPacketCustomPayload packet)
{
LiteLoader.getClientPluginChannels().onPluginChannelMessage(packet);
}
/**
* C17PacketCustomPayload::processPacket()
*
* @param netHandler
* @param packet
*/
protected void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, C17PacketCustomPayload packet)
{
LiteLoader.getServerPluginChannels().onPluginChannelMessage(netHandler, packet);
}
/**
* C15PacketClientSettings::processPacket()
*
* @param e
* @param netHandler
* @param packet
*/
private void handlePacket(PacketEventInfo<Packet> e, INetHandler netHandler, C15PacketClientSettings packet)
{
if (netHandler instanceof NetHandlerPlayServer)
{
LiteLoaderEventBroker.broker.onPlayerSettingsReceived(((NetHandlerPlayServer)netHandler).playerEntity, packet);
}
}
}