Skip to content

Commit 3ce7c50

Browse files
committed
Refactor to ensure parent objects exist before setting values
- Add `ensureParentObjectsExist` method - Update `setInt` to use `ensureParentObjectsExist` - Update `setString` to use `ensureParentObjectsExist` - Update `setBoolean` to use `ensureParentObjectsExist` - Update `setLong` to use `ensureParentObjectsExist` - Update `setStringList` to use `ensureParentObjectsExist`
1 parent 21801fa commit 3ce7c50

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

SimpleAPI/src/main/java/com/bencodez/simpleapi/file/BungeeJsonFile.java

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ private JsonObject navigateToNode(String path) {
6464
return current;
6565
}
6666

67+
private JsonObject ensureParentObjectsExist(String path) {
68+
String[] parts = path.split("\\.");
69+
JsonObject current = conf;
70+
71+
// Iterate through path parts except the last part, which is the actual key
72+
for (int i = 0; i < parts.length - 1; i++) {
73+
if (!current.has(parts[i]) || !current.get(parts[i]).isJsonObject()) {
74+
// Create a new JsonObject if none exists or it's not an object
75+
current.add(parts[i], new JsonObject());
76+
}
77+
current = current.getAsJsonObject(parts[i]);
78+
}
79+
80+
return current;
81+
}
82+
6783
private String getLastPathPart(String path) {
6884
String[] parts = path.split("\\.");
6985
return parts[parts.length - 1];
@@ -124,35 +140,29 @@ public JsonElement getNode(String path) {
124140
return node != null && node.has(lastPart) ? node.get(lastPart) : null;
125141
}
126142

127-
public void setBoolean(String path, boolean value) {
128-
JsonObject node = navigateToNode(path);
129-
if (node != null) {
130-
String lastPart = getLastPathPart(path);
131-
node.addProperty(lastPart, value);
132-
save();
133-
}
143+
public void setInt(String path, int value) {
144+
JsonObject node = ensureParentObjectsExist(path);
145+
String lastPart = getLastPathPart(path);
146+
node.addProperty(lastPart, value);
147+
save();
134148
}
135149

136-
public void setInt(String path, int value) {
137-
JsonObject node = navigateToNode(path);
138-
if (node != null) {
139-
String lastPart = getLastPathPart(path);
140-
node.addProperty(lastPart, value);
141-
save();
142-
}
150+
public void setString(String path, String value) {
151+
JsonObject node = ensureParentObjectsExist(path);
152+
String lastPart = getLastPathPart(path);
153+
node.addProperty(lastPart, value);
154+
save();
143155
}
144156

145-
public void setLong(String path, long value) {
146-
JsonObject node = navigateToNode(path);
147-
if (node != null) {
148-
String lastPart = getLastPathPart(path);
149-
node.addProperty(lastPart, value);
150-
save();
151-
}
157+
public void setBoolean(String path, boolean value) {
158+
JsonObject node = ensureParentObjectsExist(path);
159+
String lastPart = getLastPathPart(path);
160+
node.addProperty(lastPart, value);
161+
save();
152162
}
153163

154-
public void setString(String path, String value) {
155-
JsonObject node = navigateToNode(path);
164+
public void setLong(String path, long value) {
165+
JsonObject node = ensureParentObjectsExist(path);
156166
if (node != null) {
157167
String lastPart = getLastPathPart(path);
158168
node.addProperty(lastPart, value);
@@ -161,7 +171,7 @@ public void setString(String path, String value) {
161171
}
162172

163173
public void setStringList(String path, List<String> value) {
164-
JsonObject node = navigateToNode(path);
174+
JsonObject node = ensureParentObjectsExist(path);
165175
if (node != null) {
166176
String lastPart = getLastPathPart(path);
167177
JsonArray jsonArray = new JsonArray();

0 commit comments

Comments
 (0)