Exemplo n.º 1
0
  @Override
  public boolean finalizeTransaction(TransactionInfo info) {
    String stock = info.getStock().name().toLowerCase();
    Player player = info.getPlayerParticipant();
    int amount = info.getAmount();

    boolean result = false;
    int endAmount = amount * this.amount;
    ItemStack clone = is.clone();
    clone.setAmount(endAmount);

    if (stock == "sell") {
      ItemStack[] contents = player.getInventory().getContents();
      for (int i = 0; i < contents.length && endAmount > 0; ++i) {
        ItemStack nItem = contents[i];
        if (nItem != null && nItem.isSimilar(clone)) {
          int diff = endAmount - nItem.getAmount();
          if (diff < 0) nItem.setAmount(-diff);
          else player.getInventory().setItem(i, null);
          endAmount = diff;
        }
      }
      result = true;
    } else if (stock == "buy") {
      player.getInventory().addItem(clone);
      result = true;
    }
    return result;
  }
Exemplo n.º 2
0
  @Override
  public boolean allowTransaction(TransactionInfo info) {
    String stock = info.getStock().name().toLowerCase();
    Player player = info.getPlayerParticipant();
    int amount = info.getAmount();

    boolean result = false;
    int endAmount = amount * this.amount;
    if (stock == "sell") {
      result = player.getInventory().containsAtLeast(is, endAmount);
    } else if (stock == "buy") {
      ItemStack[] contents = player.getInventory().getContents();
      for (int i = 0; i < contents.length && endAmount > 0; ++i) {
        if (contents[i] == null) endAmount -= 64;
        else if (contents[i].isSimilar(is)) endAmount -= contents[i].getAmount();
      }
    }
    return result;
  }