Skip to content

Instantly share code, notes, and snippets.

@rainbowdashlabs
Last active December 22, 2022 12:24
Show Gist options
  • Save rainbowdashlabs/d36c28a00ef0dda3bd6563c4aa44d278 to your computer and use it in GitHub Desktop.
Save rainbowdashlabs/d36c28a00ef0dda3bd6563c4aa44d278 to your computer and use it in GitHub Desktop.
Classes which allows to serialize and deserialize bukkit classes which implements the ConfigurationSerializable interface using GSON or others via adapters
import org.bukkit.Material;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.Arrays;
import java.util.List;
public class InventoryContainer {
private final List<SerializeContainer> contents;
private final List<SerializeContainer> extraContents;
private final List<SerializeContainer> armorContents;
public InventoryContainer(List<SerializeContainer> contents, List<SerializeContainer> extraContents, List<SerializeContainer> armorContents) {
this.contents = contents;
this.extraContents = extraContents;
this.armorContents = armorContents;
}
public static String serialize(PlayerInventory inventory) {
var contents = serializeArray(inventory.getContents());
var extraContents = serializeArray(inventory.getExtraContents());
var armorContents = serializeArray(inventory.getArmorContents());
return SerializeContainer.GSON.toJson(new InventoryContainer(contents, extraContents, armorContents));
}
public static InventoryContainer deserialize(String json) {
return SerializeContainer.GSON.fromJson(json, InventoryContainer.class);
}
public void write(PlayerInventory inventory) {
inventory.setContents(deserializeArray(contents).toArray(ItemStack[]::new));
inventory.setExtraContents(deserializeArray(extraContents).toArray(ItemStack[]::new));
inventory.setArmorContents(deserializeArray(armorContents).toArray(ItemStack[]::new));
}
private static List<SerializeContainer> serializeArray(ConfigurationSerializable[] elements) {
return Arrays.stream(elements)
.map(item -> item == null ? new ItemStack(Material.AIR) : item)
.map(SerializeContainer::fromObject)
.toList();
}
@SuppressWarnings("unchecked")
private static <T extends ConfigurationSerializable> List<ItemStack> deserializeArray(List<SerializeContainer> elements) {
return elements.stream().map(o -> o.toObject(ItemStack.class)).toList();
}
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class SerializeContainer {
public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(SerializeContainer.class, new SerializeContainerAdapter())
.create();
private final Map<String, Object> data;
private SerializeContainer(Map<String, Object> data) {
this.data = data;
}
/**
* Create a container from a serializable object.
*
* @param obj onbect to serialize
* @return container holding the object as a map.
*/
public static SerializeContainer fromObject(ConfigurationSerializable obj) {
return new SerializeContainer(obj.serialize());
}
/**
* Create a container from a map which was serialized previously
*
* @param json json string
* @return container holding the string as a map
*/
public static SerializeContainer fromJson(String json) {
return GSON.fromJson(json, SerializeContainer.class);
}
/**
* Convert a serializable object to a json string
*
* @param obj object to serialize
* @return object as string
*/
public static String serializeToJson(ConfigurationSerializable obj) {
return fromObject(obj).toJson();
}
/**
* Convert a json serialized object to a new object instance
*
* @param json json to convert
* @param clazz clazz to determine the return type
* @param <T> type to return
* @return a new instance of the serialzied object.
*/
public static <T extends ConfigurationSerializable> T deserializeFromJson(String json, Class<T> clazz) {
return fromJson(json).toObject(clazz);
}
/**
* Convert a serializable object to a json string
*
* @param obj object to serialize
* @return object as string
*/
public static String serializeToJson(List<ConfigurationSerializable> obj) {
return GSON.toJson(obj.stream().map(SerializeContainer::fromObject).toList());
}
/**
* Convert a json serialized object to a new object instance
*
* @param json json to convert
* @param clazz clazz to determine the return type
* @param <T> type to return
* @return a new instance of the serialzied object.
*/
public static <T extends ConfigurationSerializable> List<T> deserializeListFromJson(String json, Class<T> clazz) {
Type container = new TypeToken<ArrayList<SerializeContainer>>() {}.getType();
List<SerializeContainer> ser = GSON.fromJson(json, container);
return ser.stream().map(o -> o.toObject(clazz)).toList();
}
/**
* Converts the underlying map to the object
*
* @param clazz clazz to determine the return type
* @param <T> type to return
* @return a new instance of the serialzied object.
*/
@SuppressWarnings("unchecked")
public <T extends ConfigurationSerializable> T toObject(Class<T> clazz) {
return (T) ConfigurationSerialization.deserializeObject(data, clazz);
}
/**
* Conversts the underlying map to a json string
*
* @return map as json string
*/
public String toJson() {
return GSON.toJson(this);
}
private static class SerializeContainerAdapter implements JsonDeserializer<SerializeContainer> {
private static final Type MAP_TYPE = new TypeToken<Map<String, Object>>(){}.getType();
@Override
public SerializeContainer deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
var jsonObject = json.getAsJsonObject();
Map<String, Object> deserializedMap = context.deserialize(jsonObject.get("data"), MAP_TYPE);
return new SerializeContainer(deserializedMap);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment