# TradeOfferHelper

`com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper`

Represents a merchant trade, including its inputs, output, and use state.  

The first input is required and may be price-adjusted; the second is optional and
remains at its original count. [getInput()](#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getInput-) includes the adjusted first
input and any non-empty second input, while the individual input accessors return
unadjusted stacks. Price adjustments affect only the first input.  

Trades read from an inventory screen can be selected with [select()](#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_select-) while
that screen remains open; trades read from an entity cannot be selected.  
```js
const inv = Inventories.Inventory.create(Hud.getOpenScreen());  
if (inv !== null) {
  if (inv instanceof Inventories.Villager) {
    for (const trade of inv.getTrades()) {
      // the first input is what it costs now, the second may be nothing
      const inputs = trade.getInput();  
      const parts = [];  
      for (let i = 0; i !== inputs.size(); i += 1) {
        parts.push(inputs.get(i).getName().getString());  
      }
      Chat.log(`#${trade.getIndex()} ${parts.join(" and ")} `
        + `for ${trade.getOutput().getName().getString()}`);  
    }
  }
}
```

## Constructors

### new TradeOfferHelper {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_constructor-MerchantOffer-int-VillagerInventory-}

```js
new TradeOfferHelper(MerchantOffer base, int index, VillagerInventory inv)
```

Wraps a merchant offer and stores the inventory context used by [select()](#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_select-).  
This does not select or look up the offer, or copy, validate, register, or execute a trade.

**Parameters:**
- `base`: raw merchant offer to wrap
- `index`: zero-based slot index used when selecting from the supplied inventory; not validated
- `inv`: inventory used by [select()](#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_select-), or `null` when there is no inventory selection context

## Methods

### getAdjustedPrice {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getAdjustedPrice-}

```js
getAdjustedPrice(): int
```

Returns the count of the first input after demand and special-price adjustments,  
clamped to the range from one to that item's maximum stack size. [getLeftInput()](#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getLeftInput-)
returns the original count.

**Returns:** the adjusted first-input count.

**Since:** 1.8.4

### getCurrentPriceAdjustment {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getCurrentPriceAdjustment-}

```js
getCurrentPriceAdjustment(): int
```

Returns the difference between the adjusted and original counts of the first
input. Positive values increase the count; negative values decrease it.

**Returns:** current price adjustment, negative is discount.

### getDemandBonus {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getDemandBonus-}

```js
getDemandBonus(): int
```

Returns this offer's demand value. Its contribution to the adjusted first-input
count is `max(0, floor(originalCount * demand * priceMultiplier))`; a negative
demand therefore contributes zero rather than reducing the base count. The total
price change also includes the special price difference and the final stack-size clamp.

**Returns:** the demand value used to calculate this offer's price.

**Since:** 1.8.4

### getExperience {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getExperience-}

```js
getExperience(): int
```

Returns the experience amount stored on the offer. Whether the offer is flagged
to reward experience is reported separately by [shouldRewardPlayerExperience()](#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_shouldRewardPlayerExperience-).

**Returns:** experience gained for trade

### getIndex {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getIndex-}

```js
getIndex(): int
```

The screen inventory assigns indices from zero, matching the position accepted by
[VillagerInventory.selectTrade(int)](../../../classes/inventory/VillagerInventory.md#com.jsmacrosce.jsmacros.client.api.classes.inventory.VillagerInventory_selectTrade-int-). Trades read from a merchant entity
instead have index zero; they have no screen to select on.

**Returns:** the zero-based index in the screen's offer list, or zero for an entity-read trade.

**Since:** 1.8.4

### getInput {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getInput-}

```js
getInput(): List<ItemStackHelper>
```

Returns a new list containing the adjusted first input and, when present, the
non-empty second input. The second input is not price-adjusted.

**Returns:** list of input items required

### getLeftInput {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getLeftInput-}

```js
getLeftInput(): ItemStackHelper
```

Returns the first input stack at its original, unadjusted count.

**Returns:** the first input item.

**Since:** 1.8.4

### getMaxUses {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getMaxUses-}

```js
getMaxUses(): int
```

Returns the use limit for this offer; [getUses()](#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getUses-) counts uses against it.

**Returns:** max uses before it locks

### getNBT {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getNBT-}

```js
getNBT(): NBTElementHelper<?>
```

Encodes the offer with Minecraft's merchant-offer codec and wraps the resulting
compound. Each call encodes the current offer state.

**Returns:** trade offer as nbt tag

### getOriginalFirstInput {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getOriginalFirstInput-}

```js
getOriginalFirstInput(): ItemStackHelper
```

Returns the first input stack at its original, unadjusted count.

**Returns:** the original first input stack.

**Since:** 1.8.4

### getOriginalPrice {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getOriginalPrice-}

```js
getOriginalPrice(): int
```

Returns the original count of the first input, before price adjustment.

**Returns:** the original first-input count.

**Since:** 1.8.4

### getOutput {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getOutput-}

```js
getOutput(): ItemStackHelper
```

Returns the trade's output stack, with its configured count.

**Returns:** output item that will be received

### getPriceMultiplier {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getPriceMultiplier-}

```js
getPriceMultiplier(): float
```

Returns the per-offer factor used to calculate the demand contribution to its first
input's price. The contribution is the greater of zero and the floored product of
the original count, demand, and this multiplier.

**Returns:** the demand price multiplier for this offer.

**Since:** 1.8.4

### getRightInput {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getRightInput-}

```js
getRightInput(): ItemStackHelper
```

Returns the optional second input at its original count, or an empty stack if
the trade has no second input.

**Returns:** the second input item.

**Since:** 1.8.4

### getSpecialPrice {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getSpecialPrice-}

```js
getSpecialPrice(): int
```

Returns the price difference attributed to special pricing, in items added to or
subtracted from the first input. Negative values reduce the price; positive values
increase it. The final price also includes the non-negative demand contribution and
is clamped to the item's stack-size range.

**Returns:** the special price difference in items.

**Since:** 1.8.4

### getUses {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_getUses-}

```js
getUses(): int
```

Returns the number of times this offer has been used.

**Returns:** current number of uses

### isAvailable {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_isAvailable-}

```js
isAvailable(): boolean
```

Returns whether the offer is available under its use limit.

**Returns:** `true` if the trade has uses left, `false` otherwise.

### select {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_select-}

```js
select(): TradeOfferHelper
```

Selects this trade in the screen it was read from, but only while that screen is
still open. Entity-read trades, or trades from a closed screen, are unchanged.

**Returns:** self for chaining.

### shouldRewardPlayerExperience {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_shouldRewardPlayerExperience-}

```js
shouldRewardPlayerExperience(): boolean
```

Returns the offer's flag for rewarding experience after a successful trade.

**Returns:** `true` if the offer is flagged to reward experience; `false` otherwise.

**Since:** 1.8.4

### toString {#com.jsmacrosce.jsmacros.client.api.helper.world.entity.TradeOfferHelper_toString-}

```js
toString(): String
```

Returns a diagnostic representation built from the offer's input and output helpers.  
This is not a full serialization of the offer, its pricing algorithm, or its complete state.

**Returns:** diagnostic representation of the trade inputs and output
