Skip to content

Instantly share code, notes, and snippets.

@Arcensoth
Last active Dec 23, 2019
Embed
What would you like to do?
Iterating over NBT lists using functions
#> 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
#> 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
#> mypack:iter_mylist/process
# this is where you do stuff with the current element
# accessed with: Item.tag.mylist_iter.current
#> 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