HookChat.java
4.12 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
package com.mumfrey.liteloader.core;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Map;
import net.minecraft.src.IntHashMap;
import net.minecraft.src.NetHandler;
import net.minecraft.src.Packet;
import net.minecraft.src.Packet3Chat;
import com.mumfrey.liteloader.util.PrivateFields;
/**
* Proxy packet which we will register in place of the original chat packet. The class will proxy the function calls
* through to the replaced class via reflection if the original (replaced) class is NOT the basic Packet3Chat (this
* is to maintain compatibility with things like WorldEditCUI.
*
* @author Adam Mummery-Smith
*
*/
public class HookChat extends Packet3Chat
{
/**
* True if this class was registered with the base class
*/
private static boolean registered = false;
/**
* Handler module which is registered to handle inbound chat packets
*/
private static LiteLoader packetHandler;
/**
* Class which was overridden and will be instanced for new packets
*/
private static Class<? extends Packet> proxyClass;
/**
* Instance of the proxy packet for this packet instance
*/
private Packet proxyPacket;
/**
* Create a new chat packet proxy
*/
public HookChat()
{
super();
try
{
if (proxyClass != null)
{
proxyPacket = proxyClass.newInstance();
}
}
catch (Exception ex) {}
}
/**
* Create a new chat proxy with the specified message
* @param message
*/
public HookChat(String message)
{
super(message);
try
{
if (proxyClass != null)
{
proxyPacket = proxyClass.newInstance();
if (proxyPacket instanceof Packet3Chat)
{
((Packet3Chat)proxyPacket).message = this.message;
}
}
}
catch (Exception ex) {}
}
@Override
public void readPacketData(DataInput datainputstream) throws IOException
{
if (proxyPacket != null)
{
proxyPacket.readPacketData(datainputstream);
this.message = ((Packet3Chat)proxyPacket).message;
}
else
super.readPacketData(datainputstream);
}
@Override
public void writePacketData(DataOutput dataoutputstream) throws IOException
{
if (proxyPacket != null)
proxyPacket.writePacketData(dataoutputstream);
else
super.writePacketData(dataoutputstream);
}
@Override
public void processPacket(NetHandler nethandler)
{
if (packetHandler == null || packetHandler.onChat(this))
{
if (proxyPacket != null)
proxyPacket.processPacket(nethandler);
else
super.processPacket(nethandler);
}
}
@Override
public int getPacketSize()
{
if (proxyPacket != null)
return proxyPacket.getPacketSize();
return super.getPacketSize();
}
/**
* Register the specified handler as the packet handler for this packet
* @param handler
*/
public static void registerPacketHandler(LiteLoader handler)
{
packetHandler = handler;
}
/**
* Register this packet as the new packet for packet ID 3
*/
public static void register()
{
register(false);
}
/**
* Register this packet as the new packet for packet ID 3 and optionally force re-registration even
* if registration was performed already.
*
* @param force Force registration even if registration was already performed previously.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void register(boolean force)
{
if (!registered || force)
{
try
{
IntHashMap packetIdToClassMap = Packet.packetIdToClassMap;
proxyClass = (Class<? extends Packet>)packetIdToClassMap.lookup(3);
if (proxyClass.equals(Packet3Chat.class))
{
proxyClass = null;
}
packetIdToClassMap.removeObject(3);
packetIdToClassMap.addKey(3, HookChat.class);
Map packetClassToIdMap = PrivateFields.StaticFields.packetClassToIdMap.get();
packetClassToIdMap.put(HookChat.class, Integer.valueOf(3));
registered = true;
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}