ChannelFinalizer(appeng.me.pathfinding.ChannelFinalizer)

基本信息

属性 值
源码 appeng/me/pathfinding/ChannelFinalizer.java
类声明行号 19
实现接口 appeng.api.networking.IGridConnectionVisitor
构造器 无显式构造器(隐式无参)

职责

通道分配的收尾遍历。ad hoc 通道算完之后,本类把临时结果转成 GridConnection 上的正式通道计数。

两个 visitor 回调

方法 行号 作用
visitNode(IGridNode n) 22 节点侧收尾
visitConnection(IGridConnection gcc) 29 边侧收尾 —— 真正干活的地方

visitConnection 调的是 GridConnection.finalizeChannels() (me/GridConnection.java:179-191)。

finalizeChannels() 实际只做一件事:发事件

⚠️ 容易误解 —— 它不写通道数。me/GridConnection.java:179-191:

public void finalizeChannels() {
    if (this.lastUsedChannels != this.usedChannels) {
        this.lastUsedChannels = this.usedChannels;
        if (this.sideA.getInternalGrid() != null) {
            this.sideA.getInternalGrid().postEventTo(this.sideA, EVENT);
        }
        if (this.sideB.getInternalGrid() != null) {
            this.sideB.getInternalGrid().postEventTo(this.sideB, EVENT);
        }
    }
}

即:只有当 usedChannels 相对 lastUsedChannels 真的变了,才向两端各发一次 事件(定向 postEventTo,不是广播 postEvent)。变化被吃掉后 lastUsedChannels 追平,避免重复刷事件。

真正写通道数的是 propagateChannelsUpwards()(me/GridConnection.java:169-176):

if (this.sideB.getControllerRoute() == this) { // Check that we are in B's route
    this.usedChannels = this.sideB.usedChannels;
} else {
    this.usedChannels = 0;
}

只有「本边确实在 B 的控制器路由上」才继承通道数,否则归零。

与 AdHocChannelUpdater 的分工

两个类都实现 IGridConnectionVisitor,但方向相反:

类 源文件 阶段 写什么
AdHocChannelUpdater AdHocChannelUpdater.java:19 临时:算的过程中 GridNode.setAdHocChannels(int)(:30)/ GridConnection.setAdHocChannels(int)(:37)—— 两个回调都是无条件覆写同一个值
ChannelFinalizer ChannelFinalizer.java:19 收尾:算完之后 GridConnection.finalizeChannels()(me/GridConnection.java:179)—— 只在变化时发事件

IPathItem 的两段式契约(me/pathfinding/IPathItem.java:20-38)里 /* USED BY AD HOC PATHING */ 与 /* USED BY CONTROLLER PATHING */ 两个注释块,正是对应这两个类。

生命周期钩子

无。单次使用的算法协作对象,不注册为 cache。

相关条目