Add in exception catching to MyLocationTypeAdapter, seems to finally be handling all problems without crash

Added 1.1.5b release with this fix
This commit is contained in:
Brettflan 2011-04-04 12:26:48 -05:00
parent 204f67c43b
commit 12bc5d9d74
2 changed files with 39 additions and 29 deletions

Binary file not shown.

View File

@ -1,6 +1,7 @@
package com.bukkit.mcteam.factions; package com.bukkit.mcteam.factions;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.logging.Level;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
@ -23,43 +24,52 @@ public class MyLocationTypeAdapter implements JsonDeserializer<Location>, JsonSe
@Override @Override
public Location deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public Location deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject(); try {
JsonObject obj = json.getAsJsonObject();
if (obj.isJsonNull() || obj.get(WORLD).isJsonNull()) World world = Factions.instance.getServer().getWorld(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 Location(world, x, y, z, yaw, pitch);
} catch (NullPointerException ex) {
Factions.log(Level.SEVERE, "NPE encountered while deserializing a location");
return null; return null;
}
World world = Factions.instance.getServer().getWorld(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 Location(world, x, y, z, yaw, pitch);
} }
@Override @Override
public JsonElement serialize(Location src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(Location src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject(); JsonObject obj = new JsonObject();
if (src == null) try {
{ if (src == null)
Factions.log("Passed location is null in MyLocationTypeAdapter."); {
return obj; Factions.log("Passed location is null in MyLocationTypeAdapter.");
} return obj;
else if (src.getWorld() == null) }
{ else if (src.getWorld() == null)
Factions.log("Passed location's world is null in MyLocationTypeAdapter."); {
return obj; Factions.log("Passed location's world is null in MyLocationTypeAdapter.");
} return obj;
}
obj.addProperty(WORLD, src.getWorld().getName()); obj.addProperty(WORLD, src.getWorld().getName());
obj.addProperty(X, src.getX()); obj.addProperty(X, src.getX());
obj.addProperty(Y, src.getY()); obj.addProperty(Y, src.getY());
obj.addProperty(Z, src.getZ()); obj.addProperty(Z, src.getZ());
obj.addProperty(YAW, src.getYaw()); obj.addProperty(YAW, src.getYaw());
obj.addProperty(PITCH, src.getPitch()); obj.addProperty(PITCH, src.getPitch());
return obj; return obj;
} catch (NullPointerException ex) {
Factions.log(Level.SEVERE, "NPE encountered while serializing a location");
return obj;
}
} }
} }