diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendJSON.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendJSON.java new file mode 100644 index 0000000..255bcee --- /dev/null +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendJSON.java @@ -0,0 +1,57 @@ +package org.appledash.saneeconomy.economy.backend.type; + +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.appledash.saneeconomy.economy.economable.Economable; + +import java.io.*; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by appledash on 1/22/17. + * Blackjack is best pony. + */ +public class EconomyStorageBackendJSON extends EconomyStorageBackendCaching { + private final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create(); + private File file; + + public EconomyStorageBackendJSON(File file) { + this.file = file; + } + + @Override + public void setBalance(Economable economable, double newBalance) { + balances.put(economable.getUniqueIdentifier(), newBalance); + saveDatabase(); + } + + @Override + @SuppressWarnings("unchecked") + public void reloadDatabase() { + if (!file.exists()) { + return; + } + + try { + balances = new ConcurrentHashMap<>((Map)gson.fromJson(new FileReader(file), new TypeToken>(){}.getType())); + } catch (FileNotFoundException e) { + throw new RuntimeException("Failed to load database!", e); + } + } + + @Override + public void waitUntilFlushed() { + // NOOP - Database is saved on every change. + } + + private synchronized void saveDatabase() { + try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false))) { + bufferedWriter.write(gson.toJson(balances)); + bufferedWriter.close(); + } catch (IOException e) { + throw new RuntimeException("Failed to save database", e); + } + } +} diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java index 41f6d48..cd3500b 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/utils/SaneEconomyConfiguration.java @@ -6,6 +6,7 @@ import org.appledash.saneeconomy.economy.Currency; import org.appledash.saneeconomy.economy.EconomyManager; import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend; import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendFlatfile; +import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendJSON; import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL; import org.appledash.saneeconomy.economy.economable.EconomableGeneric; import org.appledash.saneeconomy.economy.logger.TransactionLogger; @@ -81,6 +82,11 @@ public class SaneEconomyConfiguration { File backendFile = new File(saneEconomy.getDataFolder(), backendFileName); backend = new EconomyStorageBackendFlatfile(backendFile); logger.info("Initialized flatfile backend with file " + backendFile.getAbsolutePath()); + } else if (backendType.equalsIgnoreCase("json")) { + String backendFileName = config.getString("file", "economy.json"); + File backendFile = new File(saneEconomy.getDataFolder(), backendFileName); + backend = new EconomyStorageBackendJSON(backendFile); + logger.info("Initialized JSON backend with file " + backendFile.getAbsolutePath()); } else if (backendType.equalsIgnoreCase("mysql")) { EconomyStorageBackendMySQL mySQLBackend = new EconomyStorageBackendMySQL(loadCredentials(config)); diff --git a/SaneEconomyCore/src/main/resources/config.yml b/SaneEconomyCore/src/main/resources/config.yml index 2e07f88..61dcdf5 100644 --- a/SaneEconomyCore/src/main/resources/config.yml +++ b/SaneEconomyCore/src/main/resources/config.yml @@ -1,5 +1,5 @@ backend: - type: flatfile + type: json currency: name: