2020-04-04 11:09:05 +02:00
|
|
|
package com.massivecraft.factions.util.adapters;
|
2011-03-23 17:39:56 +01:00
|
|
|
|
2014-10-25 20:22:41 +02:00
|
|
|
import com.google.gson.*;
|
2019-09-14 21:13:01 +02:00
|
|
|
import com.massivecraft.factions.FactionsPlugin;
|
2020-04-04 11:09:05 +02:00
|
|
|
import com.massivecraft.factions.util.LazyLocation;
|
2011-07-18 22:06:02 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.logging.Level;
|
2011-03-28 21:38:07 +02:00
|
|
|
|
2011-04-04 19:26:48 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public class MyLocationTypeAdapter implements JsonDeserializer<LazyLocation>, JsonSerializer<LazyLocation> {
|
2014-08-05 17:17:27 +02:00
|
|
|
|
2019-09-15 11:19:06 +02:00
|
|
|
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 LazyLocation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
|
|
try {
|
|
|
|
JsonObject obj = json.getAsJsonObject();
|
|
|
|
|
|
|
|
String worldName = obj.get(WORLD).getAsString();
|
|
|
|
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 LazyLocation(worldName, x, y, z, yaw, pitch);
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
FactionsPlugin.getInstance().log(Level.WARNING, "Error encountered while deserializing a LazyLocation.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JsonElement serialize(LazyLocation src, Type typeOfSrc, JsonSerializationContext context) {
|
|
|
|
JsonObject obj = new JsonObject();
|
|
|
|
|
|
|
|
try {
|
|
|
|
obj.addProperty(WORLD, src.getWorldName());
|
|
|
|
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());
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
FactionsPlugin.getInstance().log(Level.WARNING, "Error encountered while serializing a LazyLocation.");
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
2011-03-23 17:39:56 +01:00
|
|
|
}
|