replace org.json with gjson (performance improvements)

This commit is contained in:
Bixilon 2020-07-25 14:29:31 +02:00
parent 9031d04f0c
commit 4ec65a65a5
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
16 changed files with 174 additions and 182 deletions

View File

@ -41,9 +41,9 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>json</artifactId> <artifactId>gson</artifactId>
<version>20180813</version> <version>2.8.6</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.yaml</groupId> <groupId>org.yaml</groupId>
@ -56,5 +56,4 @@
<version>29.0-jre</version> <version>29.0-jre</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft; package de.bixilon.minosoft;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.config.Configuration; import de.bixilon.minosoft.config.Configuration;
import de.bixilon.minosoft.config.GameConfiguration; import de.bixilon.minosoft.config.GameConfiguration;
import de.bixilon.minosoft.game.datatypes.Mappings; 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.FolderUtil;
import de.bixilon.minosoft.util.OSUtil; import de.bixilon.minosoft.util.OSUtil;
import de.bixilon.minosoft.util.Util; import de.bixilon.minosoft.util.Util;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; 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 { public class Minosoft {
static Configuration config; static Configuration config;
@ -65,8 +67,7 @@ public class Minosoft {
Log.info("Loading all mappings..."); Log.info("Loading all mappings...");
long mappingsStart = System.currentTimeMillis(); long mappingsStart = System.currentTimeMillis();
loadMappings(); loadMappings();
Log.info(String.format("Mappings loaded in a total of %sms", (System.currentTimeMillis() - mappingsStart))); Log.info(String.format("Mappings loaded within %sms", (System.currentTimeMillis() - mappingsStart)));
System.exit(0);
checkClientToken(); checkClientToken();
@ -143,17 +144,15 @@ public class Minosoft {
} }
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
for (Map.Entry<String, Mappings> mappingSet : mappingsHashMap.entrySet()) { for (Map.Entry<String, Mappings> mappingSet : mappingsHashMap.entrySet()) {
JSONObject data = Util.readJsonFromFile(Config.homeDir + String.format("assets/mapping/%s/%s.json", version.getVersionString(), mappingSet.getKey())); JsonObject data = Util.readJsonFromFile(Config.homeDir + String.format("assets/mapping/%s/%s.json", version.getVersionString(), mappingSet.getKey()));
for (Iterator<String> mods = data.keys(); mods.hasNext(); ) { for (String mod : data.keySet()) {
// key = mod name JsonObject modJSON = data.getAsJsonObject(mod);
String mod = mods.next();
JSONObject modJSON = data.getJSONObject(mod);
switch (mappingSet.getValue()) { switch (mappingSet.getValue()) {
case REGISTRIES: case REGISTRIES:
Items.load(mod, modJSON.getJSONObject("item").getJSONObject("entries"), version); Items.load(mod, modJSON.getAsJsonObject("item").getAsJsonObject("entries"), version);
Entities.load(mod, modJSON.getJSONObject("entity_type").getJSONObject("entries"), version); Entities.load(mod, modJSON.getAsJsonObject("entity_type").getAsJsonObject("entries"), version);
Enchantments.load(mod, modJSON.getJSONObject("enchantment").getJSONObject("entries"), version); Enchantments.load(mod, modJSON.getAsJsonObject("enchantment").getAsJsonObject("entries"), version);
Statistics.load(mod, modJSON.getJSONObject("custom_stat").getJSONObject("entries"), version); Statistics.load(mod, modJSON.getAsJsonObject("custom_stat").getAsJsonObject("entries"), version);
break; break;
case BLOCKS: case BLOCKS:
Blocks.load(mod, modJSON, version); 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())); 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()); Log.fatal("Error occurred while loading version mapping: " + e.getLocalizedMessage());
System.exit(1); System.exit(1);
} }

View File

@ -13,46 +13,45 @@
package de.bixilon.minosoft; package de.bixilon.minosoft;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import de.bixilon.minosoft.game.datatypes.TextComponent; import de.bixilon.minosoft.game.datatypes.TextComponent;
import org.json.JSONException;
import org.json.JSONObject;
public class ServerListPing { public class ServerListPing {
final JSONObject raw; final JsonObject raw;
public ServerListPing(JSONObject json) { public ServerListPing(JsonObject json) {
this.raw = json; this.raw = json;
} }
public int getProtocolNumber() { public int getProtocolNumber() {
return raw.getJSONObject("version").getInt("protocol"); return raw.getAsJsonObject("version").get("protocol").getAsInt();
} }
public String getServerBrand() { public String getServerBrand() {
return raw.getJSONObject("version").getString("name"); return raw.getAsJsonObject("version").get("name").getAsString();
} }
public int getPlayerOnline() { public int getPlayerOnline() {
return raw.getJSONObject("players").getInt("online"); return raw.getAsJsonObject("players").get("online").getAsInt();
} }
public int getMaxPlayers() { public int getMaxPlayers() {
return raw.getJSONObject("players").getInt("max"); return raw.getAsJsonObject("players").get("max").getAsInt();
} }
public String getBase64EncodedFavicon() { public String getBase64EncodedFavicon() {
return raw.getString("favicon"); return raw.get("favicon").getAsString();
} }
public TextComponent getMotd() { public TextComponent getMotd() {
try { try {
return new TextComponent(raw.getJSONObject("description")); return new TextComponent(raw.getAsJsonObject("description"));
} catch (JSONException ignored) { } 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; return this.raw;
} }
} }

View File

@ -13,27 +13,28 @@
package de.bixilon.minosoft.game.datatypes; package de.bixilon.minosoft.game.datatypes;
import org.json.JSONArray; import com.google.gson.JsonArray;
import org.json.JSONException; import com.google.gson.JsonObject;
import org.json.JSONObject; import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TextComponent { public class TextComponent {
JSONObject json; JsonObject json;
public TextComponent(String raw) { public TextComponent(String raw) {
if (raw == null) { if (raw == null) {
this.json = new JSONObject(); this.json = new JsonObject();
return; return;
} }
try { try {
this.json = new JSONObject(raw); this.json = JsonParser.parseString(raw).getAsJsonObject();
} catch (JSONException e) { } catch (IllegalStateException e) {
// not a text component, is a legacy string // not a text component, is a legacy string
this.json = new JSONObject(); this.json = new JsonObject();
JSONArray extra = new JSONArray(); JsonArray extra = new JsonArray();
String[] paragraphSplit = raw.split("§"); String[] paragraphSplit = raw.split("§");
@ -75,7 +76,7 @@ public class TextComponent {
break; break;
case "r": case "r":
//save and clear //save and clear
extra.put(getExtraByAttributes(message.toString(), color, attributesList)); extra.add(getExtraByAttributes(message.toString(), color, attributesList));
attributesList.clear(); attributesList.clear();
color = ChatAttributes.WHITE; color = ChatAttributes.WHITE;
message = new StringBuilder(); message = new StringBuilder();
@ -84,7 +85,7 @@ public class TextComponent {
} else { } else {
// save current // save current
if (!message.toString().isEmpty()) { if (!message.toString().isEmpty()) {
extra.put(getExtraByAttributes(message.toString(), color, attributesList)); extra.add(getExtraByAttributes(message.toString(), color, attributesList));
message = new StringBuilder(); message = new StringBuilder();
} }
color = ChatAttributes.byColor(colorCheck); color = ChatAttributes.byColor(colorCheck);
@ -98,55 +99,55 @@ public class TextComponent {
} }
} }
// save // 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; this.json = json;
} }
static JSONObject getExtraByAttributes(String message, ChatAttributes color, List<ChatAttributes> formatting) { static JsonObject getExtraByAttributes(String message, ChatAttributes color, List<ChatAttributes> formatting) {
JSONObject ret = new JSONObject(); JsonObject ret = new JsonObject();
ret.put("text", message); ret.addProperty("text", message);
if (color != null) { if (color != null) {
ret.put("color", color.getName()); ret.addProperty("color", color.getName());
} }
for (ChatAttributes attribute : formatting) { for (ChatAttributes attribute : formatting) {
if (attribute == ChatAttributes.BOLD && !ret.has("bold")) { if (attribute == ChatAttributes.BOLD && !ret.has("bold")) {
ret.put("bold", true); ret.addProperty("bold", true);
} else if (attribute == ChatAttributes.ITALIC && !ret.has("italic")) { } else if (attribute == ChatAttributes.ITALIC && !ret.has("italic")) {
ret.put("italic", true); ret.addProperty("italic", true);
} else if (attribute == ChatAttributes.UNDERLINED && !ret.has("underlined")) { } else if (attribute == ChatAttributes.UNDERLINED && !ret.has("underlined")) {
ret.put("underlined", true); ret.addProperty("underlined", true);
} else if (attribute == ChatAttributes.STRIKETHROUGH && !ret.has("strikethrough")) { } else if (attribute == ChatAttributes.STRIKETHROUGH && !ret.has("strikethrough")) {
ret.put("strikethrough", true); ret.addProperty("strikethrough", true);
} else if (attribute == ChatAttributes.OBFUSCATED && !ret.has("obfuscated")) { } else if (attribute == ChatAttributes.OBFUSCATED && !ret.has("obfuscated")) {
ret.put("obfuscated", true); ret.addProperty("obfuscated", true);
} }
} }
return ret; return ret;
} }
public String getRawMessage() { public String getRawMessage() {
if (json.has("text") && json.getString("text").length() != 0) { if (json.has("text") && json.get("text").getAsString().length() != 0) {
return json.getString("text"); return json.get("text").getAsString();
} }
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
if (json.has("extra")) { if (json.has("extra")) {
JSONArray arr = json.getJSONArray("extra"); JsonArray arr = json.getAsJsonArray("extra");
for (int i = 0; i < arr.length(); i++) { for (int i = 0; i < arr.size(); i++) {
JSONObject object; JsonObject object;
try { try {
object = arr.getJSONObject(i); object = arr.get(i).getAsJsonObject();
} catch (JSONException e) { } catch (JsonParseException e) {
// reset text // reset text
buffer.append(arr.getString(i)); buffer.append(arr.get(i).getAsString());
continue; continue;
} }
buffer.append(object.getString("text")); buffer.append(object.get("text").getAsString());
} }
buffer.append(ChatAttributes.RESET); buffer.append(ChatAttributes.RESET);
return buffer.toString(); return buffer.toString();
@ -154,46 +155,46 @@ public class TextComponent {
return ""; return "";
} }
public JSONObject getRaw() { public JsonObject getRaw() {
return this.json; return this.json;
} }
public String getColoredMessage() { public String getColoredMessage() {
if (json.has("text") && json.getString("text").length() != 0) { if (json.has("text") && json.get("text").getAsString().length() != 0) {
return json.getString("text"); return json.get("text").getAsString();
} }
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
if (json.has("extra")) { if (json.has("extra")) {
JSONArray arr = json.getJSONArray("extra"); JsonArray arr = json.getAsJsonArray("extra");
for (int i = 0; i < arr.length(); i++) { for (int i = 0; i < arr.size(); i++) {
JSONObject object; JsonObject object;
try { try {
object = arr.getJSONObject(i); object = arr.get(i).getAsJsonObject();
} catch (JSONException e) { } catch (JsonParseException e) {
// reset text // reset text
buffer.append(ChatAttributes.RESET); buffer.append(ChatAttributes.RESET);
buffer.append(arr.getString(i)); buffer.append(arr.get(i).getAsString());
continue; continue;
} }
if (object.has("bold") && object.getBoolean("bold")) { if (object.has("bold") && object.get("bold").getAsBoolean()) {
buffer.append(ChatAttributes.BOLD); buffer.append(ChatAttributes.BOLD);
} }
if (object.has("color")) { 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); buffer.append(ChatAttributes.ITALIC);
} }
if (object.has("underlined") && object.getBoolean("underlined")) { if (object.has("underlined") && object.get("underlined").getAsBoolean()) {
buffer.append(ChatAttributes.UNDERLINED); buffer.append(ChatAttributes.UNDERLINED);
} }
if (object.has("strikethrough") && object.getBoolean("strikethrough")) { if (object.has("strikethrough") && object.get("strikethrough").getAsBoolean()) {
buffer.append(ChatAttributes.STRIKETHROUGH); 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(ChatAttributes.OBFUSCATED);
} }
buffer.append(object.getString("text")); buffer.append(object.get("text").getAsString());
} }
buffer.append(ChatAttributes.RESET); buffer.append(ChatAttributes.RESET);
return buffer.toString(); return buffer.toString();

View File

@ -13,13 +13,12 @@ package de.bixilon.minosoft.game.datatypes.objectLoader.blocks;
*/ */
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
public class Blocks { public class Blocks {
public static Block nullBlock; public static Block nullBlock;
@ -454,36 +453,34 @@ public class Blocks {
return blockMap.get(version).get(protocolId); 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<Integer, Block> versionMapping = HashBiMap.create(); HashBiMap<Integer, Block> versionMapping = HashBiMap.create();
for (Iterator<String> identifiers = json.keys(); identifiers.hasNext(); ) { for (String identifierName : json.keySet()) {
String identifierName = identifiers.next(); JsonObject identifierJSON = json.getAsJsonObject(identifierName);
JSONObject identifierJSON = json.getJSONObject(identifierName); JsonArray statesArray = identifierJSON.getAsJsonArray("states");
JSONArray statesArray = identifierJSON.getJSONArray("states"); for (int i = 0; i < statesArray.size(); i++) {
for (int i = 0; i < statesArray.length(); i++) { JsonObject statesJSON = statesArray.get(i).getAsJsonObject();
JSONObject statesJSON = statesArray.getJSONObject(i);
if (statesJSON.has("properties")) { if (statesJSON.has("properties")) {
// properties are optional // properties are optional
JSONObject propertiesJSON = statesJSON.getJSONObject("properties"); JsonObject propertiesJSON = statesJSON.getAsJsonObject("properties");
BlockRotation rotation = BlockRotation.NONE; BlockRotation rotation = BlockRotation.NONE;
if (propertiesJSON.has("facing")) { if (propertiesJSON.has("facing")) {
rotation = rotationMapping.get(propertiesJSON.getString("facing")); rotation = rotationMapping.get(propertiesJSON.get("facing").getAsString());
propertiesJSON.remove("facing"); propertiesJSON.remove("facing");
} else if (propertiesJSON.has("rotation")) { } else if (propertiesJSON.has("rotation")) {
rotation = rotationMapping.get(propertiesJSON.getString("rotation")); rotation = rotationMapping.get(propertiesJSON.get("rotation").getAsString());
propertiesJSON.remove("rotation"); propertiesJSON.remove("rotation");
} }
BlockProperties[] properties = new BlockProperties[propertiesJSON.length()]; BlockProperties[] properties = new BlockProperties[propertiesJSON.size()];
int ii = 0; int ii = 0;
for (Iterator<String> it = propertiesJSON.keys(); it.hasNext(); ) { for (String propertyName : propertiesJSON.keySet()) {
String propertyName = it.next();
if (propertiesMapping.get(propertyName) == null) { if (propertiesMapping.get(propertyName) == null) {
throw new RuntimeException(String.format("Unknown block property: %s (identifier=%s)", propertyName, identifierName)); throw new RuntimeException(String.format("Unknown block property: %s (identifier=%s)", propertyName, identifierName));
} }
if (propertiesMapping.get(propertyName).get(propertiesJSON.getString(propertyName)) == null) { if (propertiesMapping.get(propertyName).get(propertiesJSON.get(propertyName).getAsString()) == null) {
throw new RuntimeException(String.format("Unknown block property: %s -> %s (identifier=%s)", propertyName, propertiesJSON.getString(propertyName), identifierName)); 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++; ii++;
} }
@ -528,13 +525,13 @@ public class Blocks {
blockMap.put(version, versionMapping); blockMap.put(version, versionMapping);
} }
private static int getBlockId(JSONObject json, ProtocolVersion version) { private static int getBlockId(JsonObject json, ProtocolVersion version) {
int blockId = json.getInt("id"); int blockId = json.get("id").getAsInt();
if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) { if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
// old format (with metadata) // old format (with metadata)
blockId <<= 4; blockId <<= 4;
if (json.has("meta")) { if (json.has("meta")) {
blockId |= json.getInt("meta"); blockId |= json.get("meta").getAsByte();
} }
} }
return blockId; return blockId;

View File

@ -14,12 +14,11 @@
package de.bixilon.minosoft.game.datatypes.objectLoader.enchantments; package de.bixilon.minosoft.game.datatypes.objectLoader.enchantments;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
public class Enchantments { public class Enchantments {
@ -33,15 +32,14 @@ public class Enchantments {
return enchantmentMap.get(version).get(protocolId); 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<Integer, Enchantment> versionMapping = HashBiMap.create(); HashBiMap<Integer, Enchantment> versionMapping = HashBiMap.create();
for (Iterator<String> identifiers = json.keys(); identifiers.hasNext(); ) { for (String identifierName : json.keySet()) {
String identifierName = identifiers.next();
Enchantment enchantment = new Enchantment(mod, identifierName); Enchantment enchantment = new Enchantment(mod, identifierName);
if (enchantmentList.contains(enchantment)) { if (enchantmentList.contains(enchantment)) {
enchantment = enchantmentList.get(enchantmentList.indexOf(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); enchantmentMap.put(version, versionMapping);
} }

View File

@ -14,13 +14,12 @@
package de.bixilon.minosoft.game.datatypes.objectLoader.entities; package de.bixilon.minosoft.game.datatypes.objectLoader.entities;
import com.google.common.collect.HashBiMap; 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.mob.*;
import de.bixilon.minosoft.game.datatypes.objectLoader.entities.objects.*; import de.bixilon.minosoft.game.datatypes.objectLoader.entities.objects.*;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import org.json.JSONObject;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
public class Entities { public class Entities {
@ -157,11 +156,10 @@ public class Entities {
return entityClassMap.get(identifier); 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<Integer, String> versionMapping = HashBiMap.create(); HashBiMap<Integer, String> versionMapping = HashBiMap.create();
for (Iterator<String> identifiers = json.keys(); identifiers.hasNext(); ) { for (String identifierName : json.keySet()) {
String identifierName = identifiers.next(); versionMapping.put(json.getAsJsonObject(identifierName).get("id").getAsInt(), mod + ":" + identifierName);
versionMapping.put(json.getJSONObject(identifierName).getInt("id"), mod + ":" + identifierName);
} }
entityMapping.put(version, versionMapping); entityMapping.put(version, versionMapping);
} }

View File

@ -14,12 +14,11 @@
package de.bixilon.minosoft.game.datatypes.objectLoader.entities.items; package de.bixilon.minosoft.game.datatypes.objectLoader.entities.items;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
public class Items { public class Items {
@ -43,23 +42,22 @@ public class Items {
return itemMap.get(version).get(protocolId); 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<Integer, Item> versionMapping = HashBiMap.create(); HashBiMap<Integer, Item> versionMapping = HashBiMap.create();
for (Iterator<String> identifiers = json.keys(); identifiers.hasNext(); ) { for (String identifierName : json.keySet()) {
String identifierName = identifiers.next();
Item item = getItem(mod, identifierName); Item item = getItem(mod, identifierName);
if (item == null) { if (item == null) {
// does not exist. create // does not exist. create
item = new Item(mod, identifierName); item = new Item(mod, identifierName);
itemList.add(item); itemList.add(item);
} }
JSONObject identifierJSON = json.getJSONObject(identifierName); JsonObject identifierJSON = json.getAsJsonObject(identifierName);
int itemId = identifierJSON.getInt("id"); int itemId = identifierJSON.get("id").getAsInt();
if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) { if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
// old format (with metadata) // old format (with metadata)
itemId <<= 4; itemId <<= 4;
if (identifierJSON.has("meta")) { if (identifierJSON.has("meta")) {
itemId |= identifierJSON.getInt("meta"); itemId |= identifierJSON.get("meta").getAsInt();
} }
} }
versionMapping.put(itemId, item); versionMapping.put(itemId, item);

View File

@ -14,12 +14,11 @@
package de.bixilon.minosoft.game.datatypes.objectLoader.statistics; package de.bixilon.minosoft.game.datatypes.objectLoader.statistics;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
public class Statistics { public class Statistics {
@ -41,17 +40,16 @@ public class Statistics {
return statisticsIdentifierMap.get(version).get(identifier); 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<Integer, Statistic> versionIdMapping = HashBiMap.create(); HashBiMap<Integer, Statistic> versionIdMapping = HashBiMap.create();
HashBiMap<String, Statistic> versionIdentifierMapping = HashBiMap.create(); HashBiMap<String, Statistic> versionIdentifierMapping = HashBiMap.create();
for (Iterator<String> identifiers = json.keys(); identifiers.hasNext(); ) { for (String identifierName : json.keySet()) {
String identifierName = identifiers.next();
Statistic statistic = new Statistic(mod, identifierName); Statistic statistic = new Statistic(mod, identifierName);
if (statisticList.contains(statistic)) { if (statisticList.contains(statistic)) {
statistic = statisticList.get(statisticList.indexOf(statistic)); statistic = statisticList.get(statisticList.indexOf(statistic));
} }
if (json.getJSONObject(identifierName).has("id")) { if (json.getAsJsonObject(identifierName).has("id")) {
versionIdMapping.put(json.getJSONObject(identifierName).getInt("id"), statistic); versionIdMapping.put(json.getAsJsonObject(identifierName).get("id").getAsInt(), statistic);
} else { } else {
versionIdentifierMapping.put(identifierName, statistic); versionIdentifierMapping.put(identifierName, statistic);
} }

View File

@ -13,10 +13,10 @@
package de.bixilon.minosoft.mojang.api; package de.bixilon.minosoft.mojang.api;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.Config; import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.Minosoft; import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.util.Util; import de.bixilon.minosoft.util.Util;
import org.json.JSONObject;
import java.util.UUID; import java.util.UUID;
@ -30,15 +30,15 @@ public class MojangAccount {
final String mojangUserName; final String mojangUserName;
public MojangAccount(JSONObject json) { public MojangAccount(JsonObject json) {
this.accessToken = json.getString("accessToken"); this.accessToken = json.get("accessToken").getAsString();
JSONObject profile = json.getJSONObject("selectedProfile"); JsonObject profile = json.get("selectedProfile").getAsJsonObject();
this.uuid = Util.formatUUID(profile.getString("id")); this.uuid = Util.formatUUID(profile.get("id").getAsString());
this.playerName = profile.getString("name"); this.playerName = profile.get("name").getAsString();
JSONObject mojang = json.getJSONObject("user"); JsonObject mojang = json.get("user").getAsJsonObject();
this.userId = mojang.getString("id"); this.userId = mojang.get("id").getAsString();
this.mojangUserName = mojang.getString("username"); this.mojangUserName = mojang.get("username").getAsString();
} }
public MojangAccount(String accessToken, String userId, UUID uuid, String playerName, String mojangUserName) { public MojangAccount(String accessToken, String userId, UUID uuid, String playerName, String mojangUserName) {

View File

@ -13,33 +13,38 @@
package de.bixilon.minosoft.mojang.api; 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.Config;
import de.bixilon.minosoft.Minosoft; import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.config.GameConfiguration; import de.bixilon.minosoft.config.GameConfiguration;
import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.util.HTTP; import de.bixilon.minosoft.util.HTTP;
import org.json.JSONObject;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
public class MojangAuthentication { public class MojangAuthentication {
public static MojangAccount login(String clientToken, String username, String password) { public static MojangAccount login(String clientToken, String username, String password) {
JSONObject payload = new JSONObject(); JsonObject agent = new JsonObject();
payload.put("agent", new JSONObject().put("name", "Minecraft").put("version", 1)); agent.addProperty("name", "Minecraft");
payload.put("username", username); agent.addProperty("version", 1);
payload.put("password", password);
payload.put("clientToken", clientToken); JsonObject payload = new JsonObject();
payload.put("requestUser", true); payload.add("agent", agent);
payload.addProperty("username", username);
payload.addProperty("password", password);
payload.addProperty("clientToken", clientToken);
payload.addProperty("requestUser", true);
HttpResponse<String> response = HTTP.postJson(MojangURLs.LOGIN.getUrl(), payload); HttpResponse<String> response = HTTP.postJson(MojangURLs.LOGIN.getUrl(), payload);
if (response == null) { if (response == null) {
Log.mojang(String.format("Failed to login with username %s", username)); Log.mojang(String.format("Failed to login with username %s", username));
return null; return null;
} }
JSONObject jsonResponse = new JSONObject(response.body()); JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject();
if (response.statusCode() != 200) { 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; return null;
} }
// now it is okay // now it is okay
@ -55,10 +60,10 @@ public class MojangAuthentication {
return; return;
} }
JSONObject payload = new JSONObject(); JsonObject payload = new JsonObject();
payload.put("accessToken", account.getAccessToken()); payload.addProperty("accessToken", account.getAccessToken());
payload.put("selectedProfile", account.getUUID().toString().replace("-", "")); payload.addProperty("selectedProfile", account.getUUID().toString().replace("-", ""));
payload.put("serverId", serverId); payload.addProperty("serverId", serverId);
HttpResponse<String> response = HTTP.postJson(MojangURLs.JOIN.toString(), payload); HttpResponse<String> response = HTTP.postJson(MojangURLs.JOIN.toString(), payload);
@ -67,8 +72,8 @@ public class MojangAuthentication {
return; return;
} }
if (response.statusCode() != 204) { if (response.statusCode() != 204) {
JSONObject jsonResponse = new JSONObject(response.body()); 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.getString("errorMessage") : "null")); Log.mojang(String.format("Failed to join server with error code %d: %s", response.statusCode(), jsonResponse.has("errorMessage") ? jsonResponse.get("errorMessage").getAsString() : "null"));
return; return;
} }
// joined // joined
@ -79,9 +84,9 @@ public class MojangAuthentication {
if (Config.skipAuthentication) { if (Config.skipAuthentication) {
return clientToken; return clientToken;
} }
JSONObject payload = new JSONObject(); JsonObject payload = new JsonObject();
payload.put("accessToken", accessToken); payload.addProperty("accessToken", accessToken);
payload.put("clientToken", clientToken); payload.addProperty("clientToken", clientToken);
HttpResponse<String> response; HttpResponse<String> response;
try { try {
@ -94,13 +99,13 @@ public class MojangAuthentication {
Log.mojang("Failed to refresh session"); Log.mojang("Failed to refresh session");
return null; return null;
} }
JSONObject jsonResponse = new JSONObject(response.body()); JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject();
if (response.statusCode() != 200) { 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; return null;
} }
// now it is okay // now it is okay
return jsonResponse.getString("accessToken"); return jsonResponse.get("accessToken").getAsString();
} }
public static String refresh(String accessToken) { public static String refresh(String accessToken) {

View File

@ -13,15 +13,15 @@
package de.bixilon.minosoft.mojang.api; 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.logging.Log;
import de.bixilon.minosoft.util.HTTP; 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.net.http.HttpResponse;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
public class MojangStatus { public class MojangStatus {
public static HashMap<Services, ServiceStatus> getStatus() { public static HashMap<Services, ServiceStatus> getStatus() {
@ -37,14 +37,12 @@ public class MojangStatus {
// now it is hopefully okay // now it is hopefully okay
HashMap<Services, ServiceStatus> ret = new HashMap<>(); HashMap<Services, ServiceStatus> ret = new HashMap<>();
try { try {
JSONArray json = new JSONArray(response.body()); JsonArray json = JsonParser.parseString(response.body()).getAsJsonArray();
for (int i = 0; i < json.length(); i++) { for (int i = 0; i < json.size(); i++) {
JSONObject innerJson = json.getJSONObject(i); JsonObject innerJson = json.get(i).getAsJsonObject();
Iterator<String> keys = innerJson.keys(); for (String key : innerJson.keySet()) {
while (keys.hasNext()) {
String key = keys.next();
Services service = Services.byKey(key); 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) { if (ret.size() != Services.values().length) {
@ -53,7 +51,7 @@ public class MojangStatus {
} }
return ret; return ret;
} catch (NullPointerException | JSONException e) { } catch (NullPointerException | JsonParseException e) {
e.printStackTrace(); e.printStackTrace();
return getUnknownStatusMap(); return getUnknownStatusMap();
} }

View File

@ -13,6 +13,8 @@
package de.bixilon.minosoft.protocol.protocol; 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.Direction;
import de.bixilon.minosoft.game.datatypes.TextComponent; import de.bixilon.minosoft.game.datatypes.TextComponent;
import de.bixilon.minosoft.game.datatypes.inventory.Slot; 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.nbt.tag.CompoundTag;
import de.bixilon.minosoft.util.BitByte; import de.bixilon.minosoft.util.BitByte;
import de.bixilon.minosoft.util.Util; import de.bixilon.minosoft.util.Util;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -172,8 +173,8 @@ public class InByteBuffer {
return readByte() / 32.0D; return readByte() / 32.0D;
} }
public JSONObject readJSON() { public JsonObject readJSON() {
return new JSONObject(readString()); return JsonParser.parseString(readString()).getAsJsonObject();
} }
public BlockPosition readPosition() { public BlockPosition readPosition() {

View File

@ -13,11 +13,11 @@
package de.bixilon.minosoft.protocol.protocol; package de.bixilon.minosoft.protocol.protocol;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.game.datatypes.TextComponent; import de.bixilon.minosoft.game.datatypes.TextComponent;
import de.bixilon.minosoft.game.datatypes.inventory.Slot; import de.bixilon.minosoft.game.datatypes.inventory.Slot;
import de.bixilon.minosoft.game.datatypes.world.BlockPosition; import de.bixilon.minosoft.game.datatypes.world.BlockPosition;
import de.bixilon.minosoft.nbt.tag.CompoundTag; import de.bixilon.minosoft.nbt.tag.CompoundTag;
import org.json.JSONObject;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -148,7 +148,7 @@ public class OutByteBuffer {
return bytes; return bytes;
} }
public void writeJSON(JSONObject j) { public void writeJSON(JsonObject j) {
writeString(j.toString()); writeString(j.toString());
} }

View File

@ -13,7 +13,7 @@
package de.bixilon.minosoft.util; package de.bixilon.minosoft.util;
import org.json.JSONObject; import com.google.gson.JsonObject;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -23,7 +23,7 @@ import java.net.http.HttpResponse;
public class HTTP { public class HTTP {
public static HttpResponse<String> postJson(String url, JSONObject json) { public static HttpResponse<String> postJson(String url, JsonObject json) {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url)) .uri(URI.create(url))

View File

@ -13,9 +13,10 @@
package de.bixilon.minosoft.util; 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.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import org.json.JSONObject;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -122,7 +123,7 @@ public class Util {
return stringBuilder.toString(); return stringBuilder.toString();
} }
public static JSONObject readJsonFromFile(String fileName) throws IOException { public static JsonObject readJsonFromFile(String fileName) throws IOException {
return new JSONObject(readFile(fileName)); return JsonParser.parseString(readFile(fileName)).getAsJsonObject();
} }
} }