Skip to content

Commit

Permalink
Merge pull request #436 from ZhuRuoLing/releases/1.20.1
Browse files Browse the repository at this point in the history
电网电量使用信息同步
  • Loading branch information
Gugle2308 authored Apr 28, 2024
2 parents d7e88eb + cf268af commit ebb6adb
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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<PowerComponentInfo> 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));
}
90 changes: 58 additions & 32 deletions common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
* 电网
Expand Down Expand Up @@ -334,54 +327,50 @@ 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));

private final int hash;
private final String level;
private final BlockPos pos;
private Map<BlockPos, Integer> ranges = new HashMap<>();
private final Map<BlockPos, Integer> ranges = new HashMap<>();
private List<BlockPos> blocks = new ArrayList<>();
private List<Integer> rangeByIndex = new ArrayList<>();
private final List<PowerComponentInfo> powerComponentInfoList = new ArrayList<>();
private int generate = 0; // 发电功率
private int consume = 0; // 耗电功率

private final Map<BlockPos, PowerComponentInfo> mappedPowerComponentInfo = new HashMap<>();

public SimplePowerGrid(

Check warning on line 349 in common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /home/runner/work/AnvilCraft/AnvilCraft/common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java:349:9: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
int hash,
String level,
BlockPos pos,
List<BlockPos> blocks,
List<Integer> ranges,
List<PowerComponentInfo> 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());
});
}

/**
* @param buf 缓冲区
*/
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 -> {
Expand Down Expand Up @@ -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<BlockPos, PowerComponentInfo> 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 获取范围
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,25 +97,27 @@ 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<Component> 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")
.setStyle(Style.EMPTY.applyFormat(ChatFormatting.BLUE))
);
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<Component> tooltipLines = List.of(
Expand All @@ -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
Expand Down

0 comments on commit ebb6adb

Please sign in to comment.