mirror of
https://gitlab.com/SamB440/rpgregions-2.git
synced 2025-12-28 19:29:16 +00:00
Only apply pitch and yaw to locations if needed
This commit is contained in:
@@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
@@ -22,12 +23,18 @@ public class LocationAdapter implements JsonSerializer<Location>, JsonDeserializ
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) {
|
||||
return Location.deserialize(gson.fromJson(jsonElement, new TypeToken<Map<String, Object>>(){}.getType()));
|
||||
public Location deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
|
||||
Map<String, Object> deserialised = gson.fromJson(jsonElement, new TypeToken<Map<String, Object>>(){}.getType());
|
||||
if (!deserialised.containsKey("pitch")) deserialised.put("pitch", 0.0f);
|
||||
if (!deserialised.containsKey("yaw")) deserialised.put("yaw", 0.0f);
|
||||
return Location.deserialize(deserialised);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Location location, Type type, JsonSerializationContext context) {
|
||||
return gson.toJsonTree(location.serialize());
|
||||
Map<String, Object> serialised = location.serialize();
|
||||
if (Float.compare(location.getPitch(), 0.0f) == 0) serialised.remove("pitch");
|
||||
if (Float.compare(location.getYaw(), 0.0f) == 0) serialised.remove("yaw");
|
||||
return gson.toJsonTree(serialised);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user