-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
PacketOutHandler.java
251 lines (233 loc) · 12.3 KB
/
PacketOutHandler.java
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
package net.aufdemrand.denizen.utilities.packets.intercept;
import io.netty.buffer.Unpooled;
import net.aufdemrand.denizen.events.player.PlayerReceivesMessageScriptEvent;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.commands.server.ExecuteCommand;
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptHelper;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.PlayerProfileEditor;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.EntityFakePlayer;
import net.aufdemrand.denizen.utilities.entity.HideEntity;
import net.aufdemrand.denizen.utilities.packets.PacketHelper;
import net.aufdemrand.denizencore.objects.Element;
import net.minecraft.server.v1_9_R1.*;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.UUID;
public class PacketOutHandler {
/**
* Handles all packets going out from the server.
*
* @param player the player the packet is being sent to
* @param packet the client-bound packet
* @return whether to cancel sending the packet
*/
public static boolean handle(final EntityPlayer player, Packet packet) {
try {
if (packet instanceof PacketPlayOutChat) {
if (ExecuteCommand.silencedPlayers.contains(player.getUniqueID())) {
return true;
}
PacketPlayOutChat cPacket = (PacketPlayOutChat) packet;
int pos = chat_position.getInt(cPacket);
if (pos != 2) {
PlayerReceivesMessageScriptEvent event = PlayerReceivesMessageScriptEvent.instance;
event.message = new Element(((IChatBaseComponent) chat_message.get(cPacket)).c());
event.system = new Element(pos == 1);
event.player = dPlayer.mirrorBukkitPlayer(player.getBukkitEntity());
event.cancelled = false;
event.fire();
return event.cancelled;
}
}
else if (packet instanceof PacketPlayOutSetSlot) {
PacketPlayOutSetSlot ssPacket = (PacketPlayOutSetSlot) packet;
// int windowId = set_slot_windowId.getInt(ssPacket);
// int slotId = set_slot_slotId.getInt(ssPacket);
ItemStack itemStack = (ItemStack) set_slot_itemStack.get(ssPacket);
set_slot_itemStack.set(ssPacket, removeItemScriptLore(itemStack));
}
else if (packet instanceof PacketPlayOutWindowItems) {
PacketPlayOutWindowItems wiPacket = (PacketPlayOutWindowItems) packet;
// int windowId = set_slot_windowId.getInt(wiPacket);
ItemStack[] itemStacks = (ItemStack[]) window_items_itemStackArray.get(wiPacket);
for (int i = 0; i < itemStacks.length; i++) {
itemStacks[i] = removeItemScriptLore(itemStacks[i]);
}
window_items_itemStackArray.set(wiPacket, itemStacks);
}
else if (packet instanceof PacketPlayOutNamedEntitySpawn) {
PacketPlayOutNamedEntitySpawn nesPacket = (PacketPlayOutNamedEntitySpawn) packet;
int entityId = named_spawn_entityId.getInt(nesPacket);
if (entityIsHiding(player, entityId)) {
return true;
}
UUID entityUUID = (UUID) named_spawn_entityUUID.get(nesPacket);
final Entity entity = ((WorldServer) player.getWorld()).getEntity(entityUUID);
if (entity instanceof EntityFakePlayer) {
player.playerConnection.sendPacket(new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER,
(EntityFakePlayer) entity));
Bukkit.getScheduler().runTaskLater(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
player.playerConnection.sendPacket(new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER,
(EntityFakePlayer) entity));
}
}, 5);
}
}
else if (packet instanceof PacketPlayOutSpawnEntity) {
PacketPlayOutSpawnEntity sePacket = (PacketPlayOutSpawnEntity) packet;
int entityId = spawn_entityId.getInt(sePacket);
return entityIsHiding(player, entityId);
}
else if (packet instanceof PacketPlayOutSpawnEntityLiving) {
PacketPlayOutSpawnEntityLiving selPacket = (PacketPlayOutSpawnEntityLiving) packet;
int entityId = spawn_living_entityId.getInt(selPacket);
return entityIsHiding(player, entityId);
}
else if (packet instanceof PacketPlayOutSpawnEntityPainting) {
PacketPlayOutSpawnEntityPainting sepPacket = (PacketPlayOutSpawnEntityPainting) packet;
int entityId = spawn_painting_entityId.getInt(sepPacket);
return entityIsHiding(player, entityId);
}
else if (packet instanceof PacketPlayOutSpawnEntityExperienceOrb) {
PacketPlayOutSpawnEntityExperienceOrb seePacket = (PacketPlayOutSpawnEntityExperienceOrb) packet;
int entityId = spawn_experience_entityId.getInt(seePacket);
return entityIsHiding(player, entityId);
}
else if (packet instanceof PacketPlayOutPlayerInfo) {
PlayerProfileEditor.updatePlayerProfiles((PacketPlayOutPlayerInfo) packet);
}
else if (packet instanceof PacketPlayOutCustomPayload) {
PacketPlayOutCustomPayload cPacket = (PacketPlayOutCustomPayload) packet;
String name = (String) custom_name.get(cPacket);
if (name.equals("MC|TrList")) {
PacketDataSerializer serializer = (PacketDataSerializer) custom_serializer.get(cPacket);
PacketDataSerializer newSerializer = new PacketDataSerializer(Unpooled.buffer());
// Container number, we don't need this
newSerializer.writeInt(serializer.readInt());
// Number of trades
byte trades = serializer.readByte();
newSerializer.writeByte(trades);
// The trades
for (int i = 0; i < trades; i++) {
// The first item cost
ItemStack buyItem1 = serializer.i();
newSerializer.a(removeItemScriptLore(buyItem1));
// The item to be bought
ItemStack buyItem3 = serializer.i();
newSerializer.a(removeItemScriptLore(buyItem3));
// Whether there is a second item cost
boolean hasItem2 = serializer.readBoolean();
newSerializer.writeBoolean(hasItem2);
// The second item cost, if there is one
if (hasItem2) {
ItemStack buyItem2 = serializer.i();
newSerializer.a(removeItemScriptLore(buyItem2));
}
// Has used max times
boolean usedMax = serializer.readBoolean();
newSerializer.writeBoolean(usedMax);
// Current uses
int uses = serializer.readInt();
newSerializer.writeInt(uses);
// Max uses
int maxUses = serializer.readInt();
newSerializer.writeInt(maxUses);
}
custom_serializer.set(cPacket, newSerializer);
}
}
}
catch (Exception e) {
dB.echoError(e);
}
return false;
}
private static ItemStack removeItemScriptLore(ItemStack itemStack) throws Exception {
if (itemStack != null && itemStack.hasTag() && !itemStack.getTag().isEmpty()) {
NBTTagCompound tag = itemStack.getTag();
NBTTagCompound display = tag.getCompound("display");
NBTTagList lore = (NBTTagList) display.get("Lore");
if (lore == null || lore.isEmpty()) {
return itemStack;
}
String hash = null;
for (int i = 0; i < lore.size(); i++) {
String line = lore.getString(i);
if (line.startsWith(ItemScriptHelper.ItemScriptHashID)) {
hash = line;
lore.a(i);
break;
}
}
if (hash != null) {
display.set("Lore", lore);
tag.set("display", display);
tag.setString("Denizen Item Script", hash);
itemStack.setTag(tag);
}
}
return itemStack;
}
private static boolean entityIsHiding(EntityPlayer player, int entityId) {
UUID playerUUID = player.getUniqueID();
if (!HideEntity.hiddenEntities.containsKey(playerUUID)) {
return false;
}
EntityTracker tracker = ((WorldServer) player.world).tracker;
EntityTrackerEntry entry = tracker.trackedEntities.get(entityId);
if (entry == null) {
return false;
}
UUID entityUUID = entry.tracker.getUniqueID();
if (HideEntity.hiddenEntities.get(playerUUID).contains(entityUUID)) {
entry.clear(player);
return true;
}
return false;
}
//////////////////////////////////
//// Packet Fields
///////////
private static final Field chat_message, chat_position;
private static final Field set_slot_windowId, set_slot_slotId, set_slot_itemStack;
private static final Field window_items_windowId, window_items_itemStackArray;
private static final Field named_spawn_entityId, named_spawn_entityUUID;
private static final Field spawn_entityId;
private static final Field spawn_living_entityId;
private static final Field spawn_painting_entityId;
private static final Field spawn_experience_entityId;
private static final Field custom_name, custom_serializer;
static {
Map<String, Field> fields = PacketHelper.registerFields(PacketPlayOutChat.class);
chat_message = fields.get("a");
chat_position = fields.get("b");
fields = PacketHelper.registerFields(PacketPlayOutSetSlot.class);
set_slot_windowId = fields.get("a");
set_slot_slotId = fields.get("b");
set_slot_itemStack = fields.get("c");
fields = PacketHelper.registerFields(PacketPlayOutWindowItems.class);
window_items_windowId = fields.get("a");
window_items_itemStackArray = fields.get("b");
fields = PacketHelper.registerFields(PacketPlayOutNamedEntitySpawn.class);
named_spawn_entityId = fields.get("a");
named_spawn_entityUUID = fields.get("b");
fields = PacketHelper.registerFields(PacketPlayOutSpawnEntity.class);
spawn_entityId = fields.get("a"); // Other fields currently irrelevant
fields = PacketHelper.registerFields(PacketPlayOutSpawnEntityLiving.class);
spawn_living_entityId = fields.get("a"); // Other fields currently irrelevant
fields = PacketHelper.registerFields(PacketPlayOutSpawnEntityPainting.class);
spawn_painting_entityId = fields.get("a"); // Other fields currently irrelevant
fields = PacketHelper.registerFields(PacketPlayOutSpawnEntityExperienceOrb.class);
spawn_experience_entityId = fields.get("a"); // Other fields currently irrelevant
fields = PacketHelper.registerFields(PacketPlayOutCustomPayload.class);
custom_name = fields.get("a");
custom_serializer = fields.get("b");
}
}