首次提交 AutoDig Mod

这个提交包含在:
lyizai
2026-08-20 21:04:27 +08:00
当前提交 95c4e97db0
共修改 26 个文件,包含 1851 行新增和 0 行删除
@@ -0,0 +1,174 @@
package autodig.client.gui;
import autodig.client.AutoDigModClient;
import autodig.client.config.DigConfig;
import autodig.client.config.MineTask;
import autodig.client.controller.DigController;
import java.util.function.Consumer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.CheckboxWidget;
import net.minecraft.text.Text;
/**
* AutoDig 游戏内 GUI。
*
* <p>用于选择挖什么(钻石 / 远古残骸),并快捷设置:保命优先、优先绕开岩浆、
* 自动切换工具、自动进食、杀戮光环、重生后回死亡点、挖完回主世界,
* 以及钻石层数 / 巷道长度 / 残骸数量等参数。界面中的改动在"开始/停止/关闭"时
* 同步到 mod 全局配置并落盘。</p>
*/
public final class AutoDigGui extends Screen {
private final AutoDigModClient mod;
private final DigConfig config;
private final DigController controller;
// 动态数值按钮(点击 +/- 后刷新显示)
private ButtonWidget levelBtn;
private ButtonWidget tunnelBtn;
private ButtonWidget debrisBtn;
public AutoDigGui(AutoDigModClient mod) {
super(Text.literal("AutoDig"));
this.mod = mod;
this.config = mod.config().copy();
this.controller = mod.controller();
}
@Override
protected void init() {
this.clearChildren();
int colX = 26;
int x = colX;
int y = 34;
// ===== 任务选择 =====
this.taskButton("空闲", MineTask.NONE, x, y);
this.taskButton("钻石(鱼骨)", MineTask.DIAMOND, x + 110, y);
this.taskButton("远古残骸", MineTask.ANCIENT_DEBRIS, x + 220, y);
// ===== 开关设置 =====
y += 30;
this.check("保命优先", colX, y, () -> this.config.survivalFirst, v -> this.config.survivalFirst = v);
this.check("优先绕开岩浆", colX + 150, y, () -> this.config.avoidLava, v -> this.config.avoidLava = v);
y += 24;
this.check("自动进食", colX, y, () -> this.config.autoEat, v -> this.config.autoEat = v);
this.check("自动切换工具", colX + 150, y, () -> this.config.autoTool, v -> this.config.autoTool = v);
y += 24;
this.check("杀戮光环", colX, y, () -> this.config.killAura, v -> this.config.killAura = v);
this.check("重生后回死亡点", colX + 150, y, () -> this.config.respawnReturn, v -> this.config.respawnReturn = v);
y += 24;
this.check("挖完回主世界", colX, y, () -> this.config.debrisReturnHome, v -> this.config.debrisReturnHome = v);
this.check("避难时绝杀不冒进", colX + 150, y, () -> this.config.killAuraSafe, v -> this.config.killAuraSafe = v);
// ===== 数值参数 =====
y += 32;
this.levelBtn = this.numberRow("钻石层(Y)", colX, y, () -> this.config.diamondLevel, v -> this.config.diamondLevel = v);
y += 24;
this.tunnelBtn = this.numberRow("巷道长度", colX, y, () -> this.config.tunnelLength, v -> this.config.tunnelLength = v);
y += 24;
this.debrisBtn = this.numberRow("残骸数量", colX, y, () -> this.config.debrisCount, v -> this.config.debrisCount = v);
// ===== 底部操作按钮 =====
y += 40;
ButtonWidget start = this.textButton("开始 (保存并运行)", b -> this.start(), x, y, 140);
ButtonWidget stop = this.textButton("停止", b -> this.stop(), x + 146, y, 90);
ButtonWidget saveClose = this.textButton("仅关闭", b -> this.close(), x + 242, y, 80);
this.addDrawableChild(start);
this.addDrawableChild(stop);
this.addDrawableChild(saveClose);
}
private ButtonWidget taskButton(String label, MineTask task, int x, int y) {
ButtonWidget b = this.textButton(label, button -> {
this.config.task = task;
this.init(); // 刷新选中高亮
}, x, y, 104);
this.addDrawableChild(b);
return b;
}
private void check(String label, int x, int y, java.util.function.BooleanSupplier getter, Consumer<Boolean> setter) {
CheckboxWidget box = CheckboxWidget.builder(Text.literal(label), this.textRenderer)
.pos(x, y).checked(getter.getAsBoolean())
.callback((b, value) -> setter.accept(value)).build();
this.addDrawableChild(box);
}
private ButtonWidget numberRow(String label, int x, int y, java.util.function.IntSupplier getter, Consumer<Integer> setter) {
this.addDrawableChild(ButtonWidget.builder(Text.literal("-"), b -> {
setter.accept(getter.getAsInt() - 1);
this.refreshNumberButtons();
}).dimensions(x, y, 20, 20).build());
int valueX = x + 24;
int valueW = 72;
ButtonWidget value = ButtonWidget.builder(Text.literal(label + " : " + getter.getAsInt()), b -> {
}).dimensions(valueX, y, valueW, 20).build();
value.active = false; // 纯展示
this.addDrawableChild(value);
this.addDrawableChild(ButtonWidget.builder(Text.literal("+"), b -> {
setter.accept(getter.getAsInt() + 1);
this.refreshNumberButtons();
}).dimensions(valueX + valueW + 4, y, 20, 20).build());
return value;
}
private void refreshNumberButtons() {
this.levelBtn.setMessage(Text.literal("钻石层(Y) : " + this.config.diamondLevel));
this.tunnelBtn.setMessage(Text.literal("巷道长度 : " + this.config.tunnelLength));
this.debrisBtn.setMessage(Text.literal("残骸数量 : " + this.config.debrisCount));
}
private ButtonWidget textButton(String label, ButtonWidget.PressAction action, int x, int y, int w) {
return ButtonWidget.builder(Text.literal(label), action).dimensions(x, y, w, 20).build();
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
context.drawCenteredTextWithShadow(this.textRenderer, "AutoDig \u81ea\u52a8\u6316\u77ff", this.width / 2, 10, 0xFFFFFF);
context.drawTextWithShadow(this.textRenderer,
"\u5f53\u524d\u4efb\u52a1: " + this.config.task.label() + " \u72b6\u6001: " + this.controller.statusText(),
26, this.height - 24, 0xAAAAAA);
}
/** 把界面临时配置写回 mod 全局配置并保存(供 controller 实时读取)。 */
private void applyToMod() {
DigConfig target = this.mod.config();
target.task = this.config.task;
target.survivalFirst = this.config.survivalFirst;
target.avoidLava = this.config.avoidLava;
target.autoEat = this.config.autoEat;
target.autoTool = this.config.autoTool;
target.killAura = this.config.killAura;
target.killAuraSafe = this.config.killAuraSafe;
target.respawnReturn = this.config.respawnReturn;
target.debrisReturnHome = this.config.debrisReturnHome;
target.diamondLevel = this.config.diamondLevel;
target.tunnelLength = this.config.tunnelLength;
target.debrisCount = this.config.debrisCount;
this.mod.saveConfig();
}
private void start() {
this.applyToMod();
this.mod.sendToast("AutoDig: 已开始 " + this.config.task.label() + " 任务");
this.close();
}
private void stop() {
this.applyToMod();
this.mod.config().task = MineTask.NONE;
this.mod.saveConfig();
this.controller.bridge().cancelAll();
this.mod.sendToast("AutoDig: 已停止");
this.close();
}
@Override
public void close() {
this.applyToMod();
super.close();
}
}