mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-22 10:05:16 +01:00
Implement JSON backend and make it the default.
This commit is contained in:
parent
40e6b7ad24
commit
2dcc2cb2de
@ -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<Map<String, Double>>(){}.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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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));
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
backend:
|
||||
type: flatfile
|
||||
type: json
|
||||
|
||||
currency:
|
||||
name:
|
||||
|
Loading…
Reference in New Issue
Block a user