forked from CleanroomMC/Multiblocked
-
Notifications
You must be signed in to change notification settings - Fork 0
添加MBD兼容支持
Hidocmatn edited this page Aug 2, 2024
·
1 revision
为mod添加MBD兼容非常简单,只要实现了MultiblockCapability和CapabilityProxy类并注册它们就行。 其中最重要的需要实现的方法是IO处理。这里有个ForgeEnergy的例子。
@Override
protected List<Integer> handleRecipeInner(IO io, Recipe recipe, List<Integer> left, boolean simulate) {
IEnergyStorage capability = getCapability();
if (capability == null) return left;
int sum = left.stream().reduce(0, Integer::sum);
if (io == IO.IN) {
sum = sum - capability.extractEnergy(sum, simulate);
} else if (io == IO.OUT) {
sum = sum - capability.receiveEnergy(sum, simulate);
}
return sum <= 0 ? null : Collections.singletonList(sum);
}
此外,为了GUI展示和配置,你还需要实现createContentWidget方法。
@Override
public ContentWidget<? super Integer> createContentWidget() {
return new NumberContentWidget().setContentTexture(new TextTexture("FE", color)).setUnit("FE");
}
最后,你应该覆写并实现hasInnerChaged
方法。它会检查方块内部变化以帮助异步模式下的配方搜索。一定要确保不要在这里整出竞态条件。
int stored = -1;
boolean canExtract = false;
boolean canReceive = false;
@Override
protected boolean hasInnerChanged() {
IEnergyStorage capability = getCapability();
if (capability == null) return false;
if (stored == capability.getEnergyStored() && canExtract == capability.canExtract() && canReceive == capability.canReceive()) {
return false;
}
canExtract = capability.canExtract();
canReceive = capability.canReceive();
stored = capability.getEnergyStored();
return true;
}
如果你想要MBD除了能访问一种capability外,还能创建有此种capability的机器,你需要实现CapabilityTrait
。
查看Java代码以了解更多细节。