This is going to get very freaky, very weird. Oh, maybe not for people who already have a particular kink – people with lisps, Haskel programmers. But everyone not already an FP weenie will need to take a shot of espresso and/or whiskey.
So, the build described on my previous post does indeed process astral sorcery crystals. But building all of those chains of NBT variables is a pain. Wouldn’t it be nice if there was a function that would simply get the size and cut of a crystal, nested all the way down in those NBT tags? Well, you can build them.
There are a couple of things to know. First, all of the built-in operators can be turned into an operator variable using (obviously) the operator entry in the portable programmer. You can then use the apply operator to give that variable some parameters, and it works the same as if you had simply used the operator.
Second, FP languages have an interesting method of handling functions with multiple arguments. In principle, a function only ever has one argument – it maps one thing onto another thing. So how is something like, say, addition done? Say we want to add 100 and 200. Well, the + function takes one integer argument. So when you give it a parameter of 100, it returns a function that adds 100 to things. This function is then applied to the second argument, giving you the 300 you wanted.
In Integrated Dynamics you can totally do that, you can take a function taking three arguments and bind something to the first argument, giving you a function (an operator) that takes two arguments. And so on.
I suppose the third thing to note is that all the things I am interested in start at a tag named ‘tag’. That is, various things give you back various types of lists and NBTs. The outer layers are specific to entities, inventories, blocks. For instance, an entity reader gives you an NBT that talks about the location and orientation of the entity, the inventory reader gives you an NBT talking about which slot in the inventory an item is in, the grindstone just starts at the top level, because it’s not done as a cointainer. The stuff common to these containing the astral sorcery gear starts at ‘tag’, wherever that might be. From that point you walk down: tag, astralsorcery, crystalProperties, size/collectiveCapability/purity.
So what I really want is a pair of functions which, given an NBT with a ‘tag’ entry, walk down to the integer ‘size’ entry and return it.
Let’s see how deep the rabbit-hole goes.
Walking down the tree
First, create an NBT object with that ‘tag’ at the top level. For instance, if your entity reader has a crystal floating in front of it, grab the entity aspect of the reader, then use entityNBT and then NBT.tagNBT to grab the ‘Item’ entry out of that tag. You could build a function to do this, but for now I am not going to bother.
I label the item variable ‘crystal’, its NBT ‘crystalNBT’, and the Item entry of that ‘crystalItem’.
This gives us an NBT with the structure that we will be working with – it’s our test data.
Next I use the portable programmer ‘Operator’ function to get two operators: ‘reduce’ and ‘NBT NBT Value NBT’. Yeah, I know. I label these λreduce and λgetNBT.
I then create a list of string containing the values ‘tag’,’astralsorcery’, ‘crystalProperties’. Case-sensitive, remember. I label this tag2cp.
Now at this stage, we can get the inner NBT fairly easily without needing λreduce. Create a variable ‘reduce(λgetNBT, tag2cp, crystalItem)’. This will use the reduce function to walk down the list of strings applying the λgetNBT operator each time, starting with crystalItem. At the end of the list, it will have reached the target NBT.
You can achieve the same effect using the apply3 operator: ‘λreduce apply3(λgetNBT, tag2cp, crystalItem)’.
But I don’t want a variable that extracts the stuff out of crystalItem, I want a function that will extract out of any suitable NBT.
Making the treewalk into an operator
To do this, I ‘λreduce apply(λgetNBT)’ to get another variable which I name ‘λnbtWalk’. This is a function that takes two arguments – a list of string and an NBT. It will walk down any NBT with a list, returning the final NBT.
I then create a variable ‘λnbtWalk apply( tag2cp)’, which I label ‘λnbt2cp’. This is a function which, given an NBT, will return that final NBT containing the crystla properties (if it’s there, obviously).
When you create a variable ‘λnbt2cp apply(crystalItem)’ and drop it into a display, well blow me down if it don’t display that inner NBT containing the stuff we want. Let’s label this variable ‘cprops’ for now. We wont need it in the final result, but it’s handy for testing the next bit.
Getting the integer
Now, we can’t use the λnbtWalk function to get all the way down to size and cut, because these are integers and the λgetNBT function will return nothing.
So, we create an operator variable out of ‘NBT NBT Value Integer’ and label it ‘λgetInt’. You can apply an NBT and a string to this to get an integer from inside an NBT. The problem is that the arguments are around the wrong way. If you used ‘apply’, you’d have to bind the NBT first and you’d get a function that would get any integer out of some particular NBT if you give it a tag name. NOt what we want.
So, create a new operator using ‘flip(λgetInt)’. This gives us the get Int function with its arguments swapped. I name this ‘λgetInt_YX’, with the idea that function arguments are usually named ‘X’, ‘Y’ in that order.
Create a string ‘size’ labelled – ohh, I dunno – ‘size’. At this stage, you can create a variable ‘apply2(λgetInt_YX, size, cprops)’ and drop it in a display, and you should see an integer. And not just any integer, but the size of the crystal sitting in front of the entity reader.
Now create a new variable ‘λgetInt_YX apply(size)’. This gives you a function returning the size value from any NBT that it is passed. I label this ‘λgetSize’.
Putting it together
And to put it together, we use the pipe operator: ‘.’. Simply λnbt2cp . λgetSize’ to give a new operator, which I label ‘λnbtSize’. And this is the magic, this is the thing we are trying to build. It’s a function taking one argument – the top level astral sorcery NBT containing a ‘tag’ entry, and returning the size of the crystalProperties NBT buried three levels deep. Try it! Create a variable ‘λnbtSize apply crystalItem’. Drop it in a display, and you should see the size. Naturally, you will want to make ones for purity and collectiveCapability (which I always label ‘cut’) as well.
From here, you can, well, do stuff. Turn ‘entity->get(‘Item’)->λnbtSize’ into a single operator, if you want.
But the real thing is building predicates. With these functions, you can now build a predicate ‘crystal has size 900’ by applying 900 to the == operator and piping λnbtSize into it. This can be used in item exporters and importers as a filter. Stuff like that. I’m not going to rebuild my setup using this method, but the business of sorting out completed crystals from ones still needing to be cut could have been done that way. If it were the case that you could put multiple crystals in the one growth pool, and you want to pull out the ones that are ready, then that’s the way to go.
Anyway, that’s what it took to make this happen in my creative test world. Haven’t used it in survival, yet, but that’s how it’s done.