Search in sources :

Example 16 with TEAccount

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!"));
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) TECurrency(com.erigitic.config.TECurrency) HashMap(java.util.HashMap) Currency(org.spongepowered.api.service.economy.Currency) TECurrency(com.erigitic.config.TECurrency) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount)

Example 17 with TEAccount

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();
}
Also used : ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) Currency(org.spongepowered.api.service.economy.Currency) TECurrency(com.erigitic.config.TECurrency) Text(org.spongepowered.api.text.Text) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal)

Example 18 with TEAccount

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"));
                    }
                }
            }
        }
    }
}
Also used : SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) TEAccount(com.erigitic.config.TEAccount) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Aggregations

TEAccount (com.erigitic.config.TEAccount)18 BigDecimal (java.math.BigDecimal)14 Player (org.spongepowered.api.entity.living.player.Player)12 Listener (org.spongepowered.api.event.Listener)10 Text (org.spongepowered.api.text.Text)7 Currency (org.spongepowered.api.service.economy.Currency)6 HashMap (java.util.HashMap)5 CommandException (org.spongepowered.api.command.CommandException)5 TransactionResult (org.spongepowered.api.service.economy.transaction.TransactionResult)5 TECurrency (com.erigitic.config.TECurrency)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 User (org.spongepowered.api.entity.living.player.User)3 BlockState (org.spongepowered.api.block.BlockState)2 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 BlockTrait (org.spongepowered.api.block.trait.BlockTrait)2 FishData (org.spongepowered.api.data.manipulator.mutable.item.FishData)2 Entity (org.spongepowered.api.entity.Entity)2 EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2