Skip to content

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() 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() 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 ​

new TradeOfferHelper(MerchantOffer base, int index, VillagerInventory inv)#
Wraps a merchant offer and stores the inventory context used by 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(), or null when there is no inventory selection context

Methods ​

getAdjustedPrice ​

getAdjustedPrice(): intSince1.8.4#
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() returns the original count.
Returns: the adjusted first-input count.

getCurrentPriceAdjustment ​

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 ​

getDemandBonus(): intSince1.8.4#
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.

getExperience ​

getExperience(): int#
Returns the experience amount stored on the offer. Whether the offer is flagged to reward experience is reported separately by shouldRewardPlayerExperience().
Returns: experience gained for trade

getIndex ​

getIndex(): intSince1.8.4#
The screen inventory assigns indices from zero, matching the position accepted by 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.

getInput ​

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 ​

getLeftInput(): ItemStackHelperSince1.8.4#
Returns the first input stack at its original, unadjusted count.
Returns: the first input item.

getMaxUses ​

getMaxUses(): int#
Returns the use limit for this offer; getUses() counts uses against it.
Returns: max uses before it locks

getNBT ​

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 ​

getOriginalFirstInput(): ItemStackHelperSince1.8.4#
Returns the first input stack at its original, unadjusted count.
Returns: the original first input stack.

getOriginalPrice ​

getOriginalPrice(): intSince1.8.4#
Returns the original count of the first input, before price adjustment.
Returns: the original first-input count.

getOutput ​

getOutput(): ItemStackHelper#
Returns the trade's output stack, with its configured count.
Returns: output item that will be received

getPriceMultiplier ​

getPriceMultiplier(): floatSince1.8.4#
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.

getRightInput ​

getRightInput(): ItemStackHelperSince1.8.4#
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.

getSpecialPrice ​

getSpecialPrice(): intSince1.8.4#
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.

getUses ​

getUses(): int#
Returns the number of times this offer has been used.
Returns: current number of uses

isAvailable ​

isAvailable(): boolean#
Returns whether the offer is available under its use limit.
Returns: true if the trade has uses left, false otherwise.

select ​

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 ​

shouldRewardPlayerExperience(): booleanSince1.8.4#
Returns the offer's flag for rewarding experience after a successful trade.
Returns: true if the offer is flagged to reward experience; false otherwise.

toString ​

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