Skip to content

Commit

Permalink
Command Queue: Refactor & Document
Browse files Browse the repository at this point in the history
This refactors command queue to be easier to understand and removes some
unnecessary functions.

Everything in there is now also documented (Work towards #5) and names
are changed to follow #4.
  • Loading branch information
NeunEinser committed Mar 4, 2021
1 parent d8a364e commit 784d20c
Show file tree
Hide file tree
Showing 25 changed files with 179 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#> bingo:command_queue/claculate_next_position_and_set_command_block/check_z_limit
#
# This function checks z limits and moves to the next z or y position, depending
# on the result
#
# @within function bingo:command_queue/claculate_next_position_and_set_command_block/run

execute if score $command_queue/next_pos.mody bingo.tmp matches 0 if score $command_queue.z bingo.tmp matches 15 run function bingo:command_queue/claculate_next_position_and_set_command_block/next_positive_y
execute if score $command_queue/next_pos.mody bingo.tmp matches 1 if score $command_queue.z bingo.tmp matches 0 run function bingo:command_queue/claculate_next_position_and_set_command_block/next_positive_y
execute if score $command_queue/next_pos.mody bingo.tmp matches 0 unless score $command_queue.z bingo.tmp matches 15 run function bingo:command_queue/claculate_next_position_and_set_command_block/next_positive_z
execute if score $command_queue/next_pos.mody bingo.tmp matches 1 unless score $command_queue.z bingo.tmp matches 0 run function bingo:command_queue/claculate_next_position_and_set_command_block/next_negative_z
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#> bingo:command_queue/claculate_next_position_and_set_command_block/next_negative_x
#
# This function sets a command block facing west and moves to the negative x
#
# @within function bingo:command_queue/claculate_next_position_and_set_command_block/run

setblock ~ ~ ~ minecraft:chain_command_block[facing=west]{auto: true}
scoreboard players remove $command_queue.x bingo.tmp 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#> bingo:command_queue/claculate_next_position_and_set_command_block/next_negative_z
#
# This function sets a command block facing west and moves to the negative z
#
# @within function bingo:command_queue/claculate_next_position_and_set_command_block/check_z_limit

setblock ~ ~ ~ minecraft:chain_command_block[facing=north]{auto: true}
scoreboard players remove $command_queue.z bingo.tmp 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#> bingo:command_queue/claculate_next_position_and_set_command_block/next_positive_x
#
# This function sets a command block facing east and moves to the positive x
#
# @within function bingo:command_queue/claculate_next_position_and_set_command_block/run

setblock ~ ~ ~ minecraft:chain_command_block[facing=east]{auto: true}
scoreboard players add $command_queue.x bingo.tmp 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#> bingo:command_queue/claculate_next_position_and_set_command_block/next_positive_y
#
# This function sets a command block facing up and moves to the positive y
#
# @within function bingo:command_queue/claculate_next_position_and_set_command_block/check_z_limit

setblock ~ ~ ~ minecraft:chain_command_block[facing=up]{auto: true}
scoreboard players add $command_queue.y bingo.tmp 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#> bingo:command_queue/claculate_next_position_and_set_command_block/next_positive_z
#
# This function sets a command block facing south and moves to the positive z
#
# @within function bingo:command_queue/claculate_next_position_and_set_command_block/check_z_limit

setblock ~ ~ ~ minecraft:chain_command_block[facing=south]{auto: true}
scoreboard players add $command_queue.z bingo.tmp 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#> bingo:command_queue/claculate_next_position_and_set_command_block/run
#
# This function calculates the position for the next command block and stores it
# in $command_queue.x/y/z.
#
# The position of the marker aec is also changed in here.
#
# @within function bingo:command_queue/iter
# @writes
# score_holder $command_queue.x
# score_holder $command_queue.y
# score_holder $command_queue.z

#>
# Contains the result of z % 2
#
# @within bingo:command_queue/claculate_next_position_and_set_command_block/**
#declare score_holder $command_queue/next_pos.modz
#>
# Contains the result of y % 2
#
# @within bingo:command_queue/claculate_next_position_and_set_command_block/**
#declare score_holder $command_queue/next_pos.mody

scoreboard players operation $command_queue/next_pos.modz bingo.tmp = $command_queue.z bingo.tmp
scoreboard players operation $command_queue/next_pos.modz bingo.tmp %= 2 bingo.const
scoreboard players operation $command_queue/next_pos.mody bingo.tmp = $command_queue.y bingo.tmp
scoreboard players operation $command_queue/next_pos.mody bingo.tmp %= 2 bingo.const

# Check for hitting the limit on the x axis. If (z % 2) == (y % 2), we move towards positive x, otherwise towards negative x.
execute if score $command_queue/next_pos.modz bingo.tmp = $command_queue/next_pos.mody bingo.tmp if score $command_queue.x bingo.tmp matches 15 run function bingo:command_queue/claculate_next_position_and_set_command_block/check_z_limit
execute unless score $command_queue/next_pos.modz bingo.tmp = $command_queue/next_pos.mody bingo.tmp if score $command_queue.x bingo.tmp matches 0 run function bingo:command_queue/claculate_next_position_and_set_command_block/check_z_limit

execute if score $command_queue/next_pos.modz bingo.tmp = $command_queue/next_pos.mody bingo.tmp unless score $command_queue.x bingo.tmp matches 15 run function bingo:command_queue/claculate_next_position_and_set_command_block/next_positive_x
execute unless score $command_queue/next_pos.modz bingo.tmp = $command_queue/next_pos.mody bingo.tmp unless score $command_queue.x bingo.tmp matches 0 run function bingo:command_queue/claculate_next_position_and_set_command_block/next_negative_x

execute store result entity @s Pos[0] double 1 run scoreboard players get $command_queue.x bingo.tmp
execute store result entity @s Pos[1] double 1 run scoreboard players get $command_queue.y bingo.tmp
execute store result entity @s Pos[2] double 1 run scoreboard players get $command_queue.z bingo.tmp
23 changes: 23 additions & 0 deletions data/bingo/functions/command_queue/init_for_new_tick.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#> bingo:command_queue/init_for_new_tick
#
# This function initializes some values and entities needed for a run.
# It will only be executed once per tick.
#
# The area effect cloud spawned can be used as an indicator whether this
# function was already run in the current tick.
#
# The area effect cloud can be identified by the tag "bingp.command".
#
# @within function bingo:command_queue/run

#>
# This tag identifies the area effect cloud used to set the command blocks.
#
# @within function bingo:command_queue/**
#declare tag bingo.command

summon minecraft:area_effect_cloud 1 0 0 {Tags: ["bingo.command"]}
data merge block 0 0 0 {auto: true}
scoreboard players set $command_queue.x bingo.tmp 1
scoreboard players set $command_queue.y bingo.tmp 0
scoreboard players set $command_queue.z bingo.tmp 0
23 changes: 23 additions & 0 deletions data/bingo/functions/command_queue/iter.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#> bingo:command_queue/iter
#
# One iteration of the command queue. Places one command and is executed again
# automatically until there are no more commands in the given command queue.
#
# @within
# function bingo:command_queue/iter
# function bingo:command_queue/run

#>
# Score holder to store the size of the current command queue.
#
# @private
#declare score_holder $command_queue/iter.size

execute store result score $command_queue/iter.size bingo.tmp run data get storage temp:bingo.input/command_queue queue
execute if score $command_queue/iter.size bingo.tmp matches 2.. run function bingo:command_queue/claculate_next_position_and_set_command_block/run
execute if score $command_queue/iter.size bingo.tmp matches 1 run setblock ~ ~ ~ minecraft:chain_command_block[facing=down]{auto: true}

data modify block ~ ~ ~ Command set from storage temp:bingo.input/command_queue queue[0]
data remove storage temp:bingo.input/command_queue queue[0]

execute if score $command_queue/iter.size bingo.tmp matches 2.. at @s run function bingo:command_queue/iter
37 changes: 37 additions & 0 deletions data/bingo/functions/command_queue/run.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#> bingo:command_queue/run
#
# This function runs the command queue.
# The command queue can execute a list of provided commands in the next tick.
#
# @internal
# @input storage temp:bingo.input/command_queue queue

#>
# This storage is used as input by the command queue.
# It may only be read by related functions.
#
# It holds a list of commands to execute in the next tick. The field queue
# is used for that.
#
# @internal
#declare storage temp:bingo.input/command_queue

#>
# This score holder holds the current x coordinate
#
# @within function bingo:command_queue/**
#declare score_holder $command_queue.x
#>
# This score holder holds the current y coordinate
#
# @within function bingo:command_queue/**
#declare score_holder $command_queue.y
#>
# This score holder holds the current z coordinate
#
# @within function bingo:command_queue/**
#declare score_holder $command_queue.z

execute at @e[type=minecraft:area_effect_cloud, tag=bingo.command, limit=1] run data modify storage temp:bingo.input/command_queue queue prepend from block ~ ~ ~ Command
execute unless entity @e[type=minecraft:area_effect_cloud, tag=bingo.command, limit=1] in bingo:lobby run function bingo:command_queue/init_for_new_tick
execute as @e[type=minecraft:area_effect_cloud, tag=bingo.command, limit=1] at @s run function bingo:command_queue/iter
6 changes: 0 additions & 6 deletions data/bingo/functions/init/init.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ scoreboard objectives add bingo.tmp dummy
# @public
scoreboard objectives add bingo.const dummy

#>
# This objective is used by run_command_from_string.
#
# @internal
scoreboard objectives add bingo.commands dummy

#>
# This objective is used to spread certain longer text messages across multiple
# messages.
Expand Down
12 changes: 6 additions & 6 deletions data/bingo/functions/item_detection/helper/announce.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ execute if score $items bingo.tmp matches 25 run tellraw @a {"translate": "bingo
execute store result storage bingo:tmp player int 1 run scoreboard players get @s bingo.id
data modify storage bingo:card slots[{selected: true}].players append from storage bingo:tmp player

data modify storage bingo:commands queue append from storage bingo:card slots[{selected: true}].item.clearCommand[0]
data modify storage bingo:commands queue append value "function bingo:item_detection/helper/tag_players_for_item_clear"
data modify storage bingo:commands queue append from storage bingo:card slots[{selected: true}].item.clearCommand[1]
data modify storage bingo:commands queue append value "tag @a remove bingo.clear"
data modify storage bingo:commands queue append value "data modify storage bingo:card slots[{selected: true}].selected set value false"
data modify storage temp:bingo.input/command_queue queue append from storage bingo:card slots[{selected: true}].item.clearCommand[0]
data modify storage temp:bingo.input/command_queue queue append value "function bingo:item_detection/helper/tag_players_for_item_clear"
data modify storage temp:bingo.input/command_queue queue append from storage bingo:card slots[{selected: true}].item.clearCommand[1]
data modify storage temp:bingo.input/command_queue queue append value "tag @a remove bingo.clear"
data modify storage temp:bingo.input/command_queue queue append value "data modify storage bingo:card slots[{selected: true}].selected set value false"

function bingo:run_command_from_string/run
function bingo:command_queue/run

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions data/bingo/functions/run_command_from_string/iter.mcfunction

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions data/bingo/functions/run_command_from_string/run.mcfunction

This file was deleted.

This file was deleted.

0 comments on commit 784d20c

Please sign in to comment.