RedstoneRegions
为 Folia 提供按区块切换红石引擎的功能。标记一个区块后,其导线更新速度提升 2-14 倍;其他一切保持原版行为。
如果你运行过大型服务器,你就知道其中的权衡。你可以在 paper-world.yml 中设置 redstone-implementation: alternate-current 从而获得全局的巨大加速。然后,某个玩家从 2019 年就开始用的活塞 BUD(瞬间开关)农场会悄悄停止工作,而你要花上一周时间才搞清楚原因。或者你保持所有东西都是原版的,眼睁睁看着一块 64×64 的灰尘地板吞噬你的 TPS 预算。这是一个全有或全无的开关,而“全有”和“全无”都不是你真正想要的。
所以这个插件改为按区块进行切换:
/redstone-region set alternate-current ← 仅当前区块
/redstone-region fill 8 alternate-current ← 以你为中心 17×17 个区块
/redstone-region selection eigencraft ← 使用你的 WorldEdit 选择区域
/redstone-region undo ← 哦等等,撤回操作
选择结果会持久保存(存储在区块 PDC 中),重启后依然有效,并且支持热交换,无需重启或重新加载区块。
四种引擎
| 模式 |
速度 |
备注 |
| vanilla |
1× |
默认值。一切行为如同正常 Minecraft。 |
| alternate-current |
2-14× |
Space Walker 的算法。有两个已知的边缘情况,见下文。 |
| eigencraft |
3-5× |
几乎没有边缘情况。适合活塞/观察者机械装置。 |
| disabled |
冻结 |
导线永不更新。用于你想要其处于惰性状态的存档建筑。 |
性能数据
我的基准测试是一个 32×32 的灰尘网格,由一个拉杆控制切换:
| 运行状态 |
Vanilla |
AC |
|
| Cold (冷启动) |
234 ms |
22 ms |
10.6× |
| Warm JIT (JIT 预热) |
41 ms |
14 ms |
2.9× |
| Pre-warmed (预预热) |
71 ms |
5 ms |
14.2× |
诚实的结论是:“14×” 发生在原版表现最差的时候,稳态下大约是 3×。无论哪种情况,当原版数值超过 50 ms 时都算是巨大的优化。在 64×64 个区块的灰尘网格中,原版会导致单 tick 超时 6 倍;AC 则保持在 2 以下。
当你不使用它时的成本:每次导线更新都会经过一个按区块分发的调度器,这是一个 StampedLock 乐观读加上一个 switch 语句,耗时约 50 ns。如果连这也让你介意,可以使用 timing.mode: sample(采样率为 10 时约 5 ns)或 off(完全无开销)。
包含内容
核心功能是按需切换引擎。围绕核心功能,主要是我在实际运行过程中需要的其他特性:
- WorldEdit 选择区域支持,让你可以一键将整间机器房从
//pos1 //pos2 切换
- 一个 BlueMap 叠加层,在非原版区块上绘制彩色矩形,事实证明这是发现“等等,为什么那个区块开了 AC”的最快方法
- 可选的自动扫描器,当区块的原版红石延迟超过阈值时自动将其切换为 AC
[ac] 告示牌,让受信任的玩家可以在不骚扰工作人员的情况下自行切换他们的区块(需权限,且限制半径)
/redstone-region profile 用于实时查看每个区块相对于 50 ms 预算的状态
/redstone-region why x y z,通过遍历六个相邻方块来解释导线的电力等级。听起来很傻,但在调试时节省了大量时间
/redstone-region undo 用于撤销上一次批量操作,无论是 set、fill、selection、sign 还是自动扫描器触发的
- 审计日志(JSONL 格式,每日轮转,每次切换记录执行者 + UUID + 原因)以及可选的 Discord Webhook,因为在真实服务器上人们总会问是谁动了他们的区块
- PlaceholderAPI 占位符、聊天中点击传送至区块坐标、配置热重载
- 包含英语和法语;如需其他语言,请放入
lang/<code>.yml
安装
将 Jar 文件放入 plugins/ 目录,重启服务器,站在任意位置,执行 /redstone-region set alternate-current。这就是全部。
要求:Folia 1.21.11 build 6 或更高版本(Luminol 和其他分支也可用),Java 21。BlueMap 5.16+、PlaceholderAPI 2.11.7+ 和 WorldEdit 7.3.19+ 均为可选依赖。对于 WorldEdit,建议使用经过 Folia 补丁的版本。
它真的能像原版一样工作吗?
这是我曾经最担心的部分,因此内置了一个服务器内测试工具。当前测试结果:14/14 通过:
- 在 60+ 个 tick 内对 8 种机械装置进行了逐字节与原版的比对:灰尘线、16×16 网格、4-tick 中继器时钟、AND 门、比较器减法、火把梯、火把反相器、灰尘之字形
- 在 30 秒的压力测试中进行了 596 次切换,零异常
- 在两个不同的 Folia 区域中同时运行了 780 + 780 次切换,零竞态条件,零线程检查失败(这是 Paper 自带的 AC 在 Folia 下目前无法通过的用例)
- 确认有 655 次分发进入 AC 引擎,说明路由没有静默回退
工作原理简述
自 1.21.2 起,Mojang 暴露了 RedstoneWireBlock.evaluator 字段。在启动时,我们将其替换为一个调度器,该调度器检查区块的标志并将更新路由到正确的引擎。AC 引擎是 Space Walker 算法的移植版,修改为使用每线程 WireHandlers。Paper 自带的 AC 共享一个处理器处理所有内容,一旦 Folia 并行运行区域,这就存在潜在的竞态条件。
带有图表的详细说明:docs/ARCHITECTURE.md。
坦诚说说边缘情况
有两种模式在 AC 下表现不同并可能导致故障:
- 依赖导线在中途电力等级下的自我形状更新的活塞 BUD
- 计数导线上电力等级转换的观察者
/redstone-region check 会扫描一个区块,并在你切换之前警告这两种模式。如果发现问题,要么保留该区块为原版,要么使用 eigencraft,后者没有这两个问题且仍然快 3-5 倍。这种组合几乎涵盖了我遇到的所有机械装置。
链接
许可证
MIT。内置的 Alternate Current 引擎也是 MIT 协议。这是 Space Walker 的作品,许可证位于 ac/LICENSE。
RedstoneRegions
Per-chunk redstone engine switching for Folia. Mark a chunk and its wire ticks 2-14× faster; everything else stays vanilla.
If you've run a big server you know the tradeoff. You can set redstone-implementation: alternate-current in paper-world.yml and get a huge speedup globally. Then some player's piston BUD farm from 2019 quietly stops working and you spend a week figuring out why. Or you keep everything vanilla and watch a 64×64 dust floor eat your tick budget. It's an all-or-nothing switch, and neither "all" nor "nothing" is what you actually want.
So this makes it per-chunk instead:
/redstone-region set alternate-current ← this chunk only
/redstone-region fill 8 alternate-current ← 17×17 chunks around you
/redstone-region selection eigencraft ← your WorldEdit selection
/redstone-region undo ← oh wait, no
The choice persists (stored in chunk PDC), survives restarts, and hot-swaps live, no restart or chunk reload needed.
The four engines
| Mode |
Speed |
Notes |
| vanilla |
1× |
the default. Everything behaves like normal Minecraft. |
| alternate-current |
2-14× |
Space Walker's algorithm. Two known edge cases, see below. |
| eigencraft |
3-5× |
almost no edge cases. Good for piston/observer machinery. |
| disabled |
frozen |
wire never updates. For archived builds you want inert. |
Numbers
My benchmark is a 32×32 dust grid, toggled from one lever:
| Run |
Vanilla |
AC |
|
| Cold |
234 ms |
22 ms |
10.6× |
| Warm JIT |
41 ms |
14 ms |
2.9× |
| Pre-warmed |
71 ms |
5 ms |
14.2× |
The honest takeaway: the "14×" happens when vanilla is at its worst, steady-state is more like 3×. Either is a lot when the vanilla number blows a 50 ms tick. At 64×64 chunks of dust, vanilla overshoots a tick by 6×; AC stays under 2.
Cost when you're not using it: every wire update passes through a per-chunk dispatcher, which is a StampedLock optimistic read plus a switch, about 50 ns. If even that bothers you there's timing.mode: sample (~5 ns at sample-rate 10) or off (nothing at all).
What's in the box
The core is the per-chunk switching. Around that, mostly things I ended up needing while running it:
- WorldEdit selection support, so you can flip a whole machine room from
//pos1 //pos2
- a BlueMap overlay that draws colored rectangles over non-vanilla chunks, which turned out to be the fastest way to spot "wait, why is that chunk on AC"
- an optional auto-scanner that flips chunks to AC when their vanilla redstone time crosses a threshold
[ac] signs so trusted players can flip their own chunks without bugging staff (permission-gated, radius-capped)
/redstone-region profile for a live per-chunk readout against the 50 ms budget
/redstone-region why x y z, which explains a wire's power level by walking its six neighbors. Sounds silly, saves so much time debugging
/redstone-region undo for the last batch, whatever caused it (set, fill, selection, sign, auto-scanner)
- an audit log (JSONL, rolled daily, actor + UUID + reason on every flip) and an optional Discord webhook, because on a real server people will ask who touched their chunk
- PlaceholderAPI placeholders, click-to-teleport chunk coordinates in chat, hot config reload
- English and French included; drop a
lang/<code>.yml for anything else
Install
Jar in plugins/, restart, stand somewhere, /redstone-region set alternate-current. That's the whole thing.
Requirements: Folia 1.21.11 build 6 or later (Luminol and other forks work), Java 21. BlueMap 5.16+, PlaceholderAPI 2.11.7+ and WorldEdit 7.3.19+ are all optional. For WorldEdit, prefer a Folia-patched build.
Does it actually behave like vanilla?
This was the part I was paranoid about, so there's an in-server test harness. Current results, 14/14:
- byte-for-byte parity with vanilla across 8 contraptions over 60+ ticks each: dust line, 16×16 grid, 4-tick repeater clock, AND gate, comparator subtraction, torch ladder, torch inverter, dust zigzag
- 596 toggles in a 30-second stress run, zero exceptions
- 780 + 780 toggles running in two different Folia regions at the same time, zero races, zero thread-check failures (this is the case Paper's bundled AC can't currently pass under Folia)
- 655 dispatches into the AC engine confirmed by counter, so the routing isn't silently falling back
How it works, short version
As of 1.21.2 Mojang exposes a RedstoneWireBlock.evaluator field. At boot we swap it for a dispatcher that checks the chunk's flag and routes the update to the right engine. The AC engine is a port of Space Walker's algorithm, modified to use per-thread WireHandlers. The bundled AC in Paper shares one handler across everything, which is a latent race once Folia runs regions in parallel.
Longer version with diagrams: docs/ARCHITECTURE.md.
The edge cases, honestly
Two patterns behave differently under AC and can break:
- piston BUDs that depend on the wire's self-shape-update at intermediate power levels
- observers counting power-level transitions on a wire
/redstone-region check scans a chunk and warns about both patterns before you flip it. If it finds something, either leave that chunk vanilla or use eigencraft, which has neither problem and is still 3-5× faster. That combination covers basically every machine I've come across.
Links
License
MIT. The bundled Alternate Current engine is also MIT. That's Space Walker's work, license in ac/LICENSE.
评论 0
登录后参与评分
还没有评论,来抢沙发吧