Skip to content

Commit

Permalink
✅ Add tests for the view module
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiome committed Mar 11, 2024
1 parent 2681e68 commit af1f59b
Show file tree
Hide file tree
Showing 27 changed files with 106 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# CODE ------------------------------------------------------------------------

execute unless predicate bs.view:is_looking_at_entity run return 0
execute if entity @e[tag=bs.view.is_lookable,tag=!bs.view.bi,sort=arbitrary,limit=1] run function bs.view:looked_entity/setup
execute if score #view.looked_entity bs.data matches 1.. if predicate bs.view:looked_entity/b0 run tag @e[tag=bs.view.bi,tag=!bs.view.b0] remove bs.view.bi
execute if score #view.looked_entity bs.data matches 2.. if predicate bs.view:looked_entity/b1 run tag @e[tag=bs.view.bi,tag=!bs.view.b1] remove bs.view.bi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ data modify storage bs:in raycast set from storage bs:data view.raycast

# run the command at the block that was found or return early
execute if score #raycast.distance bs.data matches 2147483647 run return 0
data modify entity B5-0-0-0-1 Pos set from storage bs:out raycast.targeted_block
$execute at B5-0-0-0-1 run $(run)
execute in minecraft:overworld run tp B5-0-0-0-1 -30000000 0 1600
$data modify storage bs:ctx _ set value '$(run)'
data modify storage bs:ctx x set from storage bs:out raycast.targeted_block[0]
data modify storage bs:ctx y set from storage bs:out raycast.targeted_block[1]
data modify storage bs:ctx z set from storage bs:out raycast.targeted_block[2]
execute at @s run function bs.view:at/run with storage bs:ctx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ data modify storage bs:in raycast set from storage bs:data view.raycast

# run the command at the hit point that was found or return early
execute if score #raycast.distance bs.data matches 2147483647 run return 0
data modify entity B5-0-0-0-1 Pos set from storage bs:out raycast.hit_point
$execute at B5-0-0-0-1 run $(run)
execute in minecraft:overworld run tp B5-0-0-0-1 -30000000 0 1600
$data modify storage bs:ctx _ set value '$(run)'
data modify storage bs:ctx x set from storage bs:out raycast.hit_point[0]
data modify storage bs:ctx y set from storage bs:out raycast.hit_point[1]
data modify storage bs:ctx z set from storage bs:out raycast.hit_point[2]
execute at @s run function bs.view:at/run with storage bs:ctx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# CODE ------------------------------------------------------------------------

execute unless predicate bs.view:is_looking_at_entity run return 0
execute if entity @e[tag=bs.view.is_lookable,tag=!bs.view.bi,sort=arbitrary,limit=1] run function bs.view:looked_entity/setup
execute if score #view.looked_entity bs.data matches 1.. if predicate bs.view:looked_entity/b0 run tag @e[tag=bs.view.bi,tag=!bs.view.b0] remove bs.view.bi
execute if score #view.looked_entity bs.data matches 2.. if predicate bs.view:looked_entity/b1 run tag @e[tag=bs.view.bi,tag=!bs.view.b1] remove bs.view.bi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ data modify storage bs:in raycast set from storage bs:data view.raycast

# run the command at the block before the block that was found or return early
execute if score #raycast.distance bs.data matches 2147483647 run return 0
$data modify storage bs:ctx _ set value '$(run)'
execute store result score #view.x1 bs.data run data get storage bs:out raycast.targeted_block[0]
execute store result score #view.y1 bs.data run data get storage bs:out raycast.targeted_block[1]
execute store result score #view.z1 bs.data run data get storage bs:out raycast.targeted_block[2]
execute store result score #view.x2 bs.data run data get storage bs:out raycast.hit_normal[0]
execute store result score #view.y2 bs.data run data get storage bs:out raycast.hit_normal[1]
execute store result score #view.z2 bs.data run data get storage bs:out raycast.hit_normal[2]
execute store result entity B5-0-0-0-1 Pos[0] double 1 run scoreboard players operation #view.x1 bs.data += #view.x2 bs.data
execute store result entity B5-0-0-0-1 Pos[1] double 1 run scoreboard players operation #view.y1 bs.data += #view.y2 bs.data
execute store result entity B5-0-0-0-1 Pos[2] double 1 run scoreboard players operation #view.z1 bs.data += #view.z2 bs.data
$execute at B5-0-0-0-1 run $(run)
execute in minecraft:overworld run tp B5-0-0-0-1 -30000000 0 1600
execute store result storage bs:ctx x int 1 run scoreboard players operation #view.x1 bs.data += #view.x2 bs.data
execute store result storage bs:ctx y int 1 run scoreboard players operation #view.y1 bs.data += #view.y2 bs.data
execute store result storage bs:ctx z int 1 run scoreboard players operation #view.z1 bs.data += #view.z2 bs.data
execute at @s run function bs.view:at/run with storage bs:ctx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$execute positioned $(x) $(y) $(z) run $(_)
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ data modify storage bs:data view.raycast set from storage bs:in raycast
data modify storage bs:in raycast merge value {block_collision:true,entity_collision:false}
data modify storage bs:in raycast.ignored_blocks set from storage bs:in view.can_see_ata.ignored_blocks
execute store result storage bs:in raycast.max_distance double 0.001 run function #bs.position:get_distance_ata {scale:1000}
execute anchored eyes positioned ^ ^ ^ run function #bs.raycast:run
execute facing entity @s eyes run function #bs.raycast:run
data modify storage bs:in raycast set from storage bs:data view.raycast
execute if score #raycast.distance bs.data matches 2147483647 run return 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"type_specific": {
"type": "player",
"looking_at": {
"nbt": "{Tags:[\"bs.view.is_lookable\"]}"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"values": [
"bs.view:as_aimed_entity"
"bs.view:as/aimed_entity"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"values": [
"bs.view:as_looked_entity"
"bs.view:as/looked_entity"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"values": [
"bs.view:at_aimed_block"
"bs.view:at/aimed_block"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"values": [
"bs.view:at_aimed_entity"
"bs.view:at/aimed_entity"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"values": [
"bs.view:at_aimed_point"
"bs.view:at/aimed_point"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"values": [
"bs.view:at_looked_entity"
"bs.view:at/looked_entity"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"values": [
"bs.view:at_placed_block"
"bs.view:at/placed_block"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Callback is run as the targeted entity
# @batch bs.view
# @dummy

summon minecraft:armor_stand ~.5 ~ ~1.5 {Tags:["bs.raycast.is_solid"],NoGravity:1b}
function #bs.view:as_aimed_entity {run:"tag @s add found"}
assert entity @e[type=minecraft:armor_stand,tag=found] inside
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Callback is run as the looked at entity
# @batch bs.view
# @dummy

summon minecraft:armor_stand ~.5 ~ ~1.5 {Tags:["bs.view.is_lookable"],NoGravity:1b}
function #bs.view:as_looked_entity {run:"tag @s add found"}
assert entity @e[type=minecraft:armor_stand,tag=found] inside
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Callback is run at the targeted block
# @batch bs.view
# @dummy

setblock ~ ~1 ~1 minecraft:sponge
function #bs.view:at_aimed_block {run:"setblock ~ ~ ~ minecraft:bookshelf"}
assert block ~ ~1 ~1 minecraft:bookshelf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Callback is run at the targeted entity
# @batch bs.view
# @dummy

summon minecraft:armor_stand ~.5 ~ ~1.5 {Tags:["bs.raycast.is_solid"],NoGravity:1b}
function #bs.view:at_aimed_entity {run:"tag @e[distance=0] add found"}
assert entity @e[type=minecraft:armor_stand,tag=found] inside
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Callback is run at the hit point
# @batch bs.view
# @dummy

setblock ~ ~1 ~1 minecraft:bookshelf
function #bs.view:at_aimed_point {run:"summon minecraft:marker ~ ~ ~ {UUID:[181,0,0,0]}"}
execute store success score #success bs.data run data modify entity B5-0-0-0-0 Pos set from storage bs:out raycast.hit_point
assert score #success bs.data matches 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Callback is run at the looked at entity
# @batch bs.view
# @dummy

summon minecraft:armor_stand ~.5 ~ ~1.5 {Tags:["bs.view.is_lookable"],NoGravity:1b}
function #bs.view:at_looked_entity {run:"tag @e[distance=0] add found"}
assert entity @e[type=minecraft:armor_stand,tag=found] inside
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Callback is run at the placed block
# @batch bs.view
# @dummy

setblock ~ ~1 ~1 minecraft:sponge
function #bs.view:at_placed_block {run:"setblock ~ ~ ~ minecraft:bookshelf"}
assert block ~ ~1 ~ minecraft:bookshelf
7 changes: 7 additions & 0 deletions datapacks/Bookshelf/data/bs.view/tests/can_see_ata.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Check the whole distance between the executor and the position
# @batch bs.view
# @dummy

setblock ~ ~1 ~1 minecraft:bookshelf
execute at @s positioned ~ ~1 ~.5 if function #bs.view:can_see_ata run fail "Position must be obstructed when reaching a block"
execute at @s positioned ~ ~1 ~.49 unless function #bs.view:can_see_ata run fail "Position must be unobstructed before reaching a block"
7 changes: 7 additions & 0 deletions datapacks/Bookshelf/data/bs.view/tests/in_view_ata.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Check multiple vision angles
# @batch bs.view
# @dummy

data modify storage bs:in view.in_view_ata.angle set value 90
execute at @s anchored eyes positioned ^1 ^ ^1 if function #bs.view:in_view_ata run fail "Position must not be within the 90-degree viewing angle"
execute at @s anchored eyes positioned ^.99 ^ ^1 unless function #bs.view:in_view_ata run fail "Position must be within the 90-degree viewing angle"
4 changes: 4 additions & 0 deletions docs/modules/view.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Run a command as the entity that is aimed by the current entity.
**Execution `as <entities>`**: Entity whose eyes determine the vision origin.
**Macro Var `run` [string]**: Command to run as the targeted entity.
**Tag**: Tag for entities that can be aimed (default: `bs.raycast.is_solid`).
```

*Run a command as the entity that you are looking at:*
Expand All @@ -86,6 +88,8 @@ Run a command at the entity that is aimed by the current entity.
**Execution `as <entities>`**: Entity whose eyes determine the vision origin.
**Macro Var `run` [string]**: Command to run at the targeted entity.
**Tag**: Tag for entities that can be aimed (default: `bs.raycast.is_solid`).
```

*Run a command at the entity that you are looking at:*
Expand Down

0 comments on commit af1f59b

Please sign in to comment.