From 3c3bd036d75373749a65388146b0b1de6134adbd Mon Sep 17 00:00:00 2001 From: Aksiome <54895777+aksiome@users.noreply.github.com> Date: Thu, 6 Mar 2025 20:31:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20tag=20to=20check=20if?= =?UTF-8?q?=20interaction=20is=20hovered=20(#350)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/_templates/changelog/v3.0.0.md | 5 ++ docs/modules/interaction.md | 58 ++++++++++++------- .../function/on_event/hover/hover.mcfunction | 2 +- .../on_event/hover_enter/as_target.mcfunction | 1 + .../on_event/hover_leave/as_target.mcfunction | 1 + .../on_event/hover_leave/try_leave.mcfunction | 2 +- .../{check_id.json => match_id.json} | 0 .../test/on_hover_once.mcfunction | 37 ++++++++++++ 8 files changed, 82 insertions(+), 24 deletions(-) rename modules/bs.interaction/data/bs.interaction/predicate/{check_id.json => match_id.json} (100%) create mode 100644 modules/bs.interaction/data/bs.interaction/test/on_hover_once.mcfunction diff --git a/docs/_templates/changelog/v3.0.0.md b/docs/_templates/changelog/v3.0.0.md index a4f7bb7df1..d23cd63f3a 100644 --- a/docs/_templates/changelog/v3.0.0.md +++ b/docs/_templates/changelog/v3.0.0.md @@ -55,6 +55,11 @@ - 🐛 **[#320](https://github.com/mcbookshelf/bookshelf/issues/320)** - Fixed functions that were unusable outside the Overworld. +### `🖱️ bs.interaction` + +- **[#300](https://github.com/mcbookshelf/bookshelf/issues/300)** - Added `bs.interaction.hovered` tag to check if an interaction is currently hovered. + + ### `🏃 bs.move` - ⚠️ **[#282](https://github.com/mcbookshelf/Bookshelf/issues/282)** - Updated `on_collison` callback: scores now use the `bs.lambda` objective, and the input requires a full command instead of a function path. Additionally, `on_collision/*` functions have been renamed to `callback/*`. diff --git a/docs/modules/interaction.md b/docs/modules/interaction.md index 49b72d13c5..c1d74be8d0 100644 --- a/docs/modules/interaction.md +++ b/docs/modules/interaction.md @@ -244,6 +244,42 @@ You can find below all tags available in this API. --- +### Is Hovered? + +**`bs.interaction.hovered`** + +Determine if an interaction is currently hovered by a player. + +```{note} +This is useful in `hover_enter` and `hover_leave` events, which are per player. Since `hover_leave` triggers when a player stops hovering but doesn't check if all players have stopped, this tag can be used to determine when every player has left the interaction. +``` + +> **Credits**: Aksiome + +--- + +### Is Source? + +**`bs.interaction.source`** + +Usable only in the callback of an event. +Determine if the current entity is the source of the event. + +> **Credits**: theogiraudet + +--- + +### Is Target? + +**`bs.interaction.target`** + +Usable only in the callback of an event. +Determine if the current entity is the target of the event. + +> **Credits**: theogiraudet + +--- + ### Listen to a Specific Event? :::::{tab-set} @@ -284,28 +320,6 @@ Determine if the current interaction listen to leave hover interaction. --- -### Is Source? - -**`bs.interaction.source`** - -Usable only in the callback of an event. -Determine if the current entity is the source of the event. - -> **Credits**: theogiraudet - ---- - -### Is Target? - -**`bs.interaction.target`** - -Usable only in the callback of an event. -Determine if the current entity is the target of the event. - -> **Credits**: theogiraudet - ---- -
**💬 Did it help you?** diff --git a/modules/bs.interaction/data/bs.interaction/function/on_event/hover/hover.mcfunction b/modules/bs.interaction/data/bs.interaction/function/on_event/hover/hover.mcfunction index ece2edf4e3..06235a70e3 100644 --- a/modules/bs.interaction/data/bs.interaction/function/on_event/hover/hover.mcfunction +++ b/modules/bs.interaction/data/bs.interaction/function/on_event/hover/hover.mcfunction @@ -18,7 +18,7 @@ tag @s add bs.interaction.source execute store result storage bs:ctx y int 1 run scoreboard players get @s bs.interaction.id -execute as @n[type=minecraft:interaction,predicate=bs.interaction:check_id] run tag @s add bs.interaction.target +execute as @n[type=minecraft:interaction,predicate=bs.interaction:match_id] run tag @s add bs.interaction.target $execute unless predicate {"condition":"minecraft:entity_properties","entity":"this","predicate":{"type_specific":{"type":"minecraft:player","looking_at":{"type":"minecraft:interaction","nbt":"{\"Tags\":[\"bs.interaction.target\"]}","distance":{"absolute":{"max":$(y)}}}}}} run return run function bs.interaction:on_event/hover_leave/hover_leave execute as @n[type=minecraft:interaction,tag=bs.interaction.target,distance=..24] run function bs.interaction:on_event/hover/as_target tag @s remove bs.interaction.source diff --git a/modules/bs.interaction/data/bs.interaction/function/on_event/hover_enter/as_target.mcfunction b/modules/bs.interaction/data/bs.interaction/function/on_event/hover_enter/as_target.mcfunction index 33ea9028c0..656dfb7c24 100644 --- a/modules/bs.interaction/data/bs.interaction/function/on_event/hover_enter/as_target.mcfunction +++ b/modules/bs.interaction/data/bs.interaction/function/on_event/hover_enter/as_target.mcfunction @@ -17,5 +17,6 @@ scoreboard players add @s bs.interaction.hover 1 scoreboard players add #interaction.active_hover bs.data 1 execute store result storage bs:ctx y int 1 run scoreboard players operation @p[distance=0,tag=bs.interaction.source] bs.interaction.id = @s bs.interaction.id execute if entity @s[tag=bs.interaction.listen_hover_enter] run function bs.interaction:on_event/hover_enter/dispatch with storage bs:ctx +tag @s add bs.interaction.hovered execute if entity @s[tag=bs.interaction.listen_hover] run function bs.interaction:on_event/hover/dispatch with storage bs:ctx tag @s remove bs.interaction.target diff --git a/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/as_target.mcfunction b/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/as_target.mcfunction index 6d5cade864..86912fb64e 100644 --- a/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/as_target.mcfunction +++ b/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/as_target.mcfunction @@ -16,5 +16,6 @@ tag @s add bs.interaction.target scoreboard players remove @s bs.interaction.hover 1 scoreboard players remove #interaction.active_hover bs.data 1 +execute if score @s bs.interaction.hover matches ..0 run tag @s remove bs.interaction.hovered execute if entity @s[tag=bs.interaction.listen_hover_leave] run function bs.interaction:on_event/hover_leave/dispatch with storage bs:ctx tag @s remove bs.interaction.target diff --git a/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/try_leave.mcfunction b/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/try_leave.mcfunction index 00a31c6157..c0a1169b13 100644 --- a/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/try_leave.mcfunction +++ b/modules/bs.interaction/data/bs.interaction/function/on_event/hover_leave/try_leave.mcfunction @@ -15,6 +15,6 @@ execute store result score #i bs.ctx run scoreboard players get @s bs.interaction.hover execute store result storage bs:ctx y int 1 run scoreboard players get @s bs.interaction.id -execute at @s store result score #p bs.ctx if entity @a[distance=..24,predicate=bs.interaction:check_id] +execute at @s store result score #p bs.ctx if entity @a[distance=..24,predicate=bs.interaction:match_id] scoreboard players operation #i bs.ctx -= #p bs.ctx execute if score #i bs.ctx matches 1.. run function bs.interaction:on_event/hover_leave/as_target diff --git a/modules/bs.interaction/data/bs.interaction/predicate/check_id.json b/modules/bs.interaction/data/bs.interaction/predicate/match_id.json similarity index 100% rename from modules/bs.interaction/data/bs.interaction/predicate/check_id.json rename to modules/bs.interaction/data/bs.interaction/predicate/match_id.json diff --git a/modules/bs.interaction/data/bs.interaction/test/on_hover_once.mcfunction b/modules/bs.interaction/data/bs.interaction/test/on_hover_once.mcfunction new file mode 100644 index 0000000000..33e37df7ef --- /dev/null +++ b/modules/bs.interaction/data/bs.interaction/test/on_hover_once.mcfunction @@ -0,0 +1,37 @@ +# ------------------------------------------------------------------------------------------------------------ +# Copyright (c) 2025 Gunivers +# +# This file is part of the Bookshelf project (https://github.com/mcbookshelf/Bookshelf). +# +# This source code is subject to the terms of the Mozilla Public License, v. 2.0. +# If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# Conditions: +# - You may use this file in compliance with the MPL v2.0 +# - Any modifications must be documented and disclosed under the same license +# +# For more details, refer to the MPL v2.0. +# ------------------------------------------------------------------------------------------------------------ + +## === SETUP === + +fill ~-1 ~-1 ~-1 ~1 ~1 ~1 minecraft:air replace minecraft:barrier +summon minecraft:interaction ~.5 ~ ~1.5 {Tags: ["bs.packtest","bs.packtest.1"], width: 1.0, height: 2.0} +execute as @n[type=minecraft:interaction,tag=bs.packtest,distance=..3] run function #bs.interaction:on_hover_enter { run: "execute unless entity @s[tag=bs.interaction.hovered] run scoreboard players add #packtest.interaction_hover_once bs.data 1", executor: "target" } +execute as @n[type=minecraft:interaction,tag=bs.packtest,distance=..3] run function #bs.interaction:on_hover_leave { run: "execute unless entity @s[tag=bs.interaction.hovered] run scoreboard players remove #packtest.interaction_hover_once bs.data 1", executor: "target" } +scoreboard players set #packtest.interaction_hover_once bs.data 0 + +## === EXPECTED BEHAVIORS === + +execute positioned ~.5 ~ ~.5 run dummy on_hover_once_1 spawn +await delay 2t +assert score #packtest.interaction_hover_once bs.data matches 1 +execute positioned ~.5 ~ ~.5 run dummy on_hover_once_2 spawn +await delay 2t +assert score #packtest.interaction_hover_once bs.data matches 1 +dummy on_hover_once_2 leave +await delay 2t +assert score #packtest.interaction_hover_once bs.data matches 1 +dummy on_hover_once_1 leave +await delay 2t +assert score #packtest.interaction_hover_once bs.data matches 0