1
mirror of https://github.com/CarmJos/MoeTeleport.git synced 2024-09-19 21:35:56 +00:00

修复报错空lastLocation的问题

This commit is contained in:
Carm Jos 2022-02-25 05:33:53 +08:00
parent 0f112280be
commit 623fc36c20
2 changed files with 9 additions and 5 deletions

View File

@ -2,9 +2,11 @@ package cc.carm.plugin.moeteleport.storage;
import cc.carm.plugin.moeteleport.configuration.location.DataLocation; import cc.carm.plugin.moeteleport.configuration.location.DataLocation;
import org.bukkit.Location; import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
public class DataSerializer { public class DataSerializer {
@ -15,12 +17,12 @@ public class DataSerializer {
return after; return after;
} }
public static String serializeLocation(DataLocation loc) { public static @Nullable String serializeLocation(@Nullable DataLocation loc) {
return loc.serializeToText(); return Optional.ofNullable(loc).map(DataLocation::serializeToText).orElse(null);
} }
public static String serializeLocation(Location loc) { public static @Nullable String serializeLocation(@Nullable Location loc) {
return serializeLocation(new DataLocation(loc)); return serializeLocation(Optional.ofNullable(loc).map(DataLocation::new).orElse(null));
} }

View File

@ -57,7 +57,9 @@ public class JSONStorage extends FileBasedStorage {
@Override @Override
public void saveUserData(@NotNull UserData data) throws Exception { public void saveUserData(@NotNull UserData data) throws Exception {
JsonObject dataObject = new JsonObject(); JsonObject dataObject = new JsonObject();
dataObject.addProperty("lastLocation", DataSerializer.serializeLocation(data.getLastLocation())); if (data.getLastLocation() != null) {
dataObject.addProperty("lastLocation", DataSerializer.serializeLocation(data.getLastLocation()));
}
dataObject.add("homes", GSON.toJsonTree(DataSerializer.serializeLocationsMap(data.getHomeLocations()))); dataObject.add("homes", GSON.toJsonTree(DataSerializer.serializeLocationsMap(data.getHomeLocations())));
FileWriter writer = new FileWriter(new File(getDataFolder(), data.getUserUUID() + ".json")); FileWriter writer = new FileWriter(new File(getDataFolder(), data.getUserUUID() + ".json"));