4
4
import java .util .Arrays ;
5
5
import java .util .Collection ;
6
6
import java .util .List ;
7
+ import java .util .Map ;
7
8
import java .util .UUID ;
8
9
import java .util .concurrent .Callable ;
9
10
import java .util .concurrent .Future ;
10
11
11
12
import org .apache .commons .lang .WordUtils ;
12
13
import org .bukkit .Bukkit ;
13
14
15
+ import com .comphenix .protocol .PacketTypeLookup .ClassLookup ;
14
16
import com .comphenix .protocol .events .ConnectionSide ;
15
17
import com .comphenix .protocol .injector .packet .PacketRegistry ;
16
18
import com .comphenix .protocol .reflect .ObjectEnum ;
@@ -635,7 +637,9 @@ public static boolean hasLegacy(int packetId) {
635
637
* @param packetId - the packet ID.
636
638
* @return The corresponding packet type.
637
639
* @throws IllegalArgumentException If the current packet could not be found.
640
+ * @deprecated IDs are no longer reliable
638
641
*/
642
+ @ Deprecated
639
643
public static PacketType findCurrent (Protocol protocol , Sender sender , int packetId ) {
640
644
PacketType type = getLookup ().getFromCurrent (protocol , sender , packetId );
641
645
@@ -645,13 +649,34 @@ public static PacketType findCurrent(Protocol protocol, Sender sender, int packe
645
649
"(Protocol: " + protocol + ", Sender: " + sender + ")" );
646
650
}
647
651
652
+ public static PacketType findCurrent (Protocol protocol , Sender sender , String name ) {
653
+ name = format (protocol , sender , name );
654
+ PacketType type = getLookup ().getFromCurrent (protocol , sender , name );
655
+
656
+ if (type != null ) {
657
+ return type ;
658
+ } else {
659
+ throw new IllegalArgumentException ("Cannot find packet " + name +
660
+ "(Protocol: " + protocol + ", Sender: " + sender + ")" );
661
+ }
662
+ }
663
+
664
+ private static String format (Protocol protocol , Sender sender , String name ) {
665
+ if (name .contains ("Packet" ))
666
+ return name ;
667
+
668
+ return String .format ("Packet%s%s%s" , protocol .getPacketName (), sender .getPacketName (), name );
669
+ }
670
+
648
671
/**
649
672
* Determine if the given packet exists.
650
673
* @param protocol - the protocol.
651
674
* @param sender - the sender.
652
675
* @param packetId - the packet ID.
653
676
* @return TRUE if it exists, FALSE otherwise.
677
+ * @deprecated IDs are no longer reliable
654
678
*/
679
+ @ Deprecated
655
680
public static boolean hasCurrent (Protocol protocol , Sender sender , int packetId ) {
656
681
return getLookup ().getFromCurrent (protocol , sender , packetId ) != null ;
657
682
}
@@ -680,7 +705,7 @@ public static PacketType fromLegacy(int id, Sender sender) {
680
705
}
681
706
682
707
/**
683
- * Retrieve a packet type from a protocol, sender and packet ID.
708
+ * Retrieve a packet type from a protocol, sender and packet ID, for pre-1.8 .
684
709
* <p>
685
710
* The packet will automatically be registered if its missing.
686
711
* @param protocol - the current protocol.
@@ -689,21 +714,59 @@ public static PacketType fromLegacy(int id, Sender sender) {
689
714
* @param packetClass - the packet class
690
715
* @return The corresponding packet type.
691
716
*/
717
+ public static PacketType fromID (Protocol protocol , Sender sender , int packetId , Class <?> packetClass ) {
718
+ PacketType type = getLookup ().getFromCurrent (protocol , sender , packetId );
719
+
720
+ if (type == null ) {
721
+ type = new PacketType (protocol , sender , packetId , -1 , PROTOCOL_VERSION , packetClass .getName ());
722
+ type .dynamic = true ;
723
+
724
+ // Many may be scheduled, but only the first will be executed
725
+ scheduleRegister (type , "Dynamic-" + UUID .randomUUID ().toString ());
726
+ }
727
+
728
+ return type ;
729
+ }
730
+
731
+ /**
732
+ * Retrieve a packet type from a protocol, sender, ID, and class for 1.8+
733
+ * <p>
734
+ * The packet will automatically be registered if its missing.
735
+ * @param protocol - the current protocol.
736
+ * @param sender - the sender.
737
+ * @param packetId - the packet ID. Can be UNKNOWN_PACKET.
738
+ * @param packetClass - the packet class.
739
+ * @return The corresponding packet type.
740
+ */
692
741
public static PacketType fromCurrent (Protocol protocol , Sender sender , int packetId , Class <?> packetClass ) {
742
+ ClassLookup lookup = getLookup ().getClassLookup ();
743
+ Map <String , PacketType > map = lookup .getMap (protocol , sender );
744
+
745
+ // Check the map first
693
746
String className = packetClass .getSimpleName ();
694
- for (PacketType type : PacketType .values ()) {
695
- for (String name : type .classNames ) {
696
- if (className .equals (name )) {
697
- return type ;
747
+ PacketType type = map .get (className );
748
+ if (type == null ) {
749
+ // Then check any aliases
750
+ for (PacketType check : map .values ()) {
751
+ String [] aliases = check .getClassNames ();
752
+ if (aliases .length > 1 ) {
753
+ for (String alias : aliases ) {
754
+ if (alias .equals (className )) {
755
+ // We have a match!
756
+ type = check ;
757
+ }
758
+ }
698
759
}
699
760
}
700
- }
701
761
702
- PacketType type = new PacketType (protocol , sender , packetId , -1 , PROTOCOL_VERSION , className );
703
- type .dynamic = true ;
762
+ // Guess we don't support this packet :/
763
+ type = new PacketType (protocol , sender , packetId , -1 , PROTOCOL_VERSION , className );
764
+ type .dynamic = true ;
765
+
766
+ // Many may be scheduled, but only the first will be executed
767
+ scheduleRegister (type , "Dynamic-" + UUID .randomUUID ().toString ());
768
+ }
704
769
705
- // Many may be scheduled, but only the first will be executed
706
- scheduleRegister (type , "Dynamic-" + UUID .randomUUID ().toString ());
707
770
return type ;
708
771
}
709
772
@@ -826,7 +889,7 @@ public PacketType(Protocol protocol, Sender sender, int currentId, int legacyId,
826
889
827
890
this .classNames = new String [names .length ];
828
891
for (int i = 0 ; i < classNames .length ; i ++) {
829
- classNames [i ] = String . format ("Packet%s%s%s" , protocol . getPacketName () , sender . getPacketName () , names [i ]);
892
+ classNames [i ] = format (protocol , sender , names [i ]);
830
893
}
831
894
}
832
895
@@ -894,6 +957,10 @@ public int getCurrentId() {
894
957
return currentId ;
895
958
}
896
959
960
+ public String [] getClassNames () {
961
+ return classNames ;
962
+ }
963
+
897
964
/**
898
965
* Retrieve the equivalent packet class.
899
966
* @return The packet class, or NULL if not found.
0 commit comments