Skip to content

Instantly share code, notes, and snippets.

@test414EFF
Created March 20, 2020 21:18
Show Gist options
  • Save test414EFF/6ac16a978b9b1f6d2939792487564e62 to your computer and use it in GitHub Desktop.
Save test414EFF/6ac16a978b9b1f6d2939792487564e62 to your computer and use it in GitHub Desktop.
package cc.modernity.moderneconomy.api;
import de.leonhard.storage.Json;
import de.leonhard.storage.internal.FlatFile;
import de.leonhard.storage.internal.settings.ReloadSettings;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
// map account-uuids in a list to the player/business (each account has a UUID, saved to player or business in a list)
// business account UUIDs can be saved with the prefix "business-"; player account UUIDs can be saved with the prefix "player-"
// accounts are saved by their UUID (in plugins/moderneconomy/accounts); saved info includes type, balance & creation date
public class AccountStorage {
private final FlatFile accounts = new Json("accounts", "plugins/ModernEconomy/");
private final Util utility = new Util();
// ACCOUNTS
public List<String> getAccountList(String accownertype, String accowner, String acctype) {
if(acctype.equals("savingsAccounts")||acctype.equals("checkingAccounts")) {
if(accownertype.equals("player")) {
Player accplayer = Bukkit.getPlayer(accowner);
return accounts.getStringList("player-" + utility.getUUID(accplayer) + "-" + acctype);
} else {
return accounts.getStringList("business-" + accowner + "-" + acctype);
}
} else {
return null;
}
}
/*
accounts stored in map of strings
account map has account type, owner, balance, and date of creation
*/
public void createAccount(Player p, String type) {
if (type.equals("savingsAccount")) {
// add account to player's account list
List<String> savingsList = accounts.getStringList("player-" + utility.getUUID(p) + "-savingsAccounts");
final String accUUID = UUID.randomUUID().toString();
savingsList.add(savingsList.size(), accUUID);
accounts.set("player-" + utility.getUUID(p) + "-savingsAccounts", savingsList);
// save bank account to server account list
List<String> allBankAccounts = accounts.getStringList("allBankAccounts");
allBankAccounts.add(allBankAccounts.size(), accUUID);
accounts.set("allBankAccounts", allBankAccounts);
// actual account data saving
HashMap account = new HashMap();
LocalDate currentDate = LocalDate.now();
account.put("owner", utility.getUUID(p));
account.put("type", "Savings");
account.put("balance", 0.0);
account.put("dateOfCreation", currentDate);
accounts.set(accUUID, account);
accounts.write();
} else if (type.equals("checkingAccount")) {
// add account to player's account list
List<String> checkingList = accounts.getStringList("player-" + utility.getUUID(p) + "-checkingAccounts");
final String accUUID = UUID.randomUUID().toString();
checkingList.add(checkingList.size(), accUUID);
accounts.set("player-" + utility.getUUID(p) + "-checkingAccounts", checkingList);
// save bank account to server account list
List<String> allBankAccounts = accounts.getStringList("allBankAccounts");
allBankAccounts.add(allBankAccounts.size(), accUUID);
accounts.set("allBankAccounts", allBankAccounts);
// actual account data saving
HashMap account = new HashMap();
LocalDate currentDate = LocalDate.now();
account.put("owner", utility.getUUID(p));
account.put("type", "Checking");
account.put("balance", 0.0);
account.put("dateOfCreation", currentDate);
accounts.set(accUUID, account);
}
}
public Object getAccountBalance(String accUUID) {
HashMap account = new HashMap(accounts.getMap(accUUID));
return account.get("balance");
}
public Object getAccountDOC(String accUUID) {
HashMap account = new HashMap(accounts.getMap(accUUID));
return account.get("dateOfCreation");
}
public Object getAccountType(String accUUID) {
HashMap account = new HashMap(accounts.getMap(accUUID));
return account.get("type");
}
public void addMoney(String accUUID, BigDecimal amount) {
HashMap account = new HashMap(accounts.getMap(accUUID));
Object currentBal = getAccountBalance(accUUID);
BigDecimal currentBalance = new BigDecimal( currentBal.toString() );
account.put("balance", currentBalance.add(amount));
accounts.set(accUUID, account);
}
public void removeMoney(String accUUID, BigDecimal amount) {
HashMap account = new HashMap(accounts.getMap(accUUID));
Object currentBal = getAccountBalance(accUUID);
BigDecimal currentBalance = new BigDecimal( currentBal.toString() );
account.put("balance", currentBalance.subtract(amount));
accounts.set(accUUID, account);
}
// TAXES
public Object checkTaxDate() {
LocalDate today = LocalDate.now();
LocalDate twoWeeksFromNow = today.plus(2, ChronoUnit.WEEKS);
return accounts.getOrSetDefault("nextTaxDate", twoWeeksFromNow);
}
public void setTaxDateToday() {
LocalDate today = LocalDate.now();
accounts.set("nextTaxDate", today);
System.out.println("Next tax date set to today!");
}
public void taxAllAccounts() {
Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getServer().getPluginManager().getPlugin("ModernEconomy"), new Runnable() {
@Override
public void run() {
List<String> allBankAccounts = accounts.getStringList("allBankAccounts");
System.out.println("Taxing account all accounts asynchronously!");
for (String currentBankAccount : allBankAccounts) {
System.out.println("Taxing account: " + currentBankAccount);
Object currentBal = getAccountBalance(currentBankAccount);
BigDecimal currentBalance = new BigDecimal( currentBal.toString() );
System.out.println("Current Balance " + currentBalance);
BigDecimal taxRate = new BigDecimal(0.1).setScale(2, RoundingMode.HALF_UP);
System.out.println("Current tax rate: " + taxRate);
BigDecimal taxAmount = currentBalance.multiply(taxRate);
removeMoney(currentBankAccount, taxAmount);
System.out.println("Removing " + taxAmount + " from " + currentBankAccount);
}
LocalDate today = LocalDate.now();
LocalDate oneWeekFromNow = today.plus(1, ChronoUnit.WEEKS);
accounts.set("nextTaxDate", oneWeekFromNow);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment