Skip to content

Commit

Permalink
Use old Gson location if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Aug 4, 2018
1 parent cc88e6b commit 7f94e21
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 104 deletions.
@@ -1,12 +1,12 @@
package net.ME1312.SubServers.Client.Bukkit.Library.Config;

import com.google.gson.Gson;
import net.ME1312.SubServers.Client.Bukkit.Library.Util;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.*;

/**
Expand Down Expand Up @@ -1053,6 +1053,13 @@ public String toString() {
* @return JSON
*/
public String toJSON() {
return new Gson().toJson(get(), Map.class);
try {
Class<?> gson = Class.forName(((Util.getDespiteException(() -> Class.forName("com.google.gson.Gson") != null, false)?"":"org.bukkit.craftbukkit.libs.")) + "com.google.gson.Gson");
//Class<?> gson = com.google.gson.Gson.class;
return (String) gson.getMethod("toJson", Object.class, Type.class).invoke(gson.newInstance(), get(), Map.class);
} catch (Exception e) {
e.printStackTrace();
return "{}";
}
}
}
@@ -1,7 +1,7 @@
package net.ME1312.SubServers.Client.Bukkit.Network.Encryption;

import com.google.gson.Gson;
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLSection;
import net.ME1312.SubServers.Client.Bukkit.SubAPI;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
Expand Down Expand Up @@ -270,11 +270,11 @@ public static int decrypt(String password, InputStream input, OutputStream outpu
* @param data Encrypted Data Array
* @return JSON Data
*/
@SuppressWarnings("unchecked")
@SuppressWarnings("deprecation")
public YAMLSection decrypt(String key, byte[] data) throws Exception {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
decrypt(key, new ByteArrayInputStream(data), bytes);
return new YAMLSection(new Gson().fromJson(new String(bytes.toByteArray(), StandardCharsets.UTF_8), Map.class));
return new YAMLSection(SubAPI.getInstance().getInternals().parseJSON(new String(bytes.toByteArray(), StandardCharsets.UTF_8)));
}

/**
Expand Down
@@ -1,7 +1,5 @@
package net.ME1312.SubServers.Client.Bukkit.Network;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import net.ME1312.SubServers.Client.Bukkit.Event.SubNetworkConnectEvent;
import net.ME1312.SubServers.Client.Bukkit.Event.SubNetworkDisconnectEvent;
import net.ME1312.SubServers.Client.Bukkit.Library.Config.YAMLSection;
Expand Down Expand Up @@ -66,8 +64,8 @@ public byte[] encrypt(String key, YAMLSection data) {
}
@Override
@SuppressWarnings("unchecked")
public YAMLSection decrypt(String key, byte[] data) {
return new YAMLSection(new Gson().fromJson(new String(data, StandardCharsets.UTF_8), Map.class));
public YAMLSection decrypt(String key, byte[] data) throws Exception {
return new YAMLSection(plugin.parseJSON(new String(data, StandardCharsets.UTF_8)));
}
};

Expand Down Expand Up @@ -144,12 +142,16 @@ private void loop() {
}
});
}
} catch (JsonParseException | YAMLException e) {
new IllegalPacketException("Unknown Packet Format: " + input).printStackTrace();
} catch (IllegalPacketException e) {
e.printStackTrace();
} catch (Exception e) {
new InvocationTargetException(e, "Exception while decoding packet").printStackTrace();
Class<?> gsone = Class.forName(((Util.getDespiteException(() -> Class.forName("com.google.gson.JsonParseException") != null, false)?"":"org.bukkit.craftbukkit.libs.")) + "com.google.gson.JsonParseException");
//Class<?> gsone = com.google.gson.JsonParseException.class;
if (e instanceof YAMLException || gsone.isInstance(e)) {
new IllegalPacketException("Unknown Packet Format: " + input).printStackTrace();
} else {
new InvocationTargetException(e, "Exception while decoding packet").printStackTrace();
}
}
}
try {
Expand Down

0 comments on commit 7f94e21

Please sign in to comment.