mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2025-12-19 15:09:24 +00:00
Fix cloning totem config on 1.17.1
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
package net.momirealms.customfishing.api.mechanic.totem;
|
package net.momirealms.customfishing.api.mechanic.totem;
|
||||||
|
|
||||||
import net.momirealms.customfishing.api.mechanic.totem.block.TotemBlock;
|
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.Axis;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) <2024> <XiaoMoMi>
|
||||||
|
*
|
||||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<String, Class<?>> 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 extends Serializable> 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> T deserialize(byte[] objectData) {
|
||||||
|
Objects.requireNonNull(objectData);
|
||||||
|
return deserialize(new ByteArrayInputStream(objectData));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user