Search in sources :

Example 1 with MassiveListDef

use of com.massivecraft.massivecore.collections.MassiveListDef in project MassiveCore by MassiveCraft.

the class AdapterBannerPatterns method deserialize.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
// Default serialization is fine.
// We do however provide a smarter deserialiser.
// This is for the sole purpose of upgrading from the old deprecated database format.
// 
// In the old version (around version 2.8.10) we made use of a list with primitives.
// id, color, id, color...
// They were just coming in that order and were not wrapped in some kind of entry.
// 
// The data types are the same in the old and new version.
// String, Number, String, Number...
// 
// In the new version we do however wrap them in the DataBannerPattern.
@Override
public MassiveListDef<DataBannerPattern> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    // Create
    MassiveListDef<DataBannerPattern> ret = new MassiveListDef<>();
    // Null (Def implementation is never null)
    if (json == null)
        return ret;
    if (json.equals(JsonNull.INSTANCE))
        return ret;
    // It is an array in both old and new version
    JsonArray array = json.getAsJsonArray();
    // Empty?
    if (array.size() == 0)
        return ret;
    // First element indicates version
    JsonElement first = array.get(0);
    if (first instanceof JsonObject) {
        // New
        for (JsonElement element : array) {
            DataBannerPattern dataBannerPattern = context.deserialize(element, DataBannerPattern.class);
            ret.add(dataBannerPattern);
        }
    } else {
        // Old aka Upgrade Mode
        Iterator<JsonElement> iterator = array.iterator();
        while (iterator.hasNext()) {
            DataBannerPattern dataBannerPattern = new DataBannerPattern();
            JsonElement idElement = iterator.next();
            String id = idElement.getAsString();
            dataBannerPattern.setId(id);
            JsonElement colorElement = iterator.next();
            Integer color = colorElement.getAsInt();
            dataBannerPattern.setColor(color);
            ret.add(dataBannerPattern);
        }
    }
    return ret;
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray) DataBannerPattern(com.massivecraft.massivecore.item.DataBannerPattern) MassiveListDef(com.massivecraft.massivecore.collections.MassiveListDef) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 2 with MassiveListDef

use of com.massivecraft.massivecore.collections.MassiveListDef in project MassiveCore by MassiveCraft.

the class MassiveCore method getMassiveCoreGsonBuilder.

public static GsonBuilder getMassiveCoreGsonBuilder() {
    // Create
    GsonBuilder ret = new GsonBuilder();
    // Basic Behavior
    ret.setPrettyPrinting();
    ret.disableHtmlEscaping();
    ret.excludeFieldsWithModifiers(Modifier.TRANSIENT);
    // Raw Adapters
    ret.registerTypeAdapter(JsonNull.class, AdapterJsonElement.get());
    ret.registerTypeAdapter(JsonPrimitive.class, AdapterJsonElement.get());
    ret.registerTypeAdapter(JsonArray.class, AdapterJsonElement.get());
    ret.registerTypeAdapter(JsonObject.class, AdapterJsonElement.get());
    // Enumeration Annotation Dodge
    ret.registerTypeAdapterFactory(AdapterModdedEnumType.ENUM_FACTORY);
    // Massive Containers
    ret.registerTypeAdapter(MassiveList.class, AdapterMassiveList.get());
    ret.registerTypeAdapter(MassiveListDef.class, AdapterMassiveList.get());
    ret.registerTypeAdapter(MassiveMap.class, AdapterMassiveMap.get());
    ret.registerTypeAdapter(MassiveMapDef.class, AdapterMassiveMap.get());
    ret.registerTypeAdapter(MassiveSet.class, AdapterMassiveSet.get());
    ret.registerTypeAdapter(MassiveSetDef.class, AdapterMassiveSet.get());
    ret.registerTypeAdapter(MassiveTreeMap.class, AdapterMassiveTreeMap.get());
    ret.registerTypeAdapter(MassiveTreeMapDef.class, AdapterMassiveTreeMap.get());
    ret.registerTypeAdapter(MassiveTreeSet.class, AdapterMassiveTreeSet.get());
    ret.registerTypeAdapter(MassiveTreeSetDef.class, AdapterMassiveTreeSet.get());
    // Entries (Is this still needed?)
    ret.registerTypeAdapter(Entry.class, AdapterEntry.get());
    // Assorted Custom
    ret.registerTypeAdapter(BackstringSet.class, AdapterBackstringSet.get());
    ret.registerTypeAdapter(PS.class, PSAdapter.get());
    ret.registerTypeAdapter(Sound.class, AdapterSound.get());
    ret.registerTypeAdapter(UUID.class, AdapterUUID.get());
    // Mson
    ret.registerTypeAdapter(Mson.class, AdapterMson.get());
    ret.registerTypeAdapter(MsonEvent.class, AdapterMsonEvent.get());
    // Banner Patterns Upgrade Adapter
    // NOTE: Must come after the "MassiveContainers" section for priority.
    Type typeBannerPatterns = new TypeToken<MassiveListDef<DataBannerPattern>>() {
    }.getType();
    ret.registerTypeAdapter(typeBannerPatterns, AdapterBannerPatterns.get());
    // ItemStack
    ret.registerTypeAdapter(ItemStack.class, AdapterItemStack.get());
    Class<?> classCraftItemStack = NmsItemStackCreate17R4P.getClassCraftItemStackCatch();
    if (classCraftItemStack != null)
        ret.registerTypeAdapter(classCraftItemStack, AdapterItemStack.get());
    // Inventory
    ret.registerTypeAdapter(Inventory.class, AdapterInventory.get());
    ret.registerTypeAdapter(PlayerInventory.class, AdapterPlayerInventory.get());
    // Storage
    ret.registerTypeAdapter(EntityInternalMap.class, AdapterEntityInternalMap.get());
    // Return
    return ret;
}
Also used : RegistryType(com.massivecraft.massivecore.command.type.RegistryType) Type(java.lang.reflect.Type) AdapterModdedEnumType(com.massivecraft.massivecore.adapter.AdapterModdedEnumType) GsonBuilder(com.massivecraft.massivecore.xlib.gson.GsonBuilder) MassiveListDef(com.massivecraft.massivecore.collections.MassiveListDef)

Aggregations

MassiveListDef (com.massivecraft.massivecore.collections.MassiveListDef)2 AdapterModdedEnumType (com.massivecraft.massivecore.adapter.AdapterModdedEnumType)1 RegistryType (com.massivecraft.massivecore.command.type.RegistryType)1 DataBannerPattern (com.massivecraft.massivecore.item.DataBannerPattern)1 GsonBuilder (com.massivecraft.massivecore.xlib.gson.GsonBuilder)1 JsonArray (com.massivecraft.massivecore.xlib.gson.JsonArray)1 JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)1 JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)1 Type (java.lang.reflect.Type)1