refactor: improve comma adding in SNBTUtilities
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package me.chayapak1.chomens_bot.util;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -57,16 +58,14 @@ public class SNBTUtilities {
|
||||
} else if (json.isJsonArray()) {
|
||||
final StringBuilder stringBuilder = new StringBuilder(BEGIN_ARRAY);
|
||||
|
||||
boolean notEmpty = false;
|
||||
final JsonArray array = json.getAsJsonArray();
|
||||
|
||||
for (final JsonElement element : json.getAsJsonArray()) {
|
||||
notEmpty = true;
|
||||
int i = 1;
|
||||
for (final JsonElement element : array) {
|
||||
stringBuilder.append(fromJson(element));
|
||||
stringBuilder.append(COMMA);
|
||||
if (i++ != array.size()) stringBuilder.append(COMMA);
|
||||
}
|
||||
|
||||
if (notEmpty) stringBuilder.deleteCharAt(stringBuilder.length() - 1); // removes comma
|
||||
|
||||
stringBuilder.append(END_ARRAY);
|
||||
|
||||
return stringBuilder.toString();
|
||||
@@ -75,15 +74,14 @@ public class SNBTUtilities {
|
||||
|
||||
final Set<Map.Entry<String, JsonElement>> entries = json.getAsJsonObject().entrySet();
|
||||
|
||||
int i = 1;
|
||||
for (final Map.Entry<String, JsonElement> entry : entries) {
|
||||
stringBuilder.append(entry.getKey()) // no escape :O (optional for adventure)
|
||||
.append(COLON)
|
||||
.append(fromJson(entry.getValue()))
|
||||
.append(COMMA);
|
||||
.append(fromJson(entry.getValue()));
|
||||
if (i++ != entries.size()) stringBuilder.append(COMMA);
|
||||
}
|
||||
|
||||
if (!entries.isEmpty()) stringBuilder.deleteCharAt(stringBuilder.length() - 1); // removes comma
|
||||
|
||||
stringBuilder.append(END_OBJECT);
|
||||
|
||||
return stringBuilder.toString();
|
||||
|
||||
Reference in New Issue
Block a user