- Fix NH bug & Gson not liking LocalDateTime

This commit is contained in:
spacerocket62 2022-02-06 19:43:37 -08:00
parent e70a01868d
commit c2206d0079
3 changed files with 31 additions and 3 deletions

View File

@ -7,6 +7,9 @@ import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import com.google.gson.GsonBuilder;
import dev.plex.util.adapter.LocalDateTimeAdapter;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -41,11 +44,11 @@ public class Punishment
public String toJSON() public String toJSON()
{ {
return new Gson().toJson(this); return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()).create().toJson(this);
} }
public static Punishment fromJson(String json) public static Punishment fromJson(String json)
{ {
return new Gson().fromJson(json, Punishment.class); return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()).create().fromJson(json, Punishment.class);
} }
} }

View File

@ -75,6 +75,12 @@ public class MojangUtils
{ {
e.printStackTrace(); e.printStackTrace();
} }
return names.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList()); return names.entrySet().stream().sorted(Map.Entry.comparingByValue((o1, o2) -> {
if (o1 == null || o2 == null)
{
return 1;
}
return o1.compareTo(o2);
})).collect(Collectors.toList());
} }
} }

View File

@ -0,0 +1,19 @@
package dev.plex.util.adapter;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeAdapter implements JsonSerializer<LocalDateTime>
{
@Override
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context)
{
return new JsonPrimitive(src.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
}