From 896bf489b72db6eedf940a5d218222d4c17fd1a4 Mon Sep 17 00:00:00 2001 From: Trent Hensler Date: Thu, 4 Jan 2018 15:44:39 -0800 Subject: [PATCH] Add method to deserialize the custom map --- .../java/com/massivecraft/factions/P.java | 8 ++- .../util/PermissionsMapTypeAdapter.java | 51 +++++++++++++++++++ .../factions/zcore/fperms/Access.java | 6 +++ .../factions/zcore/fperms/Action.java | 5 ++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index ad99ec7f..513415c5 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -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>>() { }.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>>() { + }.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 diff --git a/src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java b/src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java new file mode 100644 index 00000000..7475e01d --- /dev/null +++ b/src/main/java/com/massivecraft/factions/util/PermissionsMapTypeAdapter.java @@ -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>> { + + @Override + public Map> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { + + try { + JsonObject obj = json.getAsJsonObject(); + if (obj == null) { + return null; + } + + Map> permissionsMap = new ConcurrentHashMap<>(); + + // Top level is Relation + for (Map.Entry entry : obj.entrySet()) { + Relation relation = Relation.fromString(entry.getKey()); + + // Second level is the map between action -> access + for (Map.Entry entry2 : entry.getValue().getAsJsonObject().entrySet()) { + Map 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; + } + } +} diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/Access.java b/src/main/java/com/massivecraft/factions/zcore/fperms/Access.java index 43df0354..dc60e880 100644 --- a/src/main/java/com/massivecraft/factions/zcore/fperms/Access.java +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/Access.java @@ -19,4 +19,10 @@ public enum Access { return null; } + + + @Override + public String toString() { + return name(); + } } diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/Action.java b/src/main/java/com/massivecraft/factions/zcore/fperms/Action.java index 1810a530..318f94ea 100644 --- a/src/main/java/com/massivecraft/factions/zcore/fperms/Action.java +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/Action.java @@ -48,4 +48,9 @@ public enum Action { return null; } + + @Override + public String toString() { + return name; + } }