@Override protected boolean onSignBreak(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException { if ((sign.getLine(3).length() > 3 && sign.getLine(3).substring(2).equalsIgnoreCase(username)) || SignsPermissions.TRADE_OVERRIDE.isAuthorized(player)) { try { final Trade stored1 = getTrade(sign, 1, true, false, ess); final Trade stored2 = getTrade(sign, 2, true, false, ess); stored1.pay(player); stored2.pay(player); Trade.log("Sign", "Trade", "Break", username, stored2, username, stored1, sign.getBlock().getLocation(), ess); } catch (SignException e) { if (SignsPermissions.TRADE_OVERRIDE.isAuthorized(player)) { return true; } throw e; } return true; } else { return false; } }
@Override protected boolean onSignInteract(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException { if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) { final Trade store = rechargeSign(sign, ess, player); Trade stored = null; try { stored = getTrade(sign, 1, true, true, ess); subtractAmount(sign, 1, stored, ess); stored.pay(player); } catch (SignException e) { if (store == null) { throw new SignException(_("tradeSignEmptyOwner"), e); } } Trade.log("Sign", "Trade", "OwnerInteract", username, store, username, stored, sign.getBlock().getLocation(), ess); } else { final Trade charge = getTrade(sign, 1, false, false, ess); final Trade trade = getTrade(sign, 2, false, true, ess); charge.isAffordableFor(player); addAmount(sign, 1, charge, ess); subtractAmount(sign, 2, trade, ess); if (!trade.pay(player, false)) { 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; }
@Override protected boolean onSignCreate(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException, ChargeException { validateTrade(sign, 1, false, ess); validateTrade(sign, 2, true, ess); final Trade charge = getTrade(sign, 2, true, true, ess); charge.isAffordableFor(player); sign.setLine(3, "ยง8" + username); charge.charge(player); Trade.log("Sign", "Trade", "Create", username, charge, username, null, sign.getBlock().getLocation(), ess); return true; }
private void sellItem(IUser user, ItemStack is, String[] args, boolean isBulkSell) throws Exception { if (is == null || is.getType() == Material.AIR) { throw new Exception(_("itemSellAir")); } int id = is.getTypeId(); int amount = 0; if (args.length > 1) { amount = Integer.parseInt(args[1].replaceAll("[^0-9]", "")); if (args[1].startsWith("-")) { amount = -amount; } } double worth = ess.getWorth().getPrice(is); boolean stack = args.length > 1 && args[1].endsWith("s"); if (Double.isNaN(worth)) { throw new Exception(_("itemCannotBeSold")); } int max = 0; for (ItemStack s : user.getInventory().getContents()) { if (s == null) { continue; } if (s.getTypeId() != is.getTypeId()) { continue; } if (s.getDurability() != is.getDurability()) { continue; } if (!s.getEnchantments().equals(is.getEnchantments())) { continue; } max += s.getAmount(); } if (stack) { amount *= is.getType().getMaxStackSize(); } if (amount < 1) { amount += max; } if (amount > max || amount < 1) { if (!isBulkSell) { user.sendMessage(_("itemNotEnough1")); user.sendMessage(_("itemNotEnough2")); throw new Exception(_("itemNotEnough3")); } else { return; } } //TODO: Prices for Enchantments final ItemStack ris = is.clone(); ris.setAmount(amount); InventoryWorkaround.removeItem(user.getInventory(), true, true, ris); user.updateInventory(); Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), user.getLocation(), ess); user.giveMoney(worth * amount); user.sendMessage(_("itemSold", Util.displayCurrency(worth * amount, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(worth, ess))); logger.log(Level.INFO, _("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), Util.displayCurrency(worth * amount, ess), amount, Util.displayCurrency(worth, ess))); }