use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.
the class Commandeco method take.
private void take(BigDecimal amount, final User player, final CommandSource sender) throws ChargeException {
BigDecimal money = player.getMoney();
BigDecimal minBalance = ess.getSettings().getMinMoney();
if (money.subtract(amount).compareTo(minBalance) > 0) {
player.takeMoney(amount, sender);
} else if (sender == null) {
try {
player.setMoney(minBalance);
} catch (MaxMoneyException ex) {
// Take shouldn't be able to throw a max money exception
}
player.sendMessage(tl("takenFromAccount", NumberUtil.displayCurrency(player.getMoney(), ess)));
} else {
throw new ChargeException(tl("insufficientFunds"));
}
}
use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.
the class SignKit method onSignInteract.
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException {
final String kitName = sign.getLine(1).toLowerCase(Locale.ENGLISH).trim();
final String group = sign.getLine(2).trim();
if ((!group.isEmpty() && ("ยง2Everyone".equals(group) || player.inGroup(group))) || (group.isEmpty() && (player.isAuthorized("essentials.kits." + kitName)))) {
final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player);
try {
final Kit kit = new Kit(kitName, ess);
kit.checkDelay(player);
kit.setTime(player);
kit.expandItems(player);
charge.charge(player);
Trade.log("Sign", "Kit", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess);
} catch (NoChargeException ex) {
return false;
} catch (Exception ex) {
throw new SignException(ex.getMessage(), ex);
}
return true;
} else {
if (group.isEmpty()) {
throw new SignException(tl("noKitPermission", "essentials.kits." + kitName));
} else {
throw new SignException(tl("noKitGroup", group));
}
}
}
use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.
the class SignBuy method onSignInteract.
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
Trade items = getTrade(sign, 1, 2, player, ess);
Trade charge = getTrade(sign, 3, ess);
// Check if the player is trying to buy in bulk.
if (ess.getSettings().isAllowBulkBuySell() && player.getBase().isSneaking()) {
ItemStack heldItem = player.getItemInHand();
if (items.getItemStack().isSimilar(heldItem)) {
int initialItemAmount = items.getItemStack().getAmount();
int newItemAmount = heldItem.getAmount();
ItemStack item = items.getItemStack();
item.setAmount(newItemAmount);
items = new Trade(item, ess);
BigDecimal chargeAmount = charge.getMoney();
BigDecimal pricePerSingleItem = chargeAmount.divide(new BigDecimal(initialItemAmount));
pricePerSingleItem = pricePerSingleItem.multiply(new BigDecimal(newItemAmount));
charge = new Trade(pricePerSingleItem, ess);
}
}
charge.isAffordableFor(player);
if (!items.pay(player)) {
//TODO: TL
throw new ChargeException("Inventory full");
}
charge.charge(player);
Trade.log("Sign", "Buy", "Interact", username, charge, username, items, sign.getBlock().getLocation(), ess);
return true;
}
use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.
the class Commandrepair method repairItems.
private void repairItems(final ItemStack[] items, final IUser user, final List<String> repaired) {
for (ItemStack item : items) {
if (item == null || item.getType().isBlock() || item.getDurability() == 0) {
continue;
}
final String itemName = item.getType().toString().toLowerCase(Locale.ENGLISH);
final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), new Trade("repair-" + item.getTypeId(), new Trade("repair-item", ess), ess), ess);
try {
charge.isAffordableFor(user);
} catch (ChargeException ex) {
user.sendMessage(ex.getMessage());
continue;
}
if (!item.getEnchantments().isEmpty() && !ess.getSettings().getRepairEnchanted() && !user.isAuthorized("essentials.repair.enchanted")) {
continue;
}
try {
repairItem(item);
} catch (Exception e) {
continue;
}
try {
charge.charge(user);
} catch (ChargeException ex) {
user.sendMessage(ex.getMessage());
}
repaired.add(itemName.replace('_', ' '));
}
}
use of com.earth2me.essentials.ChargeException 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;
}
Aggregations