diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/totem/TotemModel.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/totem/TotemModel.java index 66038a63..28683ca2 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/totem/TotemModel.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/totem/TotemModel.java @@ -18,7 +18,7 @@ package net.momirealms.customfishing.api.mechanic.totem; import net.momirealms.customfishing.api.mechanic.totem.block.TotemBlock; -import org.apache.commons.lang3.SerializationUtils; +import net.momirealms.customfishing.common.util.SerializationUtils; import org.bukkit.Axis; import org.bukkit.Location; diff --git a/common/src/main/java/net/momirealms/customfishing/common/util/SerializationUtils.java b/common/src/main/java/net/momirealms/customfishing/common/util/SerializationUtils.java new file mode 100644 index 00000000..3760fa8c --- /dev/null +++ b/common/src/main/java/net/momirealms/customfishing/common/util/SerializationUtils.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.common.util; + +import java.io.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class SerializationUtils { + + private SerializationUtils() { + } + + static class ClassLoaderAwareObjectInputStream extends ObjectInputStream { + private static final Map> PRIMITIVE_TYPES = new HashMap<>(); + + static { + PRIMITIVE_TYPES.put("byte", byte.class); + PRIMITIVE_TYPES.put("short", short.class); + PRIMITIVE_TYPES.put("int", int.class); + PRIMITIVE_TYPES.put("long", long.class); + PRIMITIVE_TYPES.put("float", float.class); + PRIMITIVE_TYPES.put("double", double.class); + PRIMITIVE_TYPES.put("boolean", boolean.class); + PRIMITIVE_TYPES.put("char", char.class); + PRIMITIVE_TYPES.put("void", void.class); + } + + private final ClassLoader classLoader; + + ClassLoaderAwareObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException { + super(in); + this.classLoader = classLoader; + } + + @Override + protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { + String className = desc.getName(); + try { + return Class.forName(className, false, classLoader); + } catch (ClassNotFoundException e) { + Class primitiveClass = PRIMITIVE_TYPES.get(className); + if (primitiveClass != null) { + return primitiveClass; + } + return super.resolveClass(desc); + } + } + } + + public static T clone(T object) { + if (object == null) { + return null; + } + byte[] objectData = serialize(object); + try (ByteArrayInputStream bais = new ByteArrayInputStream(objectData); + ClassLoaderAwareObjectInputStream ois = new ClassLoaderAwareObjectInputStream(bais, object.getClass().getClassLoader())) { + @SuppressWarnings("unchecked") + T clonedObject = (T) ois.readObject(); + return clonedObject; + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException("Exception occurred while cloning object", e); + } + } + + public static T deserialize(byte[] objectData) { + Objects.requireNonNull(objectData); + return deserialize(new ByteArrayInputStream(objectData)); + } + + public static T deserialize(InputStream inputStream) { + Objects.requireNonNull(inputStream); + try (ObjectInputStream ois = new ObjectInputStream(inputStream)) { + @SuppressWarnings("unchecked") + T obj = (T) ois.readObject(); + return obj; + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException("Exception occurred while deserializing object", e); + } + } + + public static byte[] serialize(Serializable obj) { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(obj); + return baos.toByteArray(); + } catch (IOException e) { + throw new RuntimeException("Exception occurred while serializing object", e); + } + } +}