Added a null check to deserialize & serialize method for the LocationTypeAdapter.

This commit is contained in:
ProSavage 2018-10-23 13:20:20 -05:00
parent 9aeb70404d
commit 558b7bcd2c
3 changed files with 93 additions and 80 deletions

View File

@ -215,11 +215,8 @@ public class SavageFactions extends MPlugin {
} }
this.setupPlaceholderAPI();
this.postEnable();
setupPlaceholderAPI();
postEnable();
this.loadSuccessful = true; this.loadSuccessful = true;
} }

View File

@ -12,90 +12,90 @@ import java.io.Serializable;
*/ */
public class LazyLocation implements Serializable { public class LazyLocation implements Serializable {
private static final long serialVersionUID = -6049901271320963314L; private static final long serialVersionUID = - 6049901271320963314L;
private transient Location location = null; private transient Location location = null;
private String worldName; private String worldName;
private double x; private double x;
private double y; private double y;
private double z; private double z;
private float pitch; private float pitch;
private float yaw; private float yaw;
public LazyLocation(Location loc) { public LazyLocation(Location loc) {
setLocation(loc); setLocation(loc);
}
public LazyLocation(final String worldName, final double x, final double y, final double z) {
this(worldName, x, y, z, 0, 0);
}
public LazyLocation(final String worldName, final double x, final double y, final double z, final float yaw, final float pitch) {
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
// This returns the actual Location
public final Location getLocation() {
// make sure Location is initialized before returning it
initLocation();
return location;
}
// change the Location
public final void setLocation(Location loc) {
this.location = loc;
this.worldName = loc.getWorld().getName();
this.x = loc.getX();
this.y = loc.getY();
this.z = loc.getZ();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
}
// This initializes the Location
private void initLocation() {
// if location is already initialized, simply return
if (location != null) {
return;
} }
public LazyLocation(final String worldName, final double x, final double y, final double z) { // get World; hopefully it's initialized at this point
this(worldName, x, y, z, 0, 0); World world = Bukkit.getWorld(worldName);
if (world == null) {
return;
} }
public LazyLocation(final String worldName, final double x, final double y, final double z, final float yaw, final float pitch) { // store the Location for future calls, and pass it on
this.worldName = worldName; location = new Location(world, x, y, z, yaw, pitch);
this.x = x; }
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
// This returns the actual Location
public final Location getLocation() {
// make sure Location is initialized before returning it
initLocation();
return location;
}
// change the Location
public final void setLocation(Location loc) {
this.location = loc;
this.worldName = loc.getWorld().getName();
this.x = loc.getX();
this.y = loc.getY();
this.z = loc.getZ();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
}
// This initializes the Location public final String getWorldName() {
private void initLocation() { return worldName;
// if location is already initialized, simply return }
if (location != null) {
return;
}
// get World; hopefully it's initialized at this point public final double getX() {
World world = Bukkit.getWorld(worldName); return x;
if (world == null) { }
return;
}
// store the Location for future calls, and pass it on public final double getY() {
location = new Location(world, x, y, z, yaw, pitch); return y;
} }
public final double getZ() {
return z;
}
public final String getWorldName() { public final double getPitch() {
return worldName; return pitch;
} }
public final double getX() { public final double getYaw() {
return x; return yaw;
} }
public final double getY() {
return y;
}
public final double getZ() {
return z;
}
public final double getPitch() {
return pitch;
}
public final double getYaw() {
return yaw;
}
} }

View File

@ -1,32 +1,48 @@
package com.massivecraft.factions.util; package com.massivecraft.factions.util;
import com.google.gson.*; import com.google.gson.*;
import com.massivecraft.factions.SavageFactions;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.logging.Level;
public class LocationTypeAdapter implements JsonSerializer<Location>, JsonDeserializer<Location> { public class LocationTypeAdapter implements JsonSerializer<Location>, JsonDeserializer<Location> {
@Override @Override
public JsonElement serialize(Location location, Type type, JsonSerializationContext jsonSerializationContext) { public JsonElement serialize(Location location, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
try {
object.add("x", new JsonPrimitive(location.getX())); object.add("x", new JsonPrimitive(location.getX()));
object.add("y", new JsonPrimitive(location.getY())); object.add("y", new JsonPrimitive(location.getY()));
object.add("z", new JsonPrimitive(location.getZ())); object.add("z", new JsonPrimitive(location.getZ()));
object.add("world", new JsonPrimitive(location.getWorld().toString())); object.add("world", new JsonPrimitive(location.getWorld().toString()));
return object; return object;
} catch (Exception ex) {
ex.printStackTrace();
SavageFactions.plugin.log(Level.WARNING, "Error encountered while serializing a Location.");
return object;
}
} }
@Override @Override
public Location deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { public Location deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) {
JsonObject object = jsonElement.getAsJsonObject(); JsonObject object = jsonElement.getAsJsonObject();
try {
return new Location(Bukkit.getWorld(object.get("world").getAsString()), return new Location(Bukkit.getWorld(object.get("world").getAsString()),
object.get("x").getAsDouble(), object.get("x").getAsDouble(),
object.get("y").getAsDouble(), object.get("y").getAsDouble(),
object.get("z").getAsDouble()); object.get("z").getAsDouble());
} catch (Exception ex) {
ex.printStackTrace();
SavageFactions.plugin.log(Level.WARNING, "Error encountered while" +
" deserializing a Location.");
return null;
}
} }