Add BannerPatterns upgrade adapter. Give adapters some <3.

This commit is contained in:
Olof Larsson 2016-04-24 09:56:00 +02:00
parent cfa0c23b3d
commit b491cd72f8
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
20 changed files with 162 additions and 50 deletions

View File

@ -1,6 +1,7 @@
package com.massivecraft.massivecore;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
@ -13,6 +14,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import com.massivecraft.massivecore.adapter.AdapterBackstringEnumSet;
import com.massivecraft.massivecore.adapter.AdapterBannerPatterns;
import com.massivecraft.massivecore.adapter.AdapterEntry;
import com.massivecraft.massivecore.adapter.AdapterInventory;
import com.massivecraft.massivecore.adapter.AdapterItemStack;
@ -61,6 +63,7 @@ import com.massivecraft.massivecore.engine.EngineMassiveCoreTeleportMixinCause;
import com.massivecraft.massivecore.engine.EngineMassiveCoreVariable;
import com.massivecraft.massivecore.engine.EngineMassiveCoreWorldNameSet;
import com.massivecraft.massivecore.integration.vault.IntegrationVault;
import com.massivecraft.massivecore.item.DataBannerPattern;
import com.massivecraft.massivecore.item.WriterBannerPattern;
import com.massivecraft.massivecore.item.WriterFireworkEffect;
import com.massivecraft.massivecore.item.WriterItemStack;
@ -98,6 +101,7 @@ import com.massivecraft.massivecore.xlib.gson.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonNull;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.JsonPrimitive;
import com.massivecraft.massivecore.xlib.gson.reflect.TypeToken;
public class MassiveCore extends MassivePlugin
{
@ -134,39 +138,59 @@ public class MassiveCore extends MassivePlugin
public static GsonBuilder getMassiveCoreGsonBuilder()
{
return new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.registerTypeAdapter(JsonNull.class, AdapterJsonElement.get())
.registerTypeAdapter(JsonPrimitive.class, AdapterJsonElement.get())
.registerTypeAdapter(JsonArray.class, AdapterJsonElement.get())
.registerTypeAdapter(JsonObject.class, AdapterJsonElement.get())
.registerTypeAdapter(Sound.class, AdapterSound.get())
.registerTypeAdapter(UUID.class, AdapterUUID.get())
.registerTypeAdapter(ItemStack.class, AdapterItemStack.get())
.registerTypeAdapter(Inventory.class, AdapterInventory.get())
.registerTypeAdapter(PlayerInventory.class, AdapterPlayerInventory.get())
.registerTypeAdapter(PS.class, PSAdapter.get())
// Create
GsonBuilder ret = new GsonBuilder();
.registerTypeAdapter(MassiveList.class, AdapterMassiveList.get())
.registerTypeAdapter(MassiveListDef.class, AdapterMassiveList.get())
.registerTypeAdapter(MassiveMap.class, AdapterMassiveMap.get())
.registerTypeAdapter(MassiveMapDef.class, AdapterMassiveMap.get())
.registerTypeAdapter(MassiveSet.class, AdapterMassiveSet.get())
.registerTypeAdapter(MassiveSetDef.class, AdapterMassiveSet.get())
.registerTypeAdapter(MassiveTreeMap.class, AdapterMassiveTreeMap.get())
.registerTypeAdapter(MassiveTreeMapDef.class, AdapterMassiveTreeMap.get())
.registerTypeAdapter(MassiveTreeSet.class, AdapterMassiveTreeSet.get())
.registerTypeAdapter(MassiveTreeSetDef.class, AdapterMassiveTreeSet.get())
// Basic Behavior
ret.setPrettyPrinting();
ret.disableHtmlEscaping();
ret.excludeFieldsWithModifiers(Modifier.TRANSIENT);
.registerTypeAdapter(Mson.class, AdapterMson.get())
.registerTypeAdapter(MsonEvent.class, AdapterMsonEvent.get())
// 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());
.registerTypeAdapter(BackstringEnumSet.class, AdapterBackstringEnumSet.get())
.registerTypeAdapter(Entry.class, AdapterEntry.get())
// Enumeration Annotation Dodge
ret.registerTypeAdapterFactory(AdapterModdedEnumType.ENUM_FACTORY);
.registerTypeAdapterFactory(AdapterModdedEnumType.ENUM_FACTORY);
// Entries (Is this still needed?)
ret.registerTypeAdapter(Entry.class, AdapterEntry.get());
// Assorted Custom
ret.registerTypeAdapter(BackstringEnumSet.class, AdapterBackstringEnumSet.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());
// Items and Inventories
ret.registerTypeAdapter(ItemStack.class, AdapterItemStack.get());
ret.registerTypeAdapter(Inventory.class, AdapterInventory.get());
ret.registerTypeAdapter(PlayerInventory.class, AdapterPlayerInventory.get());
// Banner Patterns Upgrade Adapter
Type typeBannerPatterns = new TypeToken<MassiveListDef<DataBannerPattern>>(){}.getType();
ret.registerTypeAdapter(typeBannerPatterns, AdapterBannerPatterns.get());
// 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());
// Return
return ret;
}
public static String getServerId() { return ConfServer.serverid; }

View File

@ -25,7 +25,7 @@ public class AdapterBackstringEnumSet implements JsonDeserializer<BackstringEnum
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterBackstringEnumSet i = new AdapterBackstringEnumSet();
private static final AdapterBackstringEnumSet i = new AdapterBackstringEnumSet();
public static AdapterBackstringEnumSet get() { return i; }
// -------------------------------------------- //

View File

@ -0,0 +1,91 @@
package com.massivecraft.massivecore.adapter;
import java.lang.reflect.Type;
import java.util.Iterator;
import com.massivecraft.massivecore.collections.MassiveListDef;
import com.massivecraft.massivecore.item.DataBannerPattern;
import com.massivecraft.massivecore.xlib.gson.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonNull;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.JsonParseException;
public class AdapterBannerPatterns implements JsonDeserializer<MassiveListDef<DataBannerPattern>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static final AdapterBannerPatterns i = new AdapterBannerPatterns();
public static AdapterBannerPatterns get() { return i; }
// -------------------------------------------- //
// 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;
}
}

View File

@ -20,7 +20,7 @@ public class AdapterEntry implements JsonDeserializer<Entry<?, ?>>, JsonSerializ
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterEntry i = new AdapterEntry();
private static final AdapterEntry i = new AdapterEntry();
public static AdapterEntry get() { return i; }
// -------------------------------------------- //

View File

@ -53,7 +53,7 @@ public class AdapterInventory implements JsonDeserializer<Inventory>, JsonSerial
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterInventory i = new AdapterInventory();
private static final AdapterInventory i = new AdapterInventory();
public static AdapterInventory get() { return i; }
// -------------------------------------------- //

View File

@ -22,7 +22,7 @@ public class AdapterItemStack implements JsonDeserializer<ItemStack>, JsonSerial
// INSTANCE
// -------------------------------------------- //
public static AdapterItemStack i = new AdapterItemStack();
private static final AdapterItemStack i = new AdapterItemStack();
public static AdapterItemStack get() { return i; }
// -------------------------------------------- //

View File

@ -73,7 +73,7 @@ public class AdapterItemStackInner17 implements AdapterItemStackInner
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static AdapterItemStackInner17 i = new AdapterItemStackInner17();
private static final AdapterItemStackInner17 i = new AdapterItemStackInner17();
public static AdapterItemStackInner17 get() { return i; }
public AdapterItemStackInner17()
{

View File

@ -43,7 +43,7 @@ public class AdapterItemStackInner18 extends AdapterItemStackInner17
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static AdapterItemStackInner18 i = new AdapterItemStackInner18();
private static final AdapterItemStackInner18 i = new AdapterItemStackInner18();
public static AdapterItemStackInner18 get() { return i; }
// -------------------------------------------- //

View File

@ -32,7 +32,7 @@ public class AdapterItemStackInner19 extends AdapterItemStackInner18
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static AdapterItemStackInner19 i = new AdapterItemStackInner19();
private static final AdapterItemStackInner19 i = new AdapterItemStackInner19();
public static AdapterItemStackInner19 get() { return i; }
// -------------------------------------------- //

View File

@ -15,7 +15,7 @@ public class AdapterJsonElement implements JsonDeserializer<JsonElement>, JsonSe
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterJsonElement i = new AdapterJsonElement();
private static final AdapterJsonElement i = new AdapterJsonElement();
public static AdapterJsonElement get() { return i; }
// -------------------------------------------- //

View File

@ -14,7 +14,7 @@ public class AdapterMassiveList extends AdapterMassiveX<MassiveList<?>>
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterMassiveList i = new AdapterMassiveList();
private static final AdapterMassiveList i = new AdapterMassiveList();
public static AdapterMassiveList get() { return i; }
// -------------------------------------------- //

View File

@ -14,7 +14,7 @@ public class AdapterMassiveMap extends AdapterMassiveX<MassiveMap<?, ?>>
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterMassiveMap i = new AdapterMassiveMap();
private static final AdapterMassiveMap i = new AdapterMassiveMap();
public static AdapterMassiveMap get() { return i; }
// -------------------------------------------- //

View File

@ -14,7 +14,7 @@ public class AdapterMassiveSet extends AdapterMassiveX<MassiveSet<?>>
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterMassiveSet i = new AdapterMassiveSet();
private static final AdapterMassiveSet i = new AdapterMassiveSet();
public static AdapterMassiveSet get() { return i; }
// -------------------------------------------- //

View File

@ -14,7 +14,7 @@ public class AdapterMassiveTreeMap extends AdapterMassiveX<MassiveTreeMap<?, ?,
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterMassiveTreeMap i = new AdapterMassiveTreeMap();
private static final AdapterMassiveTreeMap i = new AdapterMassiveTreeMap();
public static AdapterMassiveTreeMap get() { return i; }
// -------------------------------------------- //

View File

@ -14,7 +14,7 @@ public class AdapterMassiveTreeSet extends AdapterMassiveX<MassiveTreeSet<?, ?>>
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterMassiveTreeSet i = new AdapterMassiveTreeSet();
private static final AdapterMassiveTreeSet i = new AdapterMassiveTreeSet();
public static AdapterMassiveTreeSet get() { return i; }
// -------------------------------------------- //

View File

@ -17,9 +17,8 @@ public class AdapterMson implements JsonDeserializer<Mson>, JsonSerializer<Mson>
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static AdapterMson i = new AdapterMson();
private static final AdapterMson i = new AdapterMson();
public static AdapterMson get() { return i; }
public AdapterMson() {}
// -------------------------------------------- //
// OVERRIDE

View File

@ -17,9 +17,8 @@ public class AdapterMsonEvent implements JsonDeserializer<MsonEvent>, JsonSerial
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static AdapterMsonEvent i = new AdapterMsonEvent();
private static final AdapterMsonEvent i = new AdapterMsonEvent();
public static AdapterMsonEvent get() { return i; }
public AdapterMsonEvent() {}
// -------------------------------------------- //
// OVERRIDE

View File

@ -17,7 +17,7 @@ public class AdapterPlayerInventory implements JsonDeserializer<PlayerInventory>
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterPlayerInventory i = new AdapterPlayerInventory();
private static final AdapterPlayerInventory i = new AdapterPlayerInventory();
public static AdapterPlayerInventory get() { return i; }
// -------------------------------------------- //

View File

@ -19,9 +19,8 @@ public class AdapterSound implements JsonDeserializer<Sound>, JsonSerializer<Sou
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static AdapterSound i = new AdapterSound();
private static final AdapterSound i = new AdapterSound();
public static AdapterSound get() { return i; }
public AdapterSound() {}
// -------------------------------------------- //
// OVERRIDE

View File

@ -17,7 +17,7 @@ public class AdapterUUID implements JsonDeserializer<UUID>, JsonSerializer<UUID>
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static AdapterUUID i = new AdapterUUID();
private static final AdapterUUID i = new AdapterUUID();
public static AdapterUUID get() { return i; }
// -------------------------------------------- //