TabooLib / taboolib

Powerful framework for creating multi-platform Minecraft plugin
https://tabooproject.org
MIT License
313 stars 100 forks source link

6.2 nms模块的建议 #468

Closed CSXFanMeng closed 2 months ago

CSXFanMeng commented 2 months ago

核心: This server is running Paper version 1.21-109-master@5a5035b (2024-07-23T08:16:30Z) (Implementing API version 1.21-R0.1-SNAPSHOT) taboolib版本: 6.2.0-beta10 taboolib-gradle插件版本: 2.0.18 Java版本: java version "21.0.2" 2024-01-16 LTS 问题描述: 在给物品添加nbt后并saveto后将物品的nbt输出给玩家和后台发现了多出的原版count和id 相关代码(简略实现): String key = nbt.key(); String value = nbt.value(); itemTag.putDeep(key, value); itemTag.saveTo(item); player.sendMessage(itemTag.toJsonSimplified()); Bukkit.getConsoleSender().sendMessage(itemTag.toJsonSimplified()); 输出结果:

"components": { "minecraft:lore": [ "{"extra":[{"bold":true,"color":"red","italic":false,"obfuscated":false,"strikethrough":true,"text":" ","underlined":false},{"bold":true,"color":"gold","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"yellow","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"green","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"aqua","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"yellow","italic":false,"strikethrough":false,"text":"装备淬炼"},{"bold":true,"color":"aqua","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"green","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"yellow","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"gold","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"red","italic":false,"strikethrough":true,"text":" "}],"text":""}" "{"extra":[{"bold":true,"color":"yellow","italic":false,"obfuscated":false,"strikethrough":false,"text":"淬炼属性: ","underlined":false},{"bold":true,"color":"white","italic":false,"text":"✯一星淬炼"}],"text":""}" ] "minecraft:custom_data": { "components": { "minecraft:lore": [ "{"extra":[{"bold":true,"color":"red","italic":false,"obfuscated":false,"strikethrough":true,"text":" ","underlined":false},{"bold":true,"color":"gold","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"yellow","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"green","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"aqua","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"yellow","italic":false,"strikethrough":false,"text":"装备淬炼"},{"bold":true,"color":"aqua","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"green","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"yellow","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"gold","italic":false,"strikethrough":true,"text":" "},{"bold":true,"color":"red","italic":false,"strikethrough":true,"text":" "}],"text":""}" "{"extra":[{"bold":true,"color":"yellow","italic":false,"obfuscated":false,"strikethrough":false,"text":"淬炼属性: ","underlined":false},{"bold":true,"color":"white","italic":false,"text":"✯一星淬炼"}],"text":""}" ] } "TemperingOrigins": { "test2": "5" } "count": 1 "id": "minecraft:netherite_sword" } } "count": 1 "id": "minecraft:netherite_sword" }

在了解了下nbt更改后,抱歉之前不够了解引起的问题。但还是想问问能不能添加进原版的components,或custom_data只添加自己的内容而不会全部写进去一遍

Micalhl commented 2 months ago

但还是想问问能不能添加进原版的components,或custom_data只添加自己的内容而不会全部写进去一遍

没明白你想要表达什么,你 itemTag 这个变量是怎么得到的也不发出来。

多出了原版的 count 和 id?

这个就是原版 NBT 的内容啊。

CSXFanMeng commented 2 months ago

但还是想问问能不能添加进原版的components,或custom_data只添加自己的内容而不会全部写进去一遍

没明白你想要表达什么,你 itemTag 这个变量是怎么得到的也不发出来。

多出了原版的 count 和 id?

这个就是原版 NBT 的内容啊。

ItemTag itemTag = NMSItemTagKt.getItemTag(item);

现在1.20.5及以上会把自己增加的nbt放进minecraft:custom_data,原版的内容则是在components里,在我这样的使用情况下minecraft:custom_data下写入了整个物品的nbt(components和count以及id),我希望的是minecraft:custom_data只有putdeep进去的内容而不是整个物品的nbt,还是说这才是正常情况,没怎么接触过高版本。

CSXFanMeng commented 2 months ago

没事了,将ItemTag itemTag = NMSItemTagKt.getItemTag(item, false);的onlycustom改为true也可以

Micalhl commented 2 months ago

没事了,将ItemTag itemTag = NMSItemTagKt.getItemTag(item, false);的onlycustom改为true也可以

是因为你没有仔细看注释。

image

在 1.20.5+ 中操作物品的 自定义 数据需要 写入读取 都将 onlyCustom 设定为 true,才可以只操作物品的 自定义 数据。只有 Fenestra 这种需要查看/操作物品 完整 NBT 数据的才要把 onlyCustom 设定为 false

默认在 Kotlin 环境下编写插件,这个参数默认为 true,甚至都不需要你传入。你提到的 custom_data 里写入了整个物品的 NBT 这个问题大概率是因为你在 Java 环境中需要给函数传完整参数,然后写入时 onlyCustom 设置为了 true,读取时为 false,把完整的数据写入了自定义数据里。

CSXFanMeng commented 2 months ago

没事了,将ItemTag itemTag = NMSItemTagKt.getItemTag(item, false);的onlycustom改为true也可以

是因为你没有仔细看注释。 image 在 1.20.5+ 中操作物品的 自定义 数据需要 写入读取 都将 onlyCustom 设定为 true,才可以只操作物品的 自定义 数据。只有 Fenestra 这种需要查看/操作物品 完整 NBT 数据的才要把 onlyCustom 设定为 false

默认在 Kotlin 环境下编写插件,这个参数默认为 true,甚至都不需要你传入。你提到的 custom_data 里写入了整个物品的 NBT 这个问题大概率是因为你在 Java 环境中需要给函数传完整参数,然后写入时 onlyCustom 设置为了 true,读取时为 false,把完整的数据写入了自定义数据里。

是的,感谢回答。