diff --git a/pom.xml b/pom.xml index 03dff9ba8..89ab0c6de 100644 --- a/pom.xml +++ b/pom.xml @@ -41,9 +41,9 @@ - org.json - json - 20180813 + com.google.code.gson + gson + 2.8.6 org.yaml @@ -56,5 +56,4 @@ 29.0-jre - \ No newline at end of file diff --git a/src/main/java/de/bixilon/minosoft/Minosoft.java b/src/main/java/de/bixilon/minosoft/Minosoft.java index 46acf12a3..6d8d15bad 100644 --- a/src/main/java/de/bixilon/minosoft/Minosoft.java +++ b/src/main/java/de/bixilon/minosoft/Minosoft.java @@ -13,6 +13,7 @@ package de.bixilon.minosoft; +import com.google.gson.JsonObject; import de.bixilon.minosoft.config.Configuration; import de.bixilon.minosoft.config.GameConfiguration; import de.bixilon.minosoft.game.datatypes.Mappings; @@ -30,12 +31,13 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.util.FolderUtil; import de.bixilon.minosoft.util.OSUtil; import de.bixilon.minosoft.util.Util; -import org.json.JSONException; -import org.json.JSONObject; import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; public class Minosoft { static Configuration config; @@ -65,8 +67,7 @@ public class Minosoft { Log.info("Loading all mappings..."); long mappingsStart = System.currentTimeMillis(); loadMappings(); - Log.info(String.format("Mappings loaded in a total of %sms", (System.currentTimeMillis() - mappingsStart))); - System.exit(0); + Log.info(String.format("Mappings loaded within %sms", (System.currentTimeMillis() - mappingsStart))); checkClientToken(); @@ -143,17 +144,15 @@ public class Minosoft { } long startTime = System.currentTimeMillis(); for (Map.Entry mappingSet : mappingsHashMap.entrySet()) { - JSONObject data = Util.readJsonFromFile(Config.homeDir + String.format("assets/mapping/%s/%s.json", version.getVersionString(), mappingSet.getKey())); - for (Iterator mods = data.keys(); mods.hasNext(); ) { - // key = mod name - String mod = mods.next(); - JSONObject modJSON = data.getJSONObject(mod); + JsonObject data = Util.readJsonFromFile(Config.homeDir + String.format("assets/mapping/%s/%s.json", version.getVersionString(), mappingSet.getKey())); + for (String mod : data.keySet()) { + JsonObject modJSON = data.getAsJsonObject(mod); switch (mappingSet.getValue()) { case REGISTRIES: - Items.load(mod, modJSON.getJSONObject("item").getJSONObject("entries"), version); - Entities.load(mod, modJSON.getJSONObject("entity_type").getJSONObject("entries"), version); - Enchantments.load(mod, modJSON.getJSONObject("enchantment").getJSONObject("entries"), version); - Statistics.load(mod, modJSON.getJSONObject("custom_stat").getJSONObject("entries"), version); + Items.load(mod, modJSON.getAsJsonObject("item").getAsJsonObject("entries"), version); + Entities.load(mod, modJSON.getAsJsonObject("entity_type").getAsJsonObject("entries"), version); + Enchantments.load(mod, modJSON.getAsJsonObject("enchantment").getAsJsonObject("entries"), version); + Statistics.load(mod, modJSON.getAsJsonObject("custom_stat").getAsJsonObject("entries"), version); break; case BLOCKS: Blocks.load(mod, modJSON, version); @@ -163,7 +162,7 @@ public class Minosoft { } Log.verbose(String.format("Loaded mappings for version %s in %dms (%s)", version, (System.currentTimeMillis() - startTime), version.getReleaseName())); } - } catch (IOException | JSONException e) { + } catch (IOException e) { Log.fatal("Error occurred while loading version mapping: " + e.getLocalizedMessage()); System.exit(1); } diff --git a/src/main/java/de/bixilon/minosoft/ServerListPing.java b/src/main/java/de/bixilon/minosoft/ServerListPing.java index a3632f08a..75c9f2cae 100644 --- a/src/main/java/de/bixilon/minosoft/ServerListPing.java +++ b/src/main/java/de/bixilon/minosoft/ServerListPing.java @@ -13,46 +13,45 @@ package de.bixilon.minosoft; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import de.bixilon.minosoft.game.datatypes.TextComponent; -import org.json.JSONException; -import org.json.JSONObject; - public class ServerListPing { - final JSONObject raw; + final JsonObject raw; - public ServerListPing(JSONObject json) { + public ServerListPing(JsonObject json) { this.raw = json; } public int getProtocolNumber() { - return raw.getJSONObject("version").getInt("protocol"); + return raw.getAsJsonObject("version").get("protocol").getAsInt(); } public String getServerBrand() { - return raw.getJSONObject("version").getString("name"); + return raw.getAsJsonObject("version").get("name").getAsString(); } public int getPlayerOnline() { - return raw.getJSONObject("players").getInt("online"); + return raw.getAsJsonObject("players").get("online").getAsInt(); } public int getMaxPlayers() { - return raw.getJSONObject("players").getInt("max"); + return raw.getAsJsonObject("players").get("max").getAsInt(); } public String getBase64EncodedFavicon() { - return raw.getString("favicon"); + return raw.get("favicon").getAsString(); } public TextComponent getMotd() { try { - return new TextComponent(raw.getJSONObject("description")); - } catch (JSONException ignored) { + return new TextComponent(raw.getAsJsonObject("description")); + } catch (JsonParseException ignored) { } - return new TextComponent(raw.getString("description")); + return new TextComponent(raw.get("description").getAsString()); } - public JSONObject getRaw() { + public JsonObject getRaw() { return this.raw; } } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/TextComponent.java b/src/main/java/de/bixilon/minosoft/game/datatypes/TextComponent.java index a6ca2ac5c..538541ba7 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/TextComponent.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/TextComponent.java @@ -13,27 +13,28 @@ package de.bixilon.minosoft.game.datatypes; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; import java.util.ArrayList; import java.util.List; public class TextComponent { - JSONObject json; + JsonObject json; public TextComponent(String raw) { if (raw == null) { - this.json = new JSONObject(); + this.json = new JsonObject(); return; } try { - this.json = new JSONObject(raw); - } catch (JSONException e) { + this.json = JsonParser.parseString(raw).getAsJsonObject(); + } catch (IllegalStateException e) { // not a text component, is a legacy string - this.json = new JSONObject(); - JSONArray extra = new JSONArray(); + this.json = new JsonObject(); + JsonArray extra = new JsonArray(); String[] paragraphSplit = raw.split("ยง"); @@ -75,7 +76,7 @@ public class TextComponent { break; case "r": //save and clear - extra.put(getExtraByAttributes(message.toString(), color, attributesList)); + extra.add(getExtraByAttributes(message.toString(), color, attributesList)); attributesList.clear(); color = ChatAttributes.WHITE; message = new StringBuilder(); @@ -84,7 +85,7 @@ public class TextComponent { } else { // save current if (!message.toString().isEmpty()) { - extra.put(getExtraByAttributes(message.toString(), color, attributesList)); + extra.add(getExtraByAttributes(message.toString(), color, attributesList)); message = new StringBuilder(); } color = ChatAttributes.byColor(colorCheck); @@ -98,55 +99,55 @@ public class TextComponent { } } // save - extra.put(getExtraByAttributes(message.toString(), color, attributesList)); + extra.add(getExtraByAttributes(message.toString(), color, attributesList)); - this.json.put("extra", extra); + this.json.add("extra", extra); } } - public TextComponent(JSONObject json) { + public TextComponent(JsonObject json) { this.json = json; } - static JSONObject getExtraByAttributes(String message, ChatAttributes color, List formatting) { - JSONObject ret = new JSONObject(); - ret.put("text", message); + static JsonObject getExtraByAttributes(String message, ChatAttributes color, List formatting) { + JsonObject ret = new JsonObject(); + ret.addProperty("text", message); if (color != null) { - ret.put("color", color.getName()); + ret.addProperty("color", color.getName()); } for (ChatAttributes attribute : formatting) { if (attribute == ChatAttributes.BOLD && !ret.has("bold")) { - ret.put("bold", true); + ret.addProperty("bold", true); } else if (attribute == ChatAttributes.ITALIC && !ret.has("italic")) { - ret.put("italic", true); + ret.addProperty("italic", true); } else if (attribute == ChatAttributes.UNDERLINED && !ret.has("underlined")) { - ret.put("underlined", true); + ret.addProperty("underlined", true); } else if (attribute == ChatAttributes.STRIKETHROUGH && !ret.has("strikethrough")) { - ret.put("strikethrough", true); + ret.addProperty("strikethrough", true); } else if (attribute == ChatAttributes.OBFUSCATED && !ret.has("obfuscated")) { - ret.put("obfuscated", true); + ret.addProperty("obfuscated", true); } } return ret; } public String getRawMessage() { - if (json.has("text") && json.getString("text").length() != 0) { - return json.getString("text"); + if (json.has("text") && json.get("text").getAsString().length() != 0) { + return json.get("text").getAsString(); } StringBuilder buffer = new StringBuilder(); if (json.has("extra")) { - JSONArray arr = json.getJSONArray("extra"); - for (int i = 0; i < arr.length(); i++) { - JSONObject object; + JsonArray arr = json.getAsJsonArray("extra"); + for (int i = 0; i < arr.size(); i++) { + JsonObject object; try { - object = arr.getJSONObject(i); - } catch (JSONException e) { + object = arr.get(i).getAsJsonObject(); + } catch (JsonParseException e) { // reset text - buffer.append(arr.getString(i)); + buffer.append(arr.get(i).getAsString()); continue; } - buffer.append(object.getString("text")); + buffer.append(object.get("text").getAsString()); } buffer.append(ChatAttributes.RESET); return buffer.toString(); @@ -154,46 +155,46 @@ public class TextComponent { return ""; } - public JSONObject getRaw() { + public JsonObject getRaw() { return this.json; } public String getColoredMessage() { - if (json.has("text") && json.getString("text").length() != 0) { - return json.getString("text"); + if (json.has("text") && json.get("text").getAsString().length() != 0) { + return json.get("text").getAsString(); } StringBuilder buffer = new StringBuilder(); if (json.has("extra")) { - JSONArray arr = json.getJSONArray("extra"); - for (int i = 0; i < arr.length(); i++) { - JSONObject object; + JsonArray arr = json.getAsJsonArray("extra"); + for (int i = 0; i < arr.size(); i++) { + JsonObject object; try { - object = arr.getJSONObject(i); - } catch (JSONException e) { + object = arr.get(i).getAsJsonObject(); + } catch (JsonParseException e) { // reset text buffer.append(ChatAttributes.RESET); - buffer.append(arr.getString(i)); + buffer.append(arr.get(i).getAsString()); continue; } - if (object.has("bold") && object.getBoolean("bold")) { + if (object.has("bold") && object.get("bold").getAsBoolean()) { buffer.append(ChatAttributes.BOLD); } if (object.has("color")) { - buffer.append(ChatAttributes.byName(object.getString("color"))); + buffer.append(ChatAttributes.byName(object.get("color").getAsString())); } - if (object.has("italic") && object.getBoolean("italic")) { + if (object.has("italic") && object.get("italic").getAsBoolean()) { buffer.append(ChatAttributes.ITALIC); } - if (object.has("underlined") && object.getBoolean("underlined")) { + if (object.has("underlined") && object.get("underlined").getAsBoolean()) { buffer.append(ChatAttributes.UNDERLINED); } - if (object.has("strikethrough") && object.getBoolean("strikethrough")) { + if (object.has("strikethrough") && object.get("strikethrough").getAsBoolean()) { buffer.append(ChatAttributes.STRIKETHROUGH); } - if (object.has("obfuscated") && object.getBoolean("obfuscated")) { + if (object.has("obfuscated") && object.get("obfuscated").getAsBoolean()) { buffer.append(ChatAttributes.OBFUSCATED); } - buffer.append(object.getString("text")); + buffer.append(object.get("text").getAsString()); } buffer.append(ChatAttributes.RESET); return buffer.toString(); diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java index f35f6f3da..cdbb28b15 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/blocks/Blocks.java @@ -13,13 +13,12 @@ package de.bixilon.minosoft.game.datatypes.objectLoader.blocks; */ import com.google.common.collect.HashBiMap; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import org.json.JSONArray; -import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; public class Blocks { public static Block nullBlock; @@ -454,36 +453,34 @@ public class Blocks { return blockMap.get(version).get(protocolId); } - public static void load(String mod, JSONObject json, ProtocolVersion version) { + public static void load(String mod, JsonObject json, ProtocolVersion version) { HashBiMap versionMapping = HashBiMap.create(); - for (Iterator identifiers = json.keys(); identifiers.hasNext(); ) { - String identifierName = identifiers.next(); - JSONObject identifierJSON = json.getJSONObject(identifierName); - JSONArray statesArray = identifierJSON.getJSONArray("states"); - for (int i = 0; i < statesArray.length(); i++) { - JSONObject statesJSON = statesArray.getJSONObject(i); + for (String identifierName : json.keySet()) { + JsonObject identifierJSON = json.getAsJsonObject(identifierName); + JsonArray statesArray = identifierJSON.getAsJsonArray("states"); + for (int i = 0; i < statesArray.size(); i++) { + JsonObject statesJSON = statesArray.get(i).getAsJsonObject(); if (statesJSON.has("properties")) { // properties are optional - JSONObject propertiesJSON = statesJSON.getJSONObject("properties"); + JsonObject propertiesJSON = statesJSON.getAsJsonObject("properties"); BlockRotation rotation = BlockRotation.NONE; if (propertiesJSON.has("facing")) { - rotation = rotationMapping.get(propertiesJSON.getString("facing")); + rotation = rotationMapping.get(propertiesJSON.get("facing").getAsString()); propertiesJSON.remove("facing"); } else if (propertiesJSON.has("rotation")) { - rotation = rotationMapping.get(propertiesJSON.getString("rotation")); + rotation = rotationMapping.get(propertiesJSON.get("rotation").getAsString()); propertiesJSON.remove("rotation"); } - BlockProperties[] properties = new BlockProperties[propertiesJSON.length()]; + BlockProperties[] properties = new BlockProperties[propertiesJSON.size()]; int ii = 0; - for (Iterator it = propertiesJSON.keys(); it.hasNext(); ) { - String propertyName = it.next(); + for (String propertyName : propertiesJSON.keySet()) { if (propertiesMapping.get(propertyName) == null) { throw new RuntimeException(String.format("Unknown block property: %s (identifier=%s)", propertyName, identifierName)); } - if (propertiesMapping.get(propertyName).get(propertiesJSON.getString(propertyName)) == null) { - throw new RuntimeException(String.format("Unknown block property: %s -> %s (identifier=%s)", propertyName, propertiesJSON.getString(propertyName), identifierName)); + if (propertiesMapping.get(propertyName).get(propertiesJSON.get(propertyName).getAsString()) == null) { + throw new RuntimeException(String.format("Unknown block property: %s -> %s (identifier=%s)", propertyName, propertiesJSON.get(propertyName).getAsString(), identifierName)); } - properties[ii] = propertiesMapping.get(propertyName).get(propertiesJSON.getString(propertyName)); + properties[ii] = propertiesMapping.get(propertyName).get(propertiesJSON.get(propertyName).getAsString()); ii++; } @@ -528,13 +525,13 @@ public class Blocks { blockMap.put(version, versionMapping); } - private static int getBlockId(JSONObject json, ProtocolVersion version) { - int blockId = json.getInt("id"); + private static int getBlockId(JsonObject json, ProtocolVersion version) { + int blockId = json.get("id").getAsInt(); if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) { // old format (with metadata) blockId <<= 4; if (json.has("meta")) { - blockId |= json.getInt("meta"); + blockId |= json.get("meta").getAsByte(); } } return blockId; diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/enchantments/Enchantments.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/enchantments/Enchantments.java index 67036095a..4f8176774 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/enchantments/Enchantments.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/enchantments/Enchantments.java @@ -14,12 +14,11 @@ package de.bixilon.minosoft.game.datatypes.objectLoader.enchantments; import com.google.common.collect.HashBiMap; +import com.google.gson.JsonObject; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; public class Enchantments { @@ -33,15 +32,14 @@ public class Enchantments { return enchantmentMap.get(version).get(protocolId); } - public static void load(String mod, JSONObject json, ProtocolVersion version) { + public static void load(String mod, JsonObject json, ProtocolVersion version) { HashBiMap versionMapping = HashBiMap.create(); - for (Iterator identifiers = json.keys(); identifiers.hasNext(); ) { - String identifierName = identifiers.next(); + for (String identifierName : json.keySet()) { Enchantment enchantment = new Enchantment(mod, identifierName); if (enchantmentList.contains(enchantment)) { enchantment = enchantmentList.get(enchantmentList.indexOf(enchantment)); } - versionMapping.put(json.getJSONObject(identifierName).getInt("id"), enchantment); + versionMapping.put(json.getAsJsonObject(identifierName).get("id").getAsInt(), enchantment); } enchantmentMap.put(version, versionMapping); } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/Entities.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/Entities.java index c86d3e03a..bf79f62d0 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/Entities.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/Entities.java @@ -14,13 +14,12 @@ package de.bixilon.minosoft.game.datatypes.objectLoader.entities; import com.google.common.collect.HashBiMap; +import com.google.gson.JsonObject; import de.bixilon.minosoft.game.datatypes.objectLoader.entities.mob.*; import de.bixilon.minosoft.game.datatypes.objectLoader.entities.objects.*; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import org.json.JSONObject; import java.util.HashMap; -import java.util.Iterator; public class Entities { @@ -157,11 +156,10 @@ public class Entities { return entityClassMap.get(identifier); } - public static void load(String mod, JSONObject json, ProtocolVersion version) { + public static void load(String mod, JsonObject json, ProtocolVersion version) { HashBiMap versionMapping = HashBiMap.create(); - for (Iterator identifiers = json.keys(); identifiers.hasNext(); ) { - String identifierName = identifiers.next(); - versionMapping.put(json.getJSONObject(identifierName).getInt("id"), mod + ":" + identifierName); + for (String identifierName : json.keySet()) { + versionMapping.put(json.getAsJsonObject(identifierName).get("id").getAsInt(), mod + ":" + identifierName); } entityMapping.put(version, versionMapping); } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/items/Items.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/items/Items.java index e991fb39d..23f592972 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/items/Items.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/entities/items/Items.java @@ -14,12 +14,11 @@ package de.bixilon.minosoft.game.datatypes.objectLoader.entities.items; import com.google.common.collect.HashBiMap; +import com.google.gson.JsonObject; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; public class Items { @@ -43,23 +42,22 @@ public class Items { return itemMap.get(version).get(protocolId); } - public static void load(String mod, JSONObject json, ProtocolVersion version) { + public static void load(String mod, JsonObject json, ProtocolVersion version) { HashBiMap versionMapping = HashBiMap.create(); - for (Iterator identifiers = json.keys(); identifiers.hasNext(); ) { - String identifierName = identifiers.next(); + for (String identifierName : json.keySet()) { Item item = getItem(mod, identifierName); if (item == null) { // does not exist. create item = new Item(mod, identifierName); itemList.add(item); } - JSONObject identifierJSON = json.getJSONObject(identifierName); - int itemId = identifierJSON.getInt("id"); + JsonObject identifierJSON = json.getAsJsonObject(identifierName); + int itemId = identifierJSON.get("id").getAsInt(); if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) { // old format (with metadata) itemId <<= 4; if (identifierJSON.has("meta")) { - itemId |= identifierJSON.getInt("meta"); + itemId |= identifierJSON.get("meta").getAsInt(); } } versionMapping.put(itemId, item); diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/statistics/Statistics.java b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/statistics/Statistics.java index f6aaefb7b..40e19da96 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/statistics/Statistics.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/objectLoader/statistics/Statistics.java @@ -14,12 +14,11 @@ package de.bixilon.minosoft.game.datatypes.objectLoader.statistics; import com.google.common.collect.HashBiMap; +import com.google.gson.JsonObject; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; public class Statistics { @@ -41,17 +40,16 @@ public class Statistics { return statisticsIdentifierMap.get(version).get(identifier); } - public static void load(String mod, JSONObject json, ProtocolVersion version) { + public static void load(String mod, JsonObject json, ProtocolVersion version) { HashBiMap versionIdMapping = HashBiMap.create(); HashBiMap versionIdentifierMapping = HashBiMap.create(); - for (Iterator identifiers = json.keys(); identifiers.hasNext(); ) { - String identifierName = identifiers.next(); + for (String identifierName : json.keySet()) { Statistic statistic = new Statistic(mod, identifierName); if (statisticList.contains(statistic)) { statistic = statisticList.get(statisticList.indexOf(statistic)); } - if (json.getJSONObject(identifierName).has("id")) { - versionIdMapping.put(json.getJSONObject(identifierName).getInt("id"), statistic); + if (json.getAsJsonObject(identifierName).has("id")) { + versionIdMapping.put(json.getAsJsonObject(identifierName).get("id").getAsInt(), statistic); } else { versionIdentifierMapping.put(identifierName, statistic); } diff --git a/src/main/java/de/bixilon/minosoft/mojang/api/MojangAccount.java b/src/main/java/de/bixilon/minosoft/mojang/api/MojangAccount.java index 737dccef9..654b5ddcb 100644 --- a/src/main/java/de/bixilon/minosoft/mojang/api/MojangAccount.java +++ b/src/main/java/de/bixilon/minosoft/mojang/api/MojangAccount.java @@ -13,10 +13,10 @@ package de.bixilon.minosoft.mojang.api; +import com.google.gson.JsonObject; import de.bixilon.minosoft.Config; import de.bixilon.minosoft.Minosoft; import de.bixilon.minosoft.util.Util; -import org.json.JSONObject; import java.util.UUID; @@ -30,15 +30,15 @@ public class MojangAccount { final String mojangUserName; - public MojangAccount(JSONObject json) { - this.accessToken = json.getString("accessToken"); - JSONObject profile = json.getJSONObject("selectedProfile"); - this.uuid = Util.formatUUID(profile.getString("id")); - this.playerName = profile.getString("name"); + public MojangAccount(JsonObject json) { + this.accessToken = json.get("accessToken").getAsString(); + JsonObject profile = json.get("selectedProfile").getAsJsonObject(); + this.uuid = Util.formatUUID(profile.get("id").getAsString()); + this.playerName = profile.get("name").getAsString(); - JSONObject mojang = json.getJSONObject("user"); - this.userId = mojang.getString("id"); - this.mojangUserName = mojang.getString("username"); + JsonObject mojang = json.get("user").getAsJsonObject(); + this.userId = mojang.get("id").getAsString(); + this.mojangUserName = mojang.get("username").getAsString(); } public MojangAccount(String accessToken, String userId, UUID uuid, String playerName, String mojangUserName) { diff --git a/src/main/java/de/bixilon/minosoft/mojang/api/MojangAuthentication.java b/src/main/java/de/bixilon/minosoft/mojang/api/MojangAuthentication.java index ce1226784..74cce9190 100644 --- a/src/main/java/de/bixilon/minosoft/mojang/api/MojangAuthentication.java +++ b/src/main/java/de/bixilon/minosoft/mojang/api/MojangAuthentication.java @@ -13,33 +13,38 @@ package de.bixilon.minosoft.mojang.api; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import de.bixilon.minosoft.Config; import de.bixilon.minosoft.Minosoft; import de.bixilon.minosoft.config.GameConfiguration; import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.util.HTTP; -import org.json.JSONObject; import java.net.http.HttpResponse; public class MojangAuthentication { public static MojangAccount login(String clientToken, String username, String password) { - JSONObject payload = new JSONObject(); - payload.put("agent", new JSONObject().put("name", "Minecraft").put("version", 1)); - payload.put("username", username); - payload.put("password", password); - payload.put("clientToken", clientToken); - payload.put("requestUser", true); + JsonObject agent = new JsonObject(); + agent.addProperty("name", "Minecraft"); + agent.addProperty("version", 1); + + JsonObject payload = new JsonObject(); + payload.add("agent", agent); + payload.addProperty("username", username); + payload.addProperty("password", password); + payload.addProperty("clientToken", clientToken); + payload.addProperty("requestUser", true); HttpResponse response = HTTP.postJson(MojangURLs.LOGIN.getUrl(), payload); if (response == null) { Log.mojang(String.format("Failed to login with username %s", username)); return null; } - JSONObject jsonResponse = new JSONObject(response.body()); + JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject(); if (response.statusCode() != 200) { - Log.mojang(String.format("Failed to login with error code %d: %s", response.statusCode(), jsonResponse.getString("errorMessage"))); + Log.mojang(String.format("Failed to login with error code %d: %s", response.statusCode(), jsonResponse.get("errorMessage").getAsString())); return null; } // now it is okay @@ -55,10 +60,10 @@ public class MojangAuthentication { return; } - JSONObject payload = new JSONObject(); - payload.put("accessToken", account.getAccessToken()); - payload.put("selectedProfile", account.getUUID().toString().replace("-", "")); - payload.put("serverId", serverId); + JsonObject payload = new JsonObject(); + payload.addProperty("accessToken", account.getAccessToken()); + payload.addProperty("selectedProfile", account.getUUID().toString().replace("-", "")); + payload.addProperty("serverId", serverId); HttpResponse response = HTTP.postJson(MojangURLs.JOIN.toString(), payload); @@ -67,8 +72,8 @@ public class MojangAuthentication { return; } if (response.statusCode() != 204) { - JSONObject jsonResponse = new JSONObject(response.body()); - Log.mojang(String.format("Failed to join server with error code %d: %s", response.statusCode(), jsonResponse.has("errorMessage") ? jsonResponse.getString("errorMessage") : "null")); + JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject(); + Log.mojang(String.format("Failed to join server with error code %d: %s", response.statusCode(), jsonResponse.has("errorMessage") ? jsonResponse.get("errorMessage").getAsString() : "null")); return; } // joined @@ -79,9 +84,9 @@ public class MojangAuthentication { if (Config.skipAuthentication) { return clientToken; } - JSONObject payload = new JSONObject(); - payload.put("accessToken", accessToken); - payload.put("clientToken", clientToken); + JsonObject payload = new JsonObject(); + payload.addProperty("accessToken", accessToken); + payload.addProperty("clientToken", clientToken); HttpResponse response; try { @@ -94,13 +99,13 @@ public class MojangAuthentication { Log.mojang("Failed to refresh session"); return null; } - JSONObject jsonResponse = new JSONObject(response.body()); + JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject(); if (response.statusCode() != 200) { - Log.mojang(String.format("Failed to refresh session with error code %d: %s", response.statusCode(), jsonResponse.getString("errorMessage"))); + Log.mojang(String.format("Failed to refresh session with error code %d: %s", response.statusCode(), jsonResponse.get("errorMessage").getAsString())); return null; } // now it is okay - return jsonResponse.getString("accessToken"); + return jsonResponse.get("accessToken").getAsString(); } public static String refresh(String accessToken) { diff --git a/src/main/java/de/bixilon/minosoft/mojang/api/MojangStatus.java b/src/main/java/de/bixilon/minosoft/mojang/api/MojangStatus.java index 3fa87e4f6..ad555206a 100644 --- a/src/main/java/de/bixilon/minosoft/mojang/api/MojangStatus.java +++ b/src/main/java/de/bixilon/minosoft/mojang/api/MojangStatus.java @@ -13,15 +13,15 @@ package de.bixilon.minosoft.mojang.api; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.util.HTTP; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import java.net.http.HttpResponse; import java.util.HashMap; -import java.util.Iterator; public class MojangStatus { public static HashMap getStatus() { @@ -37,14 +37,12 @@ public class MojangStatus { // now it is hopefully okay HashMap ret = new HashMap<>(); try { - JSONArray json = new JSONArray(response.body()); - for (int i = 0; i < json.length(); i++) { - JSONObject innerJson = json.getJSONObject(i); - Iterator keys = innerJson.keys(); - while (keys.hasNext()) { - String key = keys.next(); + JsonArray json = JsonParser.parseString(response.body()).getAsJsonArray(); + for (int i = 0; i < json.size(); i++) { + JsonObject innerJson = json.get(i).getAsJsonObject(); + for (String key : innerJson.keySet()) { Services service = Services.byKey(key); - ret.put(service, ServiceStatus.byKey(innerJson.getString(key))); + ret.put(service, ServiceStatus.byKey(innerJson.get(key).getAsString())); } } if (ret.size() != Services.values().length) { @@ -53,7 +51,7 @@ public class MojangStatus { } return ret; - } catch (NullPointerException | JSONException e) { + } catch (NullPointerException | JsonParseException e) { e.printStackTrace(); return getUnknownStatusMap(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java index b1ec9b986..b2ad96773 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java @@ -13,6 +13,8 @@ package de.bixilon.minosoft.protocol.protocol; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import de.bixilon.minosoft.game.datatypes.Direction; import de.bixilon.minosoft.game.datatypes.TextComponent; import de.bixilon.minosoft.game.datatypes.inventory.Slot; @@ -26,7 +28,6 @@ import de.bixilon.minosoft.game.datatypes.world.BlockPosition; import de.bixilon.minosoft.nbt.tag.CompoundTag; import de.bixilon.minosoft.util.BitByte; import de.bixilon.minosoft.util.Util; -import org.json.JSONObject; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @@ -172,8 +173,8 @@ public class InByteBuffer { return readByte() / 32.0D; } - public JSONObject readJSON() { - return new JSONObject(readString()); + public JsonObject readJSON() { + return JsonParser.parseString(readString()).getAsJsonObject(); } public BlockPosition readPosition() { diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java index 55d878774..766f9d151 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java @@ -13,11 +13,11 @@ package de.bixilon.minosoft.protocol.protocol; +import com.google.gson.JsonObject; import de.bixilon.minosoft.game.datatypes.TextComponent; import de.bixilon.minosoft.game.datatypes.inventory.Slot; import de.bixilon.minosoft.game.datatypes.world.BlockPosition; import de.bixilon.minosoft.nbt.tag.CompoundTag; -import org.json.JSONObject; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -148,7 +148,7 @@ public class OutByteBuffer { return bytes; } - public void writeJSON(JSONObject j) { + public void writeJSON(JsonObject j) { writeString(j.toString()); } diff --git a/src/main/java/de/bixilon/minosoft/util/HTTP.java b/src/main/java/de/bixilon/minosoft/util/HTTP.java index b9024ae50..65c5b36cb 100644 --- a/src/main/java/de/bixilon/minosoft/util/HTTP.java +++ b/src/main/java/de/bixilon/minosoft/util/HTTP.java @@ -13,7 +13,7 @@ package de.bixilon.minosoft.util; -import org.json.JSONObject; +import com.google.gson.JsonObject; import java.io.IOException; import java.net.URI; @@ -23,7 +23,7 @@ import java.net.http.HttpResponse; public class HTTP { - public static HttpResponse postJson(String url, JSONObject json) { + public static HttpResponse postJson(String url, JsonObject json) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url)) diff --git a/src/main/java/de/bixilon/minosoft/util/Util.java b/src/main/java/de/bixilon/minosoft/util/Util.java index c4735a428..a49fc5720 100644 --- a/src/main/java/de/bixilon/minosoft/util/Util.java +++ b/src/main/java/de/bixilon/minosoft/util/Util.java @@ -13,9 +13,10 @@ package de.bixilon.minosoft.util; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import de.bixilon.minosoft.protocol.protocol.InByteBuffer; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import org.json.JSONObject; import java.io.*; import java.nio.charset.StandardCharsets; @@ -122,7 +123,7 @@ public class Util { return stringBuilder.toString(); } - public static JSONObject readJsonFromFile(String fileName) throws IOException { - return new JSONObject(readFile(fileName)); + public static JsonObject readJsonFromFile(String fileName) throws IOException { + return JsonParser.parseString(readFile(fileName)).getAsJsonObject(); } }