Пример #1
0
  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();
      final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), ess);
      try {
        charge.isAffordableFor(user);
      } catch (ChargeException ex) {
        user.sendMessage(ex.getMessage());
        continue;
      }

      try {
        repairItem(item);
      } catch (Exception e) {
        continue;
      }
      try {
        charge.charge(user);
      } catch (ChargeException ex) {
        user.sendMessage(ex.getMessage());
      }
      repaired.add(itemName.replace('_', ' '));
    }
  }
  private boolean condenseStack(
      final User user, final ItemStack stack, final boolean validateReverse)
      throws ChargeException, MaxMoneyException {
    final SimpleRecipe condenseType = getCondenseType(stack);
    if (condenseType != null) {
      final ItemStack input = condenseType.getInput();
      final ItemStack result = condenseType.getResult();

      if (validateReverse) {
        boolean pass = false;
        for (Recipe revRecipe : ess.getServer().getRecipesFor(input)) {
          if (getStackOnRecipeMatch(revRecipe, result) != null) {
            pass = true;
            break;
          }
        }
        if (!pass) {
          return false;
        }
      }

      int amount = 0;

      for (final ItemStack contents : user.getBase().getInventory().getContents()) {
        if (contents != null && contents.isSimilar(stack)) {
          amount += contents.getAmount();
        }
      }

      int output = ((amount / input.getAmount()) * result.getAmount());
      amount -= amount % input.getAmount();

      if (amount > 0) {
        input.setAmount(amount);
        result.setAmount(output);
        final Trade remove = new Trade(input, ess);
        final Trade add = new Trade(result, ess);
        remove.charge(user);
        add.pay(user, OverflowType.DROP);
        return true;
      }
    }
    return false;
  }
Пример #3
0
  @Override
  public void run(
      final Server server, final User 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.getItemInHand();
      if (item == null) {
        throw new Exception(Util.i18n("repairInvalidType"));
      }
      final String itemName = item.getType().toString().toLowerCase();
      final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), ess);

      charge.isAffordableFor(user);

      repairItem(item);

      charge.charge(user);

      user.sendMessage(Util.format("repair", itemName.replace('_', ' ')));
    } else if (args[0].equalsIgnoreCase("all")) {
      final List<String> repaired = new ArrayList<String>();
      repairItems(user.getInventory().getContents(), user, repaired);

      repairItems(user.getInventory().getArmorContents(), user, repaired);

      if (repaired.isEmpty()) {
        throw new Exception(Util.format("repairNone"));
      } else {
        user.sendMessage(Util.format("repair", Util.joinList(repaired)));
      }

    } else {
      throw new NotEnoughArgumentsException();
    }
  }