@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 repairItems(final ItemStack[] items, final IUser user, final List<String> repaired) { for (ItemStack item : items) { if (item == null) { continue; } final String itemName = item.getType().toString().toLowerCase(Locale.ENGLISH); final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), "repair-item", ess); try { charge.isAffordableFor(user); } catch (ChargeException ex) { user.sendMessage(ex.getMessage()); continue; } if (!item.getEnchantments().isEmpty() && !Permissions.REPAIR_ENCHANTED.isAuthorized(user)) { continue; } try { repairItem(item); } catch (Exception e) { continue; } try { charge.charge(user); } catch (ChargeException ex) { user.sendMessage(ex.getMessage()); } repaired.add(itemName.replace('_', ' ')); } }
@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; }
private Trade rechargeSign(final ISign sign, final IEssentials ess, final IUser player) throws SignException, ChargeException { final Trade trade = getTrade(sign, 2, false, false, ess); ItemStack itemInHand = player.getPlayer().getItemInHand(); if (trade.getItemStack() != null && itemInHand != null && trade.getItemStack().getTypeId() == itemInHand.getTypeId() && trade.getItemStack().getDurability() == itemInHand.getDurability() && trade.getItemStack().getEnchantments().equals(itemInHand.getEnchantments())) { int amount = itemInHand.getAmount(); amount -= amount % trade.getItemStack().getAmount(); if (amount > 0) { final ItemStack stack = itemInHand.clone(); stack.setAmount(amount); final Trade store = new Trade(stack, ess); addAmount(sign, 2, store, ess); store.charge(player); return store; } } return null; }
@Override public void run(final IUser user, final String commandLabel, final String[] args) throws Exception { if (args.length < 1) { throw new NotEnoughArgumentsException(); } if (args[0].equalsIgnoreCase("hand")) { final ItemStack item = user.getPlayer().getItemInHand(); if (item == null) { throw new Exception(_("repairInvalidType")); } if (!item.getEnchantments().isEmpty() && !Permissions.REPAIR_ENCHANTED.isAuthorized(user)) { throw new Exception(_("repairEnchanted")); } final String itemName = item.getType().toString().toLowerCase(Locale.ENGLISH); final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), ess); charge.isAffordableFor(user); repairItem(item); charge.charge(user); user.sendMessage(_("repair", itemName.replace('_', ' '))); } else if (args[0].equalsIgnoreCase("all")) { final Trade charge = new Trade("repair-all", ess); charge.isAffordableFor(user); final List<String> repaired = new ArrayList<String>(); repairItems(user.getPlayer().getInventory().getContents(), user, repaired); if (Permissions.REPAIR_ARMOR.isAuthorized(user)) { repairItems(user.getPlayer().getInventory().getArmorContents(), user, repaired); } if (repaired.isEmpty()) { throw new Exception(_("repairNone")); } else { user.sendMessage(_("repair", Util.joinList(repaired))); } charge.charge(user); } else { throw new NotEnoughArgumentsException(); } }