Skip to content

Commit 21801fa

Browse files
committed
Add new class to bungee json api
1 parent 148694e commit 21801fa

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
package com.bencodez.simpleapi.file;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonArray;
6+
import com.google.gson.JsonElement;
7+
import com.google.gson.JsonObject;
8+
import com.google.gson.JsonParser;
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
12+
import java.io.File;
13+
import java.io.FileReader;
14+
import java.io.FileWriter;
15+
import java.io.IOException;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
public class BungeeJsonFile {
21+
@Getter
22+
@Setter
23+
private JsonObject conf;
24+
25+
@Getter
26+
private File file;
27+
28+
private Gson gson;
29+
30+
public BungeeJsonFile(File file) {
31+
this.file = file;
32+
this.gson = new GsonBuilder().setPrettyPrinting().create();
33+
34+
if (!file.exists()) {
35+
try {
36+
file.getParentFile().mkdirs();
37+
file.createNewFile();
38+
conf = new JsonObject();
39+
save(); // Save file with empty JsonObject upon creation
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
}
43+
} else {
44+
try (FileReader reader = new FileReader(file)) {
45+
conf = JsonParser.parseReader(reader).getAsJsonObject();
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
}
51+
52+
private JsonObject navigateToNode(String path) {
53+
String[] parts = path.split("\\.");
54+
JsonObject current = conf;
55+
56+
for (int i = 0; i < parts.length - 1; i++) {
57+
if (current.has(parts[i]) && current.get(parts[i]).isJsonObject()) {
58+
current = current.getAsJsonObject(parts[i]);
59+
} else {
60+
return null;
61+
}
62+
}
63+
64+
return current;
65+
}
66+
67+
private String getLastPathPart(String path) {
68+
String[] parts = path.split("\\.");
69+
return parts[parts.length - 1];
70+
}
71+
72+
public boolean getBoolean(String path, boolean def) {
73+
JsonObject node = navigateToNode(path);
74+
String lastPart = getLastPathPart(path);
75+
return node != null && node.has(lastPart) ? node.get(lastPart).getAsBoolean() : def;
76+
}
77+
78+
public int getInt(String path, int def) {
79+
JsonObject node = navigateToNode(path);
80+
String lastPart = getLastPathPart(path);
81+
return node != null && node.has(lastPart) ? node.get(lastPart).getAsInt() : def;
82+
}
83+
84+
public long getLong(String path, long def) {
85+
JsonObject node = navigateToNode(path);
86+
String lastPart = getLastPathPart(path);
87+
return node != null && node.has(lastPart) ? node.get(lastPart).getAsLong() : def;
88+
}
89+
90+
public String getString(String path, String def) {
91+
JsonObject node = navigateToNode(path);
92+
String lastPart = getLastPathPart(path);
93+
return node != null && node.has(lastPart) ? node.get(lastPart).getAsString() : def;
94+
}
95+
96+
public List<String> getStringList(String path, List<String> def) {
97+
JsonObject node = navigateToNode(path);
98+
String lastPart = getLastPathPart(path);
99+
if (node != null && node.has(lastPart)) {
100+
List<String> list = new ArrayList<>();
101+
for (JsonElement element : node.getAsJsonArray(lastPart)) {
102+
list.add(element.getAsString());
103+
}
104+
return list;
105+
}
106+
return def;
107+
}
108+
109+
public List<String> getKeys(String path) {
110+
JsonObject node = navigateToNode(path);
111+
if (node != null) {
112+
List<String> keys = new ArrayList<>();
113+
for (Map.Entry<String, JsonElement> entry : node.entrySet()) {
114+
keys.add(entry.getKey());
115+
}
116+
return keys;
117+
}
118+
return new ArrayList<>();
119+
}
120+
121+
public JsonElement getNode(String path) {
122+
JsonObject node = navigateToNode(path);
123+
String lastPart = getLastPathPart(path);
124+
return node != null && node.has(lastPart) ? node.get(lastPart) : null;
125+
}
126+
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+
}
134+
}
135+
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+
}
143+
}
144+
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+
}
152+
}
153+
154+
public void setString(String path, String value) {
155+
JsonObject node = navigateToNode(path);
156+
if (node != null) {
157+
String lastPart = getLastPathPart(path);
158+
node.addProperty(lastPart, value);
159+
save();
160+
}
161+
}
162+
163+
public void setStringList(String path, List<String> value) {
164+
JsonObject node = navigateToNode(path);
165+
if (node != null) {
166+
String lastPart = getLastPathPart(path);
167+
JsonArray jsonArray = new JsonArray();
168+
for (String item : value) {
169+
jsonArray.add(item);
170+
}
171+
node.add(lastPart, jsonArray);
172+
save();
173+
}
174+
}
175+
176+
public void remove(String path) {
177+
JsonObject node = navigateToNode(path);
178+
if (node != null) {
179+
String lastPart = getLastPathPart(path);
180+
node.remove(lastPart);
181+
save();
182+
}
183+
}
184+
185+
public void reload() {
186+
if (file.exists()) {
187+
try (FileReader reader = new FileReader(file)) {
188+
conf = JsonParser.parseReader(reader).getAsJsonObject();
189+
} catch (IOException e) {
190+
e.printStackTrace();
191+
}
192+
}
193+
}
194+
195+
public void save() {
196+
try (FileWriter writer = new FileWriter(file)) {
197+
gson.toJson(conf, writer);
198+
} catch (IOException e) {
199+
e.printStackTrace();
200+
}
201+
}
202+
}

0 commit comments

Comments
 (0)