From f830a6107230e2773d41c54ffffb9c38b95dd167 Mon Sep 17 00:00:00 2001 From: ZhuRuoLing <3489382552@qq.com> Date: Mon, 29 Apr 2024 00:01:09 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B5=E7=BD=91=E7=94=B5=E9=87=8F=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BF=A1=E6=81=AF=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/power/PowerComponentInfo.java | 23 +++++ .../dubhe/anvilcraft/api/power/PowerGrid.java | 90 ++++++++++++------- .../anvilcraft/mixin/AnvilBlockMixin.java | 1 + .../PowerGridInformationRenderMixin.java | 13 +-- 4 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerComponentInfo.java diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerComponentInfo.java b/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerComponentInfo.java new file mode 100644 index 000000000..9d8d27d68 --- /dev/null +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerComponentInfo.java @@ -0,0 +1,23 @@ +package dev.dubhe.anvilcraft.api.power; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.core.BlockPos; + +public record PowerComponentInfo( + BlockPos pos, + int consumes, + int produces, + int stores, + int capacity, + int range +) { + public static final Codec CODEC = RecordCodecBuilder.create(ins -> ins.group( + BlockPos.CODEC.fieldOf("pos").forGetter(o -> o.pos), + Codec.INT.fieldOf("consumes").forGetter(o -> o.consumes), + Codec.INT.fieldOf("produces").forGetter(o -> o.produces), + Codec.INT.fieldOf("stores").forGetter(o -> o.stores), + Codec.INT.fieldOf("capacity").forGetter(o -> o.capacity), + Codec.INT.fieldOf("range").forGetter(o -> o.range) + ).apply(ins, PowerComponentInfo::new)); +} diff --git a/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java b/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java index d1f22349d..3eaed3d83 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java @@ -1,16 +1,12 @@ package dev.dubhe.anvilcraft.api.power; -import com.mojang.datafixers.util.Pair; import com.mojang.serialization.Codec; -import com.mojang.serialization.MapCodec; -import com.mojang.serialization.codecs.PrimitiveCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import dev.dubhe.anvilcraft.AnvilCraft; import dev.dubhe.anvilcraft.client.renderer.PowerGridRenderer; import dev.dubhe.anvilcraft.network.PowerGridRemovePack; import dev.dubhe.anvilcraft.network.PowerGridSyncPack; import lombok.Getter; -import net.minecraft.client.gui.components.tabs.Tab; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtOps; @@ -30,11 +26,8 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; -import java.util.ListIterator; import java.util.Map; import java.util.Set; -import java.util.stream.IntStream; -import java.util.stream.Stream; /** * 电网 @@ -334,14 +327,10 @@ public static class SimplePowerGrid { Codec.INT.fieldOf("hash").forGetter(o -> o.hash), Codec.STRING.fieldOf("level").forGetter(o -> o.level), BlockPos.CODEC.fieldOf("pos").forGetter(o -> o.pos), - BlockPos.CODEC + PowerComponentInfo.CODEC .listOf() - .fieldOf("blocks") - .forGetter(o -> o.blocks), - Codec.INT - .listOf() - .fieldOf("rangeByIndex") - .forGetter(o -> o.rangeByIndex), + .fieldOf("powerComponentInfoList") + .forGetter(it -> it.powerComponentInfoList), Codec.INT.fieldOf("generate").forGetter(o -> o.generate), Codec.INT.fieldOf("consume").forGetter(o -> o.consume) ).apply(ins, SimplePowerGrid::new)); @@ -349,31 +338,32 @@ public static class SimplePowerGrid { private final int hash; private final String level; private final BlockPos pos; - private Map ranges = new HashMap<>(); + private final Map ranges = new HashMap<>(); private List blocks = new ArrayList<>(); - private List rangeByIndex = new ArrayList<>(); + private final List powerComponentInfoList = new ArrayList<>(); private int generate = 0; // 发电功率 private int consume = 0; // 耗电功率 + private final Map mappedPowerComponentInfo = new HashMap<>(); + public SimplePowerGrid( int hash, String level, BlockPos pos, - List blocks, - List ranges, + List powerComponentInfoList, int generate, int consume ) { this.pos = pos; this.level = level; this.hash = hash; - this.blocks = blocks; - this.rangeByIndex = ranges; - for (int i = 0; i < blocks.size(); i++) { - this.ranges.put(blocks.get(i), ranges.get(i)); - } this.generate = generate; this.consume = consume; + blocks.addAll(powerComponentInfoList.stream().map(PowerComponentInfo::pos).toList()); + powerComponentInfoList.forEach(it -> { + mappedPowerComponentInfo.put(it.pos(), it); + ranges.put(it.pos(), it.range()); + }); } /** @@ -381,7 +371,6 @@ public SimplePowerGrid( */ public void encode(@NotNull FriendlyByteBuf buf) { this.blocks = ranges.keySet().stream().toList(); - this.rangeByIndex = ranges.values().stream().toList(); var tag1 = CODEC.encodeStart(NbtOps.INSTANCE, this); Tag tag = CODEC.encodeStart(NbtOps.INSTANCE, this) .getOrThrow(false, ignored -> { @@ -412,18 +401,55 @@ public SimplePowerGrid(@NotNull PowerGrid grid) { this.hash = grid.hashCode(); this.level = grid.getLevel().dimension().location().toString(); this.pos = grid.getPos(); - grid.storages.forEach(this::addRange); - grid.producers.forEach(this::addRange); - grid.consumers.forEach(this::addRange); - grid.transmitters.forEach(this::addRange); + Map infoMap = new HashMap<>(); + grid.storages.forEach(it -> infoMap.put( + it.getPos(), + new PowerComponentInfo( + it.getPos(), + 0, + 0, + it.getPowerAmount(), + it.getCapacity(), + it.getRange() + ))); + grid.producers.forEach(it -> infoMap.put( + it.getPos(), + new PowerComponentInfo( + it.getPos(), + 0, + it.getOutputPower(), + 0, + 0, + it.getRange() + ))); + grid.consumers.forEach(it -> infoMap.put( + it.getPos(), + new PowerComponentInfo( + it.getPos(), + it.getInputPower(), + 0, + 0, + 0, + it.getRange() + ))); + grid.transmitters.forEach(it -> infoMap.put( + it.getPos(), + new PowerComponentInfo( + it.getPos(), + 0, + 0, + 0, + 0, + it.getRange() + ))); + infoMap.values().forEach(it -> { + this.ranges.put(it.pos(), it.range()); + this.powerComponentInfoList.add(it); + }); this.consume = grid.consume; this.generate = grid.generate; } - public void addRange(@NotNull IPowerComponent component) { - this.ranges.put(component.getPos(), component.getRange()); - } - /** * @return 获取范围 */ diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/AnvilBlockMixin.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/AnvilBlockMixin.java index 87affe074..39e4cc393 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/AnvilBlockMixin.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/mixin/AnvilBlockMixin.java @@ -11,6 +11,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.AnvilBlock; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.FallingBlock; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; diff --git a/common/src/main/java/dev/dubhe/anvilcraft/mixin/PowerGridInformationRenderMixin.java b/common/src/main/java/dev/dubhe/anvilcraft/mixin/PowerGridInformationRenderMixin.java index 40275385d..2660e1e14 100644 --- a/common/src/main/java/dev/dubhe/anvilcraft/mixin/PowerGridInformationRenderMixin.java +++ b/common/src/main/java/dev/dubhe/anvilcraft/mixin/PowerGridInformationRenderMixin.java @@ -3,6 +3,7 @@ import dev.dubhe.anvilcraft.api.power.IPowerComponent; import dev.dubhe.anvilcraft.api.power.IPowerConsumer; import dev.dubhe.anvilcraft.api.power.IPowerProducer; +import dev.dubhe.anvilcraft.api.power.PowerComponentInfo; import dev.dubhe.anvilcraft.api.power.PowerGrid; import dev.dubhe.anvilcraft.init.ModItems; import net.minecraft.ChatFormatting; @@ -96,17 +97,19 @@ void onHudRender(GuiGraphics guiGraphics, float partialTick, CallbackInfo ci) { if (powerGrids.isEmpty()) return; PowerGrid.SimplePowerGrid grid = powerGrids.get(0); if (grid == null) return; + PowerComponentInfo componentInfo = grid.getMappedPowerComponentInfo().get(pos); final int tooltipPosX = screenWidth / 2 + 10; final int tooltipPosY = screenHeight / 2 + 10; overloaded |= grid.getConsume() > grid.getGenerate(); List lines = new ArrayList<>(); + if (powerComponent instanceof IPowerProducer producer) { lines.add(Component.translatable("tooltip.anvilcraft.grid_information.producer_stats") .setStyle(Style.EMPTY.applyFormat(ChatFormatting.BLUE)) ); lines.add(Component.translatable( "tooltip.anvilcraft.grid_information.output_power", - producer.getOutputPower() + componentInfo == null ? producer.getOutputPower() : componentInfo.produces() ).setStyle(Style.EMPTY.applyFormat(ChatFormatting.GRAY))); } else if (powerComponent instanceof IPowerConsumer consumer) { lines.add(Component.translatable("tooltip.anvilcraft.grid_information.consumer_stats") @@ -114,7 +117,7 @@ void onHudRender(GuiGraphics guiGraphics, float partialTick, CallbackInfo ci) { ); lines.add(Component.translatable( "tooltip.anvilcraft.grid_information.input_power", - consumer.getInputPower() + componentInfo == null ? consumer.getInputPower() : componentInfo.consumes() ).setStyle(Style.EMPTY.applyFormat(ChatFormatting.GRAY))); } List tooltipLines = List.of( @@ -139,9 +142,9 @@ void onHudRender(GuiGraphics guiGraphics, float partialTick, CallbackInfo ci) { anvilCraft$renderInfo( guiGraphics, this.getFont(), - state != null - ? state.getBlock().asItem().getDefaultInstance() - : ModItems.MAGNETOELECTRIC_CORE.asStack(), + state != null + ? state.getBlock().asItem().getDefaultInstance() + : ModItems.MAGNETOELECTRIC_CORE.asStack(), lines, tooltipPosX, tooltipPosY