diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 10cfa9b4..5454713e 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -4,6 +4,7 @@ import com.massivecraft.factions.adapter.BoardAdapter; import com.massivecraft.factions.adapter.BoardMapAdapter; import com.massivecraft.factions.adapter.FFlagAdapter; import com.massivecraft.factions.adapter.FPermAdapter; +import com.massivecraft.factions.adapter.FactionPreprocessAdapter; import com.massivecraft.factions.adapter.RelAdapter; import com.massivecraft.factions.adapter.TerritoryAccessAdapter; import com.massivecraft.factions.chat.modifier.ChatModifierLc; @@ -21,6 +22,7 @@ import com.massivecraft.factions.chat.tag.ChatTagTitle; import com.massivecraft.factions.cmd.*; import com.massivecraft.factions.entity.Board; import com.massivecraft.factions.entity.BoardColls; +import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.UPlayerColls; import com.massivecraft.factions.entity.FactionColls; import com.massivecraft.factions.entity.MConfColl; @@ -41,6 +43,7 @@ import com.massivecraft.mcore.usys.Aspect; import com.massivecraft.mcore.usys.AspectColl; import com.massivecraft.mcore.usys.Multiverse; import com.massivecraft.mcore.util.MUtil; +import com.massivecraft.mcore.xlib.gson.Gson; import com.massivecraft.mcore.xlib.gson.GsonBuilder; @@ -75,6 +78,9 @@ public class Factions extends MPlugin private PowerMixin powerMixin = null; public PowerMixin getPowerMixin() { return this.powerMixin == null ? PowerMixinDefault.get() : this.powerMixin; } public void setPowerMixin(PowerMixin powerMixin) { this.powerMixin = powerMixin; } + + // Gson without preprocessors + public final Gson gsonWithoutPreprocessors = this.getGsonBuilderWithotPreprocessors().create(); // -------------------------------------------- // // OVERRIDE @@ -153,8 +159,7 @@ public class Factions extends MPlugin postEnable(); } - @Override - public GsonBuilder getGsonBuilder() + public GsonBuilder getGsonBuilderWithotPreprocessors() { return super.getGsonBuilder() .registerTypeAdapter(TerritoryAccess.class, TerritoryAccessAdapter.get()) @@ -166,4 +171,12 @@ public class Factions extends MPlugin ; } + @Override + public GsonBuilder getGsonBuilder() + { + return this.getGsonBuilderWithotPreprocessors() + .registerTypeAdapter(Faction.class, FactionPreprocessAdapter.get()) + ; + } + } diff --git a/src/com/massivecraft/factions/adapter/FactionPreprocessAdapter.java b/src/com/massivecraft/factions/adapter/FactionPreprocessAdapter.java new file mode 100644 index 00000000..a9f143a2 --- /dev/null +++ b/src/com/massivecraft/factions/adapter/FactionPreprocessAdapter.java @@ -0,0 +1,53 @@ +package com.massivecraft.factions.adapter; + +import java.lang.reflect.Type; + +import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; +import com.massivecraft.mcore.xlib.gson.JsonDeserializer; +import com.massivecraft.mcore.xlib.gson.JsonElement; +import com.massivecraft.mcore.xlib.gson.JsonObject; +import com.massivecraft.mcore.xlib.gson.JsonParseException; + +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.entity.Faction; + +public class FactionPreprocessAdapter implements JsonDeserializer +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static FactionPreprocessAdapter i = new FactionPreprocessAdapter(); + public static FactionPreprocessAdapter get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Faction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + preprocess(json); + return Factions.get().gsonWithoutPreprocessors.fromJson(json, typeOfT); + } + + public void preprocess(JsonElement json) + { + JsonObject jsonObject = json.getAsJsonObject(); + + // Renamed fields + // 1.8.X --> 2.0.0 + rename(jsonObject, "tag", "name"); + rename(jsonObject, "invites", "invitedPlayerIds"); + rename(jsonObject, "relationWish", "relationWishes"); + rename(jsonObject, "flagOverrides", "flags"); + rename(jsonObject, "permOverrides", "perms"); + } + + public void rename(final JsonObject jsonObject, final String from, final String to) + { + JsonElement element = jsonObject.remove(from); + if (element != null) jsonObject.add(to, element); + } + +} diff --git a/src/com/massivecraft/factions/entity/BoardColls.java b/src/com/massivecraft/factions/entity/BoardColls.java index 5c521984..d7b0c876 100644 --- a/src/com/massivecraft/factions/entity/BoardColls.java +++ b/src/com/massivecraft/factions/entity/BoardColls.java @@ -57,6 +57,7 @@ public class BoardColls extends XColls implements BoardInterfa this.migrate(); } + // This method is for the 1.8.X --> 2.0.0 migration public void migrate() { // Create file objects diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 0aa58670..b79f0595 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -22,7 +22,6 @@ import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.util.SenderUtil; -import com.massivecraft.mcore.xlib.gson.annotations.SerializedName; public class Faction extends Entity implements EconomyParticipator @@ -65,7 +64,6 @@ public class Faction extends Entity implements EconomyParticipator // The actual faction id looks something like "54947df8-0e9e-4471-a2f9-9af509fb5889" and that is not too easy to remember for humans. // Thus we make use of a name. Since the id is used in all foreign key situations changing the name is fine. // Null should never happen. The name must not be null. - @SerializedName("tag") private String name = null; // Factions can optionally set a description for themselves. @@ -92,22 +90,18 @@ public class Faction extends Entity implements EconomyParticipator // This is the ids of the invited players. // They are actually "senderIds" since you can invite "@console" to your faction. // Null means no one is invited - @SerializedName("invites") private Set invitedPlayerIds = null; // The keys in this map are factionIds. // Null means no special relation whishes. - @SerializedName("relationWish") private Map relationWishes = null; // The flag overrides are modifications to the default values. // Null means default for the universe. - @SerializedName("flagOverrides") private Map flags = null; // The perm overrides are modifications to the default values. // Null means default for the universe. - @SerializedName("permOverrides") private Map> perms = null; // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/FactionColls.java b/src/com/massivecraft/factions/entity/FactionColls.java index ee98ffe8..f4591eca 100644 --- a/src/com/massivecraft/factions/entity/FactionColls.java +++ b/src/com/massivecraft/factions/entity/FactionColls.java @@ -51,6 +51,7 @@ public class FactionColls extends XColls this.migrate(); } + // This method is for the 1.8.X --> 2.0.0 migration public void migrate() { // Create file objects diff --git a/src/com/massivecraft/factions/entity/UPlayerColls.java b/src/com/massivecraft/factions/entity/UPlayerColls.java index 0928e216..a731a070 100644 --- a/src/com/massivecraft/factions/entity/UPlayerColls.java +++ b/src/com/massivecraft/factions/entity/UPlayerColls.java @@ -51,6 +51,7 @@ public class UPlayerColls extends XColls this.migrate(); } + // This method is for the 1.8.X --> 2.0.0 migration public void migrate() { // Create file objects