feat: 抽离输入框创建方法

This commit is contained in:
C-H716 2025-10-09 14:13:16 +08:00
parent a6b60ebbb9
commit 78033c5df0
4 changed files with 43 additions and 69 deletions

View File

@ -10,6 +10,7 @@ import com.extendedae_plus.init.ModNetwork;
import com.extendedae_plus.network.provider.SetPerProviderScalingLimitC2SPacket;
import com.extendedae_plus.network.provider.ToggleAdvancedBlockingC2SPacket;
import com.extendedae_plus.network.provider.ToggleSmartDoublingC2SPacket;
import com.extendedae_plus.util.GuiUtil;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Tooltip;
@ -105,32 +106,14 @@ public abstract class AdvPatternProviderScreenMixin extends AEBaseScreen<AdvPatt
this.eap$SmartDoublingToggle.set(eap$SmartDoublingEnabled ? YesNo.YES : YesNo.NO);
this.addToLeftToolbar(this.eap$SmartDoublingToggle);
// 缩放上限输入框
this.eap$PerProviderLimitInput = new EditBox(this.font, 0, 0, 28, 12, Component.literal("Limit"));
this.eap$PerProviderLimitInput.setMaxLength(6);
this.eap$PerProviderLimitInput.setValue(String.valueOf(eap$PerProviderScalingLimit));
this.eap$PerProviderLimitInput.setResponder(this::eap$handleLimitChanged);
// 缩放上限输入框使用 GuiUtil 抽离
this.eap$PerProviderLimitInput = GuiUtil.createPerProviderLimitInput(this.font, this.eap$PerProviderScalingLimit, limit -> {
this.eap$PerProviderScalingLimit = limit;
ModNetwork.CHANNEL.sendToServer(new SetPerProviderScalingLimitC2SPacket(limit));
});
this.addRenderableWidget(this.eap$PerProviderLimitInput);
}
/** 输入框内容变化时调用 */
@Unique
private void eap$handleLimitChanged(String s) {
try {
// 去除前导0空字符串视为0
String sValue = (s == null || s.isBlank()) ? "0" : s.replaceFirst("^0+(?=.)", "");
if (!sValue.equals(s)) {
this.eap$PerProviderLimitInput.setValue(sValue);
}
int limit = Integer.parseInt(sValue);
// 只有变化时才发送同步包
if (limit != this.eap$PerProviderScalingLimit) {
this.eap$PerProviderScalingLimit = limit;
ModNetwork.CHANNEL.sendToServer(new SetPerProviderScalingLimitC2SPacket(limit));
}
} catch (Throwable ignored) {}
}
/* ---------------------------- 注入点 ---------------------------- */
@Inject(method = "<init>", at = @At("RETURN"))
private void eap$onInit(AdvPatternProviderMenu menu, Inventory playerInventory, Component title, ScreenStyle style, CallbackInfo ci) {

View File

@ -10,6 +10,7 @@ import com.extendedae_plus.init.ModNetwork;
import com.extendedae_plus.network.provider.SetPerProviderScalingLimitC2SPacket;
import com.extendedae_plus.network.provider.ToggleAdvancedBlockingC2SPacket;
import com.extendedae_plus.network.provider.ToggleSmartDoublingC2SPacket;
import com.extendedae_plus.util.GuiUtil;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Tooltip;
@ -106,32 +107,14 @@ public abstract class SmallAdvPatternProviderScreenMixin extends AEBaseScreen<Sm
this.eap$SmartDoublingToggle.set(eap$SmartDoublingEnabled ? YesNo.YES : YesNo.NO);
this.addToLeftToolbar(this.eap$SmartDoublingToggle);
// 缩放上限输入框
this.eap$PerProviderLimitInput = new EditBox(this.font, 0, 0, 28, 12, Component.literal("Limit"));
this.eap$PerProviderLimitInput.setMaxLength(6);
this.eap$PerProviderLimitInput.setValue(String.valueOf(eap$PerProviderScalingLimit));
this.eap$PerProviderLimitInput.setResponder(this::eap$handleLimitChanged);
// 缩放上限输入框使用 GuiUtil 抽离
this.eap$PerProviderLimitInput = GuiUtil.createPerProviderLimitInput(this.font, this.eap$PerProviderScalingLimit, limit -> {
this.eap$PerProviderScalingLimit = limit;
ModNetwork.CHANNEL.sendToServer(new SetPerProviderScalingLimitC2SPacket(limit));
});
this.addRenderableWidget(this.eap$PerProviderLimitInput);
}
/** 输入框内容变化时调用 */
@Unique
private void eap$handleLimitChanged(String s) {
try {
// 去除前导0空字符串视为0
String sValue = (s == null || s.isBlank()) ? "0" : s.replaceFirst("^0+(?=.)", "");
if (!sValue.equals(s)) {
this.eap$PerProviderLimitInput.setValue(sValue);
}
int limit = Integer.parseInt(sValue);
// 只有变化时才发送同步包
if (limit != this.eap$PerProviderScalingLimit) {
this.eap$PerProviderScalingLimit = limit;
ModNetwork.CHANNEL.sendToServer(new SetPerProviderScalingLimitC2SPacket(limit));
}
} catch (Throwable ignored) {}
}
/* ---------------------------- 注入点 ---------------------------- */
@Inject(method = "<init>", at = @At("RETURN"))
private void eap$onInit(SmallAdvPatternProviderMenu menu, Inventory playerInventory, Component title, ScreenStyle style, CallbackInfo ci) {

View File

@ -13,6 +13,7 @@ import com.extendedae_plus.init.ModNetwork;
import com.extendedae_plus.network.provider.SetPerProviderScalingLimitC2SPacket;
import com.extendedae_plus.network.provider.ToggleAdvancedBlockingC2SPacket;
import com.extendedae_plus.network.provider.ToggleSmartDoublingC2SPacket;
import com.extendedae_plus.util.GuiUtil;
import com.glodblock.github.extendedae.client.gui.GuiExPatternProvider;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
@ -108,32 +109,14 @@ public abstract class PatternProviderScreenMixin<C extends PatternProviderMenu>
this.eap$SmartDoublingToggle.set(eap$SmartDoublingEnabled ? YesNo.YES : YesNo.NO);
this.addToLeftToolbar(this.eap$SmartDoublingToggle);
// 缩放上限输入框
this.eap$PerProviderLimitInput = new EditBox(this.font, 0, 0, 28, 12, Component.literal("Limit"));
this.eap$PerProviderLimitInput.setMaxLength(6);
this.eap$PerProviderLimitInput.setValue(String.valueOf(eap$PerProviderScalingLimit));
this.eap$PerProviderLimitInput.setResponder(this::eap$handleLimitChanged);
// 缩放上限输入框使用 GuiUtil 抽离
this.eap$PerProviderLimitInput = GuiUtil.createPerProviderLimitInput(this.font, this.eap$PerProviderScalingLimit, limit -> {
this.eap$PerProviderScalingLimit = limit;
ModNetwork.CHANNEL.sendToServer(new SetPerProviderScalingLimitC2SPacket(limit));
});
this.addRenderableWidget(this.eap$PerProviderLimitInput);
}
/** 输入框内容变化时调用 */
@Unique
private void eap$handleLimitChanged(String s) {
try {
// 去除前导0空字符串视为0
String sValue = (s == null || s.isBlank()) ? "0" : s.replaceFirst("^0+(?=.)", "");
if (!sValue.equals(s)) {
this.eap$PerProviderLimitInput.setValue(sValue);
}
int limit = Integer.parseInt(sValue);
// 只有变化时才发送同步包
if (limit != this.eap$PerProviderScalingLimit) {
this.eap$PerProviderScalingLimit = limit;
ModNetwork.CHANNEL.sendToServer(new SetPerProviderScalingLimitC2SPacket(limit));
}
} catch (Throwable ignored) {}
}
/* ---------------------------- 注入点 ---------------------------- */
@Inject(method = "<init>", at = @At("RETURN"))
private void eap$onInit(C menu, Inventory playerInventory, Component title, ScreenStyle style, CallbackInfo ci) {

View File

@ -9,6 +9,7 @@ import appeng.client.gui.me.patternaccess.PatternSlot;
import appeng.client.gui.widgets.SettingToggleButton;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.Slot;
@ -17,6 +18,7 @@ import net.minecraft.world.item.ItemStack;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.function.IntConsumer;
/**
@ -204,4 +206,27 @@ public class GuiUtil {
}
};
}
/**
* 创建用于每个提供者缩放上限的输入框包含值清洗与回调处理
* @param font 字体对象
* @param initialValue 初始数值
* @param onCommit 当值解析成功后回调 int 形式提供
*/
public static EditBox createPerProviderLimitInput(Font font, int initialValue, IntConsumer onCommit) {
EditBox input = new EditBox(font, 0, 0, 28, 12, Component.literal("Limit"));
input.setMaxLength(6);
input.setValue(String.valueOf(initialValue));
input.setResponder(s -> {
try {
String sValue = (s == null || s.isBlank()) ? "0" : s.replaceFirst("^0+(?=.)", "");
if (!sValue.equals(s)) {
input.setValue(sValue);
}
int limit = Integer.parseInt(sValue);
onCommit.accept(limit);
} catch (Throwable ignored) {}
});
return input;
}
}