#All-in-one item unifier, tag-transferring, old item hiding function

49 messages · Page 1 of 1 (latest)

gloomy robin
#

This code is supposed to take items as input and output, and:

  1. Add the tags from every input to every output
  2. Remove all tags from every input
  3. Add a 'hidden' tag for use in client
  4. Take all recipes that have an input item as input (for the recipe) and replace it for all outputs

Currently, it can do two of these things [2, 4] flawlessly in every use case and all of them only if every use is single item only.
I've divided the code in 4 cases:
a. There is only one input and output
b. There is many inputs and one output
c. There is one input and many outputs
d. Both are many items

I don't know why exactly the copying works as it does, but upon testing I've ruled in a few kinks of it:

  • When copying tags, it copies asynchronically (I know this because I've got another function in another file that specifically adds a blacklist if the item is hidden)
  • When deleting tags, all tags are deleted, included output. I've yet to figure out why
  • When adding tags to two different output items from one input item, none of them get the tags

I've racked my brain trying to solve this, I'd really appreciate if someone helped me figure this one out.

real orbitBOT
#

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

gloomy robin
radiant dirgeBOT
#

Paste version of message.txt from @gloomy robin

main epoch
#

Okay let me break down some design problems here.
First thing is, you have a function that defines event listeners.
This is considered bad practice in KubeJS without an explicit reason, such as the event listener only exists with certain addons being enabled.

#

The second thing is that you have four separate event listeners, all of which look nearly identical, the only difference is whether or not something is an array or not

#

This logic is making your script overly complicated and it makes it even harder to read and debug

#

In your case, you would be able to simplify everything if both inputs and outputs were arrays

#

That would result in you only needing to keep one of those conditions, and you can get rid of the three other duplicate code sections

#

The easiest way, without changing much, would be to check if your input is an array, if it isn’t then make it an array of that one input

gloomy robin
#

oh that's genius

main epoch
#

Now that your code is cut down to 1/4 the logic.
Instead of defining the event listeners in the function, have the function store data in the global binding.
Then register one listener for tags and one for recipes.
When those listeners fire, you read from those lists and perform the operations you want to perform in that event.

#
// I am simplifying this for you just to make the code easier to read.
function getGlobalData(name) {
    if (global[name] === null || global[name] === undefined) return [];
    return global[name];
}
function addGlobalData(name, data) {
    if(!global[name]) global[name] = [];
    global[name].append(data);
}
gloomy robin
#

This is for adding it to the global data, yes?

main epoch
#

Correct

gloomy robin
#

Then I would be able to access the data in each of the two listeners separately

main epoch
#

global is a Java map that is persistent across all script types.
It does not persist between runtime instances, but can be used to pass data between event handlers and script files

gloomy robin
#

I run this every time I boot up the world once, so that's no issue. Better, in my opinion

main epoch
#

Exactly.
It’s very handy for doing JS API-like code like you are trying to make

#

It allows you to organize your code such that you can perform the same operation in different files, while reducing the amount of duplicate code and operations run.

#

If that makes any sense

gloomy robin
#

Thank you very much! It does. I'll try it and close the issue if it works.

gloomy robin
#

Sorry, but I've came to heads with an issue.
Apparently I'm accessing the globals wrong? Or I'm not using append as I should.
Probably it has something to do with the simplification of code, but alas I do not know:

radiant dirgeBOT
#

Paste version of message.txt from @gloomy robin

gloomy robin
gloomy robin
#

Apparently it was because js uses .push() instead of .append()

gloomy robin
#

Still can't get it to work, but at least there are less errors

radiant dirgeBOT
#

Paste version of message.txt from @gloomy robin

main epoch
#

It may be push and not append

#

Nvm you figured that out

#

The code looks much better at this point

main epoch
#
// Saves items to global to unify
function addToUnification(inputItems, outputItems) {
      // Checks if input/output is array. If not, converts.
      if(!Array.isArray(inputItems)) inputItems = [inputItems]
      if(!Array.isArray(outputItems)) outputItems = [outputItems]
    
      // Adds to global
      addGlobalData('itemsToUnify', [inputItems, outputItems])
}
#

^ this will actually save the output and input items as an array to the global itemsToUnify array.
Allowing you to use the first index as the inputs and second index as the output

gloomy robin
gloomy robin
#

The code is the same, I've just tried to fix this one recurring error when trying to cycle through the array when modifying recipes:

radiant dirgeBOT
#

Paste version of message.txt from @gloomy robin

gloomy robin
#

It boils down to this specific segment:

  // Moves all recipes from many inputs to many outputs (array)->(array)
  function moveRecipesFromTo(inputIDs, outputIDs) {
    inputIDs.forEach(iItem => event.replaceInput({ input: Item.of(iItem) }, Item.of(iItem), itemsToOutPut))
  }

  getGlobalData('itemsToUnify').forEach(iItem => {
    let outputItems = getGlobalData(iItem)
    moveRecipesFromTo(iItem, outputItems)
  })
})```
And the log is saying:
```[21:20:00] [ERROR] ! unifier.js#99: Error in 'ServerEvents.recipes': TypeError: Cannot find function forEach in object pamhc2crops:amaranthitem.```
Which I don't get, honestly. I've tried converting it into a list, copying the array, cycling each item individually but for some reason it won't stop buggering me about this specific call of the forEach() function
gloomy robin
gloomy robin
#

I might be dumb. I commented on top of the function: This function takes TWO arrays and then proceeded to pre-iterate one of them and send it as a parameter bigbrain

gloomy robin
#

Closing this now, this whole code is a whole buggy nonfunctional thing and I refuse to debug it further

real orbitBOT
#

Ticket closed!

gloomy robin
#

Reopening this to paste the full, working code

#

All-in-one item unifier, tag-transferring, old item hiding function

#

This is the code. To add items, call addToUnification with either arrays of item ids or straight up item ids in the [Calls to Function] section of the code. The tag used for hiding is changeable, and tied to how I hide items in my code (basically if it has that tag I make the client hide the item)
It works (for better or worse).

radiant dirgeBOT
#

Paste version of fckasscode_working.txt from @gloomy robin

gloomy robin
#

Special thanks to Pie for putting up with me on a weekend, this couldn't be done without you homie