added common serializer for primitive data types

This commit is contained in:
D3v1s0m 2023-09-18 12:02:16 +05:30
parent 28ed99a3b7
commit ec4d40563f
No known key found for this signature in database
GPG Key ID: FA1F770C7B1D40C1
3 changed files with 42 additions and 27 deletions

@ -44,7 +44,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
private final Map<String, EntityPropertyImpl<?>> byName = new HashMap<>();
public EntityPropertyRegistryImpl(MojangSkinCache skinCache) {
registerSerializer(new BooleanPropertySerializer());
registerSerializer(new ComponentPropertySerializer());
registerSerializer(new NamedTextColorPropertySerializer());
registerSerializer(new SkinDescriptorSerializer(skinCache));
@ -52,7 +51,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerSerializer(new ColorPropertySerializer());
registerSerializer(new Vector3fPropertySerializer());
registerSerializer(new BlockStatePropertySerializer());
registerSerializer(new IntegerPropertySerializer());
registerSerializer(new LookTypeSerializer());
registerEnumSerializer(NpcPose.class);
@ -77,6 +75,8 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
registerEnumSerializer(PandaGene.class);
registerEnumSerializer(PuffState.class);
registerPrimitiveSerializers(Integer.class, Boolean.class, Double.class, Float.class, Long.class, Short.class, Byte.class, String.class);
/*
registerType("using_item", false); // TODO: fix it for 1.8 and add new property to use offhand item and riptide animation
@ -517,6 +517,16 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry {
serializerMap.put(clazz, new EnumPropertySerializer<>(clazz));
}
private void registerPrimitiveSerializers(Class<?>... classes) {
for (Class<?> clazz : classes) {
registerPrimitiveSerializer(clazz);
}
}
private <T> void registerPrimitiveSerializer(Class<T> clazz) {
serializerMap.put(clazz, new PrimitivePropertySerializer<>(clazz));
}
private <T> void register(EntityPropertyImpl<?> property) {
if (byName.containsKey(property.getName()))
throw new IllegalArgumentException("Duplicate property name: " + property.getName());

@ -0,0 +1,30 @@
package lol.pyr.znpcsplus.entity;
import java.lang.reflect.InvocationTargetException;
public class PrimitivePropertySerializer<T> implements PropertySerializer<T> {
private final Class<T> clazz;
public PrimitivePropertySerializer(Class<T> clazz) {
this.clazz = clazz;
}
@Override
public String serialize(T property) {
return String.valueOf(property);
}
@Override
public T deserialize(String property) {
try {
return clazz.getConstructor(String.class).newInstance(property);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
throw new NullPointerException("Failed to deserialize property " + property + " of type " + clazz.getName() + "!");
}
}
@Override
public Class<T> getTypeClass() {
return clazz;
}
}

@ -1,25 +0,0 @@
package lol.pyr.znpcsplus.entity.serializers;
import lol.pyr.znpcsplus.entity.PropertySerializer;
public class IntegerPropertySerializer implements PropertySerializer<Integer> {
@Override
public String serialize(Integer property) {
return String.valueOf(property);
}
@Override
public Integer deserialize(String property) {
try {
return Integer.parseInt(property);
} catch (NumberFormatException e) {
e.printStackTrace();
}
return null;
}
@Override
public Class<Integer> getTypeClass() {
return Integer.class;
}
}