コード例 #1
0
  @Override
  public boolean isAvailable(final Slot slot) {
    if (entity.isAdminMode()) return true;

    final ItemStack[] inv = entity.getRawInventory();
    final ItemStack stack = inv[slot.getSlotIndex()];
    if (stack != null)
      return InventoryHelper.doesInventoryContain(
          inv,
          VendingTileEntity.INVENTORY_SLOT_START,
          VendingTileEntity.GENERAL_INVENTORY_SIZE - 1,
          stack,
          null);

    return true;
  }
コード例 #2
0
  protected ItemStack doTrade(final Slot slot, final EntityPlayer player) {

    final ItemStack[] entityInventory = entity.getRawInventory();
    final ItemStack[] playerInventory = player.inventory.mainInventory;

    // Get the result of the potential trade. If there is nothing,
    // or the player inventory cannot hold the stack then return null.
    final ItemStack result = slot.getStack();
    if (result == null
        || !InventoryHelper.canInventoryAccept(
            playerInventory, 0, playerInventory.length - 1, result, null)) return null;

    final boolean normalMode = !entity.isAdminMode();

    // Can the inventory provide the result item?
    if (normalMode
        && !InventoryHelper.doesInventoryContain(
            entityInventory,
            VendingTileEntity.INVENTORY_SLOT_START,
            VendingTileEntity.GENERAL_INVENTORY_SIZE - 1,
            result,
            null)) return null;

    // Get the input slots. We do some math on the slot index to
    // figure out the right input slots.
    final int index = slot.slotNumber / 3 + VendingTileEntity.CONFIG_SLOT_START;
    final ItemStack input1 = entity.getStackInSlot(index);
    final ItemStack input2 = entity.getStackInSlot(index + 6);

    // See if the vending inventory can accept the required items
    if (normalMode
        && !InventoryHelper.canInventoryAccept(
            entityInventory,
            VendingTileEntity.INVENTORY_SLOT_START,
            VendingTileEntity.GENERAL_INVENTORY_SIZE - 1,
            input1,
            input2)) return null;

    // See if the player can provide the payment
    if (!InventoryHelper.doesInventoryContain(
        playerInventory, 0, playerInventory.length - 1, input1, input2)) return null;

    // OK - things should work. Do the transaction.
    if (input1 != null) {
      if (!entity.isAdminMode()) {
        entity.addStackToOutput(input1.copy());
      }
      InventoryHelper.removeItemStackFromInventory(
          playerInventory, input1.copy(), 0, playerInventory.length - 1);
    }
    if (input2 != null) {
      if (!entity.isAdminMode()) {
        entity.addStackToOutput(input2.copy());
      }
      InventoryHelper.removeItemStackFromInventory(
          playerInventory, input2.copy(), 0, playerInventory.length - 1);
    }

    if (!entity.isAdminMode()) {
      entity.removeStackFromOutput(result.copy());
    }

    player.inventory.addItemStackToInventory(result.copy());

    // play a tink sound to signal the trade
    if (player.worldObj.isRemote) player.playSound("random.orb", 0.5F, 5F);
    player.onUpdate();

    return null;
  }