Search in sources :

Example 71 with Trade

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

the class SignFree method onSignInteract.

@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException {
    ItemStack itemStack = getItemStack(sign.getLine(1), 1, ess);
    itemStack = getItemMeta(itemStack, sign.getLine(2), ess);
    final ItemStack item = getItemMeta(itemStack, sign.getLine(3), ess);
    if (item.getType() == Material.AIR) {
        throw new SignException(tl("cantSpawnItem", "Air"));
    }
    item.setAmount(item.getType().getMaxStackSize());
    ItemMeta meta = item.getItemMeta();
    final String displayName = meta.hasDisplayName() ? meta.getDisplayName() : item.getType().toString();
    Inventory invent = ess.getServer().createInventory(player.getBase(), 36, displayName);
    for (int i = 0; i < 36; i++) {
        invent.addItem(item);
    }
    player.getBase().openInventory(invent);
    Trade.log("Sign", "Free", "Interact", username, null, username, new Trade(item, ess), sign.getBlock().getLocation(), ess);
    return true;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Inventory(org.bukkit.inventory.Inventory)

Example 72 with Trade

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

the class SignTrade method onSignBreak.

@Override
protected boolean onSignBreak(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, MaxMoneyException {
    final String signOwner = sign.getLine(3);
    final boolean isOwner = (signOwner.length() > 3 && signOwner.substring(2).equalsIgnoreCase(username));
    final boolean canBreak = isOwner ? true : player.isAuthorized("essentials.signs.trade.override");
    final boolean canCollect = isOwner ? true : player.isAuthorized("essentials.signs.trade.override.collect");
    if (canBreak) {
        try {
            final Trade stored1 = getTrade(sign, 1, AmountType.TOTAL, false, ess);
            final Trade stored2 = getTrade(sign, 2, AmountType.TOTAL, false, ess);
            if (!canCollect) {
                Trade.log("Sign", "Trade", "Destroy", signOwner, stored2, username, stored1, sign.getBlock().getLocation(), ess);
                return true;
            }
            final Map<Integer, ItemStack> withdraw1 = stored1.pay(player, OverflowType.RETURN);
            final Map<Integer, ItemStack> withdraw2 = stored2.pay(player, OverflowType.RETURN);
            if (withdraw1 == null && withdraw2 == null) {
                Trade.log("Sign", "Trade", "Break", signOwner, stored2, username, stored1, sign.getBlock().getLocation(), ess);
                return true;
            }
            setAmount(sign, 1, BigDecimal.valueOf(withdraw1 == null ? 0L : withdraw1.get(0).getAmount()), ess);
            Trade.log("Sign", "Trade", "Withdraw", signOwner, stored1, username, withdraw1 == null ? null : new Trade(withdraw1.get(0), ess), sign.getBlock().getLocation(), ess);
            setAmount(sign, 2, BigDecimal.valueOf(withdraw2 == null ? 0L : withdraw2.get(0).getAmount()), ess);
            Trade.log("Sign", "Trade", "Withdraw", signOwner, stored2, username, withdraw2 == null ? null : new Trade(withdraw2.get(0), ess), sign.getBlock().getLocation(), ess);
            sign.updateSign();
        } catch (SignException e) {
            if (player.isAuthorized("essentials.signs.trade.override")) {
                return true;
            }
            throw e;
        }
        return false;
    } else {
        return false;
    }
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack)

Example 73 with Trade

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

the class SignTrade method getTrade.

protected final Trade getTrade(final ISign sign, final int index, final AmountType amountType, final boolean notEmpty, final IEssentials ess) throws SignException {
    final String line = sign.getLine(index).trim();
    if (line.isEmpty()) {
        throw new SignException("Empty line");
    }
    final String[] split = line.split("[ :]+");
    if (split.length == 2) {
        try {
            final BigDecimal money = getMoney(split[0]);
            final BigDecimal amount = notEmpty ? getBigDecimalPositive(split[1]) : getBigDecimal(split[1]);
            if (money != null && amount != null) {
                return new Trade(amountType == AmountType.COST ? money : amount, ess);
            }
        } catch (SignException e) {
            throw new SignException(tl("tradeSignEmpty"), e);
        }
    }
    if (split.length == 3) {
        if (split[1].equalsIgnoreCase("exp") || split[1].equalsIgnoreCase("xp")) {
            final int stackamount = getIntegerPositive(split[0]);
            int amount = getInteger(split[2]);
            if (amountType == AmountType.ROUNDED) {
                amount -= amount % stackamount;
            }
            if (notEmpty && (amount < 1 || stackamount < 1)) {
                throw new SignException(tl("tradeSignEmpty"));
            }
            return new Trade((amountType == AmountType.COST ? stackamount : amount), ess);
        } else {
            final int stackamount = getIntegerPositive(split[0]);
            final ItemStack item = getItemStack(split[1], stackamount, ess);
            int amount = getInteger(split[2]);
            if (amountType == AmountType.ROUNDED) {
                amount -= amount % stackamount;
            }
            if (notEmpty && (amount < 1 || stackamount < 1 || item.getType() == Material.AIR || amount < stackamount)) {
                throw new SignException(tl("tradeSignEmpty"));
            }
            item.setAmount(amountType == AmountType.COST ? stackamount : amount);
            return new Trade(item, ess);
        }
    }
    throw new SignException(tl("invalidSignLine", index + 1));
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) BigDecimal(java.math.BigDecimal)

Example 74 with Trade

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

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 75 with Trade

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

the class SignWarp method onSignInteract.

@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException {
    final String warpName = sign.getLine(1);
    final String group = sign.getLine(2);
    if ((!group.isEmpty() && ("ยง2Everyone".equals(group) || player.inGroup(group))) || (group.isEmpty() && (!ess.getSettings().getPerWarpPermission() || player.isAuthorized("essentials.warps." + warpName)))) {
        final Trade charge = getTrade(sign, 3, ess);
        try {
            player.getTeleport().warp(player, warpName, charge, TeleportCause.PLUGIN);
            Trade.log("Sign", "Warp", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess);
        } catch (Exception ex) {
            throw new SignException(ex.getMessage(), ex);
        }
        return true;
    }
    return false;
}
Also used : Trade(com.earth2me.essentials.Trade) ChargeException(com.earth2me.essentials.ChargeException)

Aggregations

Trade (com.earth2me.essentials.Trade)86 ItemStack (org.bukkit.inventory.ItemStack)26 ChargeException (com.earth2me.essentials.ChargeException)20 BigDecimal (java.math.BigDecimal)18 Location (org.bukkit.Location)18 User (com.earth2me.essentials.User)14 NoChargeException (com.earth2me.essentials.commands.NoChargeException)4 MaxMoneyException (net.ess3.api.MaxMoneyException)4 World (org.bukkit.World)4 Kit (com.earth2me.essentials.Kit)2 Mob (com.earth2me.essentials.Mob)2 Teleport (com.earth2me.essentials.Teleport)2 Commandrepair (com.earth2me.essentials.commands.Commandrepair)2 NotEnoughArgumentsException (com.earth2me.essentials.commands.NotEnoughArgumentsException)2 IText (com.earth2me.essentials.textreader.IText)2 KeywordReplacer (com.earth2me.essentials.textreader.KeywordReplacer)2 TextInput (com.earth2me.essentials.textreader.TextInput)2 TextPager (com.earth2me.essentials.textreader.TextPager)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2