From ec4d40563f1cf7ae86708040981eb433443541f6 Mon Sep 17 00:00:00 2001 From: D3v1s0m Date: Mon, 18 Sep 2023 12:02:16 +0530 Subject: [PATCH] added common serializer for primitive data types --- .../entity/EntityPropertyRegistryImpl.java | 14 +++++++-- .../entity/PrimitivePropertySerializer.java | 30 +++++++++++++++++++ .../IntegerPropertySerializer.java | 25 ---------------- 3 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/PrimitivePropertySerializer.java delete mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/IntegerPropertySerializer.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java index a7c7b61..d3cd5ce 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -44,7 +44,6 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { private final Map> 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 void registerPrimitiveSerializer(Class clazz) { + serializerMap.put(clazz, new PrimitivePropertySerializer<>(clazz)); + } + private void register(EntityPropertyImpl property) { if (byName.containsKey(property.getName())) throw new IllegalArgumentException("Duplicate property name: " + property.getName()); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/PrimitivePropertySerializer.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/PrimitivePropertySerializer.java new file mode 100644 index 0000000..ad18eaf --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/PrimitivePropertySerializer.java @@ -0,0 +1,30 @@ +package lol.pyr.znpcsplus.entity; + +import java.lang.reflect.InvocationTargetException; + +public class PrimitivePropertySerializer implements PropertySerializer { + private final Class clazz; + + public PrimitivePropertySerializer(Class 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 getTypeClass() { + return clazz; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/IntegerPropertySerializer.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/IntegerPropertySerializer.java deleted file mode 100644 index e918668..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/IntegerPropertySerializer.java +++ /dev/null @@ -1,25 +0,0 @@ -package lol.pyr.znpcsplus.entity.serializers; - -import lol.pyr.znpcsplus.entity.PropertySerializer; - -public class IntegerPropertySerializer implements PropertySerializer { - @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 getTypeClass() { - return Integer.class; - } -}