From 8ea3623a79e6e343c5efdede4a21c0d98334762a Mon Sep 17 00:00:00 2001 From: Matsv Date: Tue, 27 Sep 2016 19:15:39 +0200 Subject: [PATCH] Stop using new Gson() everywhere (#505) --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 6 +-- .../commands/defaultsubs/DumpSubCmd.java | 8 ++-- .../protocols/base/BaseProtocol.java | 11 ++---- .../protocol1_9to1_8/Protocol1_9TO1_8.java | 37 +++++++++---------- .../myles/ViaVersion/update/UpdateUtil.java | 6 +-- .../us/myles/ViaVersion/util/EntityUtil.java | 1 + .../us/myles/ViaVersion/util/GsonUtil.java | 31 ++++++++++++++++ .../us/myles/ViaVersion/SpongePlugin.java | 6 +-- 8 files changed, 64 insertions(+), 42 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/util/GsonUtil.java diff --git a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 72d7cacb2..9cc8ef380 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion; -import com.google.gson.Gson; import com.google.gson.JsonObject; import lombok.Getter; import org.bukkit.Bukkit; @@ -20,8 +19,9 @@ import us.myles.ViaVersion.bukkit.commands.BukkitCommandSender; import us.myles.ViaVersion.bukkit.platform.BukkitViaAPI; import us.myles.ViaVersion.bukkit.platform.BukkitViaInjector; import us.myles.ViaVersion.bukkit.platform.BukkitViaLoader; -import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.bukkit.util.NMSUtil; +import us.myles.ViaVersion.dump.PluginInfo; +import us.myles.ViaVersion.util.GsonUtil; import java.util.ArrayList; import java.util.List; @@ -240,7 +240,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform { for (Plugin p : Bukkit.getPluginManager().getPlugins()) plugins.add(new PluginInfo(p.isEnabled(), p.getDescription().getName(), p.getDescription().getVersion(), p.getDescription().getMain(), p.getDescription().getAuthors())); - platformSpecific.add("plugins", new Gson().toJsonTree(plugins)); + platformSpecific.add("plugins", GsonUtil.getGson().toJsonTree(plugins)); // TODO more? ProtocolLib things etc? return platformSpecific; diff --git a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java index 9938cab8a..afe0ac19f 100644 --- a/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java +++ b/common/src/main/java/us/myles/ViaVersion/commands/defaultsubs/DumpSubCmd.java @@ -1,7 +1,5 @@ package us.myles.ViaVersion.commands.defaultsubs; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.api.Via; @@ -10,6 +8,7 @@ import us.myles.ViaVersion.api.command.ViaSubCommand; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.dump.DumpTemplate; import us.myles.ViaVersion.dump.VersionInfo; +import us.myles.ViaVersion.util.GsonUtil; import java.io.InputStreamReader; import java.io.InvalidObjectException; @@ -21,7 +20,6 @@ import java.util.Map; import java.util.logging.Level; public class DumpSubCmd extends ViaSubCommand { - private final Gson gson = new GsonBuilder().setPrettyPrinting().create(); @Override public String name() { @@ -59,10 +57,10 @@ public class DumpSubCmd extends ViaSubCommand { con.setDoOutput(true); OutputStream out = con.getOutputStream(); - out.write(gson.toJson(template).getBytes(Charset.forName("UTF-8"))); + out.write(GsonUtil.getGson().toJson(template).getBytes(Charset.forName("UTF-8"))); out.close(); - JsonObject output = gson.fromJson(new InputStreamReader(con.getInputStream()), JsonObject.class); + JsonObject output = GsonUtil.getGson().fromJson(new InputStreamReader(con.getInputStream()), JsonObject.class); con.getInputStream().close(); if (!output.has("key")) diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java index f6130020b..c95b51fba 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java @@ -1,9 +1,6 @@ package us.myles.ViaVersion.protocols.base; import com.google.common.base.Joiner; -import com.google.common.base.Strings; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import io.netty.channel.ChannelFuture; @@ -23,13 +20,13 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; +import us.myles.ViaVersion.util.GsonUtil; import java.util.List; import java.util.UUID; import java.util.logging.Level; public class BaseProtocol extends Protocol { - private final static Gson gson = new GsonBuilder().create(); // TODO: Possibly global gson provider? @Override protected void registerPackets() { @@ -46,12 +43,12 @@ public class BaseProtocol extends Protocol { ProtocolInfo info = wrapper.user().get(ProtocolInfo.class); String originalStatus = wrapper.get(Type.STRING, 0); try { - JsonObject json = gson.fromJson(originalStatus, JsonObject.class); + JsonObject json = GsonUtil.getGson().fromJson(originalStatus, JsonObject.class); JsonObject version = json.get("version").getAsJsonObject(); int protocolVersion = ((Long) version.get("protocol").getAsLong()).intValue(); if (Via.getConfig().isSendSupportedVersions()) //Send supported versions - version.add("supportedVersions", gson.toJsonTree(Via.getAPI().getSupportedVersions())); + version.add("supportedVersions", GsonUtil.getGson().toJsonTree(Via.getAPI().getSupportedVersions())); if (ProtocolRegistry.SERVER_PROTOCOL == -1) // Set the Server protocol if the detection on startup failed ProtocolRegistry.SERVER_PROTOCOL = protocolVersion; @@ -70,7 +67,7 @@ public class BaseProtocol extends Protocol { if (Via.getConfig().getBlockedProtocols().contains(info.getProtocolVersion())) version.addProperty("protocol", -1); // Show blocked versions as outdated - wrapper.set(Type.STRING, 0, gson.toJson(json)); // Update value + wrapper.set(Type.STRING, 0, GsonUtil.getGson().toJson(json)); // Update value } catch (JsonParseException e) { e.printStackTrace(); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java index 4f4d33dda..ed8a0e456 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/Protocol1_9TO1_8.java @@ -1,7 +1,5 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; @@ -19,22 +17,21 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTransla import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.*; +import us.myles.ViaVersion.util.GsonUtil; import java.util.List; public class Protocol1_9TO1_8 extends Protocol { - @Deprecated - public static Type> METADATA_LIST = new MetadataList1_8Type(); - @Deprecated - public static Type METADATA = new Metadata1_8Type(); - - private static final Gson gson = new GsonBuilder().create(); public static final ValueTransformer FIX_JSON = new ValueTransformer(Type.STRING) { @Override public String transform(PacketWrapper wrapper, String line) { return fixJson(line); } }; + @Deprecated + public static Type> METADATA_LIST = new MetadataList1_8Type(); + @Deprecated + public static Type METADATA = new Metadata1_8Type(); public static String fixJson(String line) { if (line == null || line.equalsIgnoreCase("null")) { @@ -48,7 +45,7 @@ public class Protocol1_9TO1_8 extends Protocol { } } try { - gson.fromJson(line, JsonObject.class); + GsonUtil.getGson().fromJson(line, JsonObject.class); } catch (Exception e) { if (Via.getConfig().isForceJsonTransform()) { return constructJson(line); @@ -63,13 +60,23 @@ public class Protocol1_9TO1_8 extends Protocol { private static String constructJson(String text) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("text", text); - return gson.toJson(jsonObject); + return GsonUtil.getGson().toJson(jsonObject); } public static Item getHandItem(final UserConnection info) { return Via.getManager().getProviders().get(HandItemProvider.class).getHandItem(info); } + public static boolean isSword(int id) { + if (id == 267) return true; // Iron + if (id == 268) return true; // Wood + if (id == 272) return true; // Stone + if (id == 276) return true; // Diamond + if (id == 283) return true; // Gold + + return false; + } + @Override protected void registerPackets() { SpawnPackets.register(this); @@ -113,14 +120,4 @@ public class Protocol1_9TO1_8 extends Protocol { // Place block tracker userConnection.put(new PlaceBlockTracker(userConnection)); } - - public static boolean isSword(int id) { - if (id == 267) return true; // Iron - if (id == 268) return true; // Wood - if (id == 272) return true; // Stone - if (id == 276) return true; // Diamond - if (id == 283) return true; // Gold - - return false; - } } diff --git a/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java b/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java index 254d5919b..32abb3d5a 100644 --- a/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java +++ b/common/src/main/java/us/myles/ViaVersion/update/UpdateUtil.java @@ -1,11 +1,10 @@ package us.myles.ViaVersion.update; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.util.GsonUtil; import java.io.BufferedReader; import java.io.IOException; @@ -21,7 +20,6 @@ public class UpdateUtil { private final static String URL = "http://api.spiget.org/v2/resources/"; private final static int PLUGIN = 19254; private final static String LATEST_VERSION = "/versions/latest"; - private final static Gson gson = new GsonBuilder().create(); public static void sendUpdateMessage(final UUID uuid) { Via.getPlatform().runAsync(new Runnable() { @@ -108,7 +106,7 @@ public class UpdateUtil { br.close(); JsonObject statistics; try { - statistics = gson.fromJson(content, JsonObject.class); + statistics = GsonUtil.getGson().fromJson(content, JsonObject.class); } catch (JsonParseException e) { e.printStackTrace(); return null; diff --git a/common/src/main/java/us/myles/ViaVersion/util/EntityUtil.java b/common/src/main/java/us/myles/ViaVersion/util/EntityUtil.java index d1a3075dd..1daa36912 100644 --- a/common/src/main/java/us/myles/ViaVersion/util/EntityUtil.java +++ b/common/src/main/java/us/myles/ViaVersion/util/EntityUtil.java @@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import us.myles.ViaVersion.api.Via; +// 1.10 Entity / Object ids public class EntityUtil { public static EntityType getTypeFromID(int typeID, boolean isObject) { diff --git a/common/src/main/java/us/myles/ViaVersion/util/GsonUtil.java b/common/src/main/java/us/myles/ViaVersion/util/GsonUtil.java new file mode 100644 index 000000000..b60c6f9e0 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/util/GsonUtil.java @@ -0,0 +1,31 @@ +package us.myles.ViaVersion.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import lombok.Getter; +import lombok.experimental.UtilityClass; + +@UtilityClass +@Getter +public class GsonUtil { + private final Gson gson = getGsonBuilder().create(); + + /** + * Get google's Gson magic + * + * @return Gson instance + */ + public Gson getGson() { + return gson; + } + + /** + * Get the GsonBuilder in case you want to add other stuff + * + * @return GsonBuilder instance + */ + public GsonBuilder getGsonBuilder() { + return new GsonBuilder(); + } + +} diff --git a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java index fb52fb9c1..d2725bf6e 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java +++ b/sponge/src/main/java/us/myles/ViaVersion/SpongePlugin.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion; -import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.inject.Inject; import org.spongepowered.api.Game; @@ -19,7 +18,7 @@ import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.dump.PluginInfo; -import us.myles.ViaVersion.sponge.*; +import us.myles.ViaVersion.sponge.VersionInfo; import us.myles.ViaVersion.sponge.commands.SpongeCommandHandler; import us.myles.ViaVersion.sponge.commands.SpongeCommandSender; import us.myles.ViaVersion.sponge.platform.SpongeConfigAPI; @@ -27,6 +26,7 @@ import us.myles.ViaVersion.sponge.platform.SpongeViaAPI; import us.myles.ViaVersion.sponge.platform.SpongeViaInjector; import us.myles.ViaVersion.sponge.platform.SpongeViaLoader; import us.myles.ViaVersion.sponge.util.LoggerWrapper; +import us.myles.ViaVersion.util.GsonUtil; import java.io.File; import java.util.ArrayList; @@ -190,7 +190,7 @@ public class SpongePlugin implements ViaPlatform { p.getAuthors() )); } - platformSpecific.add("plugins", new Gson().toJsonTree(plugins)); + platformSpecific.add("plugins", GsonUtil.getGson().toJsonTree(plugins)); return platformSpecific; }