mirror of
https://github.com/MassiveCraft/Factions.git
synced 2024-11-23 18:55:25 +01:00
Adding in a preprocessor to avoid using the SerializedName annotation.
This commit is contained in:
parent
8e59017cb1
commit
c10b6f5edc
@ -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())
|
||||
;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<Faction>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
@ -57,6 +57,7 @@ public class BoardColls extends XColls<BoardColl, Board> implements BoardInterfa
|
||||
this.migrate();
|
||||
}
|
||||
|
||||
// This method is for the 1.8.X --> 2.0.0 migration
|
||||
public void migrate()
|
||||
{
|
||||
// Create file objects
|
||||
|
@ -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<Faction> implements EconomyParticipator
|
||||
@ -65,7 +64,6 @@ public class Faction extends Entity<Faction> 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<Faction> 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<String> invitedPlayerIds = null;
|
||||
|
||||
// The keys in this map are factionIds.
|
||||
// Null means no special relation whishes.
|
||||
@SerializedName("relationWish")
|
||||
private Map<String, Rel> relationWishes = null;
|
||||
|
||||
// The flag overrides are modifications to the default values.
|
||||
// Null means default for the universe.
|
||||
@SerializedName("flagOverrides")
|
||||
private Map<FFlag, Boolean> flags = null;
|
||||
|
||||
// The perm overrides are modifications to the default values.
|
||||
// Null means default for the universe.
|
||||
@SerializedName("permOverrides")
|
||||
private Map<FPerm, Set<Rel>> perms = null;
|
||||
|
||||
// -------------------------------------------- //
|
||||
|
@ -51,6 +51,7 @@ public class FactionColls extends XColls<FactionColl, Faction>
|
||||
this.migrate();
|
||||
}
|
||||
|
||||
// This method is for the 1.8.X --> 2.0.0 migration
|
||||
public void migrate()
|
||||
{
|
||||
// Create file objects
|
||||
|
@ -51,6 +51,7 @@ public class UPlayerColls extends XColls<UPlayerColl, UPlayer>
|
||||
this.migrate();
|
||||
}
|
||||
|
||||
// This method is for the 1.8.X --> 2.0.0 migration
|
||||
public void migrate()
|
||||
{
|
||||
// Create file objects
|
||||
|
Loading…
Reference in New Issue
Block a user