Moxvallix 的 GUI 库
让数据包 GUI 开发更简单
Moxvallix 的 GUI 库依赖于 Taglib 和 Moxlib
此数据包自动化了 GUI 开发中许多繁琐的部分。
它是数据驱动的,意味着它基于存储在数据存储中的模板来渲染 GUI。
目前,该库有两种工作模式:“末影箱”(enderchest)和“箱子”(barrel)。在“末影箱”模式下,GUI 会绘制到玩家的末影箱中。这会覆盖末影箱中原有的所有内容,请谨慎使用!
末影箱模式是最容易使用的,最适合那些不需要主要末影箱功能的冒险地图。
另一种“箱子”模式会将 GUI 绘制到 ~ ~ ~ 位置的容器中。虽然这适用于任何容器方块,
但建议使用箱子(Barrel),因为它可以检测容器何时被打开,从而允许它在第一个玩家使用时轻松阻止其他玩家使用该 GUI。
这种 GUI 系统的一个好处是,由于它是数据驱动的,因此制作 GUI 生成器应该相当容易。
不过我还没有创建这样的生成器。
入门指南
概述
根据 GUI 是绘制到容器还是末影箱,API 分别命名空间为“barrel”或“enderchest”。以下指南将使用 <type> 作为此部分的占位符。将其替换为相应的方法即可。
注意:“barrel”类型应与容器方块在同一位置执行。如果你的数据包中有多个 GUI,
你可能希望有实体位于与方块相同的位置,并在它们身上执行命令。
有两个主要函数:gui:api/<type>/render 和 gui:api/<type>/watch。
Render 会根据页面 ID 绘制 GUI,而 watch 会处理 GUI 中的任何点击事件。
所有位于 api/ 子文件夹之外的函数都被视为私有,不支持运行。
watch 函数应在 GUI 处于活动状态时,以玩家身份循环运行。
如果你希望 GUI 始终处于活动状态,可以在你的 tick 函数中设置它以 @a 的身份运行。
创建索引
为了让你的 GUI 正常工作,你需要一个索引函数。
当运行此类函数时,它会读取 @s 的 gui.page 分数,并为该页面设置正确的模板数据。
建议你在单独的函数中设置模板数据,而索引函数仅调用该函数。
示例索引函数
execute if score @s gui.page matches 1 run function pack:gui/menu
execute if score @s gui.page matches 2 run function pack:gui/options
execute if score @s gui.page matches 3 run function pack:gui/shop
然后你需要注册你的索引函数以在 index 标签中运行。将其添加到 gui:index 函数标签中。
{
"values": [
"pack:gui/index"
]
}
创建模板
现在我们可以创建 GUI 模板了。这些将是索引将要运行的函数。
示例模板函数 (pack:gui/menu)
data modify storage gui:page template set value [{slot:12b,page:2s,item:{id:"minecraft:redstone_dust"}},{slot:14b,page:3s,item:{id:"minecraft:chest"}}]
上述菜单模板会在 GUI 中渲染两个物品,一个在第 12 格,另一个在第 14 格。
第 12 格:
- 具有 page:2s,点击时将渲染
pack:gui/options
- 具有 item 标签,将物品设置为
redstone_dust
第 14 格:
- 具有 page:3s,点击时将渲染
pack:gui/shop
- 具有 item 标签,将物品设置为
chest
如果你想尝试一下,请确保将你的 gui.page 分数设置为 1,然后运行 gui:api/<type>/render,或者按照上述描述让 gui:api/<type>/watch 循环运行。
在渲染时操作格子
有时,你希望在渲染时操作格子,以便拥有动态菜单。
只要定义了 slot 标签,你就可以在模板数据的格子中添加任何自定义标签。
每次要渲染格子时,它首先会调用 gui:render/slot 函数标签。
你可以向此标签添加自定义函数,以便操作格子数据。
当前的格子数据可以从数据存储 gui:slot current 中获取并进行操作。
Render 还会调用函数标签 gui:render/item,其中 @s 是一个临时的盔甲架,
当前正在渲染的物品位于其主手中。这将允许运行物品修改器。
选择时的自定义功能
GUI 的另一个关键部分是能够在选择特定格子时运行自定义函数。
每当选择一个格子时,它会以选择该格子的玩家身份调用 gui:select/slot 函数标签。
然后你将能够从数据存储 gui:slot current 中读取所选的格子。
Moxvallix's GUI Lib
Making Datapack GUIs easier
Moxvallix's GUI Lib relies on Taglib, and Moxlib
This datapack automates much of hard parts of GUI development.
It is data driven, meaning it renders the GUI based on a template stored in data storage.
Currently, the library works in two modes; "enderchest", and "barrel". In "enderchest" mode, the GUI is drawn
to the player's Enderchest. This overrides whatever contents were previously in the enderchest, so be warned!
Enderchest mode is the easiest to use, and is best for Adventure maps, where the main Enderchest functionality
is not needed.
The other, "barrel", mode, draws the GUI to a container at ~ ~ ~. While this works with any container block,
it is advised that a Barrel is used, as that allows for it to detect when the container is opened, allowing for it
to then easily prevent other players using the GUI whilst the first player is.
One benefit of this GUI system, being data driven, means that making generators for GUIs should be fairly easy.
I however have not created one yet.
Getting Started
Overview
Depending on whether the GUI should be drawn to a container, or the Enderchest, the API is namespaced under "barrel",
or "enderchest" respectively. The following guide will use <type> as a placeholder for this. Swap it out for which ever
method.
Note: type "barrel" should be executed at the same position as a container block. If you have multiple GUIs in your packs,
you may want to have entities positioned at the same location as the block, executing at them.
There are two main functions: gui:api/<type>/render and gui:api/<type>/watch.
Render will draw the gui, based on the page id, and watch will run any clicks in the GUI.
All functions outside of the api/ subfolder are to be considered private, and are not supported to be ran.
The watch function should be run as the player, on a loop, whenever the GUI should be active.
If you wanted the GUI always active, you could set it to run as @a from your tick function.
Making an Index
For your GUI to work, you will need an index function.
Such a function, when run, will take in @s score of gui.page, and will set the correct template data for that page.
It is recommended that you set the template data within a separate function, and that index merely calls said function.
An example index function
execute if score @s gui.page matches 1 run function pack:gui/menu
execute if score @s gui.page matches 2 run function pack:gui/options
execute if score @s gui.page matches 3 run function pack:gui/shop
You will then need to register your index function to run in the index tag. Add it to the gui:index function tag.
{
"values": [
"pack:gui/index"
]
}
Making a Template
Now we can create GUI templates. These will be functions that the index will run.
An example template function (pack:gui/menu)
data modify storage gui:page template set value [{slot:12b,page:2s,item:{id:"minecraft:redstone_dust"}},{slot:14b,page:3s,item:{id:"minecraft:chest"}}]
The above menu template would render two items to the GUI, one in slot 12, the other in slot 14.
Slot 12:
- has page:2s, would render
pack:gui/options when clicked
- has an item tag, setting the item to
redstone_dust
Slot 14:
- has page:3s, would render
pack:gui/shop when clicked
- has an item tag, setting the item to
chest
If you want to try it out, make sure to set your gui.page score to 1, then run either gui:api/<type>/render, or
have gui:api/<type>/watch on loop as described above.
Manipulate Slots as they Render
Sometimes, you will want to manipulate slots as they render, such that you can have dynamic menus.
You can add any custom tags you like to the slots in your template data, as long as the slot tag is defined.
Every time a slot is going to be rendered, it first calls the gui:render/slot function tag.
You can add a custom function to this tag, in order to manipulate the slot data.
The current slot data will be available to be manipulated from the data storage gui:slot current.
Render will also call the function tag gui:render/item, with @s being a temporary armor stand,
and the current item being rendered in its mainhand. This will allow for item modifiers to be run.
Custom Functionality on Selection
Another key part of a gui is being able to run custom functions when a particular slot is selected.
Whenever a slot is selected, it calls the gui:select/slot function tag,
as the player that selected the slot.
You will then be able to read the selected slot from the data storage gui:slot current.
评论 0
登录后参与评分
还没有评论,来抢沙发吧