2011-07-18 22:06:02 +02:00
|
|
|
package com.massivecraft.factions;
|
2011-03-23 17:39:56 +01:00
|
|
|
|
|
|
|
import java.lang.reflect.Type;
|
2011-04-04 19:26:48 +02:00
|
|
|
import java.util.logging.Level;
|
2011-03-23 17:39:56 +01:00
|
|
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.World;
|
2011-07-18 22:06:02 +02:00
|
|
|
|
2011-07-27 22:56:45 +02:00
|
|
|
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;
|
2011-03-23 17:39:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSerializer<Location> {
|
|
|
|
private static final String WORLD = "world";
|
|
|
|
private static final String X = "x";
|
|
|
|
private static final String Y = "y";
|
|
|
|
private static final String Z = "z";
|
|
|
|
private static final String YAW = "yaw";
|
|
|
|
private static final String PITCH = "pitch";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Location deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
2011-04-04 19:26:48 +02:00
|
|
|
try {
|
|
|
|
JsonObject obj = json.getAsJsonObject();
|
2011-04-04 14:16:53 +02:00
|
|
|
|
2011-04-06 11:08:08 +02:00
|
|
|
String worldname = obj.get(WORLD).getAsString();
|
|
|
|
World world = Factions.instance.getServer().getWorld(worldname);
|
|
|
|
if (world == null) {
|
|
|
|
Factions.log(Level.WARNING, "Stored location's world \"" + worldname + "\" not found on server; dropping the location.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-04-04 19:26:48 +02:00
|
|
|
double x = obj.get(X).getAsDouble();
|
|
|
|
double y = obj.get(Y).getAsDouble();
|
|
|
|
double z = obj.get(Z).getAsDouble();
|
|
|
|
float yaw = obj.get(YAW).getAsFloat();
|
|
|
|
float pitch = obj.get(PITCH).getAsFloat();
|
|
|
|
|
|
|
|
return new Location(world, x, y, z, yaw, pitch);
|
|
|
|
|
2011-04-06 11:08:08 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
Factions.log(Level.WARNING, "Error encountered while deserializing a location.");
|
2011-04-04 14:16:53 +02:00
|
|
|
return null;
|
2011-04-04 19:26:48 +02:00
|
|
|
}
|
2011-03-23 17:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JsonElement serialize(Location src, Type typeOfSrc, JsonSerializationContext context) {
|
|
|
|
JsonObject obj = new JsonObject();
|
2011-03-28 21:38:07 +02:00
|
|
|
|
2011-04-04 19:26:48 +02:00
|
|
|
try {
|
2011-04-06 11:08:08 +02:00
|
|
|
if (src.getWorld() == null)
|
2011-04-04 19:26:48 +02:00
|
|
|
{
|
2011-04-06 11:08:08 +02:00
|
|
|
Factions.log(Level.WARNING, "Passed location's world was not found on the server. Dropping the location.");
|
2011-04-04 19:26:48 +02:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.addProperty(WORLD, src.getWorld().getName());
|
|
|
|
obj.addProperty(X, src.getX());
|
|
|
|
obj.addProperty(Y, src.getY());
|
|
|
|
obj.addProperty(Z, src.getZ());
|
|
|
|
obj.addProperty(YAW, src.getYaw());
|
|
|
|
obj.addProperty(PITCH, src.getPitch());
|
|
|
|
|
2011-03-28 21:38:07 +02:00
|
|
|
return obj;
|
2011-04-04 19:26:48 +02:00
|
|
|
|
2011-04-06 11:08:08 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
Factions.log(Level.WARNING, "Error encountered while serializing a location.");
|
2011-03-28 21:38:07 +02:00
|
|
|
return obj;
|
|
|
|
}
|
2011-03-23 17:39:56 +01:00
|
|
|
}
|
|
|
|
}
|