Skip to content

Commit

Permalink
refactor: Better json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bconlon1 committed May 8, 2023
1 parent 8463b7b commit f80f929
Showing 1 changed file with 52 additions and 25 deletions.
77 changes: 52 additions & 25 deletions src/main/java/com/aetherteam/nitrogen/api/users/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.aetherteam.nitrogen.Nitrogen;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import net.minecraft.server.MinecraftServer;

import java.io.BufferedReader;
Expand Down Expand Up @@ -44,41 +41,71 @@ public static User queryUser(MinecraftServer server, UUID uuid) {
reader.close();

JsonElement jsonElement = JsonParser.parseString(query);
if (jsonElement != null) {
if (jsonElement != null && jsonElement.isJsonObject()) {
JsonObject json = jsonElement.getAsJsonObject();

int currentTierId = json.get("currentTier").getAsInt();
User.Tier currentTier = null;
if (currentTierId != -1) {
currentTier = User.Tier.byId(currentTierId);
try {
JsonElement currentTierElement = json.get("currentTier");
if (currentTierElement != null) {
int currentTierId = currentTierElement.getAsInt();
if (currentTierId != -1) {
currentTier = User.Tier.byId(currentTierId);
}
}
} catch (NumberFormatException e) {
Nitrogen.LOGGER.info(e.getMessage());
}

JsonArray pastTiersArray = json.getAsJsonArray("pastTiers");
User.Tier highestPastTier = null;
int pastTierLevel = 0;
for (int pastTierId : pastTiersArray.asList().stream().map((JsonElement::getAsInt)).toList()) {
if (pastTierId != -1) {
User.Tier pastTier = User.Tier.byId(pastTierId);
if (pastTier != null) {
if (pastTier.getLevel() > pastTierLevel) {
pastTierLevel = pastTier.getLevel();
highestPastTier = pastTier;
JsonElement pastTiersElement = json.get("pastTiers");
if (pastTiersElement != null && pastTiersElement.isJsonArray()) {
JsonArray pastTiersArray = pastTiersElement.getAsJsonArray();
int pastTierLevel = 0;
try {
for (int pastTierId : pastTiersArray.asList().stream().map((JsonElement::getAsInt)).toList()) {
if (pastTierId != -1) {
User.Tier pastTier = User.Tier.byId(pastTierId);
if (pastTier != null) {
if (pastTier.getLevel() > pastTierLevel) {
pastTierLevel = pastTier.getLevel();
highestPastTier = pastTier;
}
}
}
}
} catch (NumberFormatException e) {
Nitrogen.LOGGER.info(e.getMessage());
}
}

String renewalDate = json.get("renewsAt").getAsString();
String renewalDate = null;
JsonElement renewalDateElement = json.get("renewsAt");
if (renewalDateElement != null) {
try {
renewalDate = renewalDateElement.getAsString();
} catch (AssertionError e) {
Nitrogen.LOGGER.info(e.getMessage());
}
}

JsonArray groupsArray = json.getAsJsonArray("groups");
User.Group highestGroup = null;
int groupLevel = 0;
for (String groupName : groupsArray.asList().stream().map((JsonElement::getAsString)).toList()) {
User.Group group = User.Group.valueOf(groupName.toUpperCase(Locale.ROOT));
if (group.getLevel() > groupLevel) {
groupLevel = group.getLevel();
highestGroup = group;
JsonElement groupsElement = json.get("groups");
if (groupsElement != null && groupsElement.isJsonArray()) {
JsonArray groupsArray = groupsElement.getAsJsonArray();
int groupLevel = 0;
try {
for (String groupName : groupsArray.asList().stream().map((JsonElement::getAsString)).toList()) {
User.Group group = User.Group.valueOf(groupName.toUpperCase(Locale.ROOT));
if (group.getLevel() > groupLevel) {
groupLevel = group.getLevel();
highestGroup = group;
}
}
} catch (NumberFormatException e) {
Nitrogen.LOGGER.info(e.getMessage());
}

}

User user = new User(currentTier, highestPastTier, renewalDate, highestGroup);
Expand Down

0 comments on commit f80f929

Please sign in to comment.