Add method to deserialize the custom map

This commit is contained in:
Trent Hensler 2018-01-04 15:44:39 -08:00
parent c367a9cb09
commit 896bf489b7
4 changed files with 69 additions and 1 deletions

View File

@ -10,8 +10,11 @@ import com.massivecraft.factions.integration.Worldguard;
import com.massivecraft.factions.integration.dynmap.EngineDynmap; import com.massivecraft.factions.integration.dynmap.EngineDynmap;
import com.massivecraft.factions.listeners.*; import com.massivecraft.factions.listeners.*;
import com.massivecraft.factions.struct.ChatMode; import com.massivecraft.factions.struct.ChatMode;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.util.*; import com.massivecraft.factions.util.*;
import com.massivecraft.factions.zcore.MPlugin; import com.massivecraft.factions.zcore.MPlugin;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action;
import com.massivecraft.factions.zcore.util.TextUtil; import com.massivecraft.factions.zcore.util.TextUtil;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -153,7 +156,10 @@ public class P extends MPlugin {
Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() { Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() {
}.getType(); }.getType();
return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()).registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY); Type accessTypeAdatper = new TypeToken<Map<Relation, Map<Action, Access>>>() {
}.getType();
return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(accessTypeAdatper, new PermissionsMapTypeAdapter()).registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()).registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()).registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY);
} }
@Override @Override

View File

@ -0,0 +1,51 @@
package com.massivecraft.factions.util;
import com.google.gson.*;
import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.Action;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
public class PermissionsMapTypeAdapter implements JsonDeserializer<Map<Relation, Map<Action, Access>>> {
@Override
public Map<Relation, Map<Action, Access>> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
try {
JsonObject obj = json.getAsJsonObject();
if (obj == null) {
return null;
}
Map<Relation, Map<Action, Access>> permissionsMap = new ConcurrentHashMap<>();
// Top level is Relation
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
Relation relation = Relation.fromString(entry.getKey());
// Second level is the map between action -> access
for (Map.Entry<String, JsonElement> entry2 : entry.getValue().getAsJsonObject().entrySet()) {
Map<Action, Access> accessMap = new HashMap<>();
Action action = Action.fromString(entry2.getKey());
Access access = Access.fromString(entry2.getValue().getAsString());
accessMap.put(action, access);
permissionsMap.put(relation, accessMap);
}
}
return permissionsMap;
} catch (Exception ex) {
ex.printStackTrace();
P.p.log(Level.WARNING, "Error encountered while deserializing a Map of FLocations to String Sets.");
return null;
}
}
}

View File

@ -19,4 +19,10 @@ public enum Access {
return null; return null;
} }
@Override
public String toString() {
return name();
}
} }

View File

@ -48,4 +48,9 @@ public enum Action {
return null; return null;
} }
@Override
public String toString() {
return name;
}
} }