Skip to content

Commit

Permalink
1.20.2 fix for entity effects
Browse files Browse the repository at this point in the history
  • Loading branch information
retrooper committed Oct 1, 2023
1 parent 4d27d6e commit 2675b41
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public final class NMSUtils {
blockPosClass, sectionPositionClass, vec3DClass, channelFutureClass, blockClass, iBlockDataClass, nmsWorldClass, craftItemStackClass,
soundEffectClass, minecraftKeyClass, chatSerializerClass, craftMagicNumbersClass, worldSettingsClass, worldServerClass, dataWatcherClass,
dedicatedServerClass, entityHumanClass, packetDataSerializerClass, byteBufClass, dimensionManagerClass, nmsItemClass, iMaterialClass, movingObjectPositionBlockClass, boundingBoxClass,
tileEntityCommandClass, mojangEitherClass;
tileEntityCommandClass, mojangEitherClass, registryClass, builtInRegistriesClass;
public static Class<? extends Enum<?>> enumDirectionClass, enumHandClass, enumGameModeClass, enumDifficultyClass, tileEntityCommandTypeClass;
public static Method getBlockPosX, getBlockPosY, getBlockPosZ, mojangEitherLeft, mojangEitherRight;
public static Method getBlockPosX, getBlockPosY, getBlockPosZ, mojangEitherLeft, mojangEitherRight, getRegistryId, getRegistryById;
private static String nettyPrefix;
private static Method getCraftPlayerHandle, getCraftEntityHandle, getCraftWorldHandle, asBukkitCopy,
asNMSCopy, getMessageMethod, chatFromStringMethod, getMaterialFromNMSBlock, getNMSBlockFromMaterial,
getMobEffectListId, getMobEffectListById, getItemId, getItemById, getBukkitEntity;
private static Field entityPlayerPingField, entityBoundingBoxField;
private static Field entityPlayerPingField, entityBoundingBoxField, mobEffectsRegistryField;
private static Object minecraftServer;
private static Object minecraftServerConnection;

Expand Down Expand Up @@ -117,6 +117,10 @@ public static void load() {

entityBoundingBoxField = Reflection.getField(nmsEntityClass, boundingBoxClass, 0, true);

if (builtInRegistriesClass != null) {
mobEffectsRegistryField = Reflection.getField(builtInRegistriesClass, "e");
}

if (nmsEntityClass != null) {
getBukkitEntity = Reflection.getMethod(nmsEntityClass, craftEntityClass, 0);
}
Expand Down Expand Up @@ -236,6 +240,10 @@ public static void load() {
//Isn't present on every version
mojangEitherClass = Reflection.getClassByNameWithoutException("com.mojang.datafixers.util.Either");

registryClass = Reflection.getClassByNameWithoutException("net.minecraft.core.Registry");

builtInRegistriesClass = Reflection.getClassByNameWithoutException("net.minecraft.core.registries.BuiltInRegistries");

vec3DClass = NMSUtils.getNMSClassWithoutException("Vec3D");
if (vec3DClass == null) {
vec3DClass = getNMClassWithoutException("world.phys.Vec3D");
Expand Down Expand Up @@ -363,6 +371,12 @@ public static void load() {
mojangEitherLeft = Reflection.getMethod(mojangEitherClass, "left", Optional.class);
mojangEitherRight = Reflection.getMethod(mojangEitherClass, "right", Optional.class);
}

if (registryClass != null) {
getRegistryId = Reflection.getMethod(registryClass, int.class, 0);
getRegistryById = Reflection.getMethod(registryClass, 0, int.class);
}

worldSettingsClass = NMSUtils.getNMSClassWithoutException("WorldSettings");
if (worldServerClass == null) {
worldServerClass = getNMClassWithoutException("world.level.WorldSettings");
Expand Down Expand Up @@ -798,6 +812,11 @@ public static int generateEntityId() {

public static int getEffectId(Object nmsMobEffectList) {
try {
//1.20.2+
if (getMobEffectListId == null) {
Object registry = mobEffectsRegistryField.get(null);
return (int) getRegistryId.invoke(registry, nmsMobEffectList);
}
return (int) getMobEffectListId.invoke(null, nmsMobEffectList);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
Expand All @@ -807,6 +826,10 @@ public static int getEffectId(Object nmsMobEffectList) {

public static Object getMobEffectListById(int effectID) {
try {
if (getMobEffectListById != null) {
Object registry = mobEffectsRegistryField.get(null);
return getRegistryById.invoke(registry, effectID);
}
return getMobEffectListById.invoke(null, effectID);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
Expand Down

0 comments on commit 2675b41

Please sign in to comment.