#Get each item from a variable array

10 messages · Page 1 of 1 (latest)

tawdry bay
#

Hello, I have another (probably very simple) problem that I can't figure out. I'm trying to add a tag to each Rechiseled item based on which "base" item it's crafted from. Here's the code I have so far:

let rechiseledType = [
  'acacia_planks',
  'amethyst_block',
  'andesite'
  // etc.
]

rechiseledType.forEach(rechiseledType => {
  let itemInstance = `rechiseled:${rechiseledType}`
  console.log(itemInstance)
})```

Where I have `console.log(itemInstance)` for testing right now, I am trying to get it to output every item whose ID begins with 'rechiseled:' followed by the `rechiseledType` from the array.  (The `rechiseledType` will also be the tag I add to the items for that type, which is why I have to do it this way.)

The problem I'm running into is that the `itemInstance`, as it is currently written, is only the beginning of the ID for a collection of items.  How do I have `console.log()` output every item whose ID begins with `itemInstance`, without having to write out each variation that can possibly follow that string?
tulip narwhalBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

marsh briar
#
Ingredient.of(new RegExp(`${itemInstance}.*`)).itemIds.forEach(id => console.log(id))
tawdry bay
marsh briar
#

RegExp takes a string and returns a regex, and string literals are strings

tawdry bay
# marsh briar `RegExp` takes a string and returns a regex, and string literals are strings

Okay, so this partially solved my problem... I'm able to add the tags to the items using that method. But later on in the script, when I generate create:cutting recipes for each item in the Chipped tag groups, the Rechiseled items I added the Chipped tags to aren't being included when those cutting recipes are generated. So the items that are included in the Chipped tags by default have the new recipes added for them, but not the items from Rechiseled. Any ideas on what I'm doing wrong here?

// List of item types from Rechiseled to be added to Chipped groups
let rechiseledType = [
  'acacia_planks',
  'amethyst_block',
  // etc...
]

// Add Chipped tag for all items in each 'rechiseledType'
rechiseledType.forEach(rechiseledType => {
  Ingredient.of(new RegExp(`rechiseled:${rechiseledType}.*`)).itemIds.forEach(itemInstance =>
    ServerEvents.tags('item', event => {
      event.add(`chipped:${rechiseledType}`, `${itemInstance}`)
    })
  )
})

// List of tags from Chipped to have 'create:cutting' recipes added
let chippedTag = [
  'acacia_planks',
  'amethyst_block',
  // etc...
]

// Add 'create:cutting' recipe for all items in each 'chippedTag'
chippedTag.forEach(chippedTag => {
  Ingredient.of(`#chipped:${chippedTag}`).itemIds.forEach(taggedItem =>
    ServerEvents.recipes(event => {
      event.custom({
        "type": "create:cutting",
        "ingredients": [{"tag": `chipped:${chippedTag}`}],
        "results": [{"item": `${taggedItem}`}],
        "processingTime": 200
      })
    })
  )
})
marsh briar
#

Move the forEach loops inside the recipe event, it may be running before the item tags are added

tawdry bay
#

I would have thought it would run from the top to the bottom, so that the forEach loops would be processed in order? I can give that a try when I get to my computer in the morning

tawdry bay
#

For anyone who sees this looking for the full solution:

ServerEvents.tags('item', event => {

  // List of item types from Rechiseled to be added to Chipped groups
  let rechiseledType = [
    'acacia_planks',
    'amethyst_block',
    // etc...
  ]

  // Add Chipped tag for all items in each 'rechiseledType'
  rechiseledType.forEach(rechiseledType => {
    Ingredient.of(new RegExp(`rechiseled:${rechiseledType}.*`)).itemIds.forEach(itemInstance =>
      event.add(`chipped:${rechiseledType}`, `${itemInstance}`)
    )
  })
})```