Fix villager data breaking versions below 1.14

This commit is contained in:
Dan Mulloy 2019-10-30 12:02:15 -04:00
parent 59c8c8c9db
commit 84e31d032b
2 changed files with 23 additions and 10 deletions

View File

@ -2025,7 +2025,7 @@ public class MinecraftReflection {
.orElseThrow(() -> new RuntimeException("Failed to find NMS class: " + className));
}
static Class<?> getNullableNMS(String className) {
public static Class<?> getNullableNMS(String className) {
if (minecraftPackage == null)
minecraftPackage = new CachedPackage(getMinecraftPackage(), getClassSource());
return minecraftPackage.getPackageClass(className).orElse(null);

View File

@ -3,24 +3,32 @@ package com.comphenix.protocol.wrappers;
import com.comphenix.protocol.reflect.EquivalentConverter;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
import com.comphenix.protocol.utility.MinecraftReflection;
public class WrappedVillagerData extends AbstractWrapper implements ClonableWrapper {
private static final Class<?> NMS_CLASS = MinecraftReflection.getMinecraftClass("VillagerData");
private static final Class<?> TYPE_CLASS = MinecraftReflection.getMinecraftClass("VillagerType");
private static final Class<?> PROF_CLASS = MinecraftReflection.getMinecraftClass("VillagerProfession");
private static final Class<?> NMS_CLASS = MinecraftReflection.getNullableNMS("VillagerData");
private static final Class<?> TYPE_CLASS = MinecraftReflection.getNullableNMS("VillagerType");
private static final Class<?> PROF_CLASS = MinecraftReflection.getNullableNMS("VillagerProfession");
private static final EquivalentConverter<Type> TYPE_CONVERTER = new EnumWrappers.FauxEnumConverter<>(Type.class, TYPE_CLASS);
private static final EquivalentConverter<Profession> PROF_CONVERTER = new EnumWrappers.FauxEnumConverter<>(Profession.class, PROF_CLASS);
private static EquivalentConverter<Type> TYPE_CONVERTER;
private static EquivalentConverter<Profession> PROF_CONVERTER;
static {
if (NMS_CLASS != null) {
TYPE_CONVERTER = new EnumWrappers.FauxEnumConverter<>(Type.class, TYPE_CLASS);
PROF_CONVERTER = new EnumWrappers.FauxEnumConverter<>(Profession.class, PROF_CLASS);
}
}
public enum Type {
DESERT, JUNGLE, PLAINS, SAVANNA, SNOW, SWAMP, TAIGA;
DESERT, JUNGLE, PLAINS, SAVANNA, SNOW, SWAMP, TAIGA
}
public enum Profession {
NONE, ARMORER, BUTCHER, CARTOGRAPHER, CLERIC, FARMER, FISHERMAN,
FLETCHER, LEATHERWORKER, LIBRARIAN, MASON, NITWIT, SHEPHERD,
TOOLSMITH, WEAPONSMITH;
TOOLSMITH, WEAPONSMITH
}
private StructureModifier<Object> modifier;
@ -36,12 +44,17 @@ public class WrappedVillagerData extends AbstractWrapper implements ClonableWrap
return new WrappedVillagerData(handle);
}
private static ConstructorAccessor CONSTRUCTOR;
public static WrappedVillagerData fromValues(Type type, Profession profession, int level) {
Object genericType = TYPE_CONVERTER.getGeneric(type);
Object genericProf = PROF_CONVERTER.getGeneric(profession);
Object handle = Accessors.getConstructorAccessor(NMS_CLASS, TYPE_CLASS, PROF_CLASS, int.class)
.invoke(genericType, genericProf, level);
if (CONSTRUCTOR == null) {
CONSTRUCTOR = Accessors.getConstructorAccessor(NMS_CLASS, TYPE_CLASS, PROF_CLASS, int.class);
}
Object handle = CONSTRUCTOR.invoke(genericType, genericProf, level);
return fromHandle(handle);
}