PowerfulJS
基于 1.20.1 重写,PowerfulJS 现在确实更强大!
PowerfulJS 1.21 现在对 capabilities(能力)的处理更好,完全支持 ProbeJS,并且性能大幅提升!
如需查看旧版 PowerfulJS 文档,请访问 GitHub 仓库
能力系统简介
NeoForge 的 capability(能力)机制统一了物品、方块、实体等的实现方式,以处理共同的行为。因此,任何实现了 item handler(物品处理器)能力的方块都能接收或向漏斗提供物品;任何实现了 fluid handler(流体处理器)能力的物品都能被 Mekanism 流体罐填充。
PowerfulJS 使得整合包制作者能够向这些对象添加能力。通过附加能力,你可以让熔炉拥有 FE 或流体存储功能,或者让电池外壳在填充后成为电池……可能性是无限的。
快速使用
本节介绍如何使用 PowerfulJS 实现一些简单的功能。代码在安装了 PowerfulJS 的情况下即可运行,但建议安装 ProbeJS,以便在 VSCode 中获得 IDE 支持。
1. 添加能力
能力可以附加到三种类型的对象上:item(物品)、block(方块)和 entity(实体)。还有一种第四类型是 block entity(方块实体),但它包含在 block 能力中。
能力注册在游戏启动时触发,一个简单的添加能力的示例如下:
PowerfulEvents.registerCapabilities(event => {
// 添加一个具有恒定 FE 供应的能力
//
// 这样红石块现在可以持续产生 100FE/t,
// 但是你需要一个提取器(例如带有提取模式的 MEK 线缆)
// 才能获取能量,因为方块本身不会向周围分发能量
event.registerBlock('powerfuljs:constant_energy', {
maxExtract: 100,
maxReceive: 0,
}, "redstone_block")
// 添加一个具有固定容量并定义最大 I/O 的罐体
event.registerBlockEntity('powerfuljs:fixed_storage_fluid', {
capacity: 2000,
maxExtract: 250,
maxReceive: 250,
}, 'minecraft:furnace')
// 同样适用于 FE 能量存储
event.registerBlockEntity('powerfuljs:fixed_storage_fe', {
maxExtract: 1000,
maxReceive: 1000,
capacity: 10000,
}, 'minecraft:furnace')
// 添加一个具有恒定流体供应的流体能力
//
// 只需连接管道并提取无限的岩浆!
// 过于强大了!!!!!
event.registerBlock('powerfuljs:constant_fluid', {
content: Fluid.of('lava', 1000),
maxReceive: 0
}, 'minecraft:lava')
// ...所以你可以通过使其仅在特定条件下可用来限制它,
// 比如这里我们检查它是否是源方块且处于“nether”生物群系中,即 nether_waste。
//
// 然而,如果方块在工作时会改变状态,你不应该依赖这个验证器,比如熔炉
// 出于性能原因,该能力将为许多机器重用,并且由于能力已提供,
// `validate` 将不会被调用
.validate((blockContext, info) => {
return blockContext.state().fluidState.source &&
blockContext.level().getBiome(blockContext.pos())
.registeredName
.includes("nether")
})
// 还使 `netherrack`(下界岩)和 `soul sand`(灵魂沙)能够被岩浆填充
//
// 填充后,它们会变成 `magma block`(岩浆块)
//
// 不同的能力构建器之间存在一些差异,
// 你可以使用 ProbeJS 找出每个构建器支持哪些内容
event.registerItem('powerfuljs:fixed_storage_fluid', {
capacity: 1000,
maxReceive: 20,
maxExtract: 20,
validator: "minecraft:lava",
changeItemWhen: {
full: [
{ fluid: "minecraft:lava", item: "minecraft:magma_block" }
]
}
}, 'netherrack', 'soul_sand')
})
2. 拦截 Tick
为了让附加在方块上的能力更有用,PowerfulJS 还实现了一种方法来拦截方块的原始 Tick 逻辑,以及强制不 Tick 的方块进行 Tick。
一个名为 RuleSet(规则集)的系统用于定义平衡性能与灵活性的实际逻辑。理论上讲,使用此系统实现的任何逻辑都具有与原生 Java 相当的性能,尽管这在很大程度上取决于你的实现方式。
Tick 拦截是在服务器脚本加载时注册的,因此你也可以重新加载服务器脚本来刷新行为。然而,对于任何已经注入了逻辑的方块,你需要破坏并替换它们以刷新 Tick 器。
示例如下所示:
PowerfulEvents.interceptTicking(event => {
let burningFurnaceOfAnyDirection = [
"minecraft:furnace[lit=true, facing=west]",
"minecraft:furnace[lit=true, facing=east]",
"minecraft:furnace[lit=true, facing=north]",
"minecraft:furnace[lit=true, facing=south]"
]
event.intercept('minecraft:furnace', () => [
// 我们检测熔炉是否正在运行
Rules.matchBlock(burningFurnaceOfAnyDirection)
// 如果熔炉未运行,则启用它
// 以便它能正常检测配方
.negate()
// 如果熔炉正在运行
.or(
// 如果熔炉中有 1000mB 的岩浆,则保持其运行状态
Rules.hasFluid(Fluid.lava(1000), null)
// 同时消耗 1 mB 的岩浆并产生 5 FE
.effect(Effects.drainFluid(Fluid.lava(1), null))
.effect(Effects.fillEnergy(5, null))
)
// 因此,如果熔炉未运行,或者熔炉在流体支持下正在运行,
// 我们启用熔炉逻辑(
// 前面的规则评估为 true),否则禁用它(评估为 false)
.effect(Effects.toggleEnable())
])
// 上述逻辑将使熔炉至少需要 1000mB 的岩浆才能运行,
// 并以每秒消耗 1mB 岩浆为代价,每秒产生 5FE
// 箱子本身不会进行刻更新,我们通过空逻辑强制箱子开始刻更新,
// 这样我们稍后可以通过拦截添加自己的逻辑
event.forceTicker('chest', true)
event.intercept('chest', () => [
// 每 20 刻执行一次
Rules.every(20).effect(
// 奖励一个曲奇!
Effects.insertItem('cookie', null)
)
])
})
PowerfulJS
Rewritten from 1.20.1, PowerfulJS is now indeed powerful!
PowerfulJS 1.21 now has better handling for capabilities, full ProbeJS support, and way better performance!
For old PowerfulJS docs, check out the GitHub repo
Introduction to the Capability System
Neoforge's capability unified the implementation of items, blocks, entities... to handle a common behavior. So, any block that implements item handler capability will be able to receive or provide items to a hopper, any items that implements fluid handler capability will be able to be filled by a Mekanism fluid tank.
PowerfulJS makes it possible for modpack makers to add capabilities to these objects. So, with capabilities attached, you can make a furnace to have FE or fluid storage, or a battery hull that can be filled to make a battery... The possibility is infinite.
Quick usage
This section introduces you how to use PowerfulJS to implement some easy stuffs, the code can run with PowerfulJS installed, but it is always advised to install ProbeJS so you can get IDE support in VSCode.
1. Adding capabilities
Capabilities can be attached to 3 types of objects: item, block and entity, there's also a fourth type which is block entity, but it's inclued in block capabilities.
Capability registration is fired at startup, a simple example to add capabilities will be:
PowerfulEvents.registerCapabilities(event => {
// Adds a capability that has constant FE supply
//
// So the redstone block now can produce 100FE/t constantly,
// however, you need an extractor (like MEK cable with
// extract mode) in order to get the energy, as the block
// itself won't distribute energy to surroundings
event.registerBlock('powerfuljs:constant_energy', {
maxExtract: 100,
maxReceive: 0,
}, "redstone_block")
// Adds a tank that has a fixed capacity with defined
// max I/O
event.registerBlockEntity('powerfuljs:fixed_storage_fluid', {
capacity: 2000,
maxExtract: 250,
maxReceive: 250,
}, 'minecraft:furnace')
// and so does the FE energy storage
event.registerBlockEntity('powerfuljs:fixed_storage_fe', {
maxExtract: 1000,
maxReceive: 1000,
capacity: 10000,
}, 'minecraft:furnace')
// Adds a fluid capability that has constant fluid supply
//
// Just connect a pipe and extract infinite lava!
// OVERPOWERED!!!!!
event.registerBlock('powerfuljs:constant_fluid', {
content: Fluid.of('lava', 1000),
maxReceive: 0
}, 'minecraft:lava')
// ...so you can limit it by making it only available
// at certain condition, like here we check if it's a
// source block and in a "nether" biome, which is the
// nether_waste.
//
// However, you shall not rely on this validator if the
// block can change its state while working, like furnace
// The capability will be reused for many machines for
// performance reason, and validate will not be called
// as the capability is already provided
.validate((blockContext, info) => {
return blockContext.state().fluidState.source &&
blockContext.level().getBiome(blockContext.pos())
.registeredName
.includes("nether")
})
// Also make the netherrack and soul sand able to be filled
// with lava
//
// When filled, they are turned into magma block
//
// There are some differences in different capability builders,
// you can use ProbeJS to find out what kind of stuffs are
// supported by each builder
event.registerItem('powerfuljs:fixed_storage_fluid', {
capacity: 1000,
maxReceive: 20,
maxExtract: 20,
validator: "minecraft:lava",
changeItemWhen: {
full: [
{ fluid: "minecraft:lava", item: "minecraft:magma_block" }
]
}
}, 'netherrack', 'soul_sand')
})
2. Intercepting ticking
To make the capabilities attached to the block more useful, PowerfulJS also implemented a way to intercept the original ticking logic of a block, as well as forcing a block that does not tick to tick.
A system called RuleSet is used to define the actual logic to balance the performance with flexibility, theoretically speaking, implementing any logic with this system has comparable performance to native Java, though it still highly depends on your way to implement.
The ticking interception is registered when server script is loaded, so you can also reload the server script to refresh the behavior. However, for any block that already has injected logic, you need to break and replace them to refresh the ticker.
The example is shown as below:
PowerfulEvents.interceptTicking(event => {
let burningFurnaceOfAnyDirection = [
"minecraft:furnace[lit=true, facing=west]",
"minecraft:furnace[lit=true, facing=east]",
"minecraft:furnace[lit=true, facing=north]",
"minecraft:furnace[lit=true, facing=south]"
]
event.intercept('minecraft:furnace', () => [
// We test for a running furnace
Rules.matchBlock(burningFurnaceOfAnyDirection)
// We enable the furance if it's not running
// so it can detect recipes normally
.negate()
// If it's running
.or(
// If it has 1000mB of lava, then keep it running
Rules.hasFluid(Fluid.lava(1000), null)
// And we also drain 1 mB of lava and produce 5 FE
.effect(Effects.drainFluid(Fluid.lava(1), null))
.effect(Effects.fillEnergy(5, null))
)
// So, if the furnace is not running, or the furnace is
// running with fluid, we enable the furnace logic (
// previous rule evaluates to true), otherwise we disable
// it (evaluates to false)
.effect(Effects.toggleEnable())
])
// The logic described above will make the furnace requires at
// least 1000mB of lava to run, and produce 5FE/t at the cost
// of 1mB/t lava
// The chest does not tick on its own, we force the chest to
// start ticking with empty logic, so we add our logic by
// interception later
event.forceTicker('chest', true)
event.intercept('chest', () => [
// Every 20 ticks
Rules.every(20).effect(
// Have a cookie!
Effects.insertItem('cookie', null)
)
])
})
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PowerfulJS
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
- 增加了对 Farmer's Delight 和部分原版方块实体的拦截支持。
- 增加了对 Create 方块实体的拦截支持。
- 增加了对 Mekanism、CC&A、CEI 的拦截支持。
- 增加了基于区块的数据存储功能,这意味着没有方块实体的方块也可以持有数据。
- 新增了一个实用方法,让任何方块实体在放置时记录其所有者。
- 增加了一条用于检测红石能量的规则。
- 增加了一条用于测试附加物的规则。
- 增加了用于测试游戏难度和 KubeJS 包模式的规则。
null可用于方向匹配,以表示无方向的平面。- 规则现在同时在客户端和服务端运行,以获得更好的视觉效果。
- 新增了非规则方法,用于停止、启动或减缓/加速方块实体的刻数(ticking)。
- 刻率调整现在也适用于未被拦截的方块实体。
- 更改了事件名称以减少混淆。
- 移除了全局绑定
Rules和Effects以防止潜在冲突,请改用event.effects和event.rules。 - 一个方块实体现在可以拥有多组规则,它们将独立且按顺序执行。
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
一个用于收集生物掉落物的多区块模组。
模组
让你能够在世界中的任何位置定位结构。
模组
? 使用命令保存和加载玩家或他人的背包/套装。
模组
一个基于构建器的 Minecraft 配置库!
模组
一款轻量级模组,允许服务器管理员对抗透视作弊者。
模组
AE2:一款流行的自动化与存储模组
评论 0
登录后参与评分
还没有评论,来抢沙发吧