diff options
author | Cow <cow@volloeko.de> | 2020-05-04 12:54:50 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2020-05-04 12:54:50 +0200 |
commit | 6f33fc424111ce46dcabd85f214db68f4ddc8b9d (patch) | |
tree | 1e83b126d347c6e9face12222402997864595598 /src | |
parent | cba81609bd12038559754b07973ec00f598defaa (diff) | |
download | Cowlection-6f33fc424111ce46dcabd85f214db68f4ddc8b9d.tar.gz Cowlection-6f33fc424111ce46dcabd85f214db68f4ddc8b9d.tar.bz2 Cowlection-6f33fc424111ce46dcabd85f214db68f4ddc8b9d.zip |
Cleaned-up some GSON stuff
Diffstat (limited to 'src')
6 files changed, 53 insertions, 33 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/friends/Friend.java b/src/main/java/eu/olli/cowmoonication/friends/Friend.java index 889e087..88b9d87 100644 --- a/src/main/java/eu/olli/cowmoonication/friends/Friend.java +++ b/src/main/java/eu/olli/cowmoonication/friends/Friend.java @@ -1,17 +1,19 @@ package eu.olli.cowmoonication.friends; -import com.google.gson.InstanceCreator; - -import java.lang.reflect.Type; import java.util.Objects; import java.util.UUID; public class Friend { - public static final Friend FRIEND_NOT_FOUND = new Friend(null, null, -1); + public static final Friend FRIEND_NOT_FOUND = new Friend(); private UUID id; private String name; private long lastChecked; + static { + // uuid & name are null + FRIEND_NOT_FOUND.setLastChecked(0); + } + /** * No-args constructor for GSON */ @@ -19,14 +21,6 @@ public class Friend { this.lastChecked = System.currentTimeMillis(); } - private Friend(UUID uuid, String name, long lastChecked) { - this.id = uuid; - this.name = name; - if (lastChecked > 0) { - this.lastChecked = lastChecked; - } - } - public UUID getUuid() { return id; } @@ -68,11 +62,4 @@ public class Friend { public int hashCode() { return Objects.hash(id); } - - public static class FriendCreator implements InstanceCreator { - @Override - public Friend createInstance(Type type) { - return new Friend(); - } - } } diff --git a/src/main/java/eu/olli/cowmoonication/friends/Friends.java b/src/main/java/eu/olli/cowmoonication/friends/Friends.java index 8c29d16..2fb80f0 100644 --- a/src/main/java/eu/olli/cowmoonication/friends/Friends.java +++ b/src/main/java/eu/olli/cowmoonication/friends/Friends.java @@ -1,10 +1,10 @@ package eu.olli.cowmoonication.friends; -import com.google.gson.Gson; import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; import eu.olli.cowmoonication.Cowmoonication; import eu.olli.cowmoonication.util.ApiUtils; +import eu.olli.cowmoonication.util.GsonUtils; import eu.olli.cowmoonication.util.TickDelay; import io.netty.util.internal.ConcurrentSet; import net.minecraft.event.ClickEvent; @@ -136,7 +136,7 @@ public class Friends { public synchronized void saveBestFriends() { try { - String bestFriendsJsonZoned = dumpJson(this.bestFriends); + String bestFriendsJsonZoned = GsonUtils.toJson(this.bestFriends); FileUtils.writeStringToFile(this.bestFriendsFile, bestFriendsJsonZoned, StandardCharsets.UTF_8); } catch (IOException e) { main.getLogger().error("Couldn't save best friends", e); @@ -158,11 +158,7 @@ public class Friends { private Set<Friend> parseJson(String bestFriendsData) { Type collectionType = new TypeToken<Set<Friend>>() { }.getType(); - return new Gson().fromJson(bestFriendsData, collectionType); - } - - private String dumpJson(Set<Friend> bestFriends) { - return new Gson().toJson(bestFriends); + return GsonUtils.fromJson(bestFriendsData, collectionType); } private enum UpdateStatus { diff --git a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java index 9f013f3..2dc41b2 100644 --- a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java +++ b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java @@ -8,6 +8,7 @@ import com.mojang.util.UUIDTypeAdapter; import eu.olli.cowmoonication.Cowmoonication; import eu.olli.cowmoonication.config.MooConfig; import eu.olli.cowmoonication.friends.Friend; +import org.apache.http.HttpStatus; import java.io.BufferedReader; import java.io.IOException; @@ -27,7 +28,6 @@ public class ApiUtils { private static final String STALKING_URL_OFFICIAL = "https://api.hypixel.net/status?key=%s&uuid=%s"; private static final String STALKING_URL_UNOFFICIAL = "https://api.slothpixel.me/api/players/%s"; private static ExecutorService pool = Executors.newCachedThreadPool(); - private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(Friend.class, new Friend.FriendCreator()).create(); private ApiUtils() { } @@ -41,7 +41,7 @@ public class ApiUtils { if (reader == null) { return Friend.FRIEND_NOT_FOUND; } else { - return gson.fromJson(reader, Friend.class); + return GsonUtils.fromJson(reader, Friend.class); } } catch (IOException e) { e.printStackTrace(); @@ -76,7 +76,7 @@ public class ApiUtils { private static HyStalking stalkPlayer(Friend friend) { try (BufferedReader reader = makeApiCall(String.format(STALKING_URL_OFFICIAL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { if (reader != null) { - return gson.fromJson(reader, HyStalking.class); + return GsonUtils.fromJson(reader, HyStalking.class); } } catch (IOException e) { e.printStackTrace(); @@ -91,7 +91,7 @@ public class ApiUtils { private static SlothStalking stalkOfflinePlayer(Friend stalkedPlayer) { try (BufferedReader reader = makeApiCall(String.format(STALKING_URL_UNOFFICIAL, UUIDTypeAdapter.fromUUID(stalkedPlayer.getUuid())))) { if (reader != null) { - return gson.fromJson(reader, SlothStalking.class); + return GsonUtils.fromJson(reader, SlothStalking.class); } } catch (IOException e) { e.printStackTrace(); @@ -106,7 +106,7 @@ public class ApiUtils { connection.addRequestProperty("User-Agent", "Forge Mod " + Cowmoonication.MODNAME + "/" + Cowmoonication.VERSION + " (" + Cowmoonication.GITURL + ")"); connection.getResponseCode(); - if (connection.getResponseCode() == 204) { + if (connection.getResponseCode() == HttpStatus.SC_NO_CONTENT) { // http status 204 return null; } else { BufferedReader reader; diff --git a/src/main/java/eu/olli/cowmoonication/util/GsonUtils.java b/src/main/java/eu/olli/cowmoonication/util/GsonUtils.java new file mode 100644 index 0000000..4a723ec --- /dev/null +++ b/src/main/java/eu/olli/cowmoonication/util/GsonUtils.java @@ -0,0 +1,28 @@ +package eu.olli.cowmoonication.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.mojang.util.UUIDTypeAdapter; + +import java.io.Reader; +import java.lang.reflect.Type; +import java.util.UUID; + +public final class GsonUtils { + private static Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); + + private GsonUtils() { + } + + public static <T> T fromJson(String json, Type clazz) { + return gson.fromJson(json, clazz); + } + + public static <T> T fromJson(Reader json, Class<T> clazz) { + return gson.fromJson(json, clazz); + } + + public static String toJson(Object object) { + return gson.toJson(object); + } +} diff --git a/src/main/java/eu/olli/cowmoonication/util/HyStalking.java b/src/main/java/eu/olli/cowmoonication/util/HyStalking.java index a1f11ae..19f2362 100644 --- a/src/main/java/eu/olli/cowmoonication/util/HyStalking.java +++ b/src/main/java/eu/olli/cowmoonication/util/HyStalking.java @@ -7,7 +7,10 @@ public class HyStalking { private String cause; private HySession session; - public HyStalking() { + /** + * No-args constructor for GSON + */ + private HyStalking() { } public boolean isSuccess() { @@ -28,7 +31,10 @@ public class HyStalking { private String mode; private String map; - public HySession() { + /** + * No-args constructor for GSON + */ + private HySession() { } public boolean isOnline() { diff --git a/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java b/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java index 1cc7c22..9e659ec 100644 --- a/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java +++ b/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java @@ -9,6 +9,9 @@ public class SlothStalking { private long last_logout; private String last_game; + /** + * No-args constructor for GSON + */ public SlothStalking() { } |