名称与背景故事
这是一个简易的数据包,允许玩家为物品添加自定义名称和自定义背景故事,并编辑这些名称和背景故事的属性。所有功能都通过 /trigger 命令实现,因此默认情况下不需要任何权限设置,服务器上的任何玩家都可以使用。
工作原理
所有玩家输入均通过书和笔完成,不同数据存储在不同的页面上。第 1 页用于自定义名称,第 2 页用于自定义背景故事,第 3 页用于存储十六进制颜色值。在这些页面中输入数据后 不要签署书籍,而是点击“完成”。当玩家在副手持有书和笔,在主手持有物品时,他们可以使用命令来添加新的自定义名称和背景故事。在将文本放入页面时,不要按回车键,否则名称/背景故事中会包含“换行”字符。
命令
共有 9 个命令可通过 /trigger 命令供所有玩家访问,并且所有命令中都包含了数据包的前缀以防止与其他数据包产生数据冲突。例如,“help”命令的访问方式为:
/trigger names_and_lore_help
这 9 个命令如下:
new_name - 将主手持有的物品的名称设置为副手书本的第 1 页内容
new_lore - 将主手持有的物品的背景故事设置为副手书本的第 2 页内容
hex_color_name - 将主手持有的物品的名称颜色设置为副手书本的第 3 页内容
hex_color_lore set # - 将主手持有的物品的指定背景故事行的颜色设置为副手书本的第 3 页内容
clear_lore - 清除主手持有的物品上的所有背景故事数据
clear_lore_line set # - 清除指定背景故事行的数据,并将后续背景故事行向下移动 1 位
open_name_menu - 在聊天栏中打开一个菜单,用于自定义主手持有的物品名称的颜色、文本格式和字体
open_lore_menu set # - 在聊天栏中打开一个菜单,用于自定义主手持有的物品指定背景故事行的颜色、文本格式和字体
help - 在聊天栏中提供类似本说明的使用解释
对于修改物品背景故事数据的命令,它们都可以接受数字输入以指定要编辑的行。如果玩家没有在命令后使用“set #”,则默认编辑第一行背景故事;如果玩家输入的范围超出行数(例如 -8 或 74),则会分别被限制为最低或最高值。
对于 hex_color 命令,输入到书中时省略了十六进制值的“#”符号。这意味着如果玩家想要白色作为十六进制颜色值,他们在书中应输入
FFFFFF
而不是 #FFFFFF。
设置
有两个值旨在作为服务器所有者/管理员调整我设置的限制的选项。其中一个是一个名为 ?lore 的玩家计分板值。该目标对象称为 names_and_lore_lore_limit,默认加载时设置为 6。我选择 6 作为默认限制,因为个人发现物品上超过 6 行的背景故事在鼠标悬停时会覆盖太多物品栏屏幕,但允许更多行并不一定会破坏游戏内的任何东西。虽然可以更改此计分板值并立即生效,但执行 /reload 会将其重置回 6。此值可以在数据包的 load.mcfunction 文件中永久更改。
第二个选项是书籍页面上可应用至物品的最大字符数的默认值。这不是存储在计分板值中,而是硬编码在两个不同的 mcfunction 文件中:format_name_new 和 format_lore_new。默认值为 100 个字符,超过此限制会导致物品上的名称和背景故事变得非常长,以至于覆盖整个屏幕,并且仍然能够滚动出屏幕。在 100 个字符的限制下,如果使用最大宽度字符(如大写字母 W),名称/背景故事条目可能达到屏幕宽度的约 ~90%,但通常名称可以很长,但不会具有干扰性。
检查玩家 /trigger 并将其重置的主要函数位于 ticker.mcfuntion 文件中,其末尾有一个 20 刻的计时器延迟。这意味着如果玩家尝试以每秒超过一次的频率输入命令,他们将收到“您还不能触发此目标对象”的错误,直到一秒计时器结束。这是为了减少服务器负载,通过更频繁地检查所需数据来实现,但如果需要,可以降低此延迟并仍能正常工作。与此延迟相关的重要注意事项是,它以刻为单位测量,而非秒。这意味着在经历极高 MSPT(每刻毫秒数)的服务器/世界中,这 20 刻经过的时间可能会超过 1 秒。
卸载
有一个名为 uninstall.mcfunction 的函数,它将移除数据包中使用的计分板目标对象以及属于该数据包的数据存储。运行此命令后,您已清除了该数据包放入游戏的所有数据,可以安全地移除它。不使用此卸载功能只会留下一些计分板值,由于它们使用前缀命名空间和一个假玩家名称 (?lore) 作为 2 个分数,因此不会破坏任何东西。
其他说明
由于此数据包中玩家输入数据的存储和处理方式,如果此数据包有 20 刻计时器问题且服务器中有许多玩家同时使用它,出现数据重叠问题我并不感到惊讶。我为此设计的使用场景是在一个小领域中与几个朋友一起玩,这种情况下不会有问题,并且在这种情况下它可以完美工作。如果您想在拥有数百名玩家的服务器上使用它,可以尝试降低刻延迟,或者等待人们为其主要装备重命名并不再频繁与数据包交互,随着时间的推移,问题将会消失。
在说明完上述内容后,接下来的两段主要是我的致歉与解释。这是我制作的首个数据包,我预料到该数据包发布后,其他用户可能会发现一些我在开发阶段遗漏的早期问题。我已确保在文本收集与格式化系统中妥善处理了各种边界情况,并且我知道在日常使用中,该数据包能够正常运行。允许玩家通过书本使用自定义输入确实可能引发关于任意代码执行(ACE)的担忧——即利用通常需要 OP 权限才能使用的命令——但我有 99% 的把握确信这是不可能的,因为输入的解析与应用方式决定了这一点。
尝试在 1.21.5 版本发布后在线搜索有关自定义物品命名的资料时,找到的结果大多非常陈旧,已不再适用;有些仅仅是演示如何使用 /give 命令配合自定义数据;还有些则因 1.21.5 中文本/JSON 格式的变化而过时。因此,为一名数据包新手而言,确定实现所有这些功能的方法需要投入大量的研究与时间。我希望这个数据包能帮助那些正寻找其特定功能的玩家,也希望它能让正在搜索如何自行搭建类似配置的开发者们有所启发。
Names and Lore
This is a simple datapack that allows player to add custom names and custom lore to their items, and to edit the properties of those names and lore. All of the functionality is built into /trigger commands so this will not require any permission setup by default and can be used by any player on a server.
How it Works
All of the player input is done through a book and quill, with different data stored on different pages. Page 1 is used for custom names, Page 2 is used for custom lore, and page 3 is used to store a hexidecimal color value. After entering the data on these pages DO NOT SIGN THE BOOK, instead hit "done". When a player holds a book and quill in their offhand and an item in their mainhand they are able to use the commands to add new custom names and lore. Do not hit the enter key when putting text onto pages or a "new line" character will be included in your names/lore.
Commands
There are 9 total commands that are accesible to all players through the /trigger command, and all of them include the namespace of the datapack in the command to prevent data conflicts with other datapacks. For example, the "help" command is accessed with:
/trigger names_and_lore_help
The 9 commands are as follows:
- new_name - sets the name of the held item to page 1 of the offhand book
- new_lore - sets the lore of the held item to page 2 of the offhand book
- hex_color_name - sets the name color of the held item to page 3 of the offhand book
- hex_color_lore set # - sets the specified lore line color of the held item to page 3 of the offhand book
- clear_lore - clears all the lore data on the held item
- clear_lore_line set # - clears the specified lore lines' data, and shifts the following lore lines down by 1
- open_name_menu - opens a menu in text chat used to customize the color, text formatting, and font of the held items name
- open_lore_menu set # - opens a menu in text chat used to customize the color, text formatting, and font of the held items specified line of lore
- help - Gives an explanation similar to this one in text chat on how to use the datapack
For the commands that alter the lore data of an item, they are all able to take a numeric input to specify which line you want to edit. If a player does not use the "set #" following the command it defaults to editing the first line of lore, and if a player enters a number outside the range of lines, such as -8 or 74, it will be clamped to the lowest or highest value respectively.
For the hex_color commands the hex values "#" symbol is omitted when being entered in a book. This means that if a player wants the color white as a hex value what they should enter in the book is
FFFFFF
as opposed to #FFFFFF.
Settings
There are two values that are meant to act as settings for server owners/admins to be able to tweak limits I set in place. One of them is a player scoreboard value under a username of ?lore. This objective is called names_and_lore_lore_limit and is set to 6 by default and on load. I chose 6 as the default limit as I personally found having more than 6 lines of lore on an item began to cover too much of the inventory screens when moused over, but allowing for more lines will not necessarily break anything in-game. While this scoreboard value can be changed and the change will work immediately a /reload will reset it back to 6. This value can be permanently changed in the load.mcfunction file of the datapack.
The second option is for the default maximum number of characters that can be on a book page and still applied to an item. This is not stored in a scoreboard value but instead is hardcoded in 2 of the different mcfunction files: format_name_new and format_lore_new. The default value is 100 characters and going over this limit will have names and lore on items so long that they cover the entire screen AND are still able to scroll off the screen. With the 100 character limit it is possible to have a name/lore entry reach about ~90% across the screen if the only characters used are maximum width characters such as a capital W, but generally names can become long but not obstructively so.
The main function checking for player /triggers and resetting them takes place in the ticker.mcfuntion file, which ends with a 20 tick timer delay. This means if a player is trying to input commands faster than once every second they will get a "You cannot trigger this objective yet" error until the one second timer has elapsed. This is to reduce server load by checking data more frequently that is needed, but if desired this can be lowered and still function fine. An important note related to this delay is it is measured in ticks, not seconds. This does mean on a server/world that is experiencing a very high MSPT the time it takes for those 20 ticks to pass may become longer than 1 second.
Uninstall
There is a function named uninstall.mcfunction that will remove the scoreboard objectives used in the datapack, and the data storage belongs to the datapack. After running this command you have cleared all the data this datapack put into the game and can safely remove it. Not using this uninstall function will just leave a few scoreboard values that wont break anything because they use my namespace as a leading name, and a fake player name (?lore) for 2 scores.
Addtional Notes
Because of how player input data is stored and handled in this datapack, I would not be surprised if this had data overlap issues with its 20 tick timer and many players in a server using it simultaneously. The use case I designed this for was in a small realm with a few friends where this wouldn't be an issue, and this does work perfectly for that case. If you want to use this on a server that has hundreds of players you can try to lower the tick delay or wait for people to get their main set of gear renamed and not interacting with the datapack frequently and the problem will go away over time.
With all of that out of the way, these lat 2 paragraphs will be for excuse making. This is my first datapack and I expect that there will be early issues discovered by other users, once this is in the wild, that I missed. I made sure to account for edge-cases in the text gathering and formatting systems, and I know that for normal use this datapack functions correctly. Allowing players to use custom input with a book should raise concerns about Arbitrary Code Execution (ACE) in the form of using commands normally requiring OP to use, but I am 99% certain that this is not possible due to how the input is proccessed and applied.
Trying to search online about custom item naming as of 1.21.5's release is met with mostly very old results that are no longer possible, are just demonstrations of using /give with custom data, or are just outdated because of text/json formatting changes in 1.21.5 so establishing the method to do all this took a good amount of research and time for someone who is new to datapacks. I hope this is helpful to people wanting exactly what this datapack offers and I hope this is found by people searching for how to make their own setup as well.
评论 0
登录后参与评分
还没有评论,来抢沙发吧