Search in sources :

Example 16 with Trade

use of com.earth2me.essentials.Trade in project Essentials by drtshock.

the class SignTrade method onSignInteract.

@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
    if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) {
        final Trade store = rechargeSign(sign, ess, player);
        Trade stored;
        try {
            stored = getTrade(sign, 1, AmountType.TOTAL, true, ess);
            subtractAmount(sign, 1, stored, ess);
            Map<Integer, ItemStack> withdraw = stored.pay(player, OverflowType.RETURN);
            if (withdraw == null) {
                Trade.log("Sign", "Trade", "Withdraw", username, store, username, null, sign.getBlock().getLocation(), ess);
            } else {
                setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess);
                Trade.log("Sign", "Trade", "Withdraw", username, stored, username, new Trade(withdraw.get(0), ess), sign.getBlock().getLocation(), ess);
            }
        } catch (SignException e) {
            if (store == null) {
                throw new SignException(tl("tradeSignEmptyOwner"), e);
            }
        }
        Trade.log("Sign", "Trade", "Deposit", username, store, username, null, sign.getBlock().getLocation(), ess);
    } else {
        final Trade charge = getTrade(sign, 1, AmountType.COST, false, ess);
        final Trade trade = getTrade(sign, 2, AmountType.COST, true, ess);
        charge.isAffordableFor(player);
        addAmount(sign, 1, charge, ess);
        subtractAmount(sign, 2, trade, ess);
        if (!trade.pay(player)) {
            subtractAmount(sign, 1, charge, ess);
            addAmount(sign, 2, trade, ess);
            throw new ChargeException("Full inventory");
        }
        charge.charge(player);
        Trade.log("Sign", "Trade", "Interact", sign.getLine(3), charge, username, trade, sign.getBlock().getLocation(), ess);
    }
    sign.updateSign();
    return true;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) ChargeException(com.earth2me.essentials.ChargeException)

Example 17 with Trade

use of com.earth2me.essentials.Trade in project Essentials by drtshock.

the class SignTrade method rechargeSign.

private Trade rechargeSign(final ISign sign, final IEssentials ess, final User player) throws SignException, ChargeException {
    final Trade trade = getTrade(sign, 2, AmountType.COST, false, ess);
    if (trade.getItemStack() != null && player.getBase().getItemInHand() != null && trade.getItemStack().getType() == player.getBase().getItemInHand().getType() && trade.getItemStack().getDurability() == player.getBase().getItemInHand().getDurability() && trade.getItemStack().getEnchantments().equals(player.getBase().getItemInHand().getEnchantments())) {
        int amount = player.getBase().getItemInHand().getAmount();
        amount -= amount % trade.getItemStack().getAmount();
        if (amount > 0) {
            final ItemStack stack = player.getBase().getItemInHand().clone();
            stack.setAmount(amount);
            final Trade store = new Trade(stack, ess);
            addAmount(sign, 2, store, ess);
            store.charge(player);
            return store;
        }
    }
    return null;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack)

Example 18 with Trade

use of com.earth2me.essentials.Trade in project Essentials by drtshock.

the class Commandhome method run.

// This method contains an undocumented translation parameters #EasterEgg
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    final Trade charge = new Trade(this.getName(), ess);
    User player = user;
    String homeName = "";
    String[] nameParts;
    if (args.length > 0) {
        nameParts = args[0].split(":");
        if (nameParts[0].length() == args[0].length() || !user.isAuthorized("essentials.home.others")) {
            homeName = nameParts[0];
        } else {
            player = getPlayer(server, nameParts, 0, true, true);
            if (nameParts.length > 1) {
                homeName = nameParts[1];
            }
        }
    }
    try {
        if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) {
            final Location bed = player.getBase().getBedSpawnLocation();
            if (bed != null) {
                user.getTeleport().teleport(bed, charge, TeleportCause.COMMAND);
                throw new NoChargeException();
            } else {
                throw new Exception(tl("bedMissing"));
            }
        }
        goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge);
    } catch (NotEnoughArgumentsException e) {
        Location bed = player.getBase().getBedSpawnLocation();
        final List<String> homes = player.getHomes();
        if (homes.isEmpty() && player.equals(user)) {
            user.getTeleport().respawn(charge, TeleportCause.COMMAND);
        } else if (homes.isEmpty()) {
            throw new Exception(tl("noHomeSetPlayer"));
        } else if (homes.size() == 1 && player.equals(user)) {
            goHome(user, player, homes.get(0), charge);
        } else {
            final int count = homes.size();
            if (user.isAuthorized("essentials.home.bed")) {
                if (bed != null) {
                    homes.add(tl("bed"));
                } else {
                    homes.add(tl("bedNull"));
                }
            }
            user.sendMessage(tl("homes", StringUtil.joinList(homes), count, getHomeLimit(player)));
        }
    }
    throw new NoChargeException();
}
Also used : Trade(com.earth2me.essentials.Trade) User(com.earth2me.essentials.User) List(java.util.List) Location(org.bukkit.Location)

Example 19 with Trade

use of com.earth2me.essentials.Trade in project Essentials by drtshock.

the class Commandpay method updatePlayer.

@Override
protected void updatePlayer(final Server server, final CommandSource sender, final User player, final String[] args) throws ChargeException {
    User user = ess.getUser(sender.getPlayer());
    try {
        if (!player.isAcceptingPay()) {
            sender.sendMessage(tl("notAcceptingPay", player.getDisplayName()));
            return;
        }
        if (user.isPromptingPayConfirm() && !amount.equals(user.getConfirmingPayments().get(player))) {
            // Used to reset confirmations and inform to confirm when a new pay command has been inserted.
            if (!informToConfirm) {
                // User hasnt been asked to confirm payment to this player, reset all confirmed payments and ask to confirm again.
                // Clear previous confirmations to ensure that a new confirmation message is brought up.
                user.getConfirmingPayments().clear();
                this.informToConfirm = true;
            }
            user.getConfirmingPayments().put(player, amount);
            return;
        }
        user.payUser(player, amount);
        user.getConfirmingPayments().remove(player);
        Trade.log("Command", "Pay", "Player", user.getName(), new Trade(amount, ess), player.getName(), new Trade(amount, ess), user.getLocation(), ess);
    } catch (MaxMoneyException ex) {
        sender.sendMessage(tl("maxMoney"));
        try {
            user.setMoney(user.getMoney().add(amount));
        } catch (MaxMoneyException ignored) {
        // this should never happen
        }
    } catch (Exception e) {
        sender.sendMessage(e.getMessage());
    }
}
Also used : Trade(com.earth2me.essentials.Trade) User(com.earth2me.essentials.User) MaxMoneyException(net.ess3.api.MaxMoneyException) ChargeException(com.earth2me.essentials.ChargeException) MaxMoneyException(net.ess3.api.MaxMoneyException)

Example 20 with Trade

use of com.earth2me.essentials.Trade in project Essentials by drtshock.

the class Commandsell method sellItem.

private BigDecimal sellItem(User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception {
    int amount = ess.getWorth().getAmount(ess, user, is, args, isBulkSell);
    BigDecimal worth = ess.getWorth().getPrice(is);
    if (worth == null) {
        throw new Exception(tl("itemCannotBeSold"));
    }
    if (amount <= 0) {
        if (!isBulkSell) {
            user.sendMessage(tl("itemSold", NumberUtil.displayCurrency(BigDecimal.ZERO, ess), BigDecimal.ZERO, is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(worth, ess)));
        }
        return BigDecimal.ZERO;
    }
    BigDecimal result = worth.multiply(BigDecimal.valueOf(amount));
    //TODO: Prices for Enchantments
    final ItemStack ris = is.clone();
    ris.setAmount(amount);
    if (!user.getBase().getInventory().containsAtLeast(ris, amount)) {
        // This should never happen.
        throw new IllegalStateException("Trying to remove more items than are available.");
    }
    user.getBase().getInventory().removeItem(ris);
    user.getBase().updateInventory();
    Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), ess);
    user.giveMoney(result);
    user.sendMessage(tl("itemSold", NumberUtil.displayCurrency(result, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(worth, ess)));
    logger.log(Level.INFO, tl("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)));
    return result;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) BigDecimal(java.math.BigDecimal)

Aggregations

Trade (com.earth2me.essentials.Trade)43 ItemStack (org.bukkit.inventory.ItemStack)13 ChargeException (com.earth2me.essentials.ChargeException)10 BigDecimal (java.math.BigDecimal)9 Location (org.bukkit.Location)9 User (com.earth2me.essentials.User)7 NoChargeException (com.earth2me.essentials.commands.NoChargeException)2 MaxMoneyException (net.ess3.api.MaxMoneyException)2 Kit (com.earth2me.essentials.Kit)1 Mob (com.earth2me.essentials.Mob)1 Teleport (com.earth2me.essentials.Teleport)1 Commandrepair (com.earth2me.essentials.commands.Commandrepair)1 NotEnoughArgumentsException (com.earth2me.essentials.commands.NotEnoughArgumentsException)1 IText (com.earth2me.essentials.textreader.IText)1 KeywordReplacer (com.earth2me.essentials.textreader.KeywordReplacer)1 TextInput (com.earth2me.essentials.textreader.TextInput)1 TextPager (com.earth2me.essentials.textreader.TextPager)1 IOException (java.io.IOException)1 List (java.util.List)1 World (org.bukkit.World)1