#Recipe modifier to keep NBT

2 messages · Page 1 of 1 (latest)

tight comet
#

Is there a way to make a recipe modifier that transfers the NBT from the first ingredient in a recipe to the output item? I've managed to make one that finds the correct item to copy the NBT from, but I don't know how to transfer the NBT.

/**
 * @param {MetaMachine} machine
 * @param {Internal.GTRecipe} recipe
 * @param {Internal.OCParams} params
 * @param {Internal.OCResult} result
 *
 * @returns {Internal.GTRecipe}
 */
global.keepNBTRecipeModifier = (machine, recipe, params, result) => {
  if (!recipe.data.getBoolean("keep_nbt")) return recipe;
  const outputs = recipe.outputs.get($ItemRecipeCapability.CAP);
  const inputs = recipe.inputs.get($ItemRecipeCapability.CAP);
  const reagentItem = inputs[0].content;
  const prevOutput = outputs[0];

  /** @type {Internal.IItemHandler} */
  const itemHandler = machine.holder
    .getCapability(
      $ForgeCapabilities.ITEM_HANDLER,
      machine.getOutputFacingItems()
    )
    .resolve()
    .get();

  for (let i = 0; i < 9; i++) {
    let inputItem = itemHandler.getStackInSlot(i);
    if (reagentItem.test(inputItem)) {
      // Transfer inputItem.nbt to output
      break;
    }
  }

  return recipe;
};

I'm using gtceu-1.20.1-1.4.6, if that makes any difference.

tight comet
#

Figured it out: ```js
global.keepNBTRecipeModifier = (machine, recipe, params, result) => {
if (!recipe.data.getBoolean("keep_nbt")) return recipe;
const outputs = recipe.outputs.get($ItemRecipeCapability.CAP);
const inputs = recipe.inputs.get($ItemRecipeCapability.CAP);
/** @type {Internal.SizedIngredient} */
const reagentItem = inputs[0].content;
const { content, chance, maxChance, tierChanceBoost, slotName, uiName } =
outputs[0];

/** @type {Internal.IItemHandler} */
const itemHandler = machine.holder
.getCapability(
$ForgeCapabilities.ITEM_HANDLER,
machine.getOutputFacingItems()
)
.resolve()
.get();

for (let i = 0; i < 9; i++) {
let inputItem = itemHandler.getStackInSlot(i);
if (reagentItem.test(inputItem)) {
recipe.outputs.put(
$ItemRecipeCapability.CAP,
$List.of(
new $Content(
$StrictNBTIngredient["of(net.minecraft.world.item.ItemStack)"](
Item.of(content.inner, content.amount, inputItem.nbt)
),
chance,
maxChance,
tierChanceBoost,
slotName,
uiName
)
)
);
break;
}
}

return recipe;
};