summaryrefslogtreecommitdiffstats
path: root/nms-patches/PlayerConnection.patch
blob: 3ef338671d1a59bf6456fdaef070df7260f01d80 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
--- ../work/decompile-8eb82bde//net/minecraft/server/PlayerConnection.java	2015-01-06 20:20:56.507791620 +0000
+++ src/main/java/net/minecraft/server/PlayerConnection.java	2015-01-06 20:20:56.507791620 +0000
@@ -16,6 +16,48 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.HashSet;
+
+import org.bukkit.craftbukkit.entity.CraftPlayer;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.craftbukkit.inventory.CraftInventoryView;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.craftbukkit.util.CraftChatMessage;
+import org.bukkit.craftbukkit.util.LazyPlayerSet;
+import org.bukkit.craftbukkit.util.Waitable;
+
+import org.bukkit.Location;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Event;
+import org.bukkit.event.block.Action;
+import org.bukkit.event.block.SignChangeEvent;
+import org.bukkit.event.inventory.ClickType;
+import org.bukkit.event.inventory.CraftItemEvent;
+import org.bukkit.event.inventory.InventoryAction;
+import org.bukkit.event.inventory.InventoryClickEvent;
+import org.bukkit.event.inventory.InventoryCreativeEvent;
+import org.bukkit.event.inventory.InventoryType.SlotType;
+import org.bukkit.event.player.AsyncPlayerChatEvent;
+import org.bukkit.event.player.PlayerAnimationEvent;
+import org.bukkit.event.player.PlayerChatEvent;
+import org.bukkit.event.player.PlayerCommandPreprocessEvent;
+import org.bukkit.event.player.PlayerInteractEntityEvent;
+import org.bukkit.event.player.PlayerInteractAtEntityEvent;
+import org.bukkit.event.player.PlayerItemHeldEvent;
+import org.bukkit.event.player.PlayerKickEvent;
+import org.bukkit.event.player.PlayerMoveEvent;
+import org.bukkit.event.player.PlayerTeleportEvent;
+import org.bukkit.event.player.PlayerToggleFlightEvent;
+import org.bukkit.event.player.PlayerToggleSneakEvent;
+import org.bukkit.event.player.PlayerToggleSprintEvent;
+import org.bukkit.inventory.CraftingInventory;
+import org.bukkit.inventory.InventoryView;
+import org.bukkit.util.NumberConversions;
+// CraftBukkit end
+
 public class PlayerConnection implements PacketListenerPlayIn, IUpdatePlayerListBox {
 
     private static final Logger c = LogManager.getLogger();
@@ -29,13 +71,17 @@
     private int i;
     private long j;
     private long k;
-    private int chatThrottle;
+    // CraftBukkit start - multithreaded fields
+    private volatile int chatThrottle;
+    private static final AtomicIntegerFieldUpdater chatSpamField = AtomicIntegerFieldUpdater.newUpdater(PlayerConnection.class, "chatThrottle");
+    // CraftBukkit end
     private int m;
     private IntHashMap n = new IntHashMap();
     private double o;
     private double p;
     private double q;
     public boolean checkMovement = true;
+    private boolean processedDisconnect; // CraftBukkit - added
 
     public PlayerConnection(MinecraftServer minecraftserver, NetworkManager networkmanager, EntityPlayer entityplayer) {
         this.minecraftServer = minecraftserver;
@@ -43,8 +89,32 @@
         networkmanager.a((PacketListener) this);
         this.player = entityplayer;
         entityplayer.playerConnection = this;
+
+        // CraftBukkit start - add fields and methods
+        this.server = minecraftserver.server;
     }
 
+    private final org.bukkit.craftbukkit.CraftServer server;
+    private int lastTick = MinecraftServer.currentTick;
+    private int lastDropTick = MinecraftServer.currentTick;
+    private int dropCount = 0;
+    private static final int SURVIVAL_PLACE_DISTANCE_SQUARED = 6 * 6;
+    private static final int CREATIVE_PLACE_DISTANCE_SQUARED = 7 * 7;
+
+    // Get position of last block hit for BlockDamageLevel.STOPPED
+    private double lastPosX = Double.MAX_VALUE;
+    private double lastPosY = Double.MAX_VALUE;
+    private double lastPosZ = Double.MAX_VALUE;
+    private float lastPitch = Float.MAX_VALUE;
+    private float lastYaw = Float.MAX_VALUE;
+    private boolean justTeleported = false;
+
+    public CraftPlayer getPlayer() {
+        return (this.player == null) ? null : (CraftPlayer) this.player.getBukkitEntity();
+    }
+    private final static HashSet<Integer> invalidItems = new HashSet<Integer>(java.util.Arrays.asList(8, 9, 10, 11, 26, 34, 36, 43, 51, 52, 55, 59, 60, 62, 63, 64, 68, 71, 74, 75, 83, 90, 92, 93, 94, 104, 105, 115, 117, 118, 119, 125, 127, 132, 140, 141, 142, 144)); // TODO: Check after every update.
+    // CraftBukkit end
+
     public void c() {
         this.h = false;
         ++this.e;
@@ -57,9 +127,14 @@
         }
 
         this.minecraftServer.methodProfiler.b();
+        // CraftBukkit start
+        for (int spam; (spam = this.chatThrottle) > 0 && !chatSpamField.compareAndSet(this, spam, spam - 1); ) ;
+        /* Use thread-safe field access instead
         if (this.chatThrottle > 0) {
             --this.chatThrottle;
         }
+        */
+        // CraftBukkit end
 
         if (this.m > 0) {
             --this.m;
@@ -76,11 +151,28 @@
     }
 
     public void disconnect(String s) {
+        // CraftBukkit start - fire PlayerKickEvent
+        String leaveMessage = EnumChatFormat.YELLOW + this.player.getName() + " left the game.";
+
+        PlayerKickEvent event = new PlayerKickEvent(this.server.getPlayer(this.player), s, leaveMessage);
+
+        if (this.server.getServer().isRunning()) {
+            this.server.getPluginManager().callEvent(event);
+        }
+
+        if (event.isCancelled()) {
+            // Do not kick the player
+            return;
+        }
+        // Send the possibly modified leave message
+        s = event.getReason();
+        // CraftBukkit end
         ChatComponentText chatcomponenttext = new ChatComponentText(s);
 
         this.networkManager.a(new PacketPlayOutKickDisconnect(chatcomponenttext), new PlayerConnectionFuture(this, chatcomponenttext), new GenericFutureListener[0]);
+        this.a(chatcomponenttext); // CraftBukkit - fire quit instantly
         this.networkManager.k();
-        Futures.getUnchecked(this.minecraftServer.postToMainThread(new PlayerConnectionDisconnector(this)));
+        this.minecraftServer.postToMainThread(new PlayerConnectionDisconnector(this)); // CraftBukkit - Don't wait
     }
 
     public void a(PacketPlayInSteerVehicle packetplayinsteervehicle) {
@@ -90,6 +182,13 @@
 
     public void a(PacketPlayInFlying packetplayinflying) {
         PlayerConnectionUtils.ensureMainThread(packetplayinflying, this, this.player.u());
+        // CraftBukkit start - Check for NaN
+        if (Double.isNaN(packetplayinflying.x) || Double.isNaN(packetplayinflying.y) || Double.isNaN(packetplayinflying.z)) {
+            c.warn(player.getName() + " was caught trying to crash the server with an invalid position.");
+            getPlayer().kickPlayer("Nope!");
+            return;
+        }
+        // CraftBukkit end
         WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
 
         this.h = true;
@@ -108,8 +207,65 @@
                     this.checkMovement = true;
                 }
             }
+            // CraftBukkit start - fire PlayerMoveEvent
+            Player player = this.getPlayer();
+            Location from = new Location(player.getWorld(), lastPosX, lastPosY, lastPosZ, lastYaw, lastPitch); // Get the Players previous Event location.
+            Location to = player.getLocation().clone(); // Start off the To location as the Players current location.
+
+            // If the packet contains movement information then we update the To location with the correct XYZ.
+            if (packetplayinflying.hasPos && !(packetplayinflying.hasPos && packetplayinflying.y == -999.0D)) {
+                to.setX(packetplayinflying.x);
+                to.setY(packetplayinflying.y);
+                to.setZ(packetplayinflying.z);
+            }
+
+            // If the packet contains look information then we update the To location with the correct Yaw & Pitch.
+            if (packetplayinflying.hasLook) {
+                to.setYaw(packetplayinflying.yaw);
+                to.setPitch(packetplayinflying.pitch);
+            }
+
+            // Prevent 40 event-calls for less than a single pixel of movement >.>
+            double delta = Math.pow(this.lastPosX - to.getX(), 2) + Math.pow(this.lastPosY - to.getY(), 2) + Math.pow(this.lastPosZ - to.getZ(), 2);
+            float deltaAngle = Math.abs(this.lastYaw - to.getYaw()) + Math.abs(this.lastPitch - to.getPitch());
+
+            if ((delta > 1f / 256 || deltaAngle > 10f) && (this.checkMovement && !this.player.dead)) {
+                this.lastPosX = to.getX();
+                this.lastPosY = to.getY();
+                this.lastPosZ = to.getZ();
+                this.lastYaw = to.getYaw();
+                this.lastPitch = to.getPitch();
+
+                // Skip the first time we do this
+                if (from.getX() != Double.MAX_VALUE) {
+                    PlayerMoveEvent event = new PlayerMoveEvent(player, from, to);
+                    this.server.getPluginManager().callEvent(event);
+
+                    // If the event is cancelled we move the player back to their old location.
+                    if (event.isCancelled()) {
+                        this.player.playerConnection.sendPacket(new PacketPlayOutPosition(from.getX(), from.getY(), from.getZ(), from.getYaw(), from.getPitch(), Collections.emptySet()));
+                        return;
+                    }
+
+                    /* If a Plugin has changed the To destination then we teleport the Player
+                    there to avoid any 'Moved wrongly' or 'Moved too quickly' errors.
+                    We only do this if the Event was not cancelled. */
+                    if (!to.equals(event.getTo()) && !event.isCancelled()) {
+                        this.player.getBukkitEntity().teleport(event.getTo(), PlayerTeleportEvent.TeleportCause.UNKNOWN);
+                        return;
+                    }
+
+                    /* Check to see if the Players Location has some how changed during the call of the event.
+                    This can happen due to a plugin teleporting the player instead of using .setTo() */
+                    if (!from.equals(this.getPlayer().getLocation()) && this.justTeleported) {
+                        this.justTeleported = false;
+                        return;
+                    }
+                }
+            }
 
-            if (this.checkMovement) {
+            if (this.checkMovement && !this.player.dead) {
+                // CraftBukkit end
                 this.f = this.e;
                 double d7;
                 double d8;
@@ -203,12 +359,14 @@
                 double d11 = d7 - this.player.locX;
                 double d12 = d8 - this.player.locY;
                 double d13 = d9 - this.player.locZ;
-                double d14 = Math.min(Math.abs(d11), Math.abs(this.player.motX));
-                double d15 = Math.min(Math.abs(d12), Math.abs(this.player.motY));
-                double d16 = Math.min(Math.abs(d13), Math.abs(this.player.motZ));
+                // CraftBukkit start - min to max
+                double d14 = Math.max(Math.abs(d11), Math.abs(this.player.motX));
+                double d15 = Math.max(Math.abs(d12), Math.abs(this.player.motY));
+                double d16 = Math.max(Math.abs(d13), Math.abs(this.player.motZ));
+                // CraftBukkit end
                 double d17 = d14 * d14 + d15 * d15 + d16 * d16;
 
-                if (d17 > 100.0D && (!this.minecraftServer.S() || !this.minecraftServer.R().equals(this.player.getName()))) {
+                if (d17 > 100.0D && this.checkMovement && (!this.minecraftServer.S() || !this.minecraftServer.R().equals(this.player.getName()))) { // CraftBukkit - Added this.checkMovement condition to solve this check being triggered by teleports
                     PlayerConnection.c.warn(this.player.getName() + " moved too quickly! " + d11 + "," + d12 + "," + d13 + " (" + d14 + ", " + d15 + ", " + d16 + ")");
                     this.a(this.o, this.p, this.q, this.player.yaw, this.player.pitch);
                     return;
@@ -281,6 +439,49 @@
     }
 
     public void a(double d0, double d1, double d2, float f, float f1, Set set) {
+        // CraftBukkit start - Delegate to teleport(Location)
+        Player player = this.getPlayer();
+        Location from = player.getLocation();
+        Location to = new Location(this.getPlayer().getWorld(), d0, d1, d2, f, f1);
+        PlayerTeleportEvent event = new PlayerTeleportEvent(player, from, to, PlayerTeleportEvent.TeleportCause.UNKNOWN);
+        this.server.getPluginManager().callEvent(event);
+
+        from = event.getFrom();
+        to = event.isCancelled() ? from : event.getTo();
+
+        this.teleport(to, set);
+    }    
+    
+    public void teleport(Location dest) {
+        teleport(dest, Collections.emptySet());
+    }
+
+    public void teleport(Location dest, Set set) {
+        double d0, d1, d2;
+        float f, f1;
+
+        d0 = dest.getX();
+        d1 = dest.getY();
+        d2 = dest.getZ();
+        f = dest.getYaw();
+        f1 = dest.getPitch();
+
+        // TODO: make sure this is the best way to address this.
+        if (Float.isNaN(f)) {
+            f = 0;
+        }
+
+        if (Float.isNaN(f1)) {
+            f1 = 0;
+        }
+
+        this.lastPosX = d0;
+        this.lastPosY = d1;
+        this.lastPosZ = d2;
+        this.lastYaw = f;
+        this.lastPitch = f1;
+        this.justTeleported = true;
+        // CraftBukkit end
         this.checkMovement = false;
         this.o = d0;
         this.p = d1;
@@ -314,32 +515,49 @@
 
     public void a(PacketPlayInBlockDig packetplayinblockdig) {
         PlayerConnectionUtils.ensureMainThread(packetplayinblockdig, this, this.player.u());
+        if (this.player.dead) return; // CraftBukkit
         WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
         BlockPosition blockposition = packetplayinblockdig.a();
 
         this.player.z();
+        // CraftBukkit start
         switch (SwitchHelperCommandActionType.a[packetplayinblockdig.c().ordinal()]) {
-        case 1:
+        case 1: // DROP_ITEM
             if (!this.player.v()) {
+                // limit how quickly items can be dropped
+                // If the ticks aren't the same then the count starts from 0 and we update the lastDropTick.
+                if (this.lastDropTick != MinecraftServer.currentTick) {
+                    this.dropCount = 0;
+                    this.lastDropTick = MinecraftServer.currentTick;
+                } else {
+                    // Else we increment the drop count and check the amount.
+                    this.dropCount++;
+                    if (this.dropCount >= 20) {
+                        this.c.warn(this.player.getName() + " dropped their items too quickly!");
+                        this.disconnect("You dropped your items too quickly (Hacking?)");
+                        return;
+                    }
+                }
+                // CraftBukkit end
                 this.player.a(false);
             }
 
             return;
 
-        case 2:
+        case 2: // DROP_ALL_ITEMS
             if (!this.player.v()) {
                 this.player.a(true);
             }
 
             return;
 
-        case 3:
+        case 3: // RELEASE_USE_ITEM
             this.player.bT();
             return;
 
-        case 4:
-        case 5:
-        case 6:
+        case 4: // START_DESTROY_BLOCK
+        case 5: // ABORT_DESTROY_BLOCK
+        case 6: // STOP_DESTROY_BLOCK
             double d0 = this.player.locX - ((double) blockposition.getX() + 0.5D);
             double d1 = this.player.locY - ((double) blockposition.getY() + 0.5D) + 1.5D;
             double d2 = this.player.locZ - ((double) blockposition.getZ() + 0.5D);
@@ -354,7 +572,15 @@
                     if (!this.minecraftServer.a(worldserver, blockposition, this.player) && worldserver.af().a(blockposition)) {
                         this.player.playerInteractManager.a(blockposition, packetplayinblockdig.b());
                     } else {
+                        // CraftBukkit start - fire PlayerInteractEvent
+                        CraftEventFactory.callPlayerInteractEvent(this.player, Action.LEFT_CLICK_BLOCK, blockposition, packetplayinblockdig.b(), this.player.inventory.getItemInHand());
                         this.player.playerConnection.sendPacket(new PacketPlayOutBlockChange(worldserver, blockposition));
+                        // Update any tile entity data for this block
+                        TileEntity tileentity = worldserver.getTileEntity(blockposition);
+                        if (tileentity != null) {
+                            this.player.playerConnection.sendPacket(tileentity.getUpdatePacket());
+                        }
+                        // CraftBukkit end
                     }
                 } else {
                     if (packetplayinblockdig.c() == EnumPlayerDigType.STOP_DESTROY_BLOCK) {
@@ -374,11 +600,22 @@
         default:
             throw new IllegalArgumentException("Invalid player action");
         }
+        // CraftBukkit end
     }
 
     public void a(PacketPlayInBlockPlace packetplayinblockplace) {
         PlayerConnectionUtils.ensureMainThread(packetplayinblockplace, this, this.player.u());
         WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
+        
+        // CraftBukkit start
+        if (this.player.dead) return;
+        
+        // CraftBukkit - if rightclick decremented the item, always send the update packet. */
+        // this is not here for CraftBukkit's own functionality; rather it is to fix
+        // a notch bug where the item doesn't update correctly.
+        boolean always = false;
+        // CraftBukkit end
+        
         ItemStack itemstack = this.player.inventory.getItemInHand();
         boolean flag = false;
         BlockPosition blockposition = packetplayinblockplace.a();
@@ -390,7 +627,50 @@
                 return;
             }
 
-            this.player.playerInteractManager.useItem(this.player, worldserver, itemstack);
+            // CraftBukkit start
+            int itemstackAmount = itemstack.count;
+            
+            // Raytrace to look for 'rogue armswings'
+            float f1 = this.player.pitch;
+            float f2 = this.player.yaw;
+            double d0 = this.player.locX;
+            double d1 = this.player.locY + (double) this.player.getHeadHeight();
+            double d2 = this.player.locZ;
+            Vec3D vec3d = new Vec3D(d0, d1, d2);
+
+            float f3 = MathHelper.cos(-f2 * 0.017453292F - 3.1415927F);
+            float f4 = MathHelper.sin(-f2 * 0.017453292F - 3.1415927F);
+            float f5 = -MathHelper.cos(-f1 * 0.017453292F);
+            float f6 = MathHelper.sin(-f1 * 0.017453292F);
+            float f7 = f4 * f5;
+            float f8 = f3 * f5;
+            double d3 = player.playerInteractManager.getGameMode() == EnumGamemode.CREATIVE ? 5.0D : 4.5D;
+            Vec3D vec3d1 = vec3d.add((double) f7 * d3, (double) f6 * d3, (double) f8 * d3);
+            MovingObjectPosition movingobjectposition = this.player.world.rayTrace(vec3d, vec3d1, false);
+
+            boolean cancelled = false;
+            if (movingobjectposition == null || movingobjectposition.type != EnumMovingObjectType.BLOCK) {
+                org.bukkit.event.player.PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(this.player, Action.RIGHT_CLICK_AIR, itemstack);
+                cancelled = event.useItemInHand() == Event.Result.DENY;
+            } else {
+                if (player.playerInteractManager.firedInteract) {
+                    player.playerInteractManager.firedInteract = false;
+                    cancelled = player.playerInteractManager.interactResult;
+                } else {
+                    org.bukkit.event.player.PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, movingobjectposition.a(), movingobjectposition.direction, itemstack, true);
+                    cancelled = event.useItemInHand() == Event.Result.DENY;
+                }
+            }
+
+            if (!cancelled) {
+                this.player.playerInteractManager.useItem(this.player, this.player.world, itemstack);
+            }
+
+            // CraftBukkit - notch decrements the counter by 1 in the above method with food,
+            // snowballs and so forth, but he does it in a place that doesn't cause the
+            // inventory update packet to get sent
+            always = (itemstack.count != itemstackAmount) || itemstack.getItem() == Item.getItemOf(Blocks.WATERLILY);
+            // CraftBukkit end
         } else if (blockposition.getY() >= this.minecraftServer.getMaxBuildHeight() - 1 && (enumdirection == EnumDirection.UP || blockposition.getY() >= this.minecraftServer.getMaxBuildHeight())) {
             ChatMessage chatmessage = new ChatMessage("build.tooHigh", new Object[] { Integer.valueOf(this.minecraftServer.getMaxBuildHeight())});
 
@@ -398,9 +678,21 @@
             this.player.playerConnection.sendPacket(new PacketPlayOutChat(chatmessage));
             flag = true;
         } else {
+            // CraftBukkit start - Check if we can actually do something over this large a distance
+            Location eyeLoc = this.getPlayer().getEyeLocation();
+            double reachDistance = NumberConversions.square(eyeLoc.getX() - blockposition.getX()) + NumberConversions.square(eyeLoc.getY() - blockposition.getY()) + NumberConversions.square(eyeLoc.getZ() - blockposition.getZ());
+            if (reachDistance > (this.getPlayer().getGameMode() == org.bukkit.GameMode.CREATIVE ? CREATIVE_PLACE_DISTANCE_SQUARED : SURVIVAL_PLACE_DISTANCE_SQUARED)) {
+                return;
+            }
+            
+            if (!worldserver.af().a(blockposition)) {
+                return;
+            }
+
             if (this.checkMovement && this.player.e((double) blockposition.getX() + 0.5D, (double) blockposition.getY() + 0.5D, (double) blockposition.getZ() + 0.5D) < 64.0D && !this.minecraftServer.a(worldserver, blockposition, this.player) && worldserver.af().a(blockposition)) {
-                this.player.playerInteractManager.interact(this.player, worldserver, itemstack, blockposition, enumdirection, packetplayinblockplace.d(), packetplayinblockplace.e(), packetplayinblockplace.f());
+                always = !this.player.playerInteractManager.interact(this.player, worldserver, itemstack, blockposition, enumdirection, packetplayinblockplace.d(), packetplayinblockplace.e(), packetplayinblockplace.f());
             }
+            // CraftBukkit end
 
             flag = true;
         }
@@ -423,7 +715,8 @@
 
             this.player.activeContainer.b();
             this.player.g = false;
-            if (!ItemStack.matches(this.player.inventory.getItemInHand(), packetplayinblockplace.getItemStack())) {
+            // CraftBukkit - TODO CHECK IF NEEDED -- new if structure might not need 'always'. Kept it in for now, but may be able to remove in future
+            if (!ItemStack.matches(this.player.inventory.getItemInHand(), packetplayinblockplace.getItemStack()) || always) {
                 this.sendPacket(new PacketPlayOutSetSlot(this.player.activeContainer.windowId, slot.rawSlotIndex, this.player.inventory.getItemInHand()));
             }
         }
@@ -437,8 +730,8 @@
             WorldServer[] aworldserver = this.minecraftServer.worldServer;
             int i = aworldserver.length;
 
-            for (int j = 0; j < i; ++j) {
-                WorldServer worldserver = aworldserver[j];
+            // CraftBukkit - use the worlds array list
+            for (WorldServer worldserver : minecraftServer.worlds) {
 
                 if (worldserver != null) {
                     entity = packetplayinspectate.a(worldserver);
@@ -451,10 +744,11 @@
             if (entity != null) {
                 this.player.e((Entity) this.player);
                 this.player.mount((Entity) null);
+
+                /* CraftBukkit start - replace with bukkit handling for multi-world
                 if (entity.world != this.player.world) {
                     WorldServer worldserver1 = this.player.u();
                     WorldServer worldserver2 = (WorldServer) entity.world;
-
                     this.player.dimension = entity.dimension;
                     this.sendPacket(new PacketPlayOutRespawn(this.player.dimension, worldserver1.getDifficulty(), worldserver1.getWorldData().getType(), this.player.playerInteractManager.getGameMode()));
                     worldserver1.removeEntity(this.player);
@@ -475,6 +769,9 @@
                 } else {
                     this.player.enderTeleportTo(entity.locX, entity.locY, entity.locZ);
                 }
+                */
+                this.player.getBukkitEntity().teleport(entity.getBukkitEntity(), PlayerTeleportEvent.TeleportCause.SPECTATE);
+                // CraftBukkit end
             }
         }
 
@@ -483,14 +780,29 @@
     public void a(PacketPlayInResourcePackStatus packetplayinresourcepackstatus) {}
 
     public void a(IChatBaseComponent ichatbasecomponent) {
-        PlayerConnection.c.info(this.player.getName() + " lost connection: " + ichatbasecomponent);
+        // CraftBukkit start - Rarely it would send a disconnect line twice
+        if (this.processedDisconnect) {
+            return;
+        } else {
+            this.processedDisconnect = true;
+        }
+        // CraftBukkit end
+        c.info(this.player.getName() + " lost connection: " + ichatbasecomponent.c()); // CraftBukkit - Don't toString the component
         this.minecraftServer.aF();
+        // CraftBukkit start - Replace vanilla quit message handling with our own.
+        /*
         ChatMessage chatmessage = new ChatMessage("multiplayer.player.left", new Object[] { this.player.getScoreboardDisplayName()});
 
         chatmessage.getChatModifier().setColor(EnumChatFormat.YELLOW);
         this.minecraftServer.getPlayerList().sendMessage(chatmessage);
+        */
+        
         this.player.q();
-        this.minecraftServer.getPlayerList().disconnect(this.player);
+        String quitMessage = this.minecraftServer.getPlayerList().disconnect(this.player);
+        if ((quitMessage != null) && (quitMessage.length() > 0)) {
+            this.minecraftServer.getPlayerList().sendMessage(CraftChatMessage.fromString(quitMessage));
+        }
+        // CraftBukkit end
         if (this.minecraftServer.S() && this.player.getName().equals(this.minecraftServer.R())) {
             PlayerConnection.c.info("Stopping singleplayer server as player logged out");
             this.minecraftServer.safeShutdown();
@@ -511,6 +823,15 @@
                 return;
             }
         }
+ 
+        // CraftBukkit start
+        if (packet == null) {
+            return;
+        } else if (packet instanceof PacketPlayOutSpawnPosition) {
+            PacketPlayOutSpawnPosition packet6 = (PacketPlayOutSpawnPosition) packet;
+            this.player.compassTarget = new Location(this.getPlayer().getWorld(), packet6.position.getX(), packet6.position.getY(), packet6.position.getZ());
+        }
+        // CraftBukkit end
 
         try {
             this.networkManager.handle(packet);
@@ -524,18 +845,34 @@
     }
 
     public void a(PacketPlayInHeldItemSlot packetplayinhelditemslot) {
+        // CraftBukkit start
+        if (this.player.dead) return;
         PlayerConnectionUtils.ensureMainThread(packetplayinhelditemslot, this, this.player.u());
         if (packetplayinhelditemslot.a() >= 0 && packetplayinhelditemslot.a() < PlayerInventory.getHotbarSize()) {
+            PlayerItemHeldEvent event = new PlayerItemHeldEvent(this.getPlayer(), this.player.inventory.itemInHandIndex, packetplayinhelditemslot.a());
+            this.server.getPluginManager().callEvent(event);
+            if (event.isCancelled()) {
+                this.sendPacket(new PacketPlayOutHeldItemSlot(this.player.inventory.itemInHandIndex));
+                this.player.z(); // RENAME
+                return;
+            }
+            // CraftBukkit end
             this.player.inventory.itemInHandIndex = packetplayinhelditemslot.a();
             this.player.z();
         } else {
             PlayerConnection.c.warn(this.player.getName() + " tried to set an invalid carried item");
+            this.disconnect("Nope!"); // CraftBukkit
         }
     }
 
     public void a(PacketPlayInChat packetplayinchat) {
-        PlayerConnectionUtils.ensureMainThread(packetplayinchat, this, this.player.u());
-        if (this.player.getChatFlags() == EnumChatVisibility.HIDDEN) {
+        // CraftBukkit start - async chat
+        boolean isSync = packetplayinchat.a().startsWith("/");
+        if (packetplayinchat.a().startsWith("/")) {
+            PlayerConnectionUtils.ensureMainThread(packetplayinchat, this, this.player.u()); 
+        }
+        // CraftBukkit end
+        if (this.player.dead || this.player.getChatFlags() == EnumChatVisibility.HIDDEN) { // CraftBukkit - dead men tell no tales
             ChatMessage chatmessage = new ChatMessage("chat.cannotSend", new Object[0]);
 
             chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
@@ -548,39 +885,247 @@
 
             for (int i = 0; i < s.length(); ++i) {
                 if (!SharedConstants.isAllowedChatCharacter(s.charAt(i))) {
-                    this.disconnect("Illegal characters in chat");
+                    // CraftBukkit start - threadsafety
+                    if (!isSync) {
+                        Waitable waitable = new Waitable() {
+                            @Override
+                            protected Object evaluate() {
+                                PlayerConnection.this.disconnect("Illegal characters in chat");
+                                return null;
+                            }
+                        };
+
+                        this.minecraftServer.processQueue.add(waitable);
+
+                        try {
+                            waitable.get();
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                        } catch (ExecutionException e) {
+                            throw new RuntimeException(e);
+                        }
+                    } else {
+                        this.disconnect("Illegal characters in chat");
+                    }
+                    // CraftBukkit end
                     return;
                 }
             }
 
-            if (s.startsWith("/")) {
-                this.handleCommand(s);
+            // CraftBukkit start
+            if (isSync) {
+                try {
+                    this.minecraftServer.server.playerCommandState = true;
+                    this.handleCommand(s);
+                } finally {
+                    this.minecraftServer.server.playerCommandState = false;
+                }
+            } else if (s.isEmpty()) {
+                c.warn(this.player.getName() + " tried to send an empty message");
+            } else if (getPlayer().isConversing()) {
+                getPlayer().acceptConversationInput(s);
+            } else if (this.player.getChatFlags() == EnumChatVisibility.SYSTEM) { // Re-add "Command Only" flag check
+                ChatMessage chatmessage = new ChatMessage("chat.cannotSend", new Object[0]);
+
+                chatmessage.getChatModifier().setColor(EnumChatFormat.RED);
+                this.sendPacket(new PacketPlayOutChat(chatmessage));
+            } else if (true) {
+                this.chat(s, true);
+                // CraftBukkit end - the below is for reference. :)
             } else {
                 ChatMessage chatmessage1 = new ChatMessage("chat.type.text", new Object[] { this.player.getScoreboardDisplayName(), s});
 
                 this.minecraftServer.getPlayerList().sendMessage(chatmessage1, false);
             }
 
-            this.chatThrottle += 20;
-            if (this.chatThrottle > 200 && !this.minecraftServer.getPlayerList().isOp(this.player.getProfile())) {
-                this.disconnect("disconnect.spam");
-            }
+            // CraftBukkit start - replaced with thread safe throttle
+            // this.chatThrottle += 20;
+            if (chatSpamField.addAndGet(this, 20) > 200 && !this.minecraftServer.getPlayerList().isOp(this.player.getProfile())) {
+                if (!isSync) {
+                    Waitable waitable = new Waitable() {
+                        @Override
+                        protected Object evaluate() {
+                            PlayerConnection.this.disconnect("disconnect.spam");
+                            return null;
+                        }
+                    };
+
+                    this.minecraftServer.processQueue.add(waitable);
+
+                    try {
+                        waitable.get();
+                    } catch (InterruptedException e) {
+                        Thread.currentThread().interrupt();
+                    } catch (ExecutionException e) {
+                        throw new RuntimeException(e);
+                    }
+                } else {
+                    this.disconnect("disconnect.spam");
+                }
+                // CraftBukkit end
+            }   
+        }
+    }
+    
+    // CraftBukkit start - add method
+    public void chat(String s, boolean async) {
+        if (s.isEmpty() || this.player.getChatFlags() == EnumChatVisibility.HIDDEN) {
+            return;
+        }
+
+        if (!async && s.startsWith("/")) {
+            this.handleCommand(s);
+        } else if (this.player.getChatFlags() == EnumChatVisibility.SYSTEM) {
+            // Do nothing, this is coming from a plugin
+        } else {
+            Player player = this.getPlayer();
+            AsyncPlayerChatEvent event = new AsyncPlayerChatEvent(async, player, s, new LazyPlayerSet());
+            this.server.getPluginManager().callEvent(event);
+
+            if (PlayerChatEvent.getHandlerList().getRegisteredListeners().length != 0) {
+                // Evil plugins still listening to deprecated event
+                final PlayerChatEvent queueEvent = new PlayerChatEvent(player, event.getMessage(), event.getFormat(), event.getRecipients());
+                queueEvent.setCancelled(event.isCancelled());
+                Waitable waitable = new Waitable() {
+                    @Override
+                    protected Object evaluate() {
+                        org.bukkit.Bukkit.getPluginManager().callEvent(queueEvent);
+
+                        if (queueEvent.isCancelled()) {
+                            return null;
+                        }
+
+                        String message = String.format(queueEvent.getFormat(), queueEvent.getPlayer().getDisplayName(), queueEvent.getMessage());
+                        PlayerConnection.this.minecraftServer.console.sendMessage(message);
+                        if (((LazyPlayerSet) queueEvent.getRecipients()).isLazy()) {
+                            for (Object player : PlayerConnection.this.minecraftServer.getPlayerList().players) {
+                                ((EntityPlayer) player).sendMessage(CraftChatMessage.fromString(message));
+                            }
+                        } else {
+                            for (Player player : queueEvent.getRecipients()) {
+                                player.sendMessage(message);
+                            }
+                        }
+                        return null;
+                    }};
+                if (async) {
+                    minecraftServer.processQueue.add(waitable);
+                } else {
+                    waitable.run();
+                }
+                try {
+                    waitable.get();
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt(); // This is proper habit for java. If we aren't handling it, pass it on!
+                } catch (ExecutionException e) {
+                    throw new RuntimeException("Exception processing chat event", e.getCause());
+                }
+            } else {
+                if (event.isCancelled()) {
+                    return;
+                }
 
+                s = String.format(event.getFormat(), event.getPlayer().getDisplayName(), event.getMessage());
+                minecraftServer.console.sendMessage(s);
+                if (((LazyPlayerSet) event.getRecipients()).isLazy()) {
+                    for (Object recipient : minecraftServer.getPlayerList().players) {
+                        ((EntityPlayer) recipient).sendMessage(CraftChatMessage.fromString(s));
+                    }
+                } else {
+                    for (Player recipient : event.getRecipients()) {
+                        recipient.sendMessage(s);
+                    }
+                }
+            }
         }
     }
+    // CraftBukkit end
 
     private void handleCommand(String s) {
-        this.minecraftServer.getCommandHandler().a(this.player, s);
+       // CraftBukkit start - whole method
+        this.c.info(this.player.getName() + " issued server command: " + s);
+
+        CraftPlayer player = this.getPlayer();
+
+        PlayerCommandPreprocessEvent event = new PlayerCommandPreprocessEvent(player, s, new LazyPlayerSet());
+        this.server.getPluginManager().callEvent(event);
+
+        if (event.isCancelled()) {
+            return;
+        }
+
+        try {
+            if (this.server.dispatchCommand(event.getPlayer(), event.getMessage().substring(1))) {
+                return;
+            }
+        } catch (org.bukkit.command.CommandException ex) {
+            player.sendMessage(org.bukkit.ChatColor.RED + "An internal error occurred while attempting to perform this command");
+            java.util.logging.Logger.getLogger(PlayerConnection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+            return;
+        }
+        // this.minecraftServer.getCommandHandler().a(this.player, s);
+        // CraftBukkit end
     }
 
     public void a(PacketPlayInArmAnimation packetplayinarmanimation) {
+        if (this.player.dead) return; // CraftBukkit
         PlayerConnectionUtils.ensureMainThread(packetplayinarmanimation, this, this.player.u());
         this.player.z();
+        // CraftBukkit start - Raytrace to look for 'rogue armswings'
+        float f1 = this.player.pitch;
+        float f2 = this.player.yaw;
+        double d0 = this.player.locX;
+        double d1 = this.player.locY + (double) this.player.getHeadHeight();
+        double d2 = this.player.locZ;
+        Vec3D vec3d = new Vec3D(d0, d1, d2);
+
+        float f3 = MathHelper.cos(-f2 * 0.017453292F - 3.1415927F);
+        float f4 = MathHelper.sin(-f2 * 0.017453292F - 3.1415927F);
+        float f5 = -MathHelper.cos(-f1 * 0.017453292F);
+        float f6 = MathHelper.sin(-f1 * 0.017453292F);
+        float f7 = f4 * f5;
+        float f8 = f3 * f5;
+        double d3 = player.playerInteractManager.getGameMode() == EnumGamemode.CREATIVE ? 5.0D : 4.5D;
+        Vec3D vec3d1 = vec3d.add((double) f7 * d3, (double) f6 * d3, (double) f8 * d3);
+        MovingObjectPosition movingobjectposition = this.player.world.rayTrace(vec3d, vec3d1, false);
+
+        if (movingobjectposition == null || movingobjectposition.type != EnumMovingObjectType.BLOCK) {
+            CraftEventFactory.callPlayerInteractEvent(this.player, Action.LEFT_CLICK_AIR, this.player.inventory.getItemInHand());
+        }
+
+        // Arm swing animation
+        PlayerAnimationEvent event = new PlayerAnimationEvent(this.getPlayer());
+        this.server.getPluginManager().callEvent(event);
+
+        if (event.isCancelled()) return;
+        // CraftBukkit end
         this.player.bv();
     }
 
     public void a(PacketPlayInEntityAction packetplayinentityaction) {
         PlayerConnectionUtils.ensureMainThread(packetplayinentityaction, this, this.player.u());
+        // CraftBukkit start
+        if (this.player.dead) return;
+        switch (packetplayinentityaction.b()) {
+            case START_SNEAKING:
+            case STOP_SNEAKING:                
+                PlayerToggleSneakEvent event = new PlayerToggleSneakEvent(this.getPlayer(), packetplayinentityaction.b() == EnumPlayerAction.START_SNEAKING);
+                this.server.getPluginManager().callEvent(event);
+
+                if (event.isCancelled()) {
+                    return;
+                }
+                break;
+            case START_SPRINTING:
+            case STOP_SPRINTING:                
+                PlayerToggleSprintEvent e2 = new PlayerToggleSprintEvent(this.getPlayer(), packetplayinentityaction.b() == EnumPlayerAction.START_SPRINTING);
+                this.server.getPluginManager().callEvent(e2);
+
+                if (e2.isCancelled()) {
+                    return;
+                }
+                break;
+        }
         this.player.z();
         switch (SwitchHelperCommandActionType.b[packetplayinentityaction.b().ordinal()]) {
         case 1:
@@ -601,7 +1146,7 @@
 
         case 5:
             this.player.a(false, true, true);
-            this.checkMovement = false;
+            // this.checkMovement = false; // CraftBukkit - this is handled in teleport
             break;
 
         case 6:
@@ -623,6 +1168,7 @@
     }
 
     public void a(PacketPlayInUseEntity packetplayinuseentity) {
+        if (this.player.dead) return; // CraftBukkit
         PlayerConnectionUtils.ensureMainThread(packetplayinuseentity, this, this.player.u());
         WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
         Entity entity = packetplayinuseentity.a((World) worldserver);
@@ -637,18 +1183,72 @@
             }
 
             if (this.player.h(entity) < d0) {
+                ItemStack itemInHand = this.player.inventory.getItemInHand(); // CraftBukkit
+                
+                if (packetplayinuseentity.a() == EnumEntityUseAction.INTERACT 
+                        || packetplayinuseentity.a() == EnumEntityUseAction.INTERACT_AT) {
+                    // CraftBukkit start
+                    boolean triggerTagUpdate = itemInHand != null && itemInHand.getItem() == Items.NAME_TAG && entity instanceof EntityInsentient;
+                    boolean triggerChestUpdate = itemInHand != null && itemInHand.getItem() == Item.getItemOf(Blocks.CHEST) && entity instanceof EntityHorse;
+                    boolean triggerLeashUpdate = itemInHand != null && itemInHand.getItem() == Items.LEAD && entity instanceof EntityInsentient;
+                    PlayerInteractEntityEvent event;
+                    if (packetplayinuseentity.a() == EnumEntityUseAction.INTERACT) {
+                        event = new PlayerInteractEntityEvent((Player) this.getPlayer(), entity.getBukkitEntity());
+                    } else {
+                        Vec3D target = packetplayinuseentity.b();
+                        event = new PlayerInteractAtEntityEvent((Player) this.getPlayer(), entity.getBukkitEntity(), new org.bukkit.util.Vector(target.a, target.b, target.c));
+                    }
+                    this.server.getPluginManager().callEvent(event);
+
+                    if (triggerLeashUpdate && (event.isCancelled() || this.player.inventory.getItemInHand() == null || this.player.inventory.getItemInHand().getItem() != Items.LEAD)) {
+                        // Refresh the current leash state
+                        this.sendPacket(new PacketPlayOutAttachEntity(1, entity, ((EntityInsentient) entity).getLeashHolder()));
+                    }
+
+                    if (triggerTagUpdate && (event.isCancelled() || this.player.inventory.getItemInHand() == null || this.player.inventory.getItemInHand().getItem() != Items.NAME_TAG)) {
+                        // Refresh the current entity metadata
+                        this.sendPacket(new PacketPlayOutEntityMetadata(entity.getId(), entity.datawatcher, true));
+                    }
+                    if (triggerChestUpdate && (event.isCancelled() || this.player.inventory.getItemInHand() == null || this.player.inventory.getItemInHand().getItem() != Item.getItemOf(Blocks.CHEST))) {
+                        this.sendPacket(new PacketPlayOutEntityMetadata(entity.getId(), entity.datawatcher, true));
+                    }
+
+                    if (event.isCancelled()) {
+                        return;
+                    }
+                    // CraftBukkit end
+                }
+                
                 if (packetplayinuseentity.a() == EnumEntityUseAction.INTERACT) {
                     this.player.u(entity);
+                    
+                    // CraftBukkit start
+                    if (itemInHand != null && itemInHand.count <= -1) {
+                        this.player.updateInventory(this.player.activeContainer);
+                    }
+                    // CraftBukkit end
                 } else if (packetplayinuseentity.a() == EnumEntityUseAction.INTERACT_AT) {
                     entity.a((EntityHuman) this.player, packetplayinuseentity.b());
+                    
+                    // CraftBukkit start
+                    if (itemInHand != null && itemInHand.count <= -1) {
+                        this.player.updateInventory(this.player.activeContainer);
+                    }
+                    // CraftBukkit end
                 } else if (packetplayinuseentity.a() == EnumEntityUseAction.ATTACK) {
-                    if (entity instanceof EntityItem || entity instanceof EntityExperienceOrb || entity instanceof EntityArrow || entity == this.player) {
+                    if (entity instanceof EntityItem || entity instanceof EntityExperienceOrb || entity instanceof EntityArrow || (entity == this.player && !player.v())) { // CraftBukkit, RENAME
                         this.disconnect("Attempting to attack an invalid entity");
                         this.minecraftServer.warning("Player " + this.player.getName() + " tried to attack an invalid entity");
                         return;
                     }
 
                     this.player.attack(entity);
+                    
+                    // CraftBukkit start
+                    if (itemInHand != null && itemInHand.count <= -1) {
+                        this.player.updateInventory(this.player.activeContainer);
+                    }
+                    // CraftBukkit end
                 }
             }
         }
@@ -663,7 +1263,8 @@
         switch (SwitchHelperCommandActionType.c[enumclientcommand.ordinal()]) {
         case 1:
             if (this.player.viewingCredits) {
-                this.player = this.minecraftServer.getPlayerList().moveToWorld(this.player, 0, true);
+                // this.player = this.minecraftServer.getPlayerList().moveToWorld(this.player, 0, true);
+                this.minecraftServer.getPlayerList().changeDimension(this.player, 0, PlayerTeleportEvent.TeleportCause.END_PORTAL); // CraftBukkit - reroute logic through custom portal management
             } else if (this.player.u().getWorldData().isHardcore()) {
                 if (this.minecraftServer.S() && this.player.getName().equals(this.minecraftServer.R())) {
                     this.player.playerConnection.disconnect("You have died. Game over, man, it\'s game over!");
@@ -694,15 +1295,22 @@
     }
 
     public void a(PacketPlayInCloseWindow packetplayinclosewindow) {
+        if (this.player.dead) return; // CraftBukkit
         PlayerConnectionUtils.ensureMainThread(packetplayinclosewindow, this, this.player.u());
+        
+        CraftEventFactory.handleInventoryCloseEvent(this.player); // CraftBukkit
+        
         this.player.p();
     }
 
     public void a(PacketPlayInWindowClick packetplayinwindowclick) {
+        if (this.player.dead) return; // CraftBukkit
+        
         PlayerConnectionUtils.ensureMainThread(packetplayinwindowclick, this, this.player.u());
         this.player.z();
         if (this.player.activeContainer.windowId == packetplayinwindowclick.a() && this.player.activeContainer.c(this.player)) {
-            if (this.player.v()) {
+            boolean cancelled = this.player.v(); // CraftBukkit - see below if
+            if (false) { // CraftBukkit this.player.v()) {
                 ArrayList arraylist = Lists.newArrayList();
 
                 for (int i = 0; i < this.player.activeContainer.c.size(); ++i) {
@@ -711,7 +1319,270 @@
 
                 this.player.a(this.player.activeContainer, (List) arraylist);
             } else {
-                ItemStack itemstack = this.player.activeContainer.clickItem(packetplayinwindowclick.b(), packetplayinwindowclick.c(), packetplayinwindowclick.f(), this.player);
+                // ItemStack itemstack = this.player.activeContainer.clickItem(packetplayinwindowclick.b(), packetplayinwindowclick.c(), packetplayinwindowclick.f(), this.player);
+                // CraftBukkit start - Call InventoryClickEvent
+                if (packetplayinwindowclick.b() < -1 && packetplayinwindowclick.b() != -999) {
+                    return;
+                }
+
+                InventoryView inventory = this.player.activeContainer.getBukkitView();
+                SlotType type = CraftInventoryView.getSlotType(inventory, packetplayinwindowclick.b());
+
+                InventoryClickEvent event = null;
+                ClickType click = ClickType.UNKNOWN;
+                InventoryAction action = InventoryAction.UNKNOWN;
+
+                ItemStack itemstack = null;
+
+                if (packetplayinwindowclick.b() == -1) {
+                    type = SlotType.OUTSIDE; // override
+                    click = packetplayinwindowclick.c() == 0 ? ClickType.WINDOW_BORDER_LEFT : ClickType.WINDOW_BORDER_RIGHT;
+                    action = InventoryAction.NOTHING;
+                } else if (packetplayinwindowclick.f() == 0) {
+                    if (packetplayinwindowclick.c() == 0) {
+                        click = ClickType.LEFT;
+                    } else if (packetplayinwindowclick.c() == 1) {
+                        click = ClickType.RIGHT;
+                    }
+                    if (packetplayinwindowclick.c() == 0 || packetplayinwindowclick.c() == 1) {
+                        action = InventoryAction.NOTHING; // Don't want to repeat ourselves
+                        if (packetplayinwindowclick.b() == -999) {
+                            if (player.inventory.getCarried() != null) {
+                                action = packetplayinwindowclick.c() == 0 ? InventoryAction.DROP_ALL_CURSOR : InventoryAction.DROP_ONE_CURSOR;
+                            }
+                        } else {
+                            Slot slot = this.player.activeContainer.getSlot(packetplayinwindowclick.b());
+                            if (slot != null) {
+                                ItemStack clickedItem = slot.getItem();
+                                ItemStack cursor = player.inventory.getCarried();
+                                if (clickedItem == null) {
+                                    if (cursor != null) {
+                                        action = packetplayinwindowclick.c() == 0 ? InventoryAction.PLACE_ALL : InventoryAction.PLACE_ONE;
+                                    }
+                                } else if (slot.isAllowed(player)) {
+                                    if (cursor == null) {
+                                        action = packetplayinwindowclick.c() == 0 ? InventoryAction.PICKUP_ALL : InventoryAction.PICKUP_HALF;
+                                    } else if (slot.isAllowed(cursor)) {
+                                        if (clickedItem.doMaterialsMatch(cursor) && ItemStack.equals(clickedItem, cursor)) {
+                                            int toPlace = packetplayinwindowclick.c() == 0 ? cursor.count : 1;
+                                            toPlace = Math.min(toPlace, clickedItem.getMaxStackSize() - clickedItem.count);
+                                            toPlace = Math.min(toPlace, slot.inventory.getMaxStackSize() - clickedItem.count);
+                                            if (toPlace == 1) {
+                                                action = InventoryAction.PLACE_ONE;
+                                            } else if (toPlace == cursor.count) {
+                                                action = InventoryAction.PLACE_ALL;
+                                            } else if (toPlace < 0) {
+                                                action = toPlace != -1 ? InventoryAction.PICKUP_SOME : InventoryAction.PICKUP_ONE; // this happens with oversized stacks
+                                            } else if (toPlace != 0) {
+                                                action = InventoryAction.PLACE_SOME;
+                                            }
+                                        } else if (cursor.count <= slot.getMaxStackSize()) {
+                                            action = InventoryAction.SWAP_WITH_CURSOR;
+                                        }
+                                    } else if (cursor.getItem() == clickedItem.getItem() && (!cursor.usesData() || cursor.getData() == clickedItem.getData()) && ItemStack.equals(cursor, clickedItem)) {
+                                        if (clickedItem.count >= 0) {
+                                            if (clickedItem.count + cursor.count <= cursor.getMaxStackSize()) {
+                                                // As of 1.5, this is result slots only
+                                                action = InventoryAction.PICKUP_ALL;
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                } else if (packetplayinwindowclick.f() == 1) {
+                    if (packetplayinwindowclick.c() == 0) {
+                        click = ClickType.SHIFT_LEFT;
+                    } else if (packetplayinwindowclick.c() == 1) {
+                        click = ClickType.SHIFT_RIGHT;
+                    }
+                    if (packetplayinwindowclick.c() == 0 || packetplayinwindowclick.c() == 1) {
+                        if (packetplayinwindowclick.b() < 0) {
+                            action = InventoryAction.NOTHING;
+                        } else {
+                            Slot slot = this.player.activeContainer.getSlot(packetplayinwindowclick.b());
+                            if (slot != null && slot.isAllowed(this.player) && slot.hasItem()) {
+                                action = InventoryAction.MOVE_TO_OTHER_INVENTORY;
+                            } else {
+                                action = InventoryAction.NOTHING;
+                            }
+                        }
+                    }
+                } else if (packetplayinwindowclick.f() == 2) {
+                    if (packetplayinwindowclick.c() >= 0 && packetplayinwindowclick.c() < 9) {
+                        click = ClickType.NUMBER_KEY;
+                        Slot clickedSlot = this.player.activeContainer.getSlot(packetplayinwindowclick.b());
+                        if (clickedSlot.isAllowed(player)) {
+                            ItemStack hotbar = this.player.inventory.getItem(packetplayinwindowclick.c());
+                            boolean canCleanSwap = hotbar == null || (clickedSlot.inventory == player.inventory && clickedSlot.isAllowed(hotbar)); // the slot will accept the hotbar item
+                            if (clickedSlot.hasItem()) {
+                                if (canCleanSwap) {
+                                    action = InventoryAction.HOTBAR_SWAP;
+                                } else {
+                                    int firstEmptySlot = player.inventory.getFirstEmptySlotIndex();
+                                    if (firstEmptySlot > -1) {
+                                        action = InventoryAction.HOTBAR_MOVE_AND_READD;
+                                    } else {
+                                        action = InventoryAction.NOTHING; // This is not sane! Mojang: You should test for other slots of same type
+                                    }
+                                }
+                            } else if (!clickedSlot.hasItem() && hotbar != null && clickedSlot.isAllowed(hotbar)) {
+                                action = InventoryAction.HOTBAR_SWAP;
+                            } else {
+                                action = InventoryAction.NOTHING;
+                            }
+                        } else {
+                            action = InventoryAction.NOTHING;
+                        }
+                        // Special constructor for number key
+                        event = new InventoryClickEvent(inventory, type, packetplayinwindowclick.b(), click, action, packetplayinwindowclick.c());
+                    }
+                } else if (packetplayinwindowclick.f() == 3) {
+                    if (packetplayinwindowclick.c() == 2) {
+                        click = ClickType.MIDDLE;
+                        if (packetplayinwindowclick.b() == -999) {
+                            action = InventoryAction.NOTHING;
+                        } else {
+                            Slot slot = this.player.activeContainer.getSlot(packetplayinwindowclick.b());
+                            if (slot != null && slot.hasItem() && player.abilities.canInstantlyBuild && player.inventory.getCarried() == null) {
+                                action = InventoryAction.CLONE_STACK;
+                            } else {
+                                action = InventoryAction.NOTHING;
+                            }
+                        }
+                    } else {
+                        click = ClickType.UNKNOWN;
+                        action = InventoryAction.UNKNOWN;
+                    }
+                } else if (packetplayinwindowclick.f() == 4) {
+                    if (packetplayinwindowclick.b() >= 0) {
+                        if (packetplayinwindowclick.c() == 0) {
+                            click = ClickType.DROP;
+                            Slot slot = this.player.activeContainer.getSlot(packetplayinwindowclick.b());
+                            if (slot != null && slot.hasItem() && slot.isAllowed(player) && slot.getItem() != null && slot.getItem().getItem() != Item.getItemOf(Blocks.AIR)) {
+                                action = InventoryAction.DROP_ONE_SLOT;
+                            } else {
+                                action = InventoryAction.NOTHING;
+                            }
+                        } else if (packetplayinwindowclick.c() == 1) {
+                            click = ClickType.CONTROL_DROP;
+                            Slot slot = this.player.activeContainer.getSlot(packetplayinwindowclick.b());
+                            if (slot != null && slot.hasItem() && slot.isAllowed(player) && slot.getItem() != null && slot.getItem().getItem() != Item.getItemOf(Blocks.AIR)) {
+                                action = InventoryAction.DROP_ALL_SLOT;
+                            } else {
+                                action = InventoryAction.NOTHING;
+                            }
+                        }
+                    } else {
+                        // Sane default (because this happens when they are holding nothing. Don't ask why.)
+                        click = ClickType.LEFT;
+                        if (packetplayinwindowclick.c() == 1) {
+                            click = ClickType.RIGHT;
+                        }
+                        action = InventoryAction.NOTHING;
+                    }
+                } else if (packetplayinwindowclick.f() == 5) {
+                    itemstack = this.player.activeContainer.clickItem(packetplayinwindowclick.b(), packetplayinwindowclick.c(), 5, this.player);
+                } else if (packetplayinwindowclick.f() == 6) {
+                    click = ClickType.DOUBLE_CLICK;
+                    action = InventoryAction.NOTHING;
+                    if (packetplayinwindowclick.b() >= 0 && this.player.inventory.getCarried() != null) {
+                        ItemStack cursor = this.player.inventory.getCarried();
+                        action = InventoryAction.NOTHING;
+                        // Quick check for if we have any of the item
+                        if (inventory.getTopInventory().contains(org.bukkit.Material.getMaterial(Item.getId(cursor.getItem()))) || inventory.getBottomInventory().contains(org.bukkit.Material.getMaterial(Item.getId(cursor.getItem())))) {
+                            action = InventoryAction.COLLECT_TO_CURSOR;
+                        }
+                    }
+                }
+                // TODO check on updates
+
+                if (packetplayinwindowclick.f() != 5) {
+                    if (click == ClickType.NUMBER_KEY) {
+                        event = new InventoryClickEvent(inventory, type, packetplayinwindowclick.b(), click, action, packetplayinwindowclick.c());
+                    } else {
+                        event = new InventoryClickEvent(inventory, type, packetplayinwindowclick.b(), click, action);
+                    }
+
+                    org.bukkit.inventory.Inventory top = inventory.getTopInventory();
+                    if (packetplayinwindowclick.b() == 0 && top instanceof CraftingInventory) {
+                        org.bukkit.inventory.Recipe recipe = ((CraftingInventory) top).getRecipe();
+                        if (recipe != null) {
+                            if (click == ClickType.NUMBER_KEY) {
+                                event = new CraftItemEvent(recipe, inventory, type, packetplayinwindowclick.b(), click, action, packetplayinwindowclick.c());
+                            } else {
+                                event = new CraftItemEvent(recipe, inventory, type, packetplayinwindowclick.b(), click, action);
+                            }
+                        }
+                    }
+
+                    event.setCancelled(cancelled);
+                    server.getPluginManager().callEvent(event);
+
+                    switch (event.getResult()) {
+                        case ALLOW:
+                        case DEFAULT:
+                            itemstack = this.player.activeContainer.clickItem(packetplayinwindowclick.b(), packetplayinwindowclick.c(), packetplayinwindowclick.f(), this.player);
+                            break;
+                        case DENY:
+                            /* Needs enum constructor in InventoryAction
+                            if (action.modifiesOtherSlots()) {
+
+                            } else {
+                                if (action.modifiesCursor()) {
+                                    this.player.playerConnection.sendPacket(new Packet103SetSlot(-1, -1, this.player.inventory.getCarried()));
+                                }
+                                if (action.modifiesClicked()) {
+                                    this.player.playerConnection.sendPacket(new Packet103SetSlot(this.player.activeContainer.windowId, packet102windowclick.slot, this.player.activeContainer.getSlot(packet102windowclick.slot).getItem()));
+                                }
+                            }*/
+                            switch (action) {
+                                // Modified other slots
+                                case PICKUP_ALL:
+                                case MOVE_TO_OTHER_INVENTORY:
+                                case HOTBAR_MOVE_AND_READD:
+                                case HOTBAR_SWAP:
+                                case COLLECT_TO_CURSOR:
+                                case UNKNOWN:
+                                    this.player.updateInventory(this.player.activeContainer);
+                                    break;
+                                // Modified cursor and clicked
+                                case PICKUP_SOME:
+                                case PICKUP_HALF:
+                                case PICKUP_ONE:
+                                case PLACE_ALL:
+                                case PLACE_SOME:
+                                case PLACE_ONE:
+                                case SWAP_WITH_CURSOR:
+                                    this.player.playerConnection.sendPacket(new PacketPlayOutSetSlot(-1, -1, this.player.inventory.getCarried()));
+                                    this.player.playerConnection.sendPacket(new PacketPlayOutSetSlot(this.player.activeContainer.windowId, packetplayinwindowclick.b(), this.player.activeContainer.getSlot(packetplayinwindowclick.b()).getItem()));
+                                    break;
+                                // Modified clicked only
+                                case DROP_ALL_SLOT:
+                                case DROP_ONE_SLOT:
+                                    this.player.playerConnection.sendPacket(new PacketPlayOutSetSlot(this.player.activeContainer.windowId, packetplayinwindowclick.b(), this.player.activeContainer.getSlot(packetplayinwindowclick.b()).getItem()));
+                                    break;
+                                // Modified cursor only
+                                case DROP_ALL_CURSOR:
+                                case DROP_ONE_CURSOR:
+                                case CLONE_STACK:
+                                    this.player.playerConnection.sendPacket(new PacketPlayOutSetSlot(-1, -1, this.player.inventory.getCarried()));
+                                    break;
+                                // Nothing
+                                case NOTHING:
+                                    break;
+                            }
+                            return;
+                    }
+
+                    if (event instanceof CraftItemEvent) {
+                        // Need to update the inventory on crafting to
+                        // correctly support custom recipes
+                        player.updateInventory(player.activeContainer);
+                    }
+                }
+                // CraftBukkit end
 
                 if (ItemStack.matches(packetplayinwindowclick.e(), itemstack)) {
                     this.player.playerConnection.sendPacket(new PacketPlayOutTransaction(packetplayinwindowclick.a(), packetplayinwindowclick.d(), true));
@@ -772,8 +1643,50 @@
             }
 
             boolean flag1 = packetplayinsetcreativeslot.a() >= 1 && packetplayinsetcreativeslot.a() < 36 + PlayerInventory.getHotbarSize();
-            boolean flag2 = itemstack == null || itemstack.getItem() != null;
+            // CraftBukkit - Add invalidItems check
+            boolean flag2 = itemstack == null || itemstack.getItem() != null && !invalidItems.contains(Item.getId(itemstack.getItem()));
             boolean flag3 = itemstack == null || itemstack.getData() >= 0 && itemstack.count <= 64 && itemstack.count > 0;
+            
+            
+            // CraftBukkit start - Call click event
+            if (flag || (flag1 && !ItemStack.matches(this.player.defaultContainer.getSlot(packetplayinsetcreativeslot.a()).getItem(), packetplayinsetcreativeslot.getItemStack()))) { // Insist on valid slot
+
+                org.bukkit.entity.HumanEntity player = this.player.getBukkitEntity();
+                InventoryView inventory = new CraftInventoryView(player, player.getInventory(), this.player.defaultContainer);
+                org.bukkit.inventory.ItemStack item = CraftItemStack.asBukkitCopy(packetplayinsetcreativeslot.getItemStack());
+
+                SlotType type = SlotType.QUICKBAR;
+                if (flag) {
+                    type = SlotType.OUTSIDE;
+                } else if (packetplayinsetcreativeslot.a() < 36) {
+                    if (packetplayinsetcreativeslot.a() >= 5 && packetplayinsetcreativeslot.a() < 9) {
+                        type = SlotType.ARMOR;
+                    } else {
+                        type = SlotType.CONTAINER;
+                    }
+                }
+                InventoryCreativeEvent event = new InventoryCreativeEvent(inventory, type, flag ? -999 : packetplayinsetcreativeslot.a(), item);
+                server.getPluginManager().callEvent(event);
+
+                itemstack = CraftItemStack.asNMSCopy(event.getCursor());
+
+                switch (event.getResult()) {
+                case ALLOW:
+                    // Plugin cleared the id / stacksize checks
+                    flag2 = flag3 = true;
+                    break;
+                case DEFAULT:
+                    break;
+                case DENY:
+                    // Reset the slot
+                    if (packetplayinsetcreativeslot.a() >= 0) {
+                        this.player.playerConnection.sendPacket(new PacketPlayOutSetSlot(this.player.defaultContainer.windowId, packetplayinsetcreativeslot.a(), this.player.defaultContainer.getSlot(packetplayinsetcreativeslot.a()).getItem()));
+                        this.player.playerConnection.sendPacket(new PacketPlayOutSetSlot(-1, -1, null));
+                    }
+                    return;
+                }
+            }
+            // CraftBukkit end
 
             if (flag1 && flag2 && flag3) {
                 if (itemstack == null) {
@@ -796,6 +1709,7 @@
     }
 
     public void a(PacketPlayInTransaction packetplayintransaction) {
+        if (this.player.dead) return; // CraftBukkit
         PlayerConnectionUtils.ensureMainThread(packetplayintransaction, this, this.player.u());
         Short oshort = (Short) this.n.get(this.player.activeContainer.windowId);
 
@@ -806,6 +1720,7 @@
     }
 
     public void a(PacketPlayInUpdateSign packetplayinupdatesign) {
+        if (this.player.dead) return; // CraftBukkit
         PlayerConnectionUtils.ensureMainThread(packetplayinupdatesign, this, this.player.u());
         this.player.z();
         WorldServer worldserver = this.minecraftServer.getWorldServer(this.player.dimension);
@@ -822,10 +1737,24 @@
 
             if (!tileentitysign.b() || tileentitysign.c() != this.player) {
                 this.minecraftServer.warning("Player " + this.player.getName() + " just tried to change non-editable sign");
+                this.sendPacket(new PacketPlayOutUpdateSign(tileentity.world, packetplayinupdatesign.a(), tileentitysign.lines)); // CraftBukkit
                 return;
             }
 
-            System.arraycopy(packetplayinupdatesign.b(), 0, tileentitysign.lines, 0, 4);
+            // CraftBukkit start
+            Player player = this.server.getPlayer(this.player);
+            int x = packetplayinupdatesign.a().getX();
+            int y = packetplayinupdatesign.a().getY();
+            int z = packetplayinupdatesign.a().getZ();
+            SignChangeEvent event = new SignChangeEvent((org.bukkit.craftbukkit.block.CraftBlock) player.getWorld().getBlockAt(x, y, z), this.server.getPlayer(this.player), org.bukkit.craftbukkit.block.CraftSign.revertComponents(packetplayinupdatesign.b()));
+            this.server.getPluginManager().callEvent(event);
+
+            if (!event.isCancelled()) {
+                System.arraycopy(org.bukkit.craftbukkit.block.CraftSign.sanitizeLines(event.getLines()), 0, tileentitysign.lines, 0, 4);
+                tileentitysign.isEditable = false;
+            }
+            // System.arraycopy(packetplayinupdatesign.b(), 0, tileentitysign.lines, 0, 4);
+            // CraftBukkit end            
             tileentitysign.update();
             worldserver.notify(blockposition);
         }
@@ -847,11 +1776,28 @@
 
     public void a(PacketPlayInAbilities packetplayinabilities) {
         PlayerConnectionUtils.ensureMainThread(packetplayinabilities, this, this.player.u());
-        this.player.abilities.isFlying = packetplayinabilities.isFlying() && this.player.abilities.canFly;
+        // CraftBukkit start
+        if (this.player.abilities.canFly && this.player.abilities.isFlying != packetplayinabilities.isFlying()) {
+            PlayerToggleFlightEvent event = new PlayerToggleFlightEvent(this.server.getPlayer(this.player), packetplayinabilities.isFlying());
+            this.server.getPluginManager().callEvent(event);
+            if (!event.isCancelled()) {
+                this.player.abilities.isFlying = packetplayinabilities.isFlying(); // Actually set the player's flying status
+            } else {
+                this.player.updateAbilities(); // Tell the player their ability was reverted
+            }
+        }
+        // CraftBukkit end
     }
 
     public void a(PacketPlayInTabComplete packetplayintabcomplete) {
         PlayerConnectionUtils.ensureMainThread(packetplayintabcomplete, this, this.player.u());
+        // CraftBukkit start
+        if (chatSpamField.addAndGet(this, 20) > 200 && !this.minecraftServer.getPlayerList().isOp(this.player.getProfile())) {
+            this.disconnect("disconnect.spam");
+            return;
+        }
+        // CraftBukkit end
+
         ArrayList arraylist = Lists.newArrayList();
         Iterator iterator = this.minecraftServer.tabCompleteCommand(this.player, packetplayintabcomplete.a(), packetplayintabcomplete.b()).iterator();
 
@@ -891,13 +1837,15 @@
                 itemstack1 = this.player.inventory.getItemInHand();
                 if (itemstack1 != null) {
                     if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack.getItem() == itemstack1.getItem()) {
-                        itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages", 8));
+                        // itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages", 8));
+                        CraftEventFactory.handleEditBookEvent(player, itemstack); // CraftBukkit
                     }
 
                     return;
                 }
             } catch (Exception exception) {
                 PlayerConnection.c.error("Couldn\'t handle book info", exception);
+                this.disconnect("Invalid book data!"); // CraftBukkit
                 return;
             } finally {
                 packetdataserializer.release();
@@ -909,27 +1857,31 @@
 
             try {
                 itemstack = packetdataserializer.i();
-                if (itemstack == null) {
-                    return;
-                }
+                if (itemstack != null) {
+                    if (!ItemWrittenBook.b(itemstack.getTag())) {
+                        throw new IOException("Invalid book tag!");
+                    }
 
-                if (!ItemWrittenBook.b(itemstack.getTag())) {
-                    throw new IOException("Invalid book tag!");
-                }
+                    itemstack1 = this.player.inventory.getItemInHand();
+                    if (itemstack1 == null) {
+                        return;
+                    }
 
-                itemstack1 = this.player.inventory.getItemInHand();
-                if (itemstack1 != null) {
                     if (itemstack.getItem() == Items.WRITTEN_BOOK && itemstack1.getItem() == Items.WRITABLE_BOOK) {
-                        itemstack1.a("author", (NBTBase) (new NBTTagString(this.player.getName())));
-                        itemstack1.a("title", (NBTBase) (new NBTTagString(itemstack.getTag().getString("title"))));
-                        itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages", 8));
-                        itemstack1.setItem(Items.WRITTEN_BOOK);
+                        // CraftBukkit start
+                        // itemstack1.a("author", (NBTBase) (new NBTTagString(this.player.getName())));
+                        // itemstack1.a("title", (NBTBase) (new NBTTagString(itemstack.getTag().getString("title"))));
+                        // itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages", 8));
+                        // itemstack1.setItem(Items.WRITTEN_BOOK);
+                        CraftEventFactory.handleEditBookEvent(player, itemstack); 
+                        // CraftBukkit end
                     }
 
                     return;
                 }
             } catch (Exception exception1) {
                 PlayerConnection.c.error("Couldn\'t sign book", exception1);
+                this.disconnect("Invalid book data!"); // CraftBukkit
                 return;
             } finally {
                 packetdataserializer.release();
@@ -946,11 +1898,12 @@
                 }
             } catch (Exception exception2) {
                 PlayerConnection.c.error("Couldn\'t select trade", exception2);
+                this.disconnect("Invalid trade data!"); // CraftBukkit
             }
         } else if ("MC|AdvCdm".equals(packetplayincustompayload.a())) {
             if (!this.minecraftServer.getEnableCommandBlock()) {
                 this.player.sendMessage(new ChatMessage("advMode.notEnabled", new Object[0]));
-            } else if (this.player.a(2, "") && this.player.abilities.canInstantlyBuild) {
+            } else if (this.player.getBukkitEntity().isOp() && this.player.abilities.canInstantlyBuild) { // CraftBukkit - Change to Bukkit OP versus Vanilla OP
                 packetdataserializer = packetplayincustompayload.b();
 
                 try {
@@ -986,6 +1939,7 @@
                     }
                 } catch (Exception exception3) {
                     PlayerConnection.c.error("Couldn\'t set command block", exception3);
+                    this.disconnect("Invalid CommandBlock data!"); // CraftBukkit
                 } finally {
                     packetdataserializer.release();
                 }
@@ -1011,6 +1965,7 @@
                     }
                 } catch (Exception exception4) {
                     PlayerConnection.c.error("Couldn\'t set beacon", exception4);
+                    this.disconnect("Invalid beacon data!"); // CraftBukkit
                 }
             }
         } else if ("MC|ItemName".equals(packetplayincustompayload.a()) && this.player.activeContainer instanceof ContainerAnvil) {
@@ -1026,6 +1981,27 @@
                 containeranvil.a("");
             }
         }
+            // CraftBukkit start
+            else if (packetplayincustompayload.a().equals("REGISTER")) {
+                String channels = packetplayincustompayload.b().toString(com.google.common.base.Charsets.UTF_8);
+                for (String channel : channels.split("\0")) {
+                    getPlayer().addChannel(channel);
+                }
+            } else if (packetplayincustompayload.a().equals("UNREGISTER")) {
+                String channels = packetplayincustompayload.b().toString(com.google.common.base.Charsets.UTF_8);
+                for (String channel : channels.split("\0")) {
+                    getPlayer().removeChannel(channel);
+                }
+            } else {
+                byte[] data = new byte[packetplayincustompayload.b().readableBytes()];
+                packetplayincustompayload.b().readBytes(data);
+                server.getMessenger().dispatchIncomingMessage(player.getBukkitEntity(), packetplayincustompayload.a(), data);
+            }
+            // CraftBukkit end
+    }
 
+    // CraftBukkit start - Add "isDisconnected" method
+    public final boolean isDisconnected() {
+        return !this.player.joining && !NetworkManager.a(this.networkManager).config().isAutoRead();
     }
 }