/** Default Constructor so we can access this class from elsewhere */ public AccountManager(TotalEconomy totalEconomy) { this.totalEconomy = totalEconomy; logger = totalEconomy.getLogger(); server = totalEconomy.getServer(); accountsFile = new File(totalEconomy.getConfigDir(), "accounts.conf"); configManager = HoconConfigurationLoader.builder().setFile(accountsFile).build(); try { accountConfig = configManager.load(); } catch (IOException e) { logger.warn("Could not load account config!"); } }
/** * Removes an amount from the specified player's balance. Checks if the player has a balance * greater then or equal to the amount being removed. * * @param uuid object representing the UUID of a player * @param amount amount to be removed from balance */ @Override public void removeFromBalance(UUID uuid, BigDecimal amount) { if (hasAccount(uuid)) { BigDecimal newBalance = new BigDecimal(getBalance(uuid).toString()).subtract(new BigDecimal(amount.toString())); try { accountConfig .getNode(uuid.toString(), "balance") .setValue(newBalance.setScale(2, BigDecimal.ROUND_DOWN).toString()); configManager.save(accountConfig); server .getPlayer(uuid) .get() .sendMessage( Texts.of( TextColors.GOLD, totalEconomy.getCurrencySymbol(), amount, TextColors.GRAY, " has been removed from your balance.")); } catch (IOException e) { logger.warn("Could not remove from player balance!"); } } }
/** * Creates a new account for the player. * * @param uuid object representing the UUID of a player */ @Override public void createAccount(UUID uuid) { try { if (accountConfig.getNode(uuid.toString(), "balance").getValue() == null) { BigDecimal startBalance = new BigDecimal(totalEconomy.getStartingBalance()); accountConfig .getNode(uuid.toString(), "balance") .setValue(startBalance.setScale(2, BigDecimal.ROUND_DOWN).toString()); accountConfig.getNode(uuid.toString(), "job").setValue("Unemployed"); accountConfig.getNode(uuid.toString(), "jobnotifications").setValue("true"); } configManager.save(accountConfig); } catch (IOException e) { logger.warn("Could not create account!"); } }