Skip to content

Commit 7632129

Browse files
committed
Small Fixes
Fixed Multiple Json Issues Fixed Multiple Yaml Issues
1 parent 1c4e2bc commit 7632129

4 files changed

Lines changed: 60 additions & 8 deletions

File tree

src/main/java/com/imnotstable/qualityeconomy/storage/storageformats/JsonStorageType.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public boolean initStorageProcesses() {
3232
json = new Gson().fromJson(reader, JsonObject.class);
3333
}
3434
}
35-
json.getAsJsonArray("custom-currencies").forEach(currency -> currencies.add(currency.getAsString()));
35+
toggleCustomCurrencies();
36+
save();
3637
} catch (IOException exception) {
3738
new Debug.QualityError("Failed to initiate storage processes", exception).log();
3839
return false;
@@ -76,9 +77,7 @@ public void updateAccounts(@NotNull Collection<Account> accounts) {
7677
@Override
7778
public @NotNull Map<UUID, Account> getAllAccounts() {
7879
HashMap<UUID, Account> accounts = new HashMap<>();
79-
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
80-
if (entry.getKey().equals("custom-currencies"))
81-
continue;
80+
for (Map.Entry<String, JsonElement> entry : getEntrySet()) {
8281
UUID uuid = UUID.fromString(entry.getKey());
8382
JsonObject accountJson = entry.getValue().getAsJsonObject();
8483
Account account = new Account(uuid)
@@ -104,6 +103,10 @@ public void addCurrency(@NotNull String currency) {
104103
currencies = new JsonArray();
105104
currencies.add(currency);
106105
json.add("custom-currencies", currencies);
106+
for (Map.Entry<String, JsonElement> entry : getEntrySet()) {
107+
JsonObject accountJson = entry.getValue().getAsJsonObject();
108+
accountJson.addProperty(currency, 0);
109+
}
107110
addCurrencySuccess(currency);
108111
save();
109112
}
@@ -114,6 +117,10 @@ public void removeCurrency(@NotNull String currency) {
114117
JsonArray currencies = json.getAsJsonArray("custom-currencies");
115118
currencies.remove(new Gson().toJsonTree(currency));
116119
json.add("custom-currencies", currencies);
120+
for (Map.Entry<String, JsonElement> entry : getEntrySet()) {
121+
JsonObject accountJson = entry.getValue().getAsJsonObject();
122+
accountJson.remove(currency);
123+
}
117124
removeCurrencySuccess(currency);
118125
save();
119126
}

src/main/java/com/imnotstable/qualityeconomy/storage/storageformats/YamlStorageType.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ public boolean initStorageProcesses() {
2424
if (!file.createNewFile())
2525
return false;
2626
yaml = new YamlConfiguration();
27-
} else
27+
} else {
2828
yaml = YamlConfiguration.loadConfiguration(file);
29+
}
30+
toggleCustomCurrencies();
31+
save();
2932
} catch (IOException exception) {
3033
new Debug.QualityError("Failed to initiate storage processes", exception).log();
3134
return false;
@@ -69,9 +72,7 @@ public void updateAccounts(@NotNull Collection<Account> accounts) {
6972
@Override
7073
public @NotNull Map<UUID, Account> getAllAccounts() {
7174
Map<UUID, Account> accounts = new HashMap<>();
72-
for (String uuid : yaml.getKeys(false)) {
73-
if (uuid.equals("custom-currencies"))
74-
continue;
75+
for (String uuid : getAllUniqueIds()) {
7576
Account account = new Account(UUID.fromString(uuid));
7677
account.setUsername(yaml.getString(uuid + ".USERNAME"));
7778
account.setBalance(yaml.getDouble(uuid + ".BALANCE"));
@@ -93,6 +94,8 @@ public void addCurrency(@NotNull String currency) {
9394
List<String> currencies = yaml.getStringList("custom-currencies");
9495
currencies.add(currency);
9596
yaml.set("custom-currencies", currencies);
97+
for (String uuid : getAllUniqueIds())
98+
yaml.set(uuid + "." + currency, 0);
9699
addCurrencySuccess(currency);
97100
save();
98101
}
@@ -103,6 +106,8 @@ public void removeCurrency(@NotNull String currency) {
103106
List<String> currencies = yaml.getStringList("custom-currencies");
104107
currencies.remove(currency);
105108
yaml.set("custom-currencies", currencies);
109+
for (String uuid : getAllUniqueIds())
110+
yaml.set(uuid + "." + currency, null);
106111
removeCurrencySuccess(currency);
107112
save();
108113
}

src/main/java/com/imnotstable/qualityeconomy/util/storage/EasyJson.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonArray;
6+
import com.google.gson.JsonElement;
57
import com.google.gson.JsonObject;
68
import com.imnotstable.qualityeconomy.QualityEconomy;
79
import com.imnotstable.qualityeconomy.configuration.Configuration;
@@ -11,6 +13,9 @@
1113
import java.io.File;
1214
import java.io.FileWriter;
1315
import java.io.IOException;
16+
import java.util.HashSet;
17+
import java.util.Map;
18+
import java.util.Set;
1419

1520
public class EasyJson extends EasyCurrencies {
1621

@@ -30,6 +35,23 @@ protected JsonObject serialize(Account account) {
3035
return json;
3136
}
3237

38+
protected void toggleCustomCurrencies() {
39+
if (Configuration.areCustomCurrenciesEnabled()) {
40+
if (!json.has("custom-currencies"))
41+
json.add("custom-currencies", new JsonArray());
42+
json.getAsJsonArray("custom-currencies").forEach(currency -> currencies.add(currency.getAsString()));
43+
} else {
44+
json.remove("custom-currencies");
45+
}
46+
}
47+
48+
49+
protected Set<Map.Entry<String, JsonElement>> getEntrySet() {
50+
Set<Map.Entry<String, JsonElement>> entrySet = new HashSet<>(json.entrySet());
51+
entrySet.removeIf(entry -> entry.getKey().equals("custom-currencies"));
52+
return entrySet;
53+
}
54+
3355
protected void save() {
3456
Gson gson = new GsonBuilder().create();
3557
try (FileWriter fileWriter = new FileWriter(file)) {

src/main/java/com/imnotstable/qualityeconomy/util/storage/EasyYaml.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import java.io.File;
1010
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.Set;
1113
import java.util.UUID;
1214

1315
public class EasyYaml extends EasyCurrencies {
@@ -27,6 +29,22 @@ protected void setAccount(Account account) {
2729
account.getCustomBalances().forEach((currency, balance) -> yaml.set(uuid + "." + currency, balance));
2830
}
2931

32+
protected void toggleCustomCurrencies() {
33+
if (Configuration.areCustomCurrenciesEnabled()) {
34+
if (!yaml.contains("custom-currencies"))
35+
yaml.set("custom-currencies", new ArrayList<String>());
36+
currencies.addAll(yaml.getStringList("custom-currencies"));
37+
} else {
38+
yaml.set("custom-currencies", null);
39+
}
40+
}
41+
42+
protected Set<String> getAllUniqueIds() {
43+
Set<String> keys = yaml.getKeys(false);
44+
keys.remove("custom-currencies");
45+
return keys;
46+
}
47+
3048
protected void save() {
3149
try {
3250
yaml.save(file);

0 commit comments

Comments
 (0)