Skip to content

Commit a5cd53e

Browse files
author
Jan Kluka
committed
1.1-SNAPSHOT
Updated enchants api fetching of json values
1 parent 9e99d22 commit a5cd53e

2 files changed

Lines changed: 119 additions & 17 deletions

File tree

src/main/java/dev/drawethree/xprison/api/enchants/model/XPrisonEnchantmentBase.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package dev.drawethree.xprison.api.enchants.model;
22

3-
import com.google.gson.Gson;
43
import com.google.gson.JsonObject;
54
import com.google.gson.JsonParser;
65
import dev.drawethree.xprison.api.currency.CurrencyType;
6+
import dev.drawethree.xprison.api.utils.JsonUtils;
77
import lombok.Getter;
88
import lombok.Setter;
99
import org.bukkit.ChatColor;
@@ -66,15 +66,15 @@ public void load() {
6666
}
6767

6868
private void loadBaseProperties(JsonObject config) {
69-
this.id = config.get("id").getAsInt();
70-
this.rawName = config.get("rawName").getAsString();
71-
this.name = ChatColor.translateAlternateColorCodes('&', config.get("name").getAsString());
69+
this.id = JsonUtils.getRequiredInt(config,"id");
70+
this.rawName = JsonUtils.getRequiredString(config, "rawName");
71+
this.name = ChatColor.translateAlternateColorCodes('&', JsonUtils.getRequiredString(config,"name"));
7272
this.nameWithoutColor = name.replaceAll("§.", "");
73-
this.enabled = config.get("enabled").getAsBoolean();
74-
this.maxLevel = config.get("maxLevel").getAsInt();
75-
this.baseCost = config.get("initialCost").getAsLong();
76-
this.increaseCost = config.get("increaseCostBy").getAsLong();
77-
this.currencyType = CurrencyType.valueOf(config.get("currency").getAsString());
73+
this.enabled = JsonUtils.getRequiredBoolean(config,"enabled");
74+
this.maxLevel = JsonUtils.getRequiredInt(config,"maxLevel");
75+
this.baseCost = JsonUtils.getRequiredLong(config, "initialCost");
76+
this.increaseCost = JsonUtils.getRequiredLong(config,"increaseCostBy");
77+
this.currencyType = CurrencyType.valueOf(JsonUtils.getOptionalString(config,"currency", CurrencyType.TOKENS.name()));
7878
}
7979

8080
/**
@@ -83,20 +83,21 @@ private void loadBaseProperties(JsonObject config) {
8383
* @param config The root JSON object containing the "gui" section.
8484
*/
8585
private void loadGuiProperties(JsonObject config) {
86-
JsonObject gui = config.getAsJsonObject("gui");
87-
int slot = gui.get("slot").getAsInt();
88-
String guiName = ChatColor.translateAlternateColorCodes('&', gui.get("name").getAsString());
89-
Material mat = Material.valueOf(gui.get("material").getAsString());
90-
String base64 = gui.has("base64") ? gui.get("base64").getAsString() : null;
86+
JsonObject gui = JsonUtils.getRequiredObject(config, "gui");
9187

92-
List<String> desc = new Gson().fromJson(gui.get("description"), List.class);
88+
int slot = JsonUtils.getRequiredInt(gui, "slot");
89+
String guiName = ChatColor.translateAlternateColorCodes('&', JsonUtils.getRequiredString(gui, "name"));
90+
Material mat = Material.valueOf(JsonUtils.getRequiredString(gui, "material"));
91+
String base64 = JsonUtils.getOptionalString(gui, "base64", null);
92+
93+
List<String> desc = JsonUtils.getRequiredStringList(gui, "description");
9394
desc = desc.stream()
9495
.map(s -> ChatColor.translateAlternateColorCodes('&', s))
9596
.toList();
9697

97-
int customModelData = gui.get("customModelData").getAsInt();
98+
int customModelData = JsonUtils.getOptionalInt(gui, "customModelData", 0);
9899

99-
this.guiProperties = new XPrisonEnchantmentGuiPropertiesBase(slot, guiName, base64, mat, desc,customModelData);
100+
this.guiProperties = new XPrisonEnchantmentGuiPropertiesBase(slot, guiName, base64, mat, desc, customModelData);
100101
}
101102

102103
/**
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package dev.drawethree.xprison.api.utils;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.reflect.TypeToken;
7+
8+
import java.util.List;
9+
10+
public class JsonUtils {
11+
12+
public static String getRequiredString(JsonObject obj, String key) {
13+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
14+
throw new IllegalArgumentException("Missing or null key in enchantment config: \"" + key + "\"");
15+
}
16+
return obj.get(key).getAsString();
17+
}
18+
19+
public static int getRequiredInt(JsonObject obj, String key) {
20+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
21+
throw new IllegalArgumentException("Missing or null key in enchantment config: \"" + key + "\"");
22+
}
23+
return obj.get(key).getAsInt();
24+
}
25+
26+
public static long getRequiredLong(JsonObject obj, String key) {
27+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
28+
throw new IllegalArgumentException("Missing or null key in enchantment config: \"" + key + "\"");
29+
}
30+
return obj.get(key).getAsLong();
31+
}
32+
33+
public static boolean getRequiredBoolean(JsonObject obj, String key) {
34+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
35+
throw new IllegalArgumentException("Missing or null key in enchantment config: \"" + key + "\"");
36+
}
37+
return obj.get(key).getAsBoolean();
38+
}
39+
40+
public static JsonObject getRequiredObject(JsonObject parent, String key) {
41+
if (!parent.has(key)) {
42+
throw new IllegalArgumentException("Missing required object key: \"" + key + "\"");
43+
}
44+
45+
JsonElement element = parent.get(key);
46+
if (element == null || element.isJsonNull()) {
47+
throw new IllegalArgumentException("Key \"" + key + "\" is null in JSON object.");
48+
}
49+
50+
if (!element.isJsonObject()) {
51+
throw new IllegalArgumentException("Key \"" + key + "\" is not a JsonObject.");
52+
}
53+
54+
return element.getAsJsonObject();
55+
}
56+
57+
@SuppressWarnings("unchecked")
58+
public static <T> T getRequired(JsonObject obj, String key, Class<T> clazz) {
59+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
60+
throw new IllegalArgumentException("Missing or null key: \"" + key + "\"");
61+
}
62+
return new Gson().fromJson(obj.get(key), clazz);
63+
}
64+
65+
public static List<String> getRequiredStringList(JsonObject obj, String key) {
66+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
67+
throw new IllegalArgumentException("Missing or null key: \"" + key + "\"");
68+
}
69+
return new Gson().fromJson(obj.get(key), new TypeToken<List<String>>() {}.getType());
70+
}
71+
72+
public static String getOptionalString(JsonObject obj, String key, String defaultValue) {
73+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
74+
return defaultValue;
75+
}
76+
77+
JsonElement element = obj.get(key);
78+
79+
if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isString()) {
80+
return defaultValue;
81+
}
82+
83+
return element.getAsString();
84+
}
85+
86+
public static int getOptionalInt(JsonObject obj, String key, int defaultValue) {
87+
if (!obj.has(key) || obj.get(key).isJsonNull()) {
88+
return defaultValue;
89+
}
90+
91+
JsonElement element = obj.get(key);
92+
93+
if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isString()) {
94+
return defaultValue;
95+
}
96+
97+
return element.getAsInt();
98+
}
99+
100+
101+
}

0 commit comments

Comments
 (0)