v1.6
This commit is contained in:
parent
872343efcc
commit
63cab14792
|
@ -1,7 +1,10 @@
|
||||||
package com.io.yutian.aulib.serialize;
|
package com.io.yutian.aulib.serialize;
|
||||||
|
|
||||||
import com.io.yutian.aulib.exception.SerializeException;
|
import com.io.yutian.aulib.exception.SerializeException;
|
||||||
|
import com.io.yutian.aulib.serialize.serializers.ItemStackSerializer;
|
||||||
|
import com.io.yutian.aulib.serialize.serializers.UUIDSerializer;
|
||||||
import com.io.yutian.aulib.util.ReflectionUtil;
|
import com.io.yutian.aulib.util.ReflectionUtil;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import sun.misc.Unsafe;
|
import sun.misc.Unsafe;
|
||||||
|
@ -16,7 +19,7 @@ public class SerializeHelper {
|
||||||
|
|
||||||
private static Map<Class<?>, Serializer> serializers = new HashMap<>();
|
private static Map<Class<?>, Serializer> serializers = new HashMap<>();
|
||||||
|
|
||||||
public static <T, V> void registerSerializer(Class<T> clazz, Serializer<T, V> serializer) {
|
public static <T, V> void registerSerializer(Class<T> clazz, Serializer<T> serializer) {
|
||||||
serializers.put(clazz, serializer);
|
serializers.put(clazz, serializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,17 +180,8 @@ public class SerializeHelper {
|
||||||
WRAPPER_TO_PRIMITIVE.put(Float.class, float.class);
|
WRAPPER_TO_PRIMITIVE.put(Float.class, float.class);
|
||||||
WRAPPER_TO_PRIMITIVE.put(Double.class, double.class);
|
WRAPPER_TO_PRIMITIVE.put(Double.class, double.class);
|
||||||
|
|
||||||
registerSerializer(UUID.class, new Serializer<>() {
|
registerSerializer(UUID.class, new UUIDSerializer());
|
||||||
@Override
|
registerSerializer(ItemStack.class, new ItemStackSerializer());
|
||||||
public Object serialize(UUID value) {
|
|
||||||
return value.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID deserialize(Object value) {
|
|
||||||
return UUID.fromString((String) value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.io.yutian.aulib.serialize;
|
package com.io.yutian.aulib.serialize;
|
||||||
|
|
||||||
public interface Serializer<K, V> {
|
public interface Serializer<T> {
|
||||||
|
|
||||||
V serialize(K value);
|
Object serialize(T value);
|
||||||
|
|
||||||
K deserialize(V value);
|
T deserialize(Object value);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.io.yutian.aulib.serialize.serializers;
|
||||||
|
|
||||||
|
import com.io.yutian.aulib.serialize.Serializer;
|
||||||
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class ItemStackSerializer implements Serializer<ItemStack> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object serialize(ItemStack value) {
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(value);
|
||||||
|
jsonObject.put("Type", nmsItemStack.c().k().g().a());
|
||||||
|
jsonObject.put("Count", nmsItemStack.J());
|
||||||
|
NBTTagCompound nbtTagCompound = nmsItemStack.u();
|
||||||
|
jsonObject.put("Tag", nbtTagCompound.toString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack deserialize(Object value) {
|
||||||
|
JSONObject jsonObject = (JSONObject) value;
|
||||||
|
String arg = jsonObject.toString();
|
||||||
|
NBTTagCompound nbtTagCompound;
|
||||||
|
try {
|
||||||
|
nbtTagCompound = net.minecraft.nbt.MojangsonParser.a(arg);
|
||||||
|
} catch (CommandSyntaxException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.a(nbtTagCompound));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.io.yutian.aulib.serialize.serializers;
|
||||||
|
|
||||||
|
import com.io.yutian.aulib.serialize.Serializer;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class UUIDSerializer implements Serializer<UUID> {
|
||||||
|
@Override
|
||||||
|
public Object serialize(UUID value) {
|
||||||
|
return value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UUID deserialize(Object value) {
|
||||||
|
return UUID.fromString((String) value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user