Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-04 23:30:24 +01:00
Stop using new Gson() everywhere (#505)
Dieser Commit ist enthalten in:
Ursprung
866e494d3c
Commit
8ea3623a79
@ -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;
|
||||
|
@ -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"))
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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<List<Metadata>> METADATA_LIST = new MetadataList1_8Type();
|
||||
@Deprecated
|
||||
public static Type<Metadata> METADATA = new Metadata1_8Type();
|
||||
|
||||
private static final Gson gson = new GsonBuilder().create();
|
||||
public static final ValueTransformer<String, String> FIX_JSON = new ValueTransformer<String, String>(Type.STRING) {
|
||||
@Override
|
||||
public String transform(PacketWrapper wrapper, String line) {
|
||||
return fixJson(line);
|
||||
}
|
||||
};
|
||||
@Deprecated
|
||||
public static Type<List<Metadata>> METADATA_LIST = new MetadataList1_8Type();
|
||||
@Deprecated
|
||||
public static Type<Metadata> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
31
common/src/main/java/us/myles/ViaVersion/util/GsonUtil.java
Normale Datei
31
common/src/main/java/us/myles/ViaVersion/util/GsonUtil.java
Normale Datei
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren