Structure Placer API
这个简单的 API 提供了一种一次性在世界中生成结构的方法,而不是将其加入世界生成逻辑。它可能适用于创建传送门的物品,或者其他类型的结构。或者用来制作幸运方块,谁知道呢!
设置
将此库作为依赖项包含在你的 build.gradle 中:
repositories {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
content {
includeGroup "maven.modrinth"
}
}
}
dependencies {
modImplementation "maven.modrinth:structureplacerapi:<version>"
}
如果你希望将 API 包含在你的 jar 文件中,只需添加 include 字符串即可:
repositories {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
content {
includeGroup "maven.modrinth"
}
}
}
dependencies {
modImplementation "maven.modrinth:structureplacerapi:<version>"
include "maven.modrinth:structureplacerapi:<version>"
}
它的大小不到 25 kb!
使用方法
如何创建结构?
你必须使用 Minecraft 的结构方块创建一个 NBT 结构,如本视频所示:
我得到的文件应该放在哪里?
你需要将 structure.nbt 文件放入你模组的 data 文件夹中,路径如下:data/yourmodid/structures
创建新的放置器对象
为了放置结构,你需要先创建一个放置器对象,这可以通过简单地创建一个新的 StructurePlacerAPI 对象来完成。它有几个参数可供调整:
StructurePlacerAPI(ServerWorld world, Identifier templateName, BlockPos blockPos, BlockMirror mirror, BlockRotation rotation, boolean ignoreEntities, float integrity, BlockPos offset)
参数说明
- 第一个参数
world是我们将在其中放置结构的世界。你可以从实体或其他地方获取它。 - 第二个参数
templateName是一个由你的模组 ID 和结构名称组成的 Identifier。 - 第三个参数
blockPos是你的结构将要生成的区块坐标。例如,你可以从实体坐标获取它。 - 第四个参数
mirror是可选参数之一。使用此类,你可以使用BlockMirror类来镜像你的结构。无需创建另一个对象,直接输入BlockMirror.#stuff即可。 - 第五个参数
rotation与前一个类似,你将能够使用BlockRotation类旋转你的结构。无需创建另一个对象,直接输入BlockMirror.#stuff即可。(注:原文此处笔误仍写为 BlockMirror,实际应指 BlockRotation) ignoreEntities参数如果设置为 true,则不会生成结构文件内包含的实体。如果设置为 false,则会生成它们。integrity参数非常有趣,因为它是一个介于 0f 和 1f 之间的浮点值,用于决定结构的破败程度。如果设置为低于 1f 的值,在生成时将移除部分方块。- 最后,
offset参数是另一个 BlockPos,可用于重新定位结构,例如放置在实体脚下或类似位置。
放置结构
你已经创建了 StructurePlacerAPI placer = new StructurePlacerAPI(things up there),要生成它,你需要执行以下操作:
placer.loadStructure();
是的,好吧,这并不难。你只需要运行那个方法,而不是直接调用 .place 方法,因为 load 方法在生成之前会检查该结构是否存在。
一段时间后恢复旧地形
从版本 1.1.0 开始,你还可以使用:
placer.loadAndRestoreStructure(int restore_ticks);
来提供经过一定刻数(1 秒 = 20 刻)后恢复旧地形的时间。 注意:这可能会导致性能问题,特别是当结构非常大时!(它需要保存结构内的每个方块!)
你还可以通过使用以下代码为方块重新出现添加动画效果:
placer.loadAndRestoreStructureAnimated(int restore_ticks, int blocks_per_tick, boolean random);
blocks_per_ticks是每个刻替换的方块数量,所以如果你的结构由 200 个方块组成,且 blocks_per_ticks 设置为 1,那么它将花费 1 秒放置 20 个方块,并花费 20 秒完成地形恢复。random指的是方块替换的顺序。设置为 true 时,每次再生的方块都是随机的,否则它将从结构的一个角移动到另一个角。在我看来,random = true 更酷。
选择哪些方块应该被替换(从 2.0.0 版本开始)
你现在可以指定放置的结构是否可以替换基岩和屏障,或者具有特定标签的方块,或者是否只能替换具有特定标签的方块。 在你创建了 StructurePlacerAPI placer 对象后,你可以使用这些方法:
/** 设置此结构是否应该替换基岩 */
placer.setReplaceBedrock(boolean replaceBedrock)
/** 设置此结构是否应该替换屏障方块 */
placer.setReplaceBarrier(boolean replaceBarrier)
/** 设置此结构是否仅替换具有所提供标签的方块,例如仅替换空气方块 */
placer.setOnlyReplaceTaggedBlocks(boolean onlyReplaceTaggedBlocks, TagKey<Block> tag)
/** 允许指定如果具有提供的标签则不会被替换的方块 */
placer.setPreventReplacementOfTaggedBlocks(boolean preventReplacementOfTaggedBlocks, TagKey<Block> tag)
在结构放置或替换特定方块时执行操作 (2.0.0+)
现在,您可以选择在结构内的方块被放置到世界中,或者在放置结构时替换方块时执行函数。
例如,如果您希望在放置包含浆果丛的结构时随机生成一只狐狸,可以使用:
placer.actionOnBlocksPlacedByStructure(ActionOnBlockFind action, TagKey<Block> targets);
//示例:
placer.actionOnBlocksPlacedByStructure((info, world) -> {world.spawnEntity(EntityType.FoxEntity, ...)}, BlockTags.BUSHES);
该动作是一个 lambda 函数,为您提供 StructureBlockInfo 和 ServerWorldAccess(或在 mojmaps 中对应的 ServerLevelAccess 等)。您需要指定一个标签来定义要替换的方块类型。如果仅涉及单一方块类型,请为该方块创建一个标签。
您还可以在已存在于世界中的方块被结构替换时执行某些操作。 例如,当冰方块被替换时生成雪粒子效果:
placer.actionOnBlocksReplacedByStructure(ActionOnBlockFind action, TagKey<Block> targets);
//示例:
placer.actionOnBlocksReplacedByStructure((info, world) -> {world.spawnParticles(ParticleType.SNOW, ...)}, BlockTags.ICE);
示例
您可以在 LightWithin 模组中找到此类示例(顺便一提,建议您查看一下):
StructurePlacer placer = new StructurePlacer((ServerWorld) caster.getWorld(), new Identifier(MOD_ID, "frost_light"), caster.getBlockPos(), BlockMirror.NONE, BlockRotation.CLOCKWISE_90, true, 1.0f, new BlockPos(-4, -3, -3));
placer.loadStructure();
用于创建永久性结构:
StructurePlacer placer = new StructurePlacer((ServerWorld) caster.getWorld(), new Identifier(MOD_ID, "frost_light"), caster.getBlockPos(), BlockMirror.NONE, BlockRotation.CLOCKWISE_90, true, 1.0f, new BlockPos(-4, -3, -3));
placer.loadAndRestoreStructureAnimated(200, 2, true);
通过每秒替换两个方块并以随机顺序进行,使旧地形再生。
警告!
重要提示!请确保您处于服务器线程上!
if(!world.isClient){
//运行相关代码
}
支持我
如果您想请我喝杯咖啡,请点击下方链接。
或者,您可以通过使用下方的优惠码购买服务器来获得我的支持,您和朋友可享受 25% 的折扣。
许可证
本 API 采用 CC0 许可协议发布。欢迎学习并应用于您的项目中。
Structure Placer API
This simple API provides a simple way to spawn a structure in your world one-time, instead of putting it in the world generation. It could be useful for an item that creates a portal, or some other kind of structure. Or to create LuckyBlocks, who knows!
Setup
Include this library into your build.gradle as a dependency
repositories {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
content {
includeGroup "maven.modrinth"
}
}
}
dependencies {
modImplementation "maven.modrinth:structureplacerapi:<version>"
}
If you want you can include the API in your jar file by adding only the include string:
repositories {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
content {
includeGroup "maven.modrinth"
}
}
}
dependencies {
modImplementation "maven.modrinth:structureplacerapi:<version>"
include "maven.modrinth:structureplacerapi:<version>"
}
It's less than 25 kb!
How to use it
How to create the structure?
You have to create an NBT structure using minecraft's StructureBlocks, as demonstrated in this video:
Where to put the file I got?
You will need to place the structure.nbt file insde your mod's data folder, like this: data/yourmodid/structures
Creating a new placer object
In order to place the structure, you will need to create a placer object first, this is done simply by creating a new StructurePlacerAPI object. It has quite a few parameters to play with:
StructurePlacerAPI(ServerWorld world, Identifier templateName, BlockPos blockPos, BlockMirror mirror, BlockRotation rotation, boolean ignoreEntities, float integrity, BlockPos offset)
The parameters
- The first parameter
worldis the wolrd in which we are going to place a structure. You could get it from an entity, or whatever. - The second parameter
templateNameis an Identifier made up by the MOD_ID of your mod and the structure's name - The third parameter
blockPosis the block position where your structure is going to spawn. You can get it from an entity coordinates for example - The fourth,
mirroris the first of the optional parameters. With this you can mirror your structure using theBlockMirrorclass. No need to create another object just type BlockMirror.#stuff - The fifth,
rotationis like the previuous one, and you will be able to rotate your structure using theBlockRotationclass. No need to create another object just type BlockMirror.#stuff - The
ignoreEntitiesparameter if set to true won't spawn entities included inside the structure file. If set to false, it will instead spawn them - The
integrityparameter is pretty intresting, since it is a float value between 0f and 1f which will deteremine how much the structure will be run-down. If set to a value below 1f some of the blocks will be removed upon generation - Finally, the
offsetparameter is another BlockPos that could be useful to reposition the structure under an entity's feet or something.
Placing the structure
You have just created a StructurePlacerAPI placer = new StructurePlacerAPI(things up there), and to spawn it you will have to do this:
placer.loadStructure();
Yeah ok it's not that difficult. You just have to run that tho, not the .place method directly, since the load will also check for the existance of said structure before spawning it.
Restoring the old terrein after some time
Starting from version 1.1.0 you can also use:
placer.loadAndRestoreStructure(int restore_ticks);
to supply an amount of ticks ( 1 seconds = 20 ticks ) after which the old terrain will be restored. NOTICE: It could lead to performance issues, especially if the structure is really big! (It needs to save every block inside the structure!)
You can also add an animation for the blocks reappearing by using:
placer.loadAndRestoreStructureAnimated(int restore_ticks, int blocks_per_tick, boolean random);
blocks_per_ticksis the amaount of blocks that will be replaced each tick, so if your structure is made by 200 blocks, and the blocks_per_ticks is set to 1, it will take 1 second to place 20 blocks, and 20 seconds to finish the terrain restoration.randomrefers to order in which the blocks will be replaced. Setting to true, each time the regenerated block will be a random one, otherwise it will go from one angle of the structure to the other one. In my opinion, random = true is cooler.
Selecting which blocks should be replaced (from 2.0.0 onwards)
You can now specify if the structure that gets placed can replace bedrock and barriers or blocks with a certain tag, or if it can ONLY replace blocks with a certain tag. After you have created you StructurePlacerAPI placer object, ou can use these methods:
/** Sets weather or not this structure should replace the bedrock */
placer.setReplaceBedrock(boolean replaceBedrock)
/** Weather or not this structure should replace the barrier block */
placer.setReplaceBarrier(boolean replaceBarrier)
/** Sets weather or not this structure should only replace blocks with the provided tag, for example only replace air blocks */
placer.setOnlyReplaceTaggedBlocks(boolean onlyReplaceTaggedBlocks, TagKey<Block> tag)
/** Allows to specify blocks which won't be replaced if they have the provided tag*/
placer.setPreventReplacementOfTaggedBlocks(boolean preventReplacementOfTaggedBlocks, TagKey<Block> tag)
Performing an action when a certain block is placed with the structure, or replaced by it (2.0.0+)
Now you have the option to execute a function when a block inside a structure gets placed in the world or when a block is replaced while placing the structure.
For example, if you want a random fox to spawn when placing a structure containing berry bushes you can use:
placer.actionOnBlocksPlacedByStructure(ActionOnBlockFind action, TagKey<Block> targets);
//Example:
placer.actionOnBlocksPlacedByStructure((info, world) -> {world.spawnEntity(EntityType.FoxEntity, ...)}, BlockTags.BUSHES);
The action is a lambda function that provides you with the StructureBlockInfo and ServerWorldAccess (or ServerLevelAccess or whatever it is with mojmaps). You have to specify a tag for the kind of block you want to replace. If it's only a single block type create a Tag for that block.
The other thing you can do is execute something when a block that is already in the world gets replaced by the structure. Like spawning snow particles when an ice block is replaced:
placer.actionOnBlocksReplacedByStructure(ActionOnBlockFind action, TagKey<Block> targets);
//Example:
placer.actionOnBlocksReplacedByStructure((info, world) -> {world.spawnParticles(ParticleType.SNOW, ...)}, BlockTags.ICE);
Example
An example of this could be the one you find insde the LightWithin mod(whihc btw, you should check out):
StructurePlacer placer = new StructurePlacer((ServerWorld) caster.getWorld(), new Identifier(MOD_ID, "frost_light"), caster.getBlockPos(), BlockMirror.NONE, BlockRotation.CLOCKWISE_90, true, 1.0f, new BlockPos(-4, -3, -3));
placer.loadStructure();
To have a permanent structure
StructurePlacer placer = new StructurePlacer((ServerWorld) caster.getWorld(), new Identifier(MOD_ID, "frost_light"), caster.getBlockPos(), BlockMirror.NONE, BlockRotation.CLOCKWISE_90, true, 1.0f, new BlockPos(-4, -3, -3));
placer.loadAndRestoreStructureAnimated(200, 2, true);
To make the old terrain regenerate by replacing two blocks per tick, in a random order.
Warning!
IMPORTANT! MAKE SURE YOU ARE ON THE SERVER THREAD!
if(!world.isClient){
//run stuff
}
Support me
If you would like to offer me a coffee, here you go.
Alternativly, you can support me by supporting you using a 25% off on a server for you and your friend using this code down here
License
This API is available under the CC0 license. Feel free to learn from it and incorporate it in
下载 StructurePlacerAPI
下载 StructurePlacerAPI
下载 StructurePlacerAPI
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 StructurePlacerAPI
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 StructurePlacerAPI
下载 StructurePlacerAPI
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 StructurePlacerAPI
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 StructurePlacerAPI
下载 StructurePlacerAPI
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 StructurePlacerAPI
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
已更新至 26.1!
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
一个用于收集生物掉落物的多区块模组。
模组
显示你正在注视的方块的信息。(适用于 Minecraft 1.16+ 的 Hwyla/Waila 分支)
模组
查看物品与配方
模组
在游戏画面角落显示附近世界地形、玩家、生物和实体的地图。可创建路径点,帮助你找到标记过的位置。
模组
使用 JavaScript 编辑配方、添加自定义物品、编写世界事件!
模组
适用于 MC 1.18+ 的 Effekseer 加载器
评论 0
登录后参与评分
还没有评论,来抢沙发吧