-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathMinecraftReflection.java
More file actions
1936 lines (1680 loc) · 66.5 KB
/
Copy pathMinecraftReflection.java
File metadata and controls
1936 lines (1680 loc) · 66.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
/*
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2012 Kristian S. Stangeland
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
package com.comphenix.protocol.utility;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLogger;
import com.comphenix.protocol.injector.BukkitUnwrapper;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
import com.comphenix.protocol.reflect.fuzzy.AbstractFuzzyMatcher;
import com.comphenix.protocol.reflect.fuzzy.FuzzyClassContract;
import com.comphenix.protocol.reflect.fuzzy.FuzzyFieldContract;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMatchers;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
import com.comphenix.protocol.wrappers.EnumWrappers;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* Methods and constants specifically used in conjuction with reflecting Minecraft object.
*
* @author Kristian
*/
public final class MinecraftReflection {
private static final ClassSource CLASS_SOURCE = ClassSource.fromClassLoader();
/**
* Regular expression that matches a canonical Java class.
*/
private static final String CANONICAL_REGEX = "(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.)+\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*";
private static final String MINECRAFT_CLASS_NAME_REGEX = "net\\.minecraft\\." + CANONICAL_REGEX;
/**
* Represents a regular expression that will match the version string in a package: org.bukkit.craftbukkit.v1_6_R2 ->
* v1_6_R2
*/
private static final Pattern PACKAGE_VERSION_MATCHER = Pattern.compile(".*\\.(v\\d+_\\d+_\\w*\\d+)");
// Cache of getBukkitEntity
private static final Map<Class<?>, MethodAccessor> BUKKIT_ENTITY_CACHE = new HashMap<>();
/**
* The Entity package in Forge 1.5.2
*/
private static final String FORGE_ENTITY_PACKAGE = "net.minecraft.entity";
// Package private for the purpose of unit testing
static CachedPackage minecraftPackage;
static CachedPackage craftbukkitPackage;
static CachedPackage libraryPackage;
/**
* Regular expression computed dynamically.
*/
private static String DYNAMIC_PACKAGE_MATCHER = null;
/**
* The package name of all the classes that belongs to the native code in Minecraft.
*/
private static String MINECRAFT_PREFIX_PACKAGE = "net.minecraft.server";
private static String MINECRAFT_FULL_PACKAGE = null;
private static String CRAFTBUKKIT_PACKAGE = null;
// fuzzy matcher for minecraft class objects
private static AbstractFuzzyMatcher<Class<?>> fuzzyMatcher;
// The NMS version
private static String packageVersion;
// net.minecraft.server
private static Class<?> itemStackArrayClass;
// Whether we are using netty
private static Boolean cachedWatcherObject;
// ---- ItemStack conversions
private static Object itemStackAir = null;
private static Boolean nullEnforced = null;
private static MethodAccessor asNMSCopy = null;
private static MethodAccessor asCraftMirror = null;
private static MethodAccessor isEmpty = null;
private static Boolean isMojangMapped = null;
private MinecraftReflection() {
// No need to make this constructable.
}
/**
* Retrieve a regular expression that can match Minecraft package objects.
*
* @return Minecraft package matcher.
*/
public static String getMinecraftObjectRegex() {
if (DYNAMIC_PACKAGE_MATCHER == null) {
getMinecraftPackage();
}
return DYNAMIC_PACKAGE_MATCHER;
}
/**
* Retrieve a abstract fuzzy class matcher for Minecraft objects.
*
* @return A matcher for Minecraft objects.
*/
public static AbstractFuzzyMatcher<Class<?>> getMinecraftObjectMatcher() {
if (fuzzyMatcher == null) {
fuzzyMatcher = FuzzyMatchers.matchRegex(getMinecraftObjectRegex());
}
return fuzzyMatcher;
}
/**
* Retrieve the name of the Minecraft server package.
*
* @return Full canonical name of the Minecraft server package.
*/
public static String getMinecraftPackage() {
// Speed things up
if (MINECRAFT_FULL_PACKAGE != null) {
return MINECRAFT_FULL_PACKAGE;
}
try {
// get the bukkit version we're running on
Server craftServer = Bukkit.getServer();
CRAFTBUKKIT_PACKAGE = craftServer.getClass().getPackage().getName();
// Parse the package version
Matcher packageMatcher = PACKAGE_VERSION_MATCHER.matcher(CRAFTBUKKIT_PACKAGE);
if (packageMatcher.matches()) {
packageVersion = packageMatcher.group(1);
} else if (!MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) { // ignore version prefix since it's no longer needed
MinecraftVersion version = new MinecraftVersion(craftServer);
// Just assume R1 - it's probably fine (warn anyway)
packageVersion = "v" + version.getMajor() + "_" + version.getMinor() + "_R1";
ProtocolLogger.log(Level.SEVERE, "Assuming package version: " + packageVersion);
}
if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
// total rework of the NMS structure in 1.17 (at least there's no versioning)
MINECRAFT_FULL_PACKAGE = MINECRAFT_PREFIX_PACKAGE = "net.minecraft";
setDynamicPackageMatcher(MINECRAFT_CLASS_NAME_REGEX);
} else {
// extract the server version from the return type of "getHandle" in CraftEntity
Method getHandle = getCraftEntityClass().getMethod("getHandle");
MINECRAFT_FULL_PACKAGE = getHandle.getReturnType().getPackage().getName();
// Pretty important invariant
if (!MINECRAFT_FULL_PACKAGE.startsWith(MINECRAFT_PREFIX_PACKAGE)) {
// See if we got the Forge entity package
if (MINECRAFT_FULL_PACKAGE.equals(FORGE_ENTITY_PACKAGE)) {
// Use the standard NMS versioned package
MINECRAFT_FULL_PACKAGE = CachedPackage.combine(MINECRAFT_PREFIX_PACKAGE, packageVersion);
} else {
// Assume they're the same instead
MINECRAFT_PREFIX_PACKAGE = MINECRAFT_FULL_PACKAGE;
}
// The package is usually flat, so go with that assumption
String matcher =
(MINECRAFT_PREFIX_PACKAGE.length() > 0 ? Pattern.quote(MINECRAFT_PREFIX_PACKAGE + ".") : "") + CANONICAL_REGEX;
// We'll still accept the default location, however
setDynamicPackageMatcher("(" + matcher + ")|(" + MINECRAFT_CLASS_NAME_REGEX + ")");
} else {
// Use the standard matcher
setDynamicPackageMatcher(MINECRAFT_CLASS_NAME_REGEX);
}
}
return MINECRAFT_FULL_PACKAGE;
} catch (NoSuchMethodException exception) {
throw new IllegalStateException("Cannot find getHandle() in CraftEntity", exception);
}
}
/**
* Retrieve the package version of the underlying CraftBukkit server.
*
* @return The craftbukkit package version.
*/
public static String getPackageVersion() {
getMinecraftPackage();
return packageVersion;
}
/**
* Update the dynamic package matcher.
*
* @param regex - the Minecraft package regex.
*/
private static void setDynamicPackageMatcher(String regex) {
DYNAMIC_PACKAGE_MATCHER = regex;
// Ensure that the matcher is regenerated
fuzzyMatcher = null;
}
/**
* Used during debugging and testing.
*
* @param minecraftPackage - the current Minecraft package.
* @param craftBukkitPackage - the current CraftBukkit package.
*/
static void setMinecraftPackage(String minecraftPackage, String craftBukkitPackage) {
MINECRAFT_FULL_PACKAGE = minecraftPackage;
CRAFTBUKKIT_PACKAGE = craftBukkitPackage;
// Make sure it exists
if (getMinecraftServerClass() == null) {
throw new IllegalArgumentException("Cannot find MinecraftServer for package " + minecraftPackage);
}
// Standard matcher
setDynamicPackageMatcher(MINECRAFT_CLASS_NAME_REGEX);
}
/**
* Retrieve the name of the root CraftBukkit package.
*
* @return Full canonical name of the root CraftBukkit package.
*/
public static String getCraftBukkitPackage() {
// Ensure it has been initialized
if (CRAFTBUKKIT_PACKAGE == null) {
getMinecraftPackage();
}
return CRAFTBUKKIT_PACKAGE;
}
/**
* Dynamically retrieve the Bukkit entity from a given entity.
*
* @param nmsObject - the NMS entity.
* @return A bukkit entity.
* @throws RuntimeException If we were unable to retrieve the Bukkit entity.
*/
public static Object getBukkitEntity(Object nmsObject) {
if (nmsObject == null) {
return null;
}
// We will have to do this dynamically, unfortunately
try {
Class<?> clazz = nmsObject.getClass();
MethodAccessor accessor = BUKKIT_ENTITY_CACHE.get(clazz);
if (accessor == null) {
MethodAccessor created = Accessors.getMethodAccessor(clazz, "getBukkitEntity");
accessor = BUKKIT_ENTITY_CACHE.putIfAbsent(clazz, created);
// We won the race
if (accessor == null) {
accessor = created;
}
}
return accessor.invoke(nmsObject);
} catch (Exception e) {
throw new IllegalArgumentException("Cannot get Bukkit entity from " + nmsObject, e);
}
}
/**
* Retrieve the Bukkit player from a given PlayerConnection.
*
* @param playerConnection The PlayerConnection.
* @return A bukkit player.
* @throws RuntimeException If we were unable to retrieve the Bukkit player.
*/
public static Player getBukkitPlayerFromConnection(Object playerConnection) {
try {
return (Player) getBukkitEntity(MinecraftFields.getPlayerFromConnection(playerConnection));
} catch (Exception e) {
throw new IllegalArgumentException("Cannot get Bukkit entity from connection " + playerConnection, e);
}
}
/**
* Determine if a given object can be found within the package net.minecraft.server.
*
* @param obj - the object to test.
* @return TRUE if it can, FALSE otherwise.
*/
public static boolean isMinecraftObject(Object obj) {
if (obj == null) {
return false;
}
// Doesn't matter if we don't check for the version here
return obj.getClass().getName().startsWith(MINECRAFT_PREFIX_PACKAGE);
}
/**
* Determine if the given class is found within the package net.minecraft.server, or any equivalent package.
*
* @param clazz - the class to test.
* @return TRUE if it can, FALSE otherwise.
*/
public static boolean isMinecraftClass(Class<?> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("clazz cannot be NULL.");
}
return getMinecraftObjectMatcher().isMatch(clazz, null);
}
/**
* Determine if a given object is found in net.minecraft.server, and has the given name.
*
* @param obj - the object to test.
* @param className - the class name to test.
* @return TRUE if it can, FALSE otherwise.
*/
public static boolean isMinecraftObject(Object obj, String className) {
if (obj == null) {
return false;
}
String javaName = obj.getClass().getName();
return javaName.startsWith(MINECRAFT_PREFIX_PACKAGE) && javaName.endsWith(className);
}
/**
* Determine if a given Object is compatible with a given Class. That is, whether or not the Object is an instance of
* that Class or one of its subclasses. If either is null, false is returned.
*
* @param clazz Class to test for, may be null
* @param object the Object to test, may be null
* @return True if it is, false if not
* @see Class#isAssignableFrom(Class)
*/
public static boolean is(Class<?> clazz, Object object) {
if (clazz == null || object == null) {
return false;
}
// check for accidental class objects
if (object instanceof Class) {
return clazz.isAssignableFrom((Class<?>) object);
}
return clazz.isAssignableFrom(object.getClass());
}
/**
* Equivalent to {@link #is(Class, Object)} but we don't call getClass again
*/
public static boolean is(Class<?> clazz, Class<?> test) {
if (clazz == null || test == null) {
return false;
}
return clazz.isAssignableFrom(test);
}
/**
* Determine if a given object is a BlockPosition.
*
* @param obj - the object to test.
* @return TRUE if it can, FALSE otherwise.
*/
public static boolean isBlockPosition(Object obj) {
return is(getBlockPositionClass(), obj);
}
/**
* Determine if the given object is an NMS ChunkCoordIntPar.
*
* @param obj - the object.
* @return TRUE if it can, FALSE otherwise.
*/
public static boolean isChunkCoordIntPair(Object obj) {
return is(getChunkCoordIntPair(), obj);
}
/**
* Determine if the given object is actually a Minecraft packet.
*
* @param obj - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isPacketClass(Object obj) {
return is(getPacketClass(), obj);
}
public static boolean isPacketClass(Class<?> clazz) {
return is(getPacketClass(), clazz);
}
/**
* Determine if the given object is assignable to a NetServerHandler (PlayerConnection)
*
* @param obj - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isServerHandler(Object obj) {
return is(getPlayerConnectionClass(), obj);
}
/**
* Determine if the given object is actually a Minecraft packet.
*
* @param obj - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isMinecraftEntity(Object obj) {
return is(getEntityClass(), obj);
}
/**
* Determine if the given object is a NMS ItemStack.
*
* @param value - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isItemStack(Object value) {
return is(getItemStackClass(), value);
}
/**
* Determine if the given object is a CraftPlayer class.
*
* @param value - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isCraftPlayer(Object value) {
return is(getCraftPlayerClass(), value);
}
/**
* Determine if the given object is a Minecraft player entity.
*
* @param obj - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isMinecraftPlayer(Object obj) {
return is(getEntityPlayerClass(), obj);
}
/**
* Determine if the given object is a data watcher object.
*
* @param obj - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isDataWatcher(Object obj) {
return is(getDataWatcherClass(), obj);
}
/**
* Determine if the given object is an IntHashMap object.
*
* @param obj - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isIntHashMap(Object obj) {
return is(getIntHashMapClass(), obj);
}
/**
* Determine if the given object is a CraftItemStack instancey.
*
* @param obj - the given object.
* @return TRUE if it is, FALSE otherwise.
*/
public static boolean isCraftItemStack(Object obj) {
return is(getCraftItemStackClass(), obj);
}
public static boolean isIChatBaseComponent(Class<?> target) {
return is(getIChatBaseComponentClass(), target);
}
/**
* Retrieve the EntityPlayer (NMS) class.
*
* @return The entity class.
*/
public static Class<?> getEntityPlayerClass() {
try {
return getMinecraftClass("server.level.EntityPlayer", "server.level.ServerPlayer", "EntityPlayer");
} catch (RuntimeException e) {
try {
// Grab CraftPlayer's handle
Method getHandle = FuzzyReflection
.fromClass(getCraftBukkitClass("entity.CraftPlayer"))
.getMethodByName("getHandle");
// EntityPlayer is the return type
return setMinecraftClass("server.level.EntityPlayer", getHandle.getReturnType());
} catch (IllegalArgumentException e1) {
throw new RuntimeException("Could not find EntityPlayer class.", e1);
}
}
}
/**
* Retrieve the EntityHuman class.
*
* @return The entity human class.
*/
public static Class<?> getEntityHumanClass() {
// Assume its the direct superclass
return getEntityPlayerClass().getSuperclass();
}
/**
* Retrieve the GameProfile class.
*
* @return The game profile class.
*/
public static Class<?> getGameProfileClass() {
return getClass("com.mojang.authlib.GameProfile");
}
public static Class<?> getGameProfilePropertyMapClass() {
return getClass("com.mojang.authlib.properties.PropertyMap");
}
/**
* Retrieve the entity (NMS) class.
*
* @return The entity class.
*/
public static Class<?> getEntityClass() {
try {
return getMinecraftClass("world.entity.Entity", "Entity");
} catch (RuntimeException e) {
return fallbackMethodReturn("Entity", "entity.CraftEntity", "getHandle");
}
}
/**
* Retrieve the CraftChatMessage.
*
* @return The CraftChatMessage class.
*/
public static Class<?> getCraftChatMessage() {
return getCraftBukkitClass("util.CraftChatMessage");
}
/**
* Retrieve the WorldServer (NMS) class.
*
* @return The WorldServer class.
*/
public static Class<?> getWorldServerClass() {
try {
return getMinecraftClass("server.level.WorldServer", "server.level.ServerLevel", "WorldServer");
} catch (RuntimeException e) {
return fallbackMethodReturn("WorldServer", "CraftWorld", "getHandle");
}
}
/**
* Retrieve the World (NMS) class.
*
* @return The world class.
*/
public static Class<?> getNmsWorldClass() {
try {
return getMinecraftClass("world.level.World", "world.level.Level", "World");
} catch (RuntimeException e) {
return setMinecraftClass("world.level.World", getWorldServerClass().getSuperclass());
}
}
/**
* Fallback on the return value of a named method in order to get a NMS class.
*
* @param nmsClass - the expected name of the Minecraft class.
* @param craftClass - a CraftBukkit class to look at.
* @param methodName - the method we will use.
* @return The return value of this method, which will be saved to the package cache.
*/
private static Class<?> fallbackMethodReturn(String nmsClass, String craftClass, String methodName) {
Class<?> result = FuzzyReflection.fromClass(getCraftBukkitClass(craftClass))
.getMethodByName(methodName)
.getReturnType();
// Save the result
return setMinecraftClass(nmsClass, result);
}
/**
* Retrieve the packet class.
*
* @return The packet class.
*/
public static Class<?> getPacketClass() {
return getMinecraftClass("network.protocol.Packet", "Packet");
}
public static Class<?> getByteBufClass() {
return getClass("io.netty.buffer.ByteBuf");
}
/**
* Retrieve the EnumProtocol class.
*
* @return The Enum protocol class.
*/
public static Class<?> getEnumProtocolClass() {
return getMinecraftClass("network.EnumProtocol", "network.ConnectionProtocol", "EnumProtocol");
}
/**
* Retrieve the IChatBaseComponent class.
*
* @return The IChatBaseComponent.
*/
public static Class<?> getIChatBaseComponentClass() {
return getMinecraftClass("network.chat.IChatBaseComponent", "network.chat.IChatbaseComponent", "network.chat.Component", "IChatBaseComponent");
}
public static Optional<Class<?>> getPackedBundlePacketClass() {
return getOptionalNMS("network.protocol.game.ClientboundBundlePacket", "ClientboundBundlePacket");
}
public static boolean isBundlePacket(Class<?> packetClass) {
return Optionals.Equals(getPackedBundlePacketClass(), packetClass);
}
public static boolean isBundleDelimiter(Class<?> packetClass) {
Class<?> bundleDelimiterClass = getBundleDelimiterClass().orElse(null);
return bundleDelimiterClass != null && (packetClass.equals(bundleDelimiterClass) || bundleDelimiterClass.isAssignableFrom(packetClass));
}
public static Optional<Class<?>> getBundleDelimiterClass() {
return getOptionalNMS("network.protocol.BundleDelimiterPacket","BundleDelimiterPacket");
}
public static Class<?> getIChatBaseComponentArrayClass() {
return getArrayClass(getIChatBaseComponentClass());
}
/**
* Retrieve the NMS chat component text class.
*
* @return The chat component class.
*/
public static Class<?> getChatComponentTextClass() {
return getMinecraftClass("network.chat.ChatComponentText", "network.chat.TextComponent", "ChatComponentText");
}
/**
* Attempt to find the ChatSerializer class.
*
* @return The serializer class.
* @throws IllegalStateException If the class could not be found or deduced.
*/
public static Class<?> getChatSerializerClass() {
return getMinecraftClass("network.chat.IChatBaseComponent$ChatSerializer",
"network.chat.Component$Serializer", "network.chat.ComponentSerialization", "IChatBaseComponent$ChatSerializer");
}
/**
* Retrieve the component style serializer class.
*
* @return The serializer class.
*/
public static Class<?> getStyleSerializerClass() {
return getMinecraftClass(
"network.chat.Style$Serializer",
"network.chat.ChatModifier$ChatModifierSerializer",
"ChatModifier$ChatModifierSerializer");
}
/**
* Retrieve the ServerPing class.
*
* @return The ServerPing class.
*/
public static Class<?> getServerPingClass() {
return getMinecraftClass("network.protocol.status.ServerPing", "network.protocol.status.ServerStatus", "ServerPing");
}
/**
* Retrieve the ServerPingServerData class.
*
* @return The ServerPingServerData class.
*/
public static Class<?> getServerPingServerDataClass() {
return getMinecraftClass("network.protocol.status.ServerPing$ServerData", "network.protocol.status.ServerStatus$Version", "ServerPing$ServerData");
}
/**
* Retrieve the ServerPingPlayerSample class.
*
* @return The ServerPingPlayerSample class.
*/
public static Class<?> getServerPingPlayerSampleClass() {
return getMinecraftClass(
"network.protocol.status.ServerPing$ServerPingPlayerSample",
"network.protocol.status.ServerStatus$Players",
"ServerPing$ServerPingPlayerSample");
}
/**
* Retrieve the MinecraftServer class.
*
* @return MinecraftServer class.
*/
public static Class<?> getMinecraftServerClass() {
try {
return getMinecraftClass("server.MinecraftServer", "MinecraftServer");
} catch (RuntimeException e) {
// Reset cache and try again
resetCacheForNMSClass("server.MinecraftServer");
useFallbackServer();
return getMinecraftClass("server.MinecraftServer");
}
}
/**
* Retrieve the NMS statistics class.
*
* @return The statistics class.
*/
public static Class<?> getStatisticClass() {
return getMinecraftClass("stats.Statistic", "stats.Stat", "Statistic");
}
/**
* Retrieve the NMS statistic list class.
*
* @return The statistic list class.
*/
public static Class<?> getStatisticListClass() {
return getMinecraftClass("stats.StatisticList", "stats.Stats", "StatisticList");
}
/**
* Retrieve the player list class (or ServerConfigurationManager),
*
* @return The player list class.
*/
public static Class<?> getPlayerListClass() {
try {
return getMinecraftClass("server.players.PlayerList", "PlayerList");
} catch (RuntimeException e) {
// Reset cache and try again
resetCacheForNMSClass("server.players.PlayerList");
useFallbackServer();
return getMinecraftClass("server.players.PlayerList");
}
}
/**
* Retrieve the PlayerConnection class.
*
* @return The PlayerConnection class.
*/
public static Class<?> getPlayerConnectionClass() {
return getMinecraftClass("server.network.PlayerConnection", "server.network.ServerGamePacketListenerImpl", "PlayerConnection");
}
/**
* Retrieve the NetworkManager class.
*
* @return The NetworkManager class.
*/
public static Class<?> getNetworkManagerClass() {
return getMinecraftClass("network.NetworkManager", "network.Connection", "NetworkManager");
}
/**
* Retrieve the NMS ItemStack class.
*
* @return The ItemStack class.
*/
public static Class<?> getItemStackClass() {
try {
return getMinecraftClass("world.item.ItemStack", "ItemStack");
} catch (RuntimeException e) {
// Use the handle reference
return setMinecraftClass("world.item.ItemStack", FuzzyReflection.fromClass(getCraftItemStackClass(), true)
.getFieldByName("handle")
.getType());
}
}
/**
* Retrieve the Block (NMS) class.
*
* @return Block (NMS) class.
*/
public static Class<?> getBlockClass() {
return getMinecraftClass("world.level.block.Block", "Block");
}
public static Class<?> getItemClass() {
return getNullableNMS("world.item.Item", "Item");
}
public static Class<?> getFluidTypeClass() {
return getNullableNMS("world.level.material.FluidType", "world.level.material.Fluid", "FluidType");
}
public static Class<?> getParticleTypeClass() {
return getNullableNMS("core.particles.ParticleType", "core.particles.SimpleParticleType", "ParticleType");
}
public static Class<?> getParticleClass() {
return getNullableNMS("core.particles.Particle");
}
/**
* Retrieve the WorldType class.
*
* @return The WorldType class.
*/
public static Class<?> getWorldTypeClass() {
return getMinecraftClass("WorldType");
}
/**
* Retrieve the DataWatcher class.
*
* @return The DataWatcher class.
*/
public static Class<?> getDataWatcherClass() {
return getMinecraftClass("network.syncher.DataWatcher", "network.syncher.SynchedEntityData", "DataWatcher");
}
/**
* Retrieves the BlockPosition class.
*
* @return The BlockPosition class.
*/
public static Class<?> getBlockPositionClass() {
return getMinecraftClass("core.BlockPosition", "core.BlockPos", "BlockPosition");
}
/**
* Retrieves the Vec3D class.
*
* @return The Vec3D class.
*/
public static Class<?> getVec3DClass() {
return getMinecraftClass("world.phys.Vec3D", "world.phys.Vec3", "Vec3D");
}
/**
* Retrieve the ChunkCoordIntPair class.
*
* @return The ChunkCoordIntPair class.
*/
public static Class<?> getChunkCoordIntPair() {
return getMinecraftClass("world.level.ChunkCoordIntPair", "world.level.ChunkPos", "ChunkCoordIntPair");
}
/**
* Retrieve the DataWatcher Item class.
*
* @return The class
*/
public static Class<?> getDataWatcherItemClass() {
return getMinecraftClass("network.syncher.DataWatcher$Item", "network.syncher.SynchedEntityData$DataItem", "DataWatcher$Item", "DataWatcher$WatchableObject");
}
public static Class<?> getDataWatcherObjectClass() {
return getNullableNMS("network.syncher.DataWatcherObject", "network.syncher.EntityDataAccessor", "DataWatcherObject");
}
public static boolean watcherObjectExists() {
if (cachedWatcherObject == null) {
cachedWatcherObject = getDataWatcherObjectClass() != null;
}
return cachedWatcherObject;
}
public static Class<?> getDataWatcherSerializerClass() {
return getNullableNMS("network.syncher.DataWatcherSerializer", "network.syncher.EntityDataSerializer", "DataWatcherSerializer");
}
public static Class<?> getDataWatcherRegistryClass() {
return getMinecraftClass("network.syncher.DataWatcherRegistry", "network.syncher.EntityDataSerializers", "DataWatcherRegistry");
}
public static Class<?> getMinecraftKeyClass() {
return getMinecraftClass("resources.MinecraftKey", "resources.Identifier", "resources.ResourceLocation", "MinecraftKey");
}
public static Class<?> getMobEffectListClass() {
return getMinecraftClass("world.effect.MobEffectList", "MobEffectList", "world.effect.MobEffect", "world.effect.MobEffects");
}
public static Class<?> getSoundEffectClass() {
return getNullableNMS("sounds.SoundEffect", "sounds.SoundEvent", "SoundEffect", "sounds.SoundEvents");
}
/**
* Retrieve the ServerConnection abstract class.
*
* @return The ServerConnection class.
*/
public static Class<?> getServerConnectionClass() {
return getMinecraftClass("server.network.ServerConnection", "server.network.ServerConnectionListener", "ServerConnection");
}
/**
* Retrieve the NBT base class.
*
* @return The NBT base class.
*/
public static Class<?> getNBTBaseClass() {
return getMinecraftClass("nbt.NBTBase", "nbt.Tag", "NBTBase");
}
/**
* Retrieve the NBT read limiter class.
*
* @return The NBT read limiter.
*/
public static Class<?> getNBTReadLimiterClass() {
return getMinecraftClass("nbt.NBTReadLimiter", "nbt.NbtAccounter", "NBTReadLimiter");
}
/**
* Retrieve the NBT Compound class.
*
* @return The NBT Compond class.
*/
public static Class<?> getNBTCompoundClass() {
return getMinecraftClass("nbt.NBTTagCompound", "nbt.CompoundTag", "NBTTagCompound");
}
/**
* Retrieve the EntityTracker (NMS) class.
*
* @return EntityTracker class.
*/
public static Class<?> getEntityTrackerClass() {
return getMinecraftClass("server.level.PlayerChunkMap$EntityTracker", "server.level.ChunkMap$TrackedEntity", "EntityTracker");
}
/**
* Retrieve the attribute snapshot class.
* <p>
* This stores the final value of an attribute, along with all the associated computational steps.
*
* @return The attribute snapshot class.
*/
public static Class<?> getAttributeSnapshotClass() {
return getMinecraftClass(
"network.protocol.game.PacketPlayOutUpdateAttributes$AttributeSnapshot",
"network.protocol.game.ClientboundUpdateAttributesPacket$AttributeSnapshot",
"AttributeSnapshot",
"PacketPlayOutUpdateAttributes$AttributeSnapshot");
}
/**
* Retrieve the IntHashMap class.
*
* @return IntHashMap class.
*/
public static Class<?> getIntHashMapClass() {