use of com.erigitic.config.TEAccount in project TotalEconomy by Erigitic.
the class BalanceCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (src instanceof Player) {
Player sender = (Player) src;
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(sender.getUniqueId()).get();
Optional<String> optCurrencyName = args.getOne("currencyName");
Map<String, String> messageValues = new HashMap<>();
if (optCurrencyName.isPresent()) {
Optional<Currency> optCurrency = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + optCurrencyName.get().toLowerCase());
if (optCurrency.isPresent()) {
TECurrency currency = (TECurrency) optCurrency.get();
messageValues.put("currency", currency.getName());
messageValues.put("amount", currency.format(playerAccount.getBalance(currency)).toPlain());
sender.sendMessage(messageManager.getMessage("command.balance.other", messageValues));
} else {
throw new CommandException(Text.of(TextColors.RED, "[TE] The specified currency does not exist!"));
}
} else {
messageValues.put("amount", defaultCurrency.format(playerAccount.getBalance(defaultCurrency)).toPlain());
sender.sendMessage(messageManager.getMessage("command.balance.default", messageValues));
}
return CommandResult.success();
} else {
throw new CommandException(Text.of("[TE] This command can only be run by a player!"));
}
}
use of com.erigitic.config.TEAccount in project TotalEconomy by Erigitic.
the class BalanceTopCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
ConfigurationNode accountNode = accountManager.getAccountConfig();
List<Text> accountBalances = new ArrayList<>();
Map<String, BigDecimal> accountBalancesMap = new HashMap<>();
Currency defaultCurrency = totalEconomy.getDefaultCurrency();
accountNode.getChildrenMap().keySet().forEach(accountUUID -> {
UUID uuid;
// Check if the account is virtual or not. If virtual, skip the rest of the execution and move on to next account.
try {
uuid = UUID.fromString(accountUUID.toString());
} catch (IllegalArgumentException e) {
return;
}
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(uuid).get();
Text playerName = playerAccount.getDisplayName();
accountBalancesMap.put(playerName.toPlain(), playerAccount.getBalance(defaultCurrency));
});
accountBalancesMap.entrySet().stream().sorted(Map.Entry.<String, BigDecimal>comparingByValue().reversed()).limit(10).forEach(entry -> accountBalances.add(Text.of(TextColors.GRAY, entry.getKey(), ": ", TextColors.GOLD, defaultCurrency.format(entry.getValue()).toPlain())));
builder.title(Text.of(TextColors.GOLD, "Top 10 Balances")).contents(accountBalances).sendTo(src);
return CommandResult.success();
}
use of com.erigitic.config.TEAccount in project TotalEconomy by Erigitic.
the class ShopManager method onShiftClickInventory.
/**
* Handles removing items from a shop. Items will be removed if the shop owner shift clicks them, otherwise
* the event will be canceled.
*
* @param event Shift click inventory
* @param player The player shift clicking within an inventory
* @param inventory The inventory being interacted with
*/
@Listener
public void onShiftClickInventory(ClickInventoryEvent.Shift event, @First Player player, @Getter("getTargetInventory") Inventory inventory) {
Optional<PlayerShopInfo> playerShopInfoOpt = player.get(ShopKeys.PLAYER_SHOP_INFO);
if (playerShopInfoOpt.isPresent()) {
Location location = player.get(ShopKeys.PLAYER_SHOP_INFO).get().getOpenShopLocation();
Optional<TileEntity> tileEntityOpt = location.getTileEntity();
if (tileEntityOpt.isPresent()) {
Optional<Shop> shopOpt = tileEntityOpt.get().get(ShopKeys.SINGLE_SHOP);
if (shopOpt.isPresent()) {
Shop shop = shopOpt.get();
ItemStack clickedItem = ItemStack.builder().fromSnapshot(event.getTransactions().get(0).getOriginal()).build();
Optional<ShopItem> shopItemOpt = clickedItem.get(ShopKeys.SHOP_ITEM);
if (player.getUniqueId().equals(shop.getOwner()) && shopItemOpt.isPresent()) {
for (SlotTransaction transaction : event.getTransactions()) {
transaction.setCustom(ItemStack.empty());
}
ItemStack returnedItem = removeShopItemData(clickedItem.copy());
returnedItem.setQuantity(clickedItem.getQuantity());
player.getInventory().offer(returnedItem);
} else if (player.getUniqueId().equals(shop.getOwner())) {
event.setCancelled(false);
} else if (shopItemOpt.isPresent()) {
ShopItem shopItem = shopItemOpt.get();
int purchasedQuantity = clickedItem.getQuantity();
TEAccount ownerAccount = (TEAccount) accountManager.getOrCreateAccount(shop.getOwner()).get();
TEAccount customerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
if (customerAccount.getBalance(totalEconomy.getDefaultCurrency()).doubleValue() >= purchasedQuantity * shopItem.getPrice()) {
ItemStack purchasedItem = removeShopItemData(clickedItem.copy());
purchasedItem.setQuantity(purchasedQuantity);
Collection<ItemStackSnapshot> rejectedItems = player.getInventory().query(GridInventory.class, Hotbar.class).offer(purchasedItem).getRejectedItems();
if (rejectedItems.size() == 0) {
for (SlotTransaction transaction : event.getTransactions()) {
transaction.setCustom(ItemStack.empty());
}
customerAccount.transfer(ownerAccount, totalEconomy.getDefaultCurrency(), BigDecimal.valueOf(purchasedQuantity * shopItem.getPrice()), event.getCause());
player.getInventory().offer(purchasedItem);
} else {
event.getTransactions().get(0).setValid(false);
player.sendMessage(messageManager.getMessage("shops.purchase.noroom"));
}
} else {
invalidateTransactions(event.getTransactions());
player.sendMessage(messageManager.getMessage("shops.purchase.insufficientfunds"));
}
}
}
}
}
}
Aggregations