它有什么作用?
创建一种新的资源类型——“静态数据”,让模组能够以简单、结构化的方式互相搜索以及搜索其环境,从而获取兼容性和互操作性信息。
例如,一个模组可能会基于一组原版方块注册具有坡度的方块,同时也提供基于模组方块的坡度方块注册选项。标签(Tags)在此时还不可用!而静态数据可以。
模组互操作数据的责任并不锁定在某个特定位置。由模组 A 查询的与模组 B 相关的静态数据,可以由模组 A、模组 B、第三个模组甚至是一个静态数据包来提供。来自所有这些来源的数据可以混合并以累加方式使用。
为什么我们需要静态数据?
静态数据是静态的
数据包和资源包的重载不会影响静态数据和静态数据包。在整个模组生命周期中,相同的数据始终可用,并且其源模组(如果有的话)可被识别。你无需为此调用任何入口点,不必关心模组来自哪个加载器,数据就在那里。你也不必担心连接到服务器是否会改变数据,因为静态数据不会变化,你可以放心地只初始化一次。
静态数据早期可用
只要加载器中的第一个入口点在任意位置运行——无论是 quilt-loader、qsl、qfapi 还是模组集成入口点——如果你的模组被要求初始化某些内容,静态数据就已经准备好了。
你可以用它来触发注册
由于普通数据包会随时间变化,使用它们来更改冻结的注册表就像打开潘多拉魔盒一样复杂。由于静态数据不会变化,并且在注册表冻结之前就已可用,你可以使用从静态数据中获得的信息来注册方块和实体。这是其存在的主要原因——允许模组以一种在其他模组消失时信息也随之消失的方式相互传达功能信息,但不依赖模组拥有相同的模组加载器。
我如何提供静态数据?
模组像提供其他类型数据一样提供静态数据
静态数据放置在 staticdata 目录中,该目录与 assets 和 data 目录并列,项目则在 staticdata 文件夹内进行命名空间划分。因此,如果模组 “architecture_extensions” 正在查找 “blockgroups” 路径下的文件,而你提供一个名为 “obsidian.json” 的集成文件,则该文件的完整路径为:
src/main/resources/staticdata/architecture_extensions/blockgroups/obsidian.json
注意,你完全不需要使用自己的命名空间——通过将数据放在你的模组内,你的 modId 已经提供了。
玩家和整合包制作者可以提供静态数据包
根文件夹命名为 “staticdata” 的 zip 文件是一个有效的静态数据包。这与资源包的工作原理完全相同,只是用于静态数据。为了提供上一节中相同的文件,该文件在 zip 内的路径如下所示:
/staticdata/architecture_extensions/blockgroups/obsidian.json
根目录下包含 pack.mcmeta 和 pack.png 是可选的,这些文件目前会被忽略。
静态数据包 zip 可以从两个位置加载:
- 模组内部,位于根目录
src/main/resources/staticdata/ 文件夹中
- 根目录
.minecraft/staticdata/ 文件夹中,当任何模组请求静态数据时,如果该文件夹不存在则会自动创建
放置在子目录中的静态数据包将无法访问。
原始文件可以放置在 staticdata 文件夹中
这些文件仍然需要命名空间,但再次以上述为例:
.minecraft/staticdata/architecture_extensions/blockgroups/obsidian.json
只要文件放置在正确的命名空间文件夹中,它们就会贡献于数据搜索。
我如何在模组中使用静态数据?
首先,依赖 staticdata。这需要 gradle 中的 modImplementation 条目以及 quilt.mod.json 中的 depends 条目。请参阅源代码仓库以获取当前版本和 maven/gradle 详细信息。
其次,建议在 gradle 中添加 include,以便库打包在你的 jar 文件中。出于某种原因,这似乎并不意味着 modImplementation,所以你可能需要同时添加这两行。
第三,在模组初始化期间,你可以执行类似以下操作:
// 你可以在此处使用 quilt-json5 或 jankson
Gson gson = new GsonBuilder().create();
// 获取此文件夹及子文件夹中的所有静态数据
List<StaticDataItem> dataItems = StaticData.getDataInDirectory(
new Identifier(MODID, "bees"), // 搜索 mymod:bees
true // 是的,递归搜索子文件夹
);
for(StaticDataItem dataItem : dataItems) {
// MyDataObject 在这里是你想要表示数据的类
MyDataObject dataObject = gson.fromJson(item.getAsString(), MyDataObject.class);
// 这只是一个建议——如果你条件性地加载数据,staticData 将最有价值!
String requiredMod = dataObject.only_if_present;
if (requiredMod != null && !requiredMod.isBlank()) {
// 仅在指示的模组存在时才应加载
if (!QuiltLoader.isModLoaded(requiredMod)) continue;
}
// 现在你可以使用这些数据了!
doSomethingWithTheData(dataObject);
}
(假设 MyDataObject 类的样子大致如下)
public class MyDataObject {
public String only_if_present;
// 其他数据以公共字段形式放在此处
}
我如何在整合包中使用该模组?
这是一个 jar-in-jar 模组,通常嵌入在其他模组中,且模组的性质使得配置免费的模组互操作成为可能。因此,只需下载利用它的模组并将它们一起游玩,你就能获得静态数据的基础好处。
然而,如果你想深入挖掘,可以考虑制作一个静态数据包并将其随你的整合包分发!每个模组对互操作的处理方式不同,请查阅模组的文档以了解更多详情。
What does it do?
Creates a third class of resource, "static data", which lets mods search each other and their environment in an easy, structured way for compatibility and interop information.
For instance, a mod might register a slope-shaped block based on a set of vanilla blocks, but also offer to register new slopes based on modded blocks. Tags aren't available early enough for this! Static data is.
Responsibility for mod interop data isn't locked into one particular place. Static data relating to mod B, queried by mod A, can be supplied by mod A, mod B, a third mod, or even a static datapack. Data from all these sources can be mixed and used additively.
Why do we need static data?
Static data is static
Datapack and Resource-pack reloads do not affect static data and static datapacks. The same data is available, and its source mod (if any) identifiable, through the entire lifecycle of your mod. You'll never have to call any entrypoints for this data, you don't have to care what loader the mod is from, the data is just already there. You'll never have to worry if connecting to a server has changed the data, static data doesn't change so you can initialize once with confidence.
Static data is available early
As soon as the first entrypoint runs anywhere in the loader - whether that entrypoint is quilt-loader, qsl, qfapi, or a mod integration entrypoint, if your mod has been asked to initialize something, static data is already ready.
You can use it to trigger registrations
Since ordinary datapacks change over time, it's a can of worms to use them to change frozen registries. Since static data does not change, and is available before registries are frozen, you can register blocks and entities using information you get from static data. This is the primary reason for its existence - to allow mods to communicate feature information to each other in a way that vanishes if the other mod is gone, but doesn't rely on the mods having the same modloader.
How can I provide staticdata?
Mods provde staticdata like other kinds of data
Static data is placed in a staticdata directory alongside the assets and data directories, and items are namespaced within the staticdata folder. So if the mod "architecture_extensions" is looking for files in the "blockgroups" path, and you provide an integration called "obsidian.json", the full path of this file would be
src/main/resources/staticdata/architecture_extensions/blockgroups/obsidian.json
Note that you do not use your own namespace at all - your modId is already provided by placing the data inside your mod.
Players and Pack-Makers can provide static datapacks
A zip file whose root folder is named "staticdata" is a valid static datapack. This works exactly like a resource pack except for static data. To provde the same file as the previous section, the file's path inside the zip would look like:
/staticdata/architecture_extensions/blockgroups/obsidian.json
Having a pack.mcmeta and pack.png inside the root directory is optional, and these files are currently ignored.
Static datapack zips can be loaded from two places:
- from within mods, in the root
src/main/resources/staticdata/ folder
- from the root
.minecraft/staticdata/ folder, which will be created if not present when any mod requests static data
Static datapacks placed within subdirectories will not be accessible.
Raw files can be placed in the staticdata folder
These files still need to be namespaced, but again using the above example:
.minecraft/staticdata/architecture_extensions/blockgroups/obsidian.json
As long as the files are placed in the correct namespaced folder, they will contribute to data searches.
How can I use static data in my mod?
First, depend on staticdata. This requires a gradle modImplementation entry and a quilt.mod.json depends entry. See the source repository for current version and maven/gradle details.
Second, it is recommended to add an include to your gradle so that the library ships inside your jar. For some reason this doesn't seem to imply modImplementation so you'll probably need both lines.
Third, during your modInitializer, you can do something like
// You could use quilt-json5 or jankson here instead
Gson gson = new GsonBuilder().create();
// Get all the static data in this folder and subfolders
List<StaticDataItem> dataItems = StaticData.getDataInDirectory(
new Identifier(MODID, "bees"), //Search for mymod:bees
true // Yes, do a recursive search through subfolders
);
for(StaticDataItem dataItem : dataItems) {
// MyDataObject here is the class that represents the data you want
MyDataObject dataObject = gson.fromJson(item.getAsString(), MyDataObject.class);
// This is just a suggestion - staticData is most useful if you
// load the data conditionally!
String requiredMod = dataObject.only_if_present;
if (requiredMod != null && !requiredMod.isBlank()) {
// Should only be loaded when the indicated mod is present
if (!QuiltLoader.isModLoaded(requiredMod)) continue;
}
//And now you're ready to use the data!
doSomethingWithTheData(dataObject);
}
(Assuming that the MyDataObject class looks something like)
public class MyDataObject {
public String only_if_present;
//other data goes here in public fields
}
How do I use the mod in my modpack?
This is a jar-in-jar mod which commonly comes embedded within other mods, and the nature of the mod enables configuration-free mod interop. So you can gain the base benefit of static data by just downloading mods which take advantage of it and playing them together.
However, if you want to dive deeper, consider making a static datapack and shipping it with your modpack! Each mod approaches interop differently, so consult the mod's documentation to find out more.
评论 0
登录后参与评分
还没有评论,来抢沙发吧