mirror of
https://github.com/MassiveCraft/Factions.git
synced 2024-12-03 15:43:24 +01:00
Add method to deserialize the custom map
This commit is contained in:
parent
c367a9cb09
commit
896bf489b7
@ -10,8 +10,11 @@ import com.massivecraft.factions.integration.Worldguard;
|
||||
import com.massivecraft.factions.integration.dynmap.EngineDynmap;
|
||||
import com.massivecraft.factions.listeners.*;
|
||||
import com.massivecraft.factions.struct.ChatMode;
|
||||
import com.massivecraft.factions.struct.Relation;
|
||||
import com.massivecraft.factions.util.*;
|
||||
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 net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -153,7 +156,10 @@ public class P extends MPlugin {
|
||||
Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() {
|
||||
}.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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -19,4 +19,10 @@ public enum Access {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name();
|
||||
}
|
||||
}
|
||||
|
@ -48,4 +48,9 @@ public enum Action {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user