Skip to content

Commit d7fadad

Browse files
committed
organize general normalize in NBTMapNormalizer
1 parent ecdfec4 commit d7fadad

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

nbt/src/main/java/io/github/projectunified/craftitem/nbt/NBTMapNormalizer.java

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,30 @@ public static Object normalize(Object value) {
4646
* @throws IllegalArgumentException if forced-value map is invalid
4747
*/
4848
public static Object normalize(Object value, UnaryOperator<String> translator) {
49-
if (value == null) {
50-
return null;
49+
if (value instanceof List) {
50+
return normalizeList((List<?>) value, translator);
5151
}
5252

53-
if (!(value instanceof Map)) {
54-
return value;
55-
}
56-
57-
@SuppressWarnings("unchecked")
58-
Map<String, Object> map = (Map<String, Object>) value;
53+
if (value instanceof Map) {
54+
@SuppressWarnings("unchecked")
55+
Map<String, Object> map = (Map<String, Object>) value;
5956

60-
// Check if this is a forced-value map
61-
if (map.containsKey("$type")) {
62-
if (!map.containsKey("$value")) {
63-
throw new IllegalArgumentException(
64-
"Map with '$type' entry must also have '$value' entry");
57+
// Check if this is a forced-value map
58+
if (map.containsKey("$type")) {
59+
if (!map.containsKey("$value")) {
60+
throw new IllegalArgumentException("Map with '$type' entry must also have '$value' entry");
61+
}
62+
return normalizeForcedValue(map.get("$type"), map.get("$value"), translator);
6563
}
66-
return normalizeForcedValue(map.get("$type"), map.get("$value"), translator);
67-
}
6864

69-
// Recursively normalize map values
70-
Map<String, Object> result = new HashMap<>(map);
71-
for (Map.Entry<String, Object> entry : result.entrySet()) {
72-
Object entryValue = entry.getValue();
73-
if (entryValue instanceof Map) {
74-
@SuppressWarnings("unchecked")
75-
Map<String, Object> nested = (Map<String, Object>) entryValue;
76-
result.put(entry.getKey(), normalize(nested, translator));
77-
} else if (entryValue instanceof List) {
78-
result.put(entry.getKey(), normalizeList((List<?>) entryValue, translator));
65+
Map<String, Object> result = new HashMap<>();
66+
for (Map.Entry<String, Object> entry : map.entrySet()) {
67+
result.put(entry.getKey(), normalize(entry.getValue(), translator));
7968
}
69+
return result;
8070
}
81-
return result;
71+
72+
return value;
8273
}
8374

8475
/**
@@ -133,16 +124,9 @@ private static Object normalizeForcedValue(Object type, Object value, UnaryOpera
133124
* Normalizes a list by recursively normalizing its elements
134125
*/
135126
private static List<?> normalizeList(List<?> list, UnaryOperator<String> translator) {
136-
List<Object> result = new ArrayList<>(list);
137-
for (int i = 0; i < result.size(); i++) {
138-
Object item = result.get(i);
139-
if (item instanceof Map) {
140-
@SuppressWarnings("unchecked")
141-
Map<String, Object> nested = (Map<String, Object>) item;
142-
result.set(i, normalize(nested, translator));
143-
} else if (item instanceof List) {
144-
result.set(i, normalizeList((List<?>) item, translator));
145-
}
127+
List<Object> result = new ArrayList<>();
128+
for (Object value : list) {
129+
result.add(normalize(value, translator));
146130
}
147131
return result;
148132
}

0 commit comments

Comments
 (0)