Changes package to net.knarcraft
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package net.knarcraft.factions.adapter;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.knarcraft.factions.entity.Board;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class BoardAdapter implements JsonDeserializer<Board>, JsonSerializer<Board> {
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static BoardAdapter i = new BoardAdapter();
|
||||
|
||||
public static BoardAdapter get() {
|
||||
return i;
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Board deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
return new Board(context.deserialize(json, Board.MAP_TYPE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Board src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return context.serialize(src.getMap(), Board.MAP_TYPE);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
package net.knarcraft.factions.adapter;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.knarcraft.factions.TerritoryAccess;
|
||||
import com.massivecraft.massivecore.ps.PS;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
|
||||
public class BoardMapAdapter implements JsonDeserializer<Map<PS, TerritoryAccess>>, JsonSerializer<Map<PS, TerritoryAccess>> {
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static BoardMapAdapter i = new BoardMapAdapter();
|
||||
|
||||
public static BoardMapAdapter get() {
|
||||
return i;
|
||||
}
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
// -------------------------------------------- //
|
||||
|
||||
@Override
|
||||
public Map<PS, TerritoryAccess> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
Map<PS, TerritoryAccess> ret = new ConcurrentSkipListMap<>();
|
||||
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
|
||||
for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
|
||||
String[] ChunkCoordParts = entry.getKey().split("[,\\s]+");
|
||||
int chunkX = Integer.parseInt(ChunkCoordParts[0]);
|
||||
int chunkZ = Integer.parseInt(ChunkCoordParts[1]);
|
||||
PS chunk = PS.valueOf(chunkX, chunkZ);
|
||||
|
||||
TerritoryAccess territoryAccess = context.deserialize(entry.getValue(), TerritoryAccess.class);
|
||||
|
||||
ret.put(chunk, territoryAccess);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Map<PS, TerritoryAccess> src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject ret = new JsonObject();
|
||||
|
||||
for (Entry<PS, TerritoryAccess> entry : src.entrySet()) {
|
||||
PS ps = entry.getKey();
|
||||
TerritoryAccess territoryAccess = entry.getValue();
|
||||
|
||||
ret.add(ps.getChunkX().toString() + "," + ps.getChunkZ().toString(), context.serialize(territoryAccess, TerritoryAccess.class));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,121 @@
|
||||
package net.knarcraft.factions.adapter;
|
||||
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.knarcraft.factions.TerritoryAccess;
|
||||
import com.massivecraft.massivecore.store.migrator.MigratorUtil;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
public class TerritoryAccessAdapter implements JsonDeserializer<TerritoryAccess>, JsonSerializer<TerritoryAccess> {
|
||||
// -------------------------------------------- //
|
||||
// CONSTANTS
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static final String HOST_FACTION_ID = "hostFactionId";
|
||||
public static final String HOST_FACTION_ALLOWED = "hostFactionAllowed";
|
||||
public static final String GRANTED_IDS = "grantedIds";
|
||||
public static final String CHUNK_NAME = "chunkName";
|
||||
|
||||
public static final Type SET_OF_STRING_TYPE = new TypeToken<Set<String>>() {
|
||||
}.getType();
|
||||
|
||||
// -------------------------------------------- //
|
||||
// INSTANCE & CONSTRUCT
|
||||
// -------------------------------------------- //
|
||||
|
||||
private static TerritoryAccessAdapter i = new TerritoryAccessAdapter();
|
||||
|
||||
public static TerritoryAccessAdapter get() {
|
||||
return i;
|
||||
}
|
||||
|
||||
//----------------------------------------------//
|
||||
// OVERRIDE
|
||||
//----------------------------------------------//
|
||||
|
||||
@Override
|
||||
public TerritoryAccess deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
// isDefault <=> simple hostFactionId string
|
||||
if (json.isJsonPrimitive()) {
|
||||
String hostFactionId = json.getAsString();
|
||||
return TerritoryAccess.valueOf(hostFactionId);
|
||||
}
|
||||
|
||||
// Otherwise object
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
|
||||
// Prepare variables
|
||||
String hostFactionId = null;
|
||||
Boolean hostFactionAllowed = null;
|
||||
Set<String> grantedIds = Collections.emptySet();
|
||||
String chunkName = null;
|
||||
|
||||
// Read variables (test old values first)
|
||||
JsonElement element = null;
|
||||
|
||||
element = obj.get(HOST_FACTION_ID);
|
||||
hostFactionId = element.getAsString();
|
||||
|
||||
element = obj.get(HOST_FACTION_ALLOWED);
|
||||
if (element != null) {
|
||||
hostFactionAllowed = element.getAsBoolean();
|
||||
}
|
||||
|
||||
element = obj.get(GRANTED_IDS);
|
||||
if (element != null) {
|
||||
grantedIds = context.deserialize(element, SET_OF_STRING_TYPE);
|
||||
}
|
||||
|
||||
element = obj.get(CHUNK_NAME);
|
||||
if (element != null) {
|
||||
chunkName = element.getAsString();
|
||||
}
|
||||
|
||||
return TerritoryAccess.valueOf(hostFactionId, hostFactionAllowed, grantedIds, chunkName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(TerritoryAccess src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
if (src == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// isDefault <=> simple hostFactionId string
|
||||
if (src.isDefault()) {
|
||||
return new JsonPrimitive(src.getHostFactionId());
|
||||
}
|
||||
|
||||
// Otherwise object
|
||||
JsonObject obj = new JsonObject();
|
||||
|
||||
obj.addProperty(HOST_FACTION_ID, src.getHostFactionId());
|
||||
|
||||
if (!src.isHostFactionAllowed()) {
|
||||
obj.addProperty(HOST_FACTION_ALLOWED, src.isHostFactionAllowed());
|
||||
}
|
||||
|
||||
if (!src.getGrantedIds().isEmpty()) {
|
||||
obj.add(GRANTED_IDS, context.serialize(src.getGrantedIds(), SET_OF_STRING_TYPE));
|
||||
}
|
||||
|
||||
if (src.getChunkName() != null) {
|
||||
obj.addProperty(CHUNK_NAME, src.getChunkName());
|
||||
}
|
||||
|
||||
obj.add(MigratorUtil.VERSION_FIELD_NAME, new JsonPrimitive(src.version));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user