From 053f80f7e624cf79e54a9a97a645086efe36225c Mon Sep 17 00:00:00 2001 From: Smimg Date: Mon, 7 Oct 2024 13:38:35 -0400 Subject: [PATCH] Added a few Object Serializers --- build.gradle | 2 +- .../serialization/SerializeToBytes.java | 40 ++++++++++++++++ .../serialization/SerializeToPDC.java | 48 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToBytes.java create mode 100644 src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToPDC.java diff --git a/build.gradle b/build.gradle index eac572e..03c413b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'org.vindicterra' -version = '0.0.0-testing' +version = '0.0.0-dev' publishing { publications { diff --git a/src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToBytes.java b/src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToBytes.java new file mode 100644 index 0000000..479391a --- /dev/null +++ b/src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToBytes.java @@ -0,0 +1,40 @@ +package org.vindicterra.vindicterraLib.serialization; + +import java.io.*; + +/** + * Generic implementation of Java object serialization. + * Objects to be serialized must implement the "java.io.Serializable" interface. + * For a higher-level PDC serializer, check vindicterraLib.serialization.SerializeToPDC + */ +public class SerializeToBytes { + /** + * + * @param obj The Object to serialize + * @return A byte[] representing the serialized object + * @throws IOException (I've never had it raise this, so I don't know what would cause it) + */ + public static byte[] serialize(Object obj) throws IOException { + try(ByteArrayOutputStream b = new ByteArrayOutputStream()){ + try(ObjectOutputStream o = new ObjectOutputStream(b)){ + o.writeObject(obj); + } + return b.toByteArray(); + } + } + + /** + * + * @param bytes The byte[] to attempt to deserialize into an Object + * @return If successful, the deserialized Object + * @throws IOException (I've never had it raise this, so I don't know what would cause it) + * @throws ClassNotFoundException If the serialized data does not contain a Java class + */ + public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException { + try(ByteArrayInputStream b = new ByteArrayInputStream(bytes)){ + try(ObjectInputStream o = new ObjectInputStream(b)){ + return o.readObject(); + } + } + } +} diff --git a/src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToPDC.java b/src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToPDC.java new file mode 100644 index 0000000..46c2bec --- /dev/null +++ b/src/main/java/org/vindicterra/vindicterraLib/serialization/SerializeToPDC.java @@ -0,0 +1,48 @@ +package org.vindicterra.vindicterraLib.serialization; + +import org.bukkit.NamespacedKey; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; + +/** + * Utility for serializing an Object into a Player's PDC + */ +public class SerializeToPDC { + /** + * + * @param pdc The PersistentDataContainer to serialize the Object to + * @param key The NamespacedKey to serialize the Object under + * @param obj The Object to serialize + * @throws RuntimeException if the serializer raises IOException - see serialization.SerializeToBytes + */ + public static void serialize(PersistentDataContainer pdc, NamespacedKey key, Object obj) { + byte[] bytes; + try { + bytes = SerializeToBytes.serialize(obj); + } catch (IOException e) { + throw new RuntimeException(e); + } + pdc.set(key, PersistentDataType.BYTE_ARRAY, bytes); + } + + /** + * + * @param pdc The PersistentDataContainer to deserialize from + * @param key The NamespacedKey to deserialize from + * @throws RuntimeException if the serializer raises IOException - see serialization.SerializeToBytes + * @return `null` if the object was unable to be deserialized. Otherwise, the Object + */ + public static @Nullable Object deserialize(PersistentDataContainer pdc, NamespacedKey key) { + byte[] bytes = pdc.get(key, PersistentDataType.BYTE_ARRAY); + try { + return SerializeToBytes.deserialize(bytes); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + return null; + } + } +}