Minecraft 1.14 algorithm for iterating over an NBT list via recursive function looping.
Last active
December 23, 2019 05:46
-
-
Save Arcensoth/8a8a63df6985f289430a98fa4b5cbe99 to your computer and use it in GitHub Desktop.
Iterating over NBT lists using functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#> mypack:iter_mylist | |
# create left-hand and right-hand lists to shift elements between | |
data modify entity @s Item.tag.mylist_iter.left set from entity @s Item.tag.mylist | |
data modify entity @s Item.tag.mylist_iter.right set value [] | |
# start the recursive loop | |
function mypack:iter_mylist/try_loop | |
# copy the final result back into the original list | |
data modify entity @s Item.tag.mylist set from entity @s Item.tag.mylist_iter.right |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#> mypack:iter_mylist/loop | |
# pop the last element from the left-hand list as the current element | |
data modify entity @s Item.tag.mylist_iter.current set from entity @s Item.tag.mylist_iter.left[-1] | |
data remove entity @s Item.tag.mylist_iter.left[-1] | |
# do what we want with the current element | |
function mypack:iter_mylist/process | |
# copy the current element over to the right-hand list | |
data modify entity @s Item.tag.mylist_iter.right append from entity @s Item.tag.mylist_iter.current | |
# check if we have more elements to process | |
function mypack:iter_mylist/try_loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#> mypack:iter_mylist/process | |
# this is where you do stuff with the current element | |
# accessed with: Item.tag.mylist_iter.current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#> mypack:iter_mylist/try_loop | |
# check if there is another element to process | |
execute store result score $temp temp run data get entity @s Item.tag.mylist_iter.left | |
execute if score $temp temp matches 1.. run function mypack:iter_mylist/loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment