重新调整实体加速器代码结构,提升可读性

This commit is contained in:
C-H716 2025-09-27 00:19:50 +08:00
parent 848cf86624
commit 47c4b2cab9
5 changed files with 416 additions and 453 deletions

View File

@ -18,140 +18,159 @@ import net.minecraftforge.registries.ForgeRegistries;
import java.util.List; import java.util.List;
// 实体加速器菜单负责与客户端界面同步数据 /**
* 实体加速器菜单负责管理客户端与服务端的数据同步处理加速卡能量卡和目标方块的状态
*/
public class EntitySpeedTickerMenu extends UpgradeableMenu<EntitySpeedTickerPart> { public class EntitySpeedTickerMenu extends UpgradeableMenu<EntitySpeedTickerPart> {
@GuiSync(716) public boolean accelerateEnabled = true; @GuiSync(716) public boolean accelerateEnabled = true; // 是否启用加速
// 已安装的实体加速卡数量用于能耗计算 @GuiSync(717) public int entitySpeedCardCount; // 已安装的实体加速卡数量
@GuiSync(717) public int entitySpeedCardCount; @GuiSync(718) public int energyCardCount; // 已安装的能量卡数量
// 已安装的能量卡数量 @GuiSync(719) public int effectiveSpeed = 1; // 当前生效的加速倍率
@GuiSync(718) public int energyCardCount; @GuiSync(720) public double multiplier = 1.0; // 目标方块的配置倍率
// 当前生效的配置倍率从配置中读取并同步 @GuiSync(721) public boolean targetBlacklisted = false; // 目标方块是否在黑名单中
// 当前计算出的生效速度product of multipliers同步给客户端用于显示 @GuiSync(722) public boolean networkEnergySufficient = true; // 网络能量是否充足
@GuiSync(719) public int effectiveSpeed = 1;
@GuiSync(720) public double multiplier = 1.0;
@GuiSync(721) public boolean targetBlacklisted = false;
// 来自部件的网络能量充足提示服务端设置客户端用于显示警告
@GuiSync(722) public boolean networkEnergySufficient = true;
/**
* 构造函数初始化菜单并绑定部件
* @param id 菜单ID
* @param ip 玩家背包
* @param host 关联的实体加速器部件
*/
public EntitySpeedTickerMenu(int id, Inventory ip, EntitySpeedTickerPart host) {
super(ModMenuTypes.ENTITY_TICKER_MENU.get(), id, ip, host);
if (host != null) {
host.menu = this; // 绑定菜单到部件
this.accelerateEnabled = host.getAccelerateEnabled(); // 同步初始开关状态
}
}
/**
* 获取加速开关状态
* @return 是否启用加速
*/
public boolean getAccelerateEnabled() { public boolean getAccelerateEnabled() {
return this.accelerateEnabled; return this.accelerateEnabled;
} }
/**
* 设置加速开关状态并同步到部件
* @param enabled 是否启用加速
*/
public void setAccelerateEnabled(boolean enabled) { public void setAccelerateEnabled(boolean enabled) {
this.accelerateEnabled = enabled; this.accelerateEnabled = enabled;
if (getHost() != null) {
getHost().setAccelerateEnabled(enabled); // 同步到部件
}
broadcastChanges(); // 广播状态变化
} }
/** /**
* Part 更新 networkEnergySufficient 的封装方法由服务器调用 * 更新网络能量充足状态并广播到客户端
* 该方法会更新 @GuiSync 字段并广播变化到客户端 * @param sufficient 是否能量充足
*/ */
public void setNetworkEnergySufficient(boolean sufficient) { public void setNetworkEnergySufficient(boolean sufficient) {
this.networkEnergySufficient = sufficient; this.networkEnergySufficient = sufficient;
// 触发一次数据广播使客户端立即接收到最新状态 broadcastChanges();
this.broadcastChanges();
} }
/**
// 构造方法初始化菜单并与部件绑定 * 服务端数据同步到客户端时调用更新卡数量目标状态和生效速度
public EntitySpeedTickerMenu(int id, Inventory ip, EntitySpeedTickerPart host) { */
super(ModMenuTypes.ENTITY_TICKER_MENU.get(), id, ip, host);
// 让部件持有当前菜单实例便于通信
getHost().menu = this;
// 初始同步部件上的开关状态到菜单服务器端构造时保证一致
try {
this.accelerateEnabled = getHost().getAccelerateEnabled();
} catch (Exception ignored) {}
}
// 当服务器数据同步到客户端时调用
@Override @Override
public void onServerDataSync() { public void onServerDataSync() {
super.onServerDataSync(); super.onServerDataSync();
// 重新统计实体加速卡和能量卡数量 updateCardCounts(); // 更新卡数量
this.entitySpeedCardCount = this.getUpgrades().getInstalledUpgrades(ModItems.ENTITY_SPEED_CARD.get()); updateTargetStatus(); // 更新目标方块的黑名单和倍率
this.energyCardCount = this.getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD); updateEffectiveSpeed(); // 计算生效速度
updateNetworkEnergyStatus(); // 同步能量状态
// 计算当前面向方块的倍率服务器端并同步给客户端
double mult = 1.0;
try {
BlockEntity target = getHost().getLevel().getBlockEntity(getHost().getBlockEntity().getBlockPos().relative(getHost().getSide()));
if (target != null) {
String blockId = ForgeRegistries.BLOCKS.getKey(target.getBlockState().getBlock()).toString();
for (ConfigParsingUtils.MultiplierEntry me : ConfigParsingUtils.getCachedMultiplierEntries(List.of(ModConfig.INSTANCE.entityTickerMultipliers))) {
if (me.pattern.matcher(blockId).matches()) {
mult = Math.max(mult, me.multiplier);
}
}
}
} catch (Exception ignored) {}
this.multiplier = mult;
// 检查目标是否在黑名单中如果是则标记并将生效速度设为 0服务器端计算
boolean blacklisted = false;
try {
BlockEntity target = getHost().getLevel().getBlockEntity(getHost().getBlockEntity().getBlockPos().relative(getHost().getSide()));
if (target != null) {
String blockId = ForgeRegistries.BLOCKS.getKey(target.getBlockState().getBlock()).toString();
for (java.util.regex.Pattern p : ConfigParsingUtils.getCachedBlacklist(List.of(ModConfig.INSTANCE.entityTickerBlackList))) {
if (p.matcher(blockId).matches()) {
blacklisted = true;
break;
}
}
}
} catch (Exception ignored) {}
this.targetBlacklisted = blacklisted;
// 计算生效速度如果被黑名单则为 0否则进行正常计算使用工具类从菜单直接计算 product with cap最多 8
if (this.targetBlacklisted) {
this.effectiveSpeed = 0;
} else {
this.effectiveSpeed = (int) PowerUtils.computeProductWithCapFromMenu(this, 8);
}
// 从部件同步网络能量充足状态仅在服务器端从部件读取客户端应使用由 @GuiSync 同步过来的值
try {
EntitySpeedTickerPart host = getHost();
if (host != null && !isClientSide()) {
this.networkEnergySufficient = host.isNetworkEnergySufficient();
}
} catch (Exception ignored) {}
// 如果在客户端刷新界面
if (isClientSide()) { if (isClientSide()) {
if (Minecraft.getInstance().screen instanceof EntitySpeedTickerScreen screen) { refreshClientGui(); // 客户端刷新界面
screen.refreshGui();
}
} }
} }
// 当任意槽位发生变化时调用 /**
* 当槽位内容变化时调用客户端更新卡数量和生效速度
* @param slot 发生变化的槽位
*/
@Override @Override
public void onSlotChange(net.minecraft.world.inventory.Slot slot) { public void onSlotChange(net.minecraft.world.inventory.Slot slot) {
super.onSlotChange(slot); super.onSlotChange(slot);
// 客户端重新统计卡数量并刷新界面
if (isClientSide()) { if (isClientSide()) {
this.entitySpeedCardCount = this.getUpgrades().getInstalledUpgrades(ModItems.ENTITY_SPEED_CARD.get()); updateCardCounts();
this.energyCardCount = this.getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD); updateEffectiveSpeed();
// 立即在客户端计算生效速度以便界面即时反馈使用与服务端相同的工具方法最多 8 张卡 refreshClientGui();
this.effectiveSpeed = (int) PowerUtils.computeProductWithCapFromMenu(this, 8);
if (Minecraft.getInstance().screen instanceof EntitySpeedTickerScreen screen) {
screen.refreshGui();
}
} }
} }
/**
* 广播数据变化清理未启用槽位的显示堆栈
*/
@Override @Override
public void broadcastChanges(){ public void broadcastChanges() {
// 遍历所有槽位清理未启用但有物品显示的 OptionalFakeSlot
for (Object o : this.slots) { for (Object o : this.slots) {
if (o instanceof OptionalFakeSlot fs) { if (o instanceof OptionalFakeSlot fs && !fs.isSlotEnabled() && !fs.getDisplayStack().isEmpty()) {
if (!fs.isSlotEnabled() && !fs.getDisplayStack().isEmpty()) { fs.clearStack(); // 清理未启用槽位的显示
fs.clearStack();
}
} }
} }
// 调用标准的同步方法通知监听者数据已更新 standardDetectAndSendChanges();
this.standardDetectAndSendChanges(); }
/**
* 更新加速卡和能量卡的数量
*/
private void updateCardCounts() {
this.entitySpeedCardCount = this.getUpgrades().getInstalledUpgrades(ModItems.ENTITY_SPEED_CARD.get());
this.energyCardCount = this.getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD);
}
/**
* 更新目标方块的黑名单状态和倍率
*/
private void updateTargetStatus() {
BlockEntity target = getTargetBlockEntity();
if (target == null) {
this.multiplier = 1.0;
this.targetBlacklisted = false;
return;
}
String blockId = ForgeRegistries.BLOCKS.getKey(target.getBlockState().getBlock()).toString();
this.multiplier = ConfigParsingUtils.getMultiplierForBlock(blockId, List.of(ModConfig.INSTANCE.entityTickerMultipliers));
this.targetBlacklisted = ConfigParsingUtils.isBlockBlacklisted(blockId, List.of(ModConfig.INSTANCE.entityTickerBlackList));
}
/**
* 计算生效速度考虑黑名单和卡数量
*/
private void updateEffectiveSpeed() {
this.effectiveSpeed = targetBlacklisted ? 0 : (int) PowerUtils.computeProductWithCap(getUpgrades(), 8);
}
/**
* 同步网络能量状态仅服务端
*/
private void updateNetworkEnergyStatus() {
if (!isClientSide() && getHost() != null) {
this.networkEnergySufficient = getHost().isNetworkEnergySufficient();
}
}
/**
* 客户端刷新界面
*/
private void refreshClientGui() {
if (Minecraft.getInstance().screen instanceof EntitySpeedTickerScreen screen) {
screen.refreshGui();
}
}
/**
* 获取目标方块实体
* @return 目标方块实体或 null
*/
private BlockEntity getTargetBlockEntity() {
return getHost() != null ?
getHost().getLevel().getBlockEntity(
getHost().getBlockEntity().getBlockPos().relative(getHost().getSide())
) : null;
} }
} }

View File

@ -24,7 +24,6 @@ import com.extendedae_plus.init.ModItems;
import com.extendedae_plus.init.ModMenuTypes; import com.extendedae_plus.init.ModMenuTypes;
import com.extendedae_plus.util.ConfigParsingUtils; import com.extendedae_plus.util.ConfigParsingUtils;
import com.extendedae_plus.util.PowerUtils; import com.extendedae_plus.util.PowerUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
@ -41,52 +40,38 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
/** /**
* EntitySpeedTickerPart 是一个可升级的 AE2 部件<p> * 实体加速器部件用于加速目标方块实体的 tick 速率消耗 AE 网络能量支持加速卡和能量卡升级
* 该部件可以加速目标方块实体的 tick 速率消耗 AE 网络能量并支持加速卡升级<p> * 灵感来源于 <a href="https://github.com/GilbertzRivi/crazyae2addons">Crazy AE2 Addons</a>
* 功能受<a href="https://github.com/GilbertzRivi/crazyae2addons">Crazy AE2 Addons</a>启发
*/ */
public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTickable, MenuProvider, IUpgradeableObject { public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTickable, MenuProvider, IUpgradeableObject {
// 当前打开的菜单实例如果有
public EntitySpeedTickerMenu menu;
public static final ResourceLocation MODEL_BASE = new ResourceLocation( public static final ResourceLocation MODEL_BASE = new ResourceLocation(
ExtendedAEPlus.MODID, "part/entity_speed_ticker_part"); ExtendedAEPlus.MODID, "part/entity_speed_ticker_part");
@PartModels @PartModels
public static final PartModel MODELS_OFF; public static final PartModel MODELS_OFF = new PartModel(MODEL_BASE, new ResourceLocation(ExtendedAEPlus.MODID, "part/entity_speed_ticker_off"));
@PartModels @PartModels
public static final PartModel MODELS_ON; public static final PartModel MODELS_ON = new PartModel(MODEL_BASE, new ResourceLocation(ExtendedAEPlus.MODID, "part/entity_speed_ticker_on"));
@PartModels @PartModels
public static final PartModel MODELS_HAS_CHANNEL; public static final PartModel MODELS_HAS_CHANNEL = new PartModel(MODEL_BASE, new ResourceLocation(ExtendedAEPlus.MODID, "part/entity_speed_ticker_has_channel"));
static { public EntitySpeedTickerMenu menu; // 当前打开的菜单实例
MODELS_OFF = new PartModel(MODEL_BASE, new ResourceLocation(ExtendedAEPlus.MODID, "part/entity_speed_ticker_off")); private boolean accelerateEnabled = true; // 是否启用加速
MODELS_ON = new PartModel(MODEL_BASE, new ResourceLocation(ExtendedAEPlus.MODID, "part/entity_speed_ticker_on")); private boolean networkEnergySufficient = true; // 网络能量是否充足
MODELS_HAS_CHANNEL = new PartModel(MODEL_BASE, new ResourceLocation(ExtendedAEPlus.MODID, "part/entity_speed_ticker_has_channel"));
}
/** /**
* 构造函数初始化部件并设置网络节点属性 * 构造函数初始化部件并设置网络节点属性
* @param partItem 部件物品 * @param partItem 部件物品
*/ */
public EntitySpeedTickerPart(IPartItem<?> partItem) { public EntitySpeedTickerPart(IPartItem<?> partItem) {
super(partItem); super(partItem);
// 设置网络节点属性需要通道空闲功耗为1并注册为 IGridTickable 服务
this.getMainNode() this.getMainNode()
.setFlags(GridFlags.REQUIRE_CHANNEL) .setFlags(GridFlags.REQUIRE_CHANNEL)
.setIdlePowerUsage(1) .setIdlePowerUsage(1)
.addService(IGridTickable.class, this); .addService(IGridTickable.class, this);
} }
// 控制是否启用加速默认启用
private boolean accelerateEnabled = true;
// 标记网络中能量是否充足用于 GUI 提示默认充足
private boolean networkEnergySufficient = true;
public boolean getAccelerateEnabled() { public boolean getAccelerateEnabled() {
return this.accelerateEnabled; return this.accelerateEnabled;
} }
@ -95,29 +80,33 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTicka
return this.networkEnergySufficient; return this.networkEnergySufficient;
} }
public void setAccelerateEnabled(boolean accelerateEnabled) {
this.accelerateEnabled = accelerateEnabled;
}
/** /**
* 更新网络能量充足标记并在菜单存在且状态变化时触发同步 * 设置加速开关状态并通知菜单
* @param sufficient 是否能量充足 * @param enabled 是否启用加速
*/ */
private void updateNetworkEnergySufficient(boolean sufficient) { public void setAccelerateEnabled(boolean enabled) {
// 保持部件内部状态一致部件为权威来源 this.accelerateEnabled = enabled;
this.networkEnergySufficient = sufficient; if (menu != null) {
if (this.menu != null) { menu.setAccelerateEnabled(enabled);
try {
// 使用菜单的封装方法更新并广播以保持封装性
this.menu.setNetworkEnergySufficient(sufficient);
} catch (Exception ignored) {}
} }
} }
/** /**
* 获取当前状态下的静态模型用于渲染 * 更新网络能量充足状态并通知菜单
* @param sufficient 是否能量充足
*/
private void updateNetworkEnergySufficient(boolean sufficient) {
this.networkEnergySufficient = sufficient;
if (menu != null) {
menu.setNetworkEnergySufficient(sufficient);
}
}
/**
* 获取当前状态的渲染模型
* @return 当前状态的模型 * @return 当前状态的模型
*/ */
@Override
public IPartModel getStaticModels() { public IPartModel getStaticModels() {
if (this.isActive() && this.isPowered()) { if (this.isActive() && this.isPowered()) {
return MODELS_HAS_CHANNEL; return MODELS_HAS_CHANNEL;
@ -129,15 +118,14 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTicka
} }
/** /**
* 当玩家激活部件右键时调用打开自定义菜单 * 处理玩家右键激活部件打开菜单
* @param player 玩家 * @param player 玩家
* @param hand * @param hand
* @param pos 点击位置 * @param pos 点击位置
* @return 总是返回 true表示激活成功 * @return 总是返回 true
*/ */
@Override @Override
public boolean onPartActivate(Player player, InteractionHand hand, Vec3 pos) { public boolean onPartActivate(Player player, InteractionHand hand, Vec3 pos) {
// 仅在服务端打开菜单
if (!player.getCommandSenderWorld().isClientSide()) { if (!player.getCommandSenderWorld().isClientSide()) {
MenuOpener.open(ModMenuTypes.ENTITY_TICKER_MENU.get(), player, MenuLocators.forPart(this)); MenuOpener.open(ModMenuTypes.ENTITY_TICKER_MENU.get(), player, MenuLocators.forPart(this));
} }
@ -145,7 +133,7 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTicka
} }
/** /**
* 定义部件的碰撞箱用于物理碰撞和渲染 * 定义部件的碰撞箱
* @param bch 碰撞辅助器 * @param bch 碰撞辅助器
*/ */
@Override @Override
@ -155,43 +143,37 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTicka
} }
/** /**
* 获取定时请求决定本部件多久 tick 一次 * 获取定时请求指定 tick 频率
* @param iGridNode 网络节点 * @param iGridNode 网络节点
* @return TickingRequest 对象 * @return TickingRequest 对象
*/ */
@Override @Override
public TickingRequest getTickingRequest(IGridNode iGridNode) { public TickingRequest getTickingRequest(IGridNode iGridNode) {
// 1 tick 执行一次
return new TickingRequest(1, 1, false, true); return new TickingRequest(1, 1, false, true);
} }
/** /**
* 当升级卡数量发生变化时调用通知菜单更新 * 当升级卡变化时通知菜单更新
*/ */
@Override @Override
public void upgradesChanged() { public void upgradesChanged() {
if (this.menu != null) { if (menu != null) {
// 使用 AE2 风格当升级发生变化时让菜单广播变化/数据会被同步客户端会基于槽内容重新计算并刷新界面 menu.broadcastChanges();
this.menu.broadcastChanges();
} }
} }
/** /**
* 网络定时回调每次 tick 时调用 * 网络定时回调处理目标方块实体的加速
* @param iGridNode 网络节点 * @param iGridNode 网络节点
* @param ticksSinceLastCall 距离上次调用经过 tick * @param ticksSinceLastCall 距离上次调用 tick
* @return TickRateModulation.IDLE 表示继续保持当前 tick 速率 * @return TickRateModulation.IDLE
*/ */
@Override @Override
public TickRateModulation tickingRequest(IGridNode iGridNode, int ticksSinceLastCall) { public TickRateModulation tickingRequest(IGridNode iGridNode, int ticksSinceLastCall) {
// 如果部件的加速开关被关闭则不进行加速提前返回
if (!this.getAccelerateEnabled()) { if (!this.getAccelerateEnabled()) {
return TickRateModulation.IDLE; return TickRateModulation.IDLE;
} }
// 获取目标方块实体本部件朝向的方块
BlockEntity target = getLevel().getBlockEntity(getBlockEntity().getBlockPos().relative(getSide())); BlockEntity target = getLevel().getBlockEntity(getBlockEntity().getBlockPos().relative(getSide()));
// 仅在目标存在且部件处于激活状态时执行加速
if (target != null && isActive()) { if (target != null && isActive()) {
ticker(target); ticker(target);
} }
@ -199,85 +181,108 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTicka
} }
/** /**
* 以指定速度对目标方块实体进行 tick 操作 * 对目标方块实体执行加速 tick 操作
* @param blockEntity 需要被 tick 方块实体 * @param blockEntity 目标方块实体
* @param <T> 方块实体类型 * @param <T> 方块实体类型
*/ */
private <T extends BlockEntity> void ticker(@NotNull T blockEntity) { private <T extends BlockEntity> void ticker(@NotNull T blockEntity) {
if (this.getGridNode() == null if (!isValidForTicking()) {
|| this.getMainNode() == null
|| this.getMainNode().getGrid() == null) {
return; return;
} }
// 获取方块实体的位置 String blockId = ForgeRegistries.BLOCKS.getKey(blockEntity.getBlockState().getBlock()).toString();
BlockPos pos = blockEntity.getBlockPos(); if (ConfigParsingUtils.isBlockBlacklisted(blockId, List.of(ModConfig.INSTANCE.entityTickerBlackList))) {
if (blockEntity.getLevel() == null) return; return;
// 检查黑名单支持通配符/正则
String blockId = Objects.requireNonNull(ForgeRegistries.BLOCKS.getKey(blockEntity.getBlockState().getBlock())).toString();
// 使用工具类的缓存接口工具类内部负责懒加载/线程安全
List<Pattern> compiledBlacklist = ConfigParsingUtils.getCachedBlacklist(List.of(ModConfig.INSTANCE.entityTickerBlackList));
for (Pattern p : compiledBlacklist) {
if (p.matcher(blockId).matches()) return;
} }
// 获取该方块实体的 Ticker BlockEntityTicker<T> ticker = getTicker(blockEntity);
@SuppressWarnings("unchecked") if (ticker == null) {
BlockEntityTicker<T> blockEntityTicker = this.getLevel() return;
.getBlockState(pos) }
.getTicker(this.getLevel(), (BlockEntityType<T>) blockEntity.getType());
if (blockEntityTicker == null) return;
// 使用集中定义的 CardDef 列表支持以后添加等级或改倍率而无需修改此逻辑 int speed = calculateSpeed();
int energyCardCount = getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD); if (speed <= 0) {
return;
}
// 使用已注册的单一 Item 计算已安装卡数量总计用于能耗计算 double requiredPower = calculateRequiredPower(speed, blockId);
if (!extractPower(requiredPower)) {
return;
}
performTicks(blockEntity, ticker, speed);
}
/**
* 检查网络节点是否有效
* @return 是否可以执行 tick
*/
private boolean isValidForTicking() {
return getGridNode() != null && getMainNode() != null && getMainNode().getGrid() != null;
}
/**
* 获取目标方块实体的 ticker
* @param blockEntity 目标方块实体
* @return ticker null
*/
private <T extends BlockEntity> BlockEntityTicker<T> getTicker(T blockEntity) {
return getLevel().getBlockState(blockEntity.getBlockPos())
.getTicker(getLevel(), (BlockEntityType<T>) blockEntity.getType());
}
/**
* 计算加速倍率
* @return 生效的加速倍率
*/
private int calculateSpeed() {
int entitySpeedCardCount = getUpgrades().getInstalledUpgrades(ModItems.ENTITY_SPEED_CARD.get()); int entitySpeedCardCount = getUpgrades().getInstalledUpgrades(ModItems.ENTITY_SPEED_CARD.get());
if (entitySpeedCardCount <= 0) return 0;
return (int) PowerUtils.computeProductWithCap(getUpgrades(), 8);
}
// 使用工具方法从槽位直接计算乘积并应用 cap最多 8 张卡 /**
long product = PowerUtils.computeProductWithCapFromStacks(this.getUpgrades(), 8); * 计算所需能量
* @param speed 加速倍率
* @param blockId 目标方块ID
* @return 所需能量
*/
private double calculateRequiredPower(int speed, String blockId) {
int energyCardCount = getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD);
double multiplier = ConfigParsingUtils.getMultiplierForBlock(blockId, List.of(ModConfig.INSTANCE.entityTickerMultipliers));
return PowerUtils.computeFinalPowerForProduct(speed, energyCardCount) * multiplier;
}
// 如果没有任何实体加速卡则不进行加速且不消耗额外能量只保留部件的被动功耗 /**
if (entitySpeedCardCount <= 0) return; * 提取网络能量并更新状态
* @param requiredPower 所需能量
// 计算本次 tick 所需能量使用工具类根据 product 计算最终能耗 * @return 是否成功提取
double requiredPower = PowerUtils.computeFinalPowerForProduct(product, energyCardCount); */
private boolean extractPower(double requiredPower) {
int speed = (int) product; var energyService = getMainNode().getGrid().getEnergyService();
double simulated = energyService.extractAEPower(requiredPower, Actionable.SIMULATE, PowerMultiplier.CONFIG);
double multiplier = 1.0;
for (ConfigParsingUtils.MultiplierEntry me : ConfigParsingUtils.getCachedMultiplierEntries(List.of(ModConfig.INSTANCE.entityTickerMultipliers))) {
if (me.pattern.matcher(blockId).matches()) {
multiplier = Math.max(multiplier, me.multiplier);
}
}
requiredPower *= multiplier;
// 先模拟提取以检查网络中是否有足够能量再真正抽取
double simulated = getMainNode().getGrid().getEnergyService()
.extractAEPower(requiredPower, Actionable.SIMULATE, PowerMultiplier.CONFIG);
if (simulated < requiredPower) { if (simulated < requiredPower) {
updateNetworkEnergySufficient(false); // 能量不足 updateNetworkEnergySufficient(false);
return; return false;
} }
double extracted = energyService.extractAEPower(requiredPower, Actionable.MODULATE, PowerMultiplier.CONFIG);
boolean sufficient = extracted >= requiredPower;
updateNetworkEnergySufficient(sufficient);
return sufficient;
}
double extractedPower = getMainNode().getGrid().getEnergyService() /**
.extractAEPower(requiredPower, Actionable.MODULATE, PowerMultiplier.CONFIG); * 执行加速 tick 操作
if (extractedPower < requiredPower) { * @param blockEntity 目标方块实体
updateNetworkEnergySufficient(false); // 能量不足 * @param ticker 方块实体 ticker
return; * @param speed 加速倍率
} */
updateNetworkEnergySufficient(true); // 能量充足 private <T extends BlockEntity> void performTicks(T blockEntity,
BlockEntityTicker<T> ticker,
// 计算加速倍数基于 2 的次方并把 8 张映射到最大 1024x2^10 int speed) {
// 已由 product 计算得到 speed上面已在没有卡时提前返回 // 执行 speed-1 次额外 tick原生 tick 已包含 1
// 执行 tick 操作
for (int i = 0; i < speed - 1; i++) { for (int i = 0; i < speed - 1; i++) {
blockEntityTicker.tick( ticker.tick(
blockEntity.getLevel(), blockEntity.getLevel(),
blockEntity.getBlockPos(), blockEntity.getBlockPos(),
blockEntity.getBlockState(), blockEntity.getBlockState(),
@ -286,26 +291,18 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTicka
} }
} }
/**
* 判断部件是否有自定义名称
* @return 是否有自定义名称
*/
@Override @Override
public boolean hasCustomName() { public boolean hasCustomName() {
return super.hasCustomName(); return super.hasCustomName();
} }
/**
* 获取部件的显示名称
* @return 显示名称
*/
@Override @Override
public @NotNull Component getDisplayName() { public @NotNull Component getDisplayName() {
return super.getDisplayName(); return super.getDisplayName();
} }
/** /**
* 创建自定义菜单GUI * 创建菜单实例
* @param containerId 容器ID * @param containerId 容器ID
* @param playerInventory 玩家背包 * @param playerInventory 玩家背包
* @param player 玩家 * @param player 玩家
@ -319,7 +316,7 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTicka
} }
/** /**
* 获取可用的升级卡槽数量 * 获取升级卡槽数量
* @return 升级卡槽数量 * @return 升级卡槽数量
*/ */
@Override @Override

View File

@ -15,125 +15,111 @@ import com.extendedae_plus.util.PowerUtils;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Inventory;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/**
* 实体加速器界面显示加速状态卡数量能耗和倍率信息
*/
public class EntitySpeedTickerScreen<C extends EntitySpeedTickerMenu> extends UpgradeableScreen<C> { public class EntitySpeedTickerScreen<C extends EntitySpeedTickerMenu> extends UpgradeableScreen<C> {
private boolean eap$entitySpeedTickerEnabled = false; private boolean eap$entitySpeedTickerEnabled = false; // 本地缓存的加速开关状态
private SettingToggleButton<YesNo> eap$entitySpeedTickerToggle; private final SettingToggleButton<YesNo> eap$entitySpeedTickerToggle; // 加速开关按钮
public EntitySpeedTickerScreen( /**
EntitySpeedTickerMenu menu, Inventory playerInventory, Component title, ScreenStyle style) { * 构造函数初始化界面和控件
* @param menu 实体加速器菜单
* @param playerInventory 玩家背包
* @param title 界面标题
* @param style 界面样式
*/
public EntitySpeedTickerScreen(EntitySpeedTickerMenu menu, Inventory playerInventory, Component title, ScreenStyle style) {
super((C) menu, playerInventory, title, style); super((C) menu, playerInventory, title, style);
this.addToLeftToolbar(CommonButtons.togglePowerUnit()); this.addToLeftToolbar(CommonButtons.togglePowerUnit()); // 添加功率单位切换按钮
this.eap$entitySpeedTickerEnabled = menu.getAccelerateEnabled();
try{ // 初始化加速开关按钮
this.eap$entitySpeedTickerEnabled = menu.getAccelerateEnabled();
}catch (Exception ignored){}
// 使用 SettingToggleButton<YesNo> 的外观原版图标但自定义悬停描述为智能阻挡
// 不做本地切换点击仅发送自定义C2S显示由@GuiSync回传
eap$entitySpeedTickerToggle = new SettingToggleButton<>( eap$entitySpeedTickerToggle = new SettingToggleButton<>(
Settings.BLOCKING_MODE, Settings.BLOCKING_MODE,
this.eap$entitySpeedTickerEnabled ? YesNo.YES : YesNo.NO, this.eap$entitySpeedTickerEnabled ? YesNo.YES : YesNo.NO,
(btn, backwards) -> { (btn, backwards) ->
// 不做本地切换点击仅发送自定义C2S显示由@GuiSync回传 ModNetwork.CHANNEL.sendToServer(new ToggleEntityTickerC2SPacket())
ModNetwork.CHANNEL.sendToServer(new ToggleEntityTickerC2SPacket());
}
) { ) {
@Override @Override
public List<Component> getTooltipMessage() { public List<Component> getTooltipMessage() {
// 如果目标在黑名单中直接显示已禁用的提示 if (menu.targetBlacklisted) {
try { return List.of(
if (menu != null && menu.targetBlacklisted) { Component.literal("实体加速"),
var title = Component.literal("实体加速"); Component.literal("已禁用(目标在黑名单)")
var stateLine = Component.literal("已禁用(目标在黑名单)"); );
return List.of(title, stateLine); }
}
} catch (Exception ignored) {}
boolean enabled = eap$entitySpeedTickerEnabled; boolean enabled = eap$entitySpeedTickerEnabled;
var title = Component.literal("实体加速"); return List.of(
var stateLine = enabled Component.literal("实体加速"),
? Component.literal("已启用: 将加速目标方块实体的tick") enabled ? Component.literal("已启用: 将加速目标方块实体的tick") :
: Component.literal("已关闭: 不会对目标方块实体进行加速"); Component.literal("已关闭: 不会对目标方块实体进行加速")
return List.of(title, stateLine); );
} }
@Override @Override
protected Icon getIcon() { protected Icon getIcon() {
try { if (menu.targetBlacklisted) return Icon.INVALID;
if (menu != null && menu.targetBlacklisted) { return this.getCurrentValue() == YesNo.YES ? Icon.VALID : Icon.INVALID;
// 黑名单时显示禁用图标
return Icon.INVALID;
}
} catch (Exception ignored) {}
// 根据当前值显示不同图标可按需替换 Icon 常量
if (this.getCurrentValue() == YesNo.YES) {
return Icon.VALID;
} else {
return Icon.INVALID;
}
} }
}; };
// 初始化后立刻对齐当前@GuiSync状态避免首帧显示不一致
eap$entitySpeedTickerToggle.set(this.eap$entitySpeedTickerEnabled ? YesNo.YES : YesNo.NO); eap$entitySpeedTickerToggle.set(this.eap$entitySpeedTickerEnabled ? YesNo.YES : YesNo.NO);
this.addToLeftToolbar(eap$entitySpeedTickerToggle); this.addToLeftToolbar(eap$entitySpeedTickerToggle);
} }
/**
* 在渲染前更新界面状态
*/
@Override @Override
protected void updateBeforeRender() { protected void updateBeforeRender() {
super.updateBeforeRender(); super.updateBeforeRender();
if (eap$entitySpeedTickerToggle != null && menu != null) {
if (this.eap$entitySpeedTickerToggle != null) { eap$entitySpeedTickerEnabled = menu.getAccelerateEnabled();
boolean desired = this.eap$entitySpeedTickerEnabled; // 如果目标在黑名单禁用按钮并显示关闭状态
if (this.menu != null) { eap$entitySpeedTickerToggle.set(menu.targetBlacklisted ? YesNo.NO : (eap$entitySpeedTickerEnabled ? YesNo.YES : YesNo.NO));
desired = this.menu.getAccelerateEnabled(); eap$entitySpeedTickerToggle.active = !menu.targetBlacklisted;
}
this.eap$entitySpeedTickerEnabled = desired;
// 如果目标在黑名单中禁用切换并强制显示为关闭
if (this.menu != null && this.menu.targetBlacklisted) {
this.eap$entitySpeedTickerToggle.set(YesNo.NO);
this.eap$entitySpeedTickerToggle.active = false;
} else {
this.eap$entitySpeedTickerToggle.set(desired ? YesNo.YES : YesNo.NO);
this.eap$entitySpeedTickerToggle.active = true;
}
} }
textData(); textData();
} }
/**
* 刷新界面显示
*/
public void refreshGui() { public void refreshGui() {
textData(); textData();
} }
/**
* 更新界面文本内容包括加速状态速度能耗和倍率
*/
private void textData() { private void textData() {
// 如果目标被黑名单禁止则显示禁用状态并把数值显示为 0 Map<String, Component> textContents = new HashMap<>();
if (getMenu().targetBlacklisted) { if (getMenu().targetBlacklisted) {
setTextContent("enable", Component.translatable("screen.extendedae_plus.entity_speed_ticker.enable")); // 黑名单禁用时的默认显示
setTextContent("speed", Component.translatable("screen.extendedae_plus.entity_speed_ticker.speed", 0)); textContents.put("enable", Component.translatable("screen.extendedae_plus.entity_speed_ticker.enable"));
setTextContent("energy", Component.translatable("screen.extendedae_plus.entity_speed_ticker.energy", Platform.formatPower(0.0, false))); textContents.put("speed", Component.translatable("screen.extendedae_plus.entity_speed_ticker.speed", 0));
setTextContent("power_ratio", Component.translatable("screen.extendedae_plus.entity_speed_ticker.power_ratio", PowerUtils.formatPercentage(0.0))); textContents.put("energy", Component.translatable("screen.extendedae_plus.entity_speed_ticker.energy", Platform.formatPower(0.0, false)));
setTextContent("multiplier", Component.translatable("screen.extendedae_plus.entity_speed_ticker.multiplier", String.format("%.2fx", 0.0))); textContents.put("power_ratio", Component.translatable("screen.extendedae_plus.entity_speed_ticker.power_ratio", PowerUtils.formatPercentage(0.0)));
return; textContents.put("multiplier", Component.translatable("screen.extendedae_plus.entity_speed_ticker.multiplier", String.format("%.2fx", 0.0)));
} } else {
// 正常状态下显示实际数据
int energyCardCount = getMenu().energyCardCount;
double multiplier = getMenu().multiplier;
int effectiveSpeed = getMenu().effectiveSpeed;
double finalPower = PowerUtils.computeFinalPowerForProduct(effectiveSpeed, energyCardCount);
double remainingRatio = PowerUtils.getRemainingRatio(energyCardCount);
int energyCardCount = getMenu().energyCardCount; textContents.put("enable", getMenu().networkEnergySufficient ? null :
double multiplier = getMenu().multiplier; Component.translatable("screen.extendedae_plus.entity_speed_ticker.warning_network_energy_insufficient"));
int effectiveSpeed = getMenu().effectiveSpeed; textContents.put("speed", Component.translatable("screen.extendedae_plus.entity_speed_ticker.speed", effectiveSpeed));
double finalPower = PowerUtils.computeFinalPowerForProduct(effectiveSpeed, energyCardCount); textContents.put("energy", Component.translatable("screen.extendedae_plus.entity_speed_ticker.energy", Platform.formatPower(finalPower, false)));
double remainingRatio = PowerUtils.getRemainingRatio(energyCardCount); textContents.put("power_ratio", Component.translatable("screen.extendedae_plus.entity_speed_ticker.power_ratio", PowerUtils.formatPercentage(remainingRatio)));
textContents.put("multiplier", Component.translatable("screen.extendedae_plus.entity_speed_ticker.multiplier", String.format("%.2fx", multiplier)));
if (!getMenu().networkEnergySufficient) {
setTextContent("enable", Component.translatable("screen.extendedae_plus.entity_speed_ticker.warning_network_energy_insufficient"));
}else {
setTextContent("enable", null);
} }
setTextContent("speed", Component.translatable("screen.extendedae_plus.entity_speed_ticker.speed", effectiveSpeed)); textContents.forEach(this::setTextContent);
setTextContent("energy", Component.translatable("screen.extendedae_plus.entity_speed_ticker.energy", Platform.formatPower(finalPower, false)));
setTextContent("power_ratio", Component.translatable("screen.extendedae_plus.entity_speed_ticker.power_ratio", PowerUtils.formatPercentage(remainingRatio)));
setTextContent("multiplier", Component.translatable("screen.extendedae_plus.entity_speed_ticker.multiplier", String.format("%.2fx", multiplier)));
} }
} }

View File

@ -6,6 +6,8 @@ import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import static com.extendedae_plus.util.ExtendedAELogger.LOGGER;
/** /**
* 配置解析工具类用于解析黑名单与倍率配置的字符串 * 配置解析工具类用于解析黑名单与倍率配置的字符串
*/ */
@ -24,35 +26,32 @@ public final class ConfigParsingUtils {
/** /**
* 编译用户提供的匹配串支持简单的 glob 语法'*' '?'以及完整的正则表达式 * 编译用户提供的匹配串支持简单的 glob 语法'*' '?'以及完整的正则表达式
* 规则
* - 如果字符串包含 ".*" 或其他正则元字符优先尝试按正则编译若失败则回退到 glob 转换
* - 否则若包含 '*' '?'将按 glob 语法转换为正则
* - 否则按字面量匹配处理
*/ */
public static Pattern compilePattern(String raw) { public static Pattern compilePattern(String raw) {
if (raw == null) throw new IllegalArgumentException("pattern is null"); if (raw == null || raw.trim().isEmpty()) {
LOGGER.warn("Invalid pattern: {}", raw);
throw new IllegalArgumentException("Pattern is null or empty");
}
raw = raw.trim(); raw = raw.trim();
if (raw.isEmpty()) throw new IllegalArgumentException("pattern is empty");
// If it looks like regex (contains '.*' or regex metachar), try regex first // Try regex first if it contains regex metacharacters
if (raw.contains(".*") || raw.matches(".*[\\[\\(\\+\\{\\\\].*")) { if (raw.contains(".*") || raw.matches(".*[\\[\\(\\+\\{\\\\].*")) {
try { try {
return Pattern.compile("^" + raw + "$"); return Pattern.compile("^" + raw + "$");
} catch (PatternSyntaxException ignored) { } catch (PatternSyntaxException e) {
// fallback to glob below LOGGER.warn("Failed to compile regex pattern '{}': {}", raw, e.getMessage());
// Fallback to glob
} }
} }
// If contains glob chars, convert to regex // Convert glob to regex
if (raw.contains("*") || raw.contains("?")) { if (raw.contains("*") || raw.contains("?")) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder("^");
sb.append('^');
for (char c : raw.toCharArray()) { for (char c : raw.toCharArray()) {
switch (c) { switch (c) {
case '*': sb.append(".*"); break; case '*': sb.append(".*"); break;
case '?': sb.append('.'); break; case '?': sb.append('.'); break;
default: default:
// escape regex special chars
if (".\\+[]{}()^$|".indexOf(c) >= 0) { if (".\\+[]{}()^$|".indexOf(c) >= 0) {
sb.append('\\'); sb.append('\\');
} }
@ -63,41 +62,71 @@ public final class ConfigParsingUtils {
return Pattern.compile(sb.toString()); return Pattern.compile(sb.toString());
} }
// Otherwise treat as a literal (match exact) // Literal match
return Pattern.compile("^" + Pattern.quote(raw) + "$"); return Pattern.compile("^" + Pattern.quote(raw) + "$");
} }
/** /**
* Parse multiplier entries like 'modid:block 2x' into MultiplierEntry objects. * 解析倍率条目 'modid:block 2x'
* Accepts values with optional trailing 'x' (case-insensitive).
*/ */
public static MultiplierEntry parseMultiplierEntry(String entry) { public static MultiplierEntry parseMultiplierEntry(String entry) {
if (entry == null) return null; if (entry == null || entry.trim().isEmpty()) return null;
String[] parts = entry.trim().split("\\s+"); String[] parts = entry.trim().split("\\s+");
if (parts.length < 2) return null; if (parts.length < 2) {
LOGGER.warn("Invalid multiplier entry: {}", entry);
return null;
}
String key = parts[0]; String key = parts[0];
String val = parts[1].toLowerCase(); String val = parts[1].toLowerCase();
if (val.endsWith("x")) val = val.substring(0, val.length() - 1); if (val.endsWith("x")) val = val.substring(0, val.length() - 1);
double m; double multiplier;
try { try {
m = Double.parseDouble(val); multiplier = Double.parseDouble(val);
} catch (NumberFormatException ex) { } catch (NumberFormatException e) {
LOGGER.warn("Invalid multiplier value in '{}': {}", entry, val);
return null; return null;
} }
try { try {
Pattern p = compilePattern(key); Pattern pattern = compilePattern(key);
return new MultiplierEntry(p, m); return new MultiplierEntry(pattern, multiplier);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.warn("Failed to compile pattern in '{}': {}", entry, e.getMessage());
return null; return null;
} }
} }
/**
* 检查方块是否在黑名单中
*/
public static boolean isBlockBlacklisted(String blockId, List<? extends String> blacklist) {
if (blockId == null) return false;
return getCachedBlacklist(blacklist).stream().anyMatch(p -> p.matcher(blockId).matches());
}
/**
* 获取方块的倍率
*/
public static double getMultiplierForBlock(String blockId, List<? extends String> multipliers) {
if (blockId == null) return 1.0;
double maxMultiplier = 1.0;
for (MultiplierEntry me : getCachedMultiplierEntries(multipliers)) {
if (me.pattern.matcher(blockId).matches()) {
maxMultiplier = Math.max(maxMultiplier, me.multiplier);
}
}
return maxMultiplier;
}
public static List<Pattern> compilePatterns(List<? extends String> raw) { public static List<Pattern> compilePatterns(List<? extends String> raw) {
List<Pattern> out = new ArrayList<>(); List<Pattern> out = new ArrayList<>();
if (raw == null) return out; if (raw == null) return out;
for (String s : raw) { for (String s : raw) {
if (s == null || s.isBlank()) continue; if (s == null || s.trim().isEmpty()) continue;
try { out.add(compilePattern(s)); } catch (IllegalArgumentException ignored) {} try {
out.add(compilePattern(s));
} catch (IllegalArgumentException e) {
LOGGER.warn("Failed to compile pattern '{}': {}", s, e.getMessage());
}
} }
return out; return out;
} }
@ -122,14 +151,11 @@ public final class ConfigParsingUtils {
/** /**
* 获取已解析并缓存的黑名单线程安全懒加载 * 获取已解析并缓存的黑名单线程安全懒加载
*/ */
public static List<Pattern> getCachedBlacklist(java.util.List<? extends String> source) { public static List<Pattern> getCachedBlacklist(List<? extends String> source) {
List<String> normalized = normalizeSource(source); List<String> normalized = normalizeSource(source);
// fast path: identical snapshot reference or equal contents
if (cachedBlacklist != null && listEquals(cachedBlacklistSourceSnapshot, normalized)) { if (cachedBlacklist != null && listEquals(cachedBlacklistSourceSnapshot, normalized)) {
return Collections.unmodifiableList(cachedBlacklist); return Collections.unmodifiableList(cachedBlacklist);
} }
synchronized (CACHE_LOCK) { synchronized (CACHE_LOCK) {
if (cachedBlacklist == null || !listEquals(cachedBlacklistSourceSnapshot, normalized)) { if (cachedBlacklist == null || !listEquals(cachedBlacklistSourceSnapshot, normalized)) {
cachedBlacklist = compilePatterns(normalized); cachedBlacklist = compilePatterns(normalized);
@ -142,13 +168,11 @@ public final class ConfigParsingUtils {
/** /**
* 获取已解析并缓存的倍率列表线程安全懒加载 * 获取已解析并缓存的倍率列表线程安全懒加载
*/ */
public static List<MultiplierEntry> getCachedMultiplierEntries(java.util.List<? extends String> source) { public static List<MultiplierEntry> getCachedMultiplierEntries(List<? extends String> source) {
List<String> normalized = normalizeSource(source); List<String> normalized = normalizeSource(source);
if (cachedMultiplierEntries != null && listEquals(cachedMultiplierSourceSnapshot, normalized)) { if (cachedMultiplierEntries != null && listEquals(cachedMultiplierSourceSnapshot, normalized)) {
return Collections.unmodifiableList(cachedMultiplierEntries); return Collections.unmodifiableList(cachedMultiplierEntries);
} }
synchronized (CACHE_LOCK) { synchronized (CACHE_LOCK) {
if (cachedMultiplierEntries == null || !listEquals(cachedMultiplierSourceSnapshot, normalized)) { if (cachedMultiplierEntries == null || !listEquals(cachedMultiplierSourceSnapshot, normalized)) {
cachedMultiplierEntries = parseMultiplierList(normalized); cachedMultiplierEntries = parseMultiplierList(normalized);
@ -189,8 +213,8 @@ public final class ConfigParsingUtils {
synchronized (CACHE_LOCK) { synchronized (CACHE_LOCK) {
cachedBlacklist = null; cachedBlacklist = null;
cachedMultiplierEntries = null; cachedMultiplierEntries = null;
cachedBlacklistSourceSnapshot = null;
cachedMultiplierSourceSnapshot = null;
} }
} }
} }

View File

@ -1,39 +1,51 @@
package com.extendedae_plus.util; package com.extendedae_plus.util;
import appeng.api.upgrades.IUpgradeInventory;
import com.extendedae_plus.ae.definitions.upgrades.EntitySpeedCardItem; import com.extendedae_plus.ae.definitions.upgrades.EntitySpeedCardItem;
import com.extendedae_plus.config.ModConfig; import com.extendedae_plus.config.ModConfig;
import net.minecraft.world.item.ItemStack;
import java.util.ArrayList;
import java.util.List;
/** /**
* 用于计算实体加速器的能耗与加速倍率的工具类 * 用于计算实体加速器的能耗与加速倍率的工具类
*/ */
public final class PowerUtils { public final class PowerUtils {
private PowerUtils() {} private PowerUtils() {}
// ---- 重构后的 API ----
/** /**
* card multipliers按插槽序计算乘积并应用 cap 规则 capForHighestMultiplier * 计算加速卡的乘积并应用上限
* @param multipliers iterable of per-card multipliers * @param upgrades 升级槽位
* @param maxCards 计入的卡数 * @param maxCards 计入的卡数
* @return cap 约束后的乘积 * @return 上限约束后的乘积
*/ */
public static long computeProductWithCap(Iterable<Integer> multipliers, int maxCards) { public static long computeProductWithCap(IUpgradeInventory upgrades, int maxCards) {
long product = 1L; List<Integer> multipliers = new ArrayList<>();
int considered = 0; int considered = 0;
for (ItemStack stack : upgrades) {
if (considered >= maxCards) break;
if (stack != null && !stack.isEmpty() && stack.getItem() instanceof EntitySpeedCardItem) {
int multVal = EntitySpeedCardItem.readMultiplier(stack);
int count = Math.min(stack.getCount(), maxCards - considered);
for (int i = 0; i < count; i++) {
multipliers.add(multVal);
considered++;
}
}
}
long product = 1L;
int highest = 1; int highest = 1;
for (Integer m : multipliers) { for (Integer m : multipliers) {
if (m == null) continue; if (m == null || m <= 0) continue;
if (considered >= maxCards) break; product *= m;
int mult = m.intValue(); highest = Math.max(highest, m);
if (mult <= 0) mult = 1;
product *= mult;
highest = Math.max(highest, mult);
considered++;
} }
long cap = capForHighestMultiplier(highest); return Math.min(product, capForHighestMultiplier(highest));
return Math.min(product, cap);
} }
/** /**
* 根据最高单卡 multiplier 返回 cap * 根据最高单卡倍率返回上限值
*/ */
public static long capForHighestMultiplier(int highestMultiplier) { public static long capForHighestMultiplier(int highestMultiplier) {
if (highestMultiplier >= 16) return 1024L; if (highestMultiplier >= 16) return 1024L;
@ -44,118 +56,43 @@ public final class PowerUtils {
} }
/** /**
* 从菜单对象读取前 maxCards 个加速卡的 multiplier 并计算 product with cap * 计算最终能耗
*/ * @param product 加速卡乘积
public static long computeProductWithCapFromMenu(appeng.menu.implementations.UpgradeableMenu<?> menu, int maxCards) { * @param energyCardCount 能量卡数量
java.util.List<Integer> list = new java.util.ArrayList<>(); * @return 最终能耗值
int considered = 0;
for (var stack : menu.getUpgrades()) {
if (considered >= maxCards) break;
if (stack != null && !stack.isEmpty() && stack.getItem() instanceof EntitySpeedCardItem) {
int multVal = EntitySpeedCardItem.readMultiplier(stack);
int count = Math.min(stack.getCount(), maxCards - considered);
for (int i = 0; i < count; i++) {
list.add(multVal);
considered++;
if (considered >= maxCards) break;
}
}
}
return computeProductWithCap(list, maxCards);
}
/**
* 从一组 ItemStack升级槽直接计算 product with cap最多 maxCards
*/
public static long computeProductWithCapFromStacks(Iterable<net.minecraft.world.item.ItemStack> stacks, int maxCards) {
java.util.List<Integer> list = new java.util.ArrayList<>();
int considered = 0;
for (var stack : stacks) {
if (considered >= maxCards) break;
if (stack != null && !stack.isEmpty() && stack.getItem() instanceof EntitySpeedCardItem) {
int multVal = EntitySpeedCardItem.readMultiplier(stack);
int count = Math.min(stack.getCount(), maxCards - considered);
for (int i = 0; i < count; i++) {
list.add(multVal);
considered++;
if (considered >= maxCards) break;
}
}
}
return computeProductWithCap(list, maxCards);
}
/**
* 计算最终消耗 product 转换为等效卡数log2并调用 getFinalPower
*/ */
public static double computeFinalPowerForProduct(long product, int energyCardCount) { public static double computeFinalPowerForProduct(long product, int energyCardCount) {
if (product <= 1L) return 0.0; if (product <= 1L) return 0.0;
double base = ModConfig.INSTANCE.entityTickerCost; double base = ModConfig.INSTANCE.entityTickerCost;
// 计算以2为底的对数用于分档与公式
double log2 = Math.log(product) / Math.log(2.0); double log2 = Math.log(product) / Math.log(2.0);
// 分档product==2 为一档4..256 为中档512..1024 为高档
double raw; double raw;
if (product == 2L) { if (product == 2L) {
// 轻量档线性小幅增长
raw = base * 4; raw = base * 4;
} else if (product <= 256L) { } else if (product <= 256L) {
// 中档增长放缓使用 1.5 * log2
raw = base * Math.pow(2.0, 1.5 * log2) * 2; raw = base * Math.pow(2.0, 1.5 * log2) * 2;
} else { } else {
// 高档增长较快使用 2.5 * log2
raw = base * Math.pow(2.0, 2.5 * log2); raw = base * Math.pow(2.0, 2.5 * log2);
} }
return raw * getRemainingRatio(energyCardCount) / 8.0;
double reduction = getReductionPercent(energyCardCount);
return raw * (1.0 - reduction) / 8.0;
}
/* ----------------- legacy helpers (restored) ----------------- */
public static double getGrowthFactor(int speedCardCount) {
if (speedCardCount <= 0) return 1.0;
if (speedCardCount == 1) return 2.0;
if (speedCardCount <= 6) return Math.pow(2.0, 2.0 * speedCardCount);
return Math.pow(2.0, 3.0 * speedCardCount);
}
public static double getRawPower(int speedCardCount) {
double base = ModConfig.INSTANCE.entityTickerCost;
return base * getGrowthFactor(speedCardCount);
}
public static double getReductionPercent(int energyCardCount) {
if (energyCardCount <= 0) return 0.0;
if (energyCardCount == 1) return 0.1;
if (energyCardCount >= 8) return 0.5;
return 0.5 * (1.0 - Math.pow(0.7, energyCardCount));
}
public static double getFinalPower(int speedCardCount, int energyCardCount) {
double raw = getRawPower(speedCardCount);
double reduction = getReductionPercent(energyCardCount);
return raw * (1.0 - reduction) / 4.0;
} }
/** /**
* 返回能源卡减免后剩余的功耗比率例如 1 张能源卡 -> 0.9 * 计算能量卡减免后的剩余功耗比率
* @param energyCardCount 能源卡数量
* @return 剩余功耗比率
*/ */
public static double getRemainingRatio(int energyCardCount) { public static double getRemainingRatio(int energyCardCount) {
return 1.0 - getReductionPercent(energyCardCount); if (energyCardCount <= 0) return 1.0;
if (energyCardCount == 1) return 0.9;
if (energyCardCount >= 8) return 0.5;
return 1.0 - 0.5 * (1.0 - Math.pow(0.7, energyCardCount));
} }
/** /**
* 将剩余功耗比率格式化为百分比字符串例如 0.9 -> "90%" * 将剩余功耗比率格式化为百分比字符串
*/ */
public static String formatPercentage(double ratio) { public static String formatPercentage(double ratio) {
double pct = ratio * 100.0; double pct = ratio * 100.0;
// 如果为整数则无小数 return Math.abs(pct - Math.round(pct)) < 1e-9 ?
if (Math.abs(pct - Math.round(pct)) < 1e-9) { String.format("%d%%", Math.round(pct)) :
return String.format("%d%%", Math.round(pct)); String.format("%.2f%%", pct);
}
return String.format("%.2f%%", pct);
} }
} }