Create 自动化 KubeJS 钩子
KJSCAuto 允许 KubeJS 脚本修改 Create 中的关键自动化行为。
本模组将多个 Create 操作暴露为脚本事件,使整合包制作者能够拦截并修改这些行为,而无需编写 Java 模组。
配方修改功能有意未包含在内,因为该功能已由 KubeJS Create 提供。
目前支持的钩子包括:
- 发射器(Deployer)的物品/工具使用
- 水槽(Basin)配方操作
- 钻头与锯的方块破坏
- 机械结构(Contraption)拆解
这些事件允许脚本执行以下操作:
- 调整发射器上的工具耐久消耗
- 修改水槽配方的输入和输出
- 拦截或替换由机器或机械结构破坏时掉落的方块
选择这些钩子是为了解决我在 KubeJS 社区中经常看到的几个常见自动化控制问题。
如果您认为还有其他 Create 行为适合添加脚本钩子,欢迎在讨论区提出建议。
在 1.21.1 版本中引入了一些破坏性更新,因此以下示例在该版本中无法正常运行。请查看 wiki 获取 1.21.1 版本的示例。
示例脚本:(全部放入服务器脚本中。)
部署工具时不消耗耐久度
当在传送带/仓库下方放置一个钻石块时,部署器会获得一种特殊效果:砂纸不再损失耐久度,且打磨结果变为钻石。
KJSCAutoEvents.deployerUse(event => {
var {block, outputs, heldItem} = event
if(heldItem != "create:sand_paper") return //仅对砂纸有效
if(!block.offset(0, -3, 0).blockState.is(Blocks.DIAMOND_BLOCK)) return; //仅当传送带/仓库(y = -2)下方有钻石块时有效
event.setDamage(0) //将耐久度消耗设为 0
while(outputs.length > 0) outputs.remove(0) //移除所有原始输出
outputs.add(0, Item.of("diamond")) //添加钻石
})
混合羊毛与红色染料且不消耗染料
这是为了实现某些“催化剂”配方。
技术说明: 在 Create 的 Basin(水车盆)中,输出物品与输入/原料库存是分开存储的,因此作为输出的物品不会被当作下一次操作的原料。
KJSCAutoEvents.basinOperation(event => {
var {basin, block, fluidOutputs, heatLevel, outputs, recipe} = event;
if(recipe.getId().toString() == "minecraft:dye_red_wool"){
event.addInput("red_dye")
}
})
钻头的条件性丝触效果
当满足特定条件时,钻头可以获得类似“精准采集”的效果。如果在矿石下方放置紫水晶块,钻头将直接掉落矿石方块本身,而不是常规掉落物。
//适用于固定式钻头
KJSCAutoEvents.blockDestroy(event => {
const { level, targetPos, targetBlock } = event
const below = level.getBlockState(targetPos.below())
if (!below.is("minecraft:amethyst_block")) return
if (!(targetBlock.blockState.is("minecraft:diamond_ore") || targetBlock.blockState.is("minecraft:emerald_ore")))return
targetBlock.popItem(targetBlock.blockState.block.asItem())
level.destroyBlock(targetPos, false)
event.cancel()
})
//适用于安装在机械结构上的钻头
ContraptionEvents.afterBlockDestroy(event => {
const { level, targetPos, targetBlock, drops } = event
const below = level.getBlockState(targetPos.below())
if (!below.is("minecraft:amethyst_block")) return
if (!(targetBlock.blockState.is("minecraft:diamond_ore") || targetBlock.blockState.is("minecraft:emerald_ore")))return
while(drops.length > 0) { drops.remove(0) }
drops.add(Item.of(targetBlock.blockState.block.asItem()))
})
机械结构的保护地面
放置在灵魂土上的方块不能被机械结构破坏。
ContraptionEvents.beforeBlockDestroy(event => {
const { level, targetPos } = event
const below = level.getBlockState(targetPos.below())
if (!below.is("minecraft:soul_soil")) return
event.cancel() // 防止机械结构破坏该方块
})
禁用拆解功能
当拆解范围内存在海晶灯时,阻止机械结构进行拆解。
ContraptionEvents.beforeDisassemble(event => {
console.log("disassemble")
let shouldCancel = event.testTargetBlock(block => {
return block == "minecraft:sea_lantern"
})
if(shouldCancel) event.cancel()
})
支持
如果您想支持我的模组开发工作,包括维护、更新和兼容性适配,可以通过 Patreon 进行赞助:
KubeJS Hooks for Create Automation
KJSCAuto allows KubeJS scripts to modify key automation behaviors in Create.
This mod exposes several Create actions as script events so pack makers can intercept and modify them without writing Java mods.
Recipe modification is intentionally not included, as that functionality is already provided by KubeJS Create.
Currently supported hooks include:
- Deployer item/tool usage
- Basin recipe operations
- Block breaking by drills and saws
- Contraption disassembly
These events allow scripts to:
- adjust tool durability usage on Deployer
- modify Basin recipe inputs and outputs
- intercept or replace block drops when destroyed by machines or contraptions
These hooks were selected to address several common automation control issues I often see discussed in the KubeJS community.
If you feel there are other Create behaviors that would benefit from script hooks, feel free to suggest them in the discussion forum.
Several breaking changes are introduced in 1.21.1, so the following example would not work well in that version. Check the wiki for 1.21.1 example.
Example Script: (All goes into server script.)
Deploying tool without consuming durability.
When a diamond block is placed beneath the belt/depot, the Deployer gains a special effect: sandpaper no longer loses durability, and the sanding result turns into a diamond.
KJSCAutoEvents.deployerUse(event => {
var {block, outputs, heldItem} = event
if(heldItem != "create:sand_paper") return //only works for sand paper
if(!block.offset(0, -3, 0).blockState.is(Blocks.DIAMOND_BLOCK)) return; //only works when the belt/deposit (y = -2) has a diamond block below.
event.setDamage(0) //set the durability usage to 0
while(outputs.length > 0) outputs.remove(0) //remove all original outputs
outputs.add(0, Item.of("diamond")) //add diamond
})
Mixing wool and red dye while not consuming the dye
This is to enable some "catalyst" recipe.
Technical note: In Create’s Basin, outputs are kept separate from the input/ingredient inventory, so an item returned as an output will not be treated as an ingredient for the next operation.
KJSCAutoEvents.basinOperation(event => {
var {basin, block, fluidOutputs, heatLevel, outputs, recipe} = event;
if(recipe.getId().toString() == "minecraft:dye_red_wool"){
event.addInput("red_dye")
}
})
Conditional Silk Touch for Drills
Drills can gain a Silk Touch-like effect when a certain condition is met. If an amethyst block is placed beneath the ore, the drill will drop the ore block itself instead of the normal drops.
//This works for stationary drill.
KJSCAutoEvents.blockDestroy(event => {
const { level, targetPos, targetBlock } = event
const below = level.getBlockState(targetPos.below())
if (!below.is("minecraft:amethyst_block")) return
if (!(targetBlock.blockState.is("minecraft:diamond_ore") || targetBlock.blockState.is("minecraft:emerald_ore")))return
targetBlock.popItem(targetBlock.blockState.block.asItem())
level.destroyBlock(targetPos, false)
event.cancel()
})
//This works for drill on contraption.
ContraptionEvents.afterBlockDestroy(event => {
const { level, targetPos, targetBlock, drops } = event
const below = level.getBlockState(targetPos.below())
if (!below.is("minecraft:amethyst_block")) return
if (!(targetBlock.blockState.is("minecraft:diamond_ore") || targetBlock.blockState.is("minecraft:emerald_ore")))return
while(drops.length > 0) { drops.remove(0) }
drops.add(Item.of(targetBlock.blockState.block.asItem()))
})
Protective Ground for Contraptions
Blocks placed on soul soil cannot be broken contraption machines.
ContraptionEvents.beforeBlockDestroy(event => {
const { level, targetPos } = event
const below = level.getBlockState(targetPos.below())
if (!below.is("minecraft:soul_soil")) return
event.cancel() // prevent the contraption from breaking the block
})
Cancel Disassembly
Prevent contraption disassembly when a sea lantern is in the disassembly range.
ContraptionEvents.beforeDisassemble(event => {
console.log("disassemble")
let shouldCancel = event.testTargetBlock(block => {
return block == "minecraft:sea_lantern"
})
if(shouldCancel) event.cancel()
})
Support
If you'd like to support my modding work, including maintenance, updates, and compatibility work, Patreon is available here:
评论 0
登录后参与评分
还没有评论,来抢沙发吧