From 934169bf6a7989bb74dd661e2415a116f4cd2caa Mon Sep 17 00:00:00 2001 From: ProSavage Date: Mon, 1 Oct 2018 17:04:40 -0500 Subject: [PATCH] Created a location type adapter, and fixed f checkpoint issues --- .../java/com/massivecraft/factions/P.java | 1 + .../factions/util/LocationTypeAdapter.java | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/main/java/com/massivecraft/factions/util/LocationTypeAdapter.java diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index fa12f84b..376e7d9f 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -394,6 +394,7 @@ public class P extends MPlugin { .registerTypeAdapter(LazyLocation.class, new MyLocationTypeAdapter()) .registerTypeAdapter(mapFLocToStringSetType, new MapFLocToStringSetTypeAdapter()) .registerTypeAdapter(Inventory.class, new InventoryTypeAdapter()) + .registerTypeAdapter(Location.class, new LocationTypeAdapter()) .registerTypeAdapterFactory(EnumTypeAdapter.ENUM_FACTORY); } diff --git a/src/main/java/com/massivecraft/factions/util/LocationTypeAdapter.java b/src/main/java/com/massivecraft/factions/util/LocationTypeAdapter.java new file mode 100644 index 00000000..a4c8401f --- /dev/null +++ b/src/main/java/com/massivecraft/factions/util/LocationTypeAdapter.java @@ -0,0 +1,34 @@ +package com.massivecraft.factions.util; + +import com.google.gson.*; +import org.bukkit.Bukkit; +import org.bukkit.Location; + +import java.lang.reflect.Type; + +public class LocationTypeAdapter implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(Location location, Type type, JsonSerializationContext jsonSerializationContext) { + + JsonObject object = new JsonObject(); + object.add("x", new JsonPrimitive(location.getX())); + object.add("y", new JsonPrimitive(location.getY())); + object.add("z", new JsonPrimitive(location.getZ())); + object.add("world", new JsonPrimitive(location.getWorld().toString())); + return object; + } + + + @Override + public Location deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { + JsonObject object = jsonElement.getAsJsonObject(); + return new Location(Bukkit.getWorld(object.get("world").getAsString()), + object.get("x").getAsDouble(), + object.get("y").getAsDouble(), + object.get("z").getAsDouble()); + + } + + +}