首次提交 AutoDig Mod
这个提交包含在:
@@ -0,0 +1,125 @@
|
||||
package autodig.client.miner;
|
||||
|
||||
import autodig.client.bridge.BaritoneBridge;
|
||||
import autodig.client.config.DigConfig;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.registry.RegistryKey;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
/**
|
||||
* 远古残骸自动挖矿流转(下界)。
|
||||
*
|
||||
* <p>流程:主世界 -> 走进最近传送门 -> 进入下界 -> 挖远古残骸 ->
|
||||
* (背包满/挖够)-> 找下界传送门走回主世界。死亡重生的返回逻辑由控制器统一处理,
|
||||
* 本类每次 tick 依据当前所在维度自适应地执行对应阶段。</p>
|
||||
*/
|
||||
public final class DebrisHandler {
|
||||
|
||||
private final BaritoneBridge bridge;
|
||||
|
||||
private enum Stage {
|
||||
/** 在下界从传送门前往主世界(或已在主世界即完成) */
|
||||
GO_COMMIT,
|
||||
/** 在前一个维度前往传送门 */
|
||||
GO_PORTAL,
|
||||
/** 挖远古残骸 */
|
||||
MINE,
|
||||
/** 返回主世界 */
|
||||
RETURN_PORTAL
|
||||
}
|
||||
|
||||
private Stage stage = Stage.GO_COMMIT;
|
||||
private boolean done;
|
||||
/** 派发节流 */
|
||||
private long lastIssue;
|
||||
|
||||
public DebrisHandler(BaritoneBridge bridge) {
|
||||
this.bridge = bridge;
|
||||
}
|
||||
|
||||
/** 任务结束或需要切换任务时归还控制权。 */
|
||||
public void reset() {
|
||||
this.stage = Stage.GO_COMMIT;
|
||||
this.done = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 每帧推进下界挖矿流转。
|
||||
*
|
||||
* @param config 挖矿配置
|
||||
* @return 是否已全部完成(含回到主世界)
|
||||
*/
|
||||
public boolean tick(DigConfig config) {
|
||||
MinecraftClient client = MinecraftClient.getInstance();
|
||||
if (client.player == null || client.world == null) {
|
||||
return this.done;
|
||||
}
|
||||
PlayerEntity player = client.player;
|
||||
RegistryKey<World> dim = client.world.getRegistryKey();
|
||||
boolean inOverworld = dim == World.OVERWORLD;
|
||||
boolean inNether = dim == World.NETHER;
|
||||
|
||||
switch (this.stage) {
|
||||
case GO_COMMIT -> {
|
||||
// 选择去哪个维度
|
||||
if (inNether) {
|
||||
this.stage = Stage.MINE;
|
||||
} else {
|
||||
this.stage = Stage.GO_PORTAL;
|
||||
}
|
||||
}
|
||||
case GO_PORTAL -> {
|
||||
if (inNether) {
|
||||
this.stage = Stage.MINE; // 已通过传送门进入下界
|
||||
} else {
|
||||
this.issue(() -> this.bridge.toNetherPortal());
|
||||
}
|
||||
}
|
||||
case MINE -> {
|
||||
if (!inNether) {
|
||||
// 意外回到主世界,重走流程
|
||||
this.stage = Stage.GO_COMMIT;
|
||||
} else if (this.inventoryFull(player) || this.bridge.isActive() == false) {
|
||||
// 背包满或 Baritone 空闲即为本段结束;若期望回程则前往传送门
|
||||
if (config.debrisReturnHome) {
|
||||
this.stage = Stage.RETURN_PORTAL;
|
||||
} else {
|
||||
this.stage = Stage.GO_COMMIT; // 停在下界挖(不返回)
|
||||
}
|
||||
} else {
|
||||
this.issue(() -> this.bridge.mineByName(config.debrisCount, "ancient_debris"));
|
||||
}
|
||||
}
|
||||
case RETURN_PORTAL -> {
|
||||
if (inOverworld) {
|
||||
this.done = true; // 已回到主世界,全流程完成
|
||||
} else {
|
||||
this.issue(() -> this.bridge.toNetherPortal());
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.done;
|
||||
}
|
||||
|
||||
private void issue(Runnable action) {
|
||||
long now = System.currentTimeMillis();
|
||||
if (now - this.lastIssue > 1000) {
|
||||
action.run();
|
||||
this.lastIssue = now;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean inventoryFull(PlayerEntity player) {
|
||||
for (int i = 0; i < 36; i++) {
|
||||
if (player.getInventory().main.get(i).isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
return this.done;
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户