Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Health module tests #166

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"criteria": {
"requirement": {
"trigger": "minecraft:effects_changed",
"conditions": {
"player": [
{
"condition": "minecraft:inverted",
"term": {
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"effects": {
"minecraft:instant_health": {
"amplifier": 28
}
}
}
}
}
]
}
}
},
"rewards": {
"function": "bs.health:apply/remove_modifier"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

scoreboard objectives add bs.data dummy [{"text":"BS ","color":"dark_gray"},{"text":"Data","color":"aqua"}]
scoreboard objectives add bs.ttl dummy [{"text":"BS ","color":"dark_gray"},{"text":"Time to Live","color":"aqua"}]
scoreboard objectives add bs.health.x dummy [{"text":"BS ","color":"dark_gray"},{"text":"Health [internal]","color":"aqua"}]
scoreboard objectives add bs.health.y dummy [{"text":"BS ","color":"dark_gray"},{"text":"Health [internal]","color":"aqua"}]

data modify storage bs:const health.point set value 100000
data modify storage bs:const health.ttl.units set value [ \
data modify storage bs:const health.units set value [ \
{name:"t",scale:1}, \
{name:"tick",scale:1}, \
{name:"s",scale:20}, \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@

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

execute as @a[predicate=bs.health:is_stale] run function bs.health:apply/update_health

execute as @e[scores={bs.ttl=0}] run function bs.health:time_to_live/time_out with entity @s
scoreboard players remove @e[scores={bs.ttl=1..}] bs.ttl 1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@

scoreboard objectives remove bs.data
scoreboard objectives remove bs.ttl
scoreboard objectives remove bs.health.x
scoreboard objectives remove bs.health.y

data remove storage bs:data health
21 changes: 16 additions & 5 deletions datapacks/Bookshelf/data/bs.health/functions/add/health.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@
# Authors: Aksiome
# Contributors:

# Version: 1.1
# Version: 1.2
# Created: 15/09/2023 (1.20.2)
# Last modification: 21/11/2023 (1.20.3)
# Last modification: 19/03/2024 (1.20.4)

# Documentation: https://bookshelf.docs.gunivers.net/en/latest/modules/health.html##add-remove
# Dependencies:
# Note:
# Note: Thanks to XanBelOr for giving the idea to use an advancement to subtick heal the player.

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

$execute store result score #health bs.data run data get storage bs:const health.point $(points)
scoreboard players operation @s bs.health.x += #health bs.data
execute if score @s bs.health.x matches 0 run scoreboard players reset @s bs.health.x

execute store success score #success bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d
execute if score #success bs.data matches 1 store result score #health.points bs.data run attribute @s minecraft:generic.max_health get 100000
execute if score #success bs.data matches 0 store result score #health.points bs.data run data get entity @s Health 100000
scoreboard players operation #health bs.data += #health.points bs.data

execute store result score #health.max bs.data run attribute @s minecraft:generic.max_health get 100000
execute store result score #health.mod bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d 100000
scoreboard players operation #health.mod bs.data -= #health.max bs.data
execute store result storage bs:ctx x double 0.00001 run scoreboard players operation #health.mod bs.data += #health bs.data
execute if score #health.mod bs.data matches 1.. run data modify storage bs:ctx x set value 0
execute if score #health.points bs.data > #health bs.data run return run function bs.health:apply/decrease_health with storage bs:ctx
execute if score #health.points bs.data < #health bs.data run return run function bs.health:apply/increase_health with storage bs:ctx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# Authors: Aksiome
# Contributors:

# Version: 1.1
# Version: 1.2
# Created: 15/09/2023 (1.20.2)
# Last modification: 21/11/2023 (1.20.3)
# Last modification: 19/03/2024 (1.20.4)

# Documentation: https://bookshelf.docs.gunivers.net/en/latest/modules/health.html##add-remove
# Dependencies:
Expand All @@ -15,5 +15,14 @@
# CODE ------------------------------------------------------------------------

$execute store result score #health bs.data run data get storage bs:const health.point $(points)
scoreboard players operation @s bs.health.y += #health bs.data
execute if score @s bs.health.y matches 0 run scoreboard players reset @s bs.health.y

execute store result score #health.max bs.data run attribute @s minecraft:generic.max_health get 100000
execute store result score #health.mod bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d 100000
scoreboard players operation #health.max bs.data -= #health.mod bs.data
execute store result storage bs:ctx y double 0.00001 run scoreboard players operation #health.max bs.data += #health bs.data

execute store success score #success bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d
execute if score #success bs.data matches 1 store result storage bs:ctx x double 0.00001 run scoreboard players operation #health.mod bs.data -= #health bs.data
execute if score #success bs.data matches 1 if score #health.mod bs.data matches ..0 run function bs.health:apply/increase_health with storage bs:ctx

function bs.health:apply/set_max_health with storage bs:ctx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
$attribute @s minecraft:generic.max_health base set $(x)
advancement grant @s only bs.health:restore_health
attribute @s minecraft:generic.max_health modifier remove 68f0b501-5dca-4abb-8b69-057945a9583d
$attribute @s minecraft:generic.max_health modifier add 68f0b501-5dca-4abb-8b69-057945a9583d "bs.health" $(x) add
effect give @s minecraft:health_boost 1 0 true
effect clear @s minecraft:health_boost

scoreboard players reset @s bs.health.x
effect clear @s minecraft:instant_health
attribute @s minecraft:generic.max_health modifier remove 68f0b501-5dca-4abb-8b69-057945a9583d
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
$attribute @s minecraft:generic.max_health base set $(x)
attribute @s minecraft:generic.max_health modifier remove 68f0b501-5dca-4abb-8b69-057945a9583d
$attribute @s minecraft:generic.max_health modifier add 68f0b501-5dca-4abb-8b69-057945a9583d "bs.health" $(x) add
effect give @s minecraft:instant_health 1 28 true

tag @s add bs.health.break
scoreboard players operation @s bs.health.y = #health.max bs.data
scoreboard players operation @s bs.health.y -= #health.points bs.data
execute if score @s bs.health.y matches 0 run scoreboard players reset @s bs.health.y
scoreboard players reset @s bs.health.x
advancement revoke @s only bs.health:restore_health
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
attribute @s minecraft:generic.max_health modifier remove 68f0b501-5dca-4abb-8b69-057945a9583d
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
$attribute @s minecraft:generic.max_health base set $(y)
effect give @s minecraft:health_boost 1 0 true
effect clear @s minecraft:health_boost

tag @s remove bs.health.break
scoreboard players reset @s bs.health.y

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
# Authors: Aksiome
# Contributors:

# Version: 1.0
# Version: 1.1
# Created: 23/10/2023 (1.20.2)
# Last modification: 21/11/2023 (1.20.3)
# Last modification: 19/03/2024 (1.20.4)

# Documentation: https://bookshelf.docs.gunivers.net/en/latest/modules/health.html#get
# Dependencies:
# Note:

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

execute if entity @s[tag=!bs.health.break] store result score #health bs.data run data get entity @s Health 100000
execute if entity @s[tag=bs.health.break] store result score #health bs.data run attribute @s minecraft:generic.max_health get 100000
scoreboard players operation #health bs.data += @s bs.health.y
execute store result storage bs:out health.get_health double 0.00001 run scoreboard players get #health bs.data
execute store success score #success bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d
execute if score #success bs.data matches 1 store result storage bs:out health.get_health double 0.00001 run attribute @s minecraft:generic.max_health get 100000
execute if score #success bs.data matches 0 run data modify storage bs:out health.get_health set from entity @s Health
$return run data get storage bs:out health.get_health $(scale)
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
# Authors: Aksiome
# Contributors:

# Version: 1.0
# Version: 1.1
# Created: 23/10/2023 (1.20.2)
# Last modification: 21/11/2023 (1.20.3)
# Last modification: 19/03/2024 (1.20.4)

# Documentation: https://bookshelf.docs.gunivers.net/en/latest/modules/health.html#get
# Dependencies:
# Note:

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

execute store result score #health bs.data run attribute @s minecraft:generic.max_health get 100000
scoreboard players operation #health bs.data += @s bs.health.x
execute store result storage bs:out health.get_max_health double 0.00001 run scoreboard players get #health bs.data
execute store result score #health.max bs.data run attribute @s minecraft:generic.max_health get 100000
execute store result score #health.mod bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d 100000
execute store result storage bs:out health.get_max_health double 0.00001 run scoreboard players operation #health.max bs.data -= #health.mod bs.data
$return run data get storage bs:out health.get_max_health $(scale)
22 changes: 14 additions & 8 deletions datapacks/Bookshelf/data/bs.health/functions/set/health.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
# Authors: Aksiome
# Contributors:

# Version: 1.1
# Version: 1.2
# Created: 15/09/2023 (1.20.2)
# Last modification: 21/11/2023 (1.20.3)
# Last modification: 19/03/2024 (1.20.4)

# Documentation: https://bookshelf.docs.gunivers.net/en/latest/modules/health.html#set
# Dependencies:
# Note:
# Note: Thanks to XanBelOr for giving the idea to use an advancement to subtick heal the player.

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

$execute store result score @s bs.health.x run data get storage bs:const health.point $(points)
execute if entity @s[tag=!bs.health.break] store result score #health bs.data run data get entity @s Health 100000
execute if entity @s[tag=bs.health.break] store result score #health bs.data run attribute @s minecraft:generic.max_health get 100000
scoreboard players operation @s bs.health.x -= #health bs.data
execute if score @s bs.health.x matches 0 run scoreboard players reset @s bs.health.x
$execute store result score #health bs.data run data get storage bs:const health.point $(points)

execute store result score #health.max bs.data run attribute @s minecraft:generic.max_health get 100000
execute store result score #health.mod bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d 100000
scoreboard players operation #health.max bs.data -= #health.mod bs.data

execute store result score #health.points bs.data run data get entity @s Health 100000
execute store result storage bs:ctx x double -0.00001 run scoreboard players operation #health.max bs.data -= #health bs.data
execute if score #health.max bs.data matches ..-1 run data modify storage bs:ctx x set value 0
execute if score #health.points bs.data > #health bs.data run return run function bs.health:apply/decrease_health with storage bs:ctx
execute if score #health.points bs.data < #health bs.data run return run function bs.health:apply/increase_health with storage bs:ctx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
# Authors: Aksiome
# Contributors:

# Version: 1.1
# Version: 1.2
# Created: 15/09/2023 (1.20.2)
# Last modification: 21/11/2023 (1.20.3)
# Last modification: 19/03/2024 (1.20.4)

# Documentation: https://bookshelf.docs.gunivers.net/en/latest/modules/health.html#set
# Dependencies:
# Note:

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

$execute store result score @s bs.health.y run data get storage bs:const health.point $(points)
execute store result score #health bs.data run attribute @s minecraft:generic.max_health get 100000
scoreboard players operation @s bs.health.y -= #health bs.data
execute if score @s bs.health.y matches 0 run scoreboard players reset @s bs.health.y
$execute store result storage bs:ctx y double 0.00001 store result score #health bs.data run data get storage bs:const health.point $(points)

execute store success score #success bs.data run attribute @s minecraft:generic.max_health modifier value get 68f0b501-5dca-4abb-8b69-057945a9583d
execute if score #success bs.data matches 1 store result score #health.max bs.data run attribute @s minecraft:generic.max_health get 100000
execute if score #success bs.data matches 1 store result storage bs:ctx x double 0.00001 run scoreboard players operation #health.max bs.data -= #health bs.data
execute if score #success bs.data matches 1 if score #health.max bs.data matches ..0 run function bs.health:apply/increase_health with storage bs:ctx

function bs.health:apply/set_max_health with storage bs:ctx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$execute store success score #success bs.data run function bs.health:time_to_live/normalize_unit with storage bs:const health.ttl.units[{name:'$(unit)'}]
$execute store success score #success bs.data run function bs.health:time_to_live/normalize_unit with storage bs:const health.units[{name:'$(unit)'}]
execute if score #success bs.data matches 0 run function #bs.log:error { \
feature:"health.time_to_live", \
message:'"The unit provided is not supported."', \
Expand Down
19 changes: 0 additions & 19 deletions datapacks/Bookshelf/data/bs.health/predicates/is_stale.json

This file was deleted.

15 changes: 15 additions & 0 deletions datapacks/Bookshelf/data/bs.health/tests/add/concurrent.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Max HP and HP are added or removed correctly in a single tick
# @batch bs.health
# @dummy

function #bs.health:add_health {points:5.0}
function #bs.health:add_max_health {points:10.0}
function #bs.health:add_health {points:5.0}
function #bs.health:add_health {points:-10.0}
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 15

function #bs.health:add_health {points:20.0}
await delay 1t
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 30
12 changes: 12 additions & 0 deletions datapacks/Bookshelf/data/bs.health/tests/add/health.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# HP are added or removed correctly
# @batch bs.health
# @dummy

function #bs.health:add_health {points:-10}
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 10

function #bs.health:add_health {points:5}
await delay 1t
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 15
11 changes: 11 additions & 0 deletions datapacks/Bookshelf/data/bs.health/tests/add/max_health.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Max HP are added or removed correctly
# @batch bs.health
# @dummy

function #bs.health:add_max_health {points:10}
execute store result score #result bs.data run attribute @s minecraft:generic.max_health get
assert score #result bs.data matches 30

function #bs.health:add_max_health {points:-5}
execute store result score #result bs.data run attribute @s minecraft:generic.max_health get
assert score #result bs.data matches 25
10 changes: 10 additions & 0 deletions datapacks/Bookshelf/data/bs.health/tests/get/health.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Get HP returns correct value
# @batch bs.health
# @dummy

execute store result score #result bs.data run function #bs.health:get_health {scale:1}
assert score #result bs.data matches 20

attribute @s minecraft:generic.max_health modifier add 68f0b501-5dca-4abb-8b69-057945a9583d "bs.health" -5 add
execute store result score #result bs.data run function #bs.health:get_health {scale:1}
assert score #result bs.data matches 15
10 changes: 10 additions & 0 deletions datapacks/Bookshelf/data/bs.health/tests/get/max_health.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Get Max HP returns correct value
# @batch bs.health
# @dummy

execute store result score #result bs.data run function #bs.health:get_max_health {scale:1}
assert score #result bs.data matches 20

attribute @s minecraft:generic.max_health modifier add 68f0b501-5dca-4abb-8b69-057945a9583d "bs.health" -10 add
execute store result score #result bs.data run function #bs.health:get_max_health {scale:1}
assert score #result bs.data matches 20
14 changes: 14 additions & 0 deletions datapacks/Bookshelf/data/bs.health/tests/set/concurrent.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Max HP and HP are set correctly in a single tick
# @batch bs.health
# @dummy

function #bs.health:set_health {points:10.0}
function #bs.health:set_max_health {points:30.0}
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 10

function #bs.health:set_max_health {points:20.0}
function #bs.health:set_health {points:30.0}
await delay 1t
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 20
12 changes: 12 additions & 0 deletions datapacks/Bookshelf/data/bs.health/tests/set/health.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# HP are set correctly
# @batch bs.health
# @dummy

function #bs.health:set_health {points:10}
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 10

function #bs.health:set_health {points:15}
await delay 1t
execute store result score #result bs.data run data get entity @s Health
assert score #result bs.data matches 15
Loading
Loading