Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

电网信息层显示 #416

Merged
merged 6 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ public interface IPowerStorage extends IPowerProducer, IPowerConsumer {
*/
int extract(int power);


/**
* 获取已存储的电量
*
* @return 电量值
*/
int getPowerAmount();

/**
* 获取储电容量
*
* @return 储电容量
*/
int getCapacity();

@Override
default @NotNull PowerComponentType getComponentType() {
return PowerComponentType.STORAGE;
Expand Down
117 changes: 92 additions & 25 deletions common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
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;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -13,13 +23,18 @@
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
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 @@ -182,9 +197,9 @@
BlockPos center = this.pos;
BlockPos vec3 = component.getPos();
VoxelShape range = component.getShape().move(
vec3.getX() - center.getX(),
vec3.getY() - center.getY(),
vec3.getZ() - center.getZ()
vec3.getX() - center.getX(),
vec3.getY() - center.getY(),
vec3.getZ() - center.getZ()
);
this.shape = Shapes.join(this.shape, range, BooleanOp.OR);
}
Expand Down Expand Up @@ -255,9 +270,9 @@
public boolean isInRange(@NotNull IPowerComponent component) {
BlockPos vec3 = component.getPos().subtract(this.getPos());
VoxelShape range = Shapes.join(
this.shape,
component.getShape().move(vec3.getX(), vec3.getY(), vec3.getZ()),
BooleanOp.AND
this.shape,
component.getShape().move(vec3.getX(), vec3.getY(), vec3.getZ()),
BooleanOp.AND
);
return !range.isEmpty();
}
Expand Down Expand Up @@ -314,29 +329,72 @@

@Getter
public static class SimplePowerGrid {

public static final Codec<SimplePowerGrid> CODEC = RecordCodecBuilder.create(ins -> ins.group(
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
.listOf()
.fieldOf("blocks")
.forGetter(o -> o.blocks),
Codec.INT
.listOf()
.fieldOf("rangeByIndex")
.forGetter(o -> o.rangeByIndex),
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 final Map<BlockPos, Integer> ranges = new HashMap<>();
private Map<BlockPos, Integer> ranges = new HashMap<>();
private List<BlockPos> blocks = new ArrayList<>();
private List<Integer> rangeByIndex = new ArrayList<>();
private int generate = 0; // 发电功率
private int consume = 0; // 耗电功率

public SimplePowerGrid(

Check warning on line 358 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:358: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,
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;
}

/**
* @param buf 缓冲区
*/
public void encode(@NotNull FriendlyByteBuf buf) {
buf.writeInt(this.hash);
buf.writeUtf(this.level);
buf.writeBlockPos(this.pos);
Set<Map.Entry<BlockPos, Integer>> set = this.ranges.entrySet();
buf.writeVarInt(set.size());
for (Map.Entry<BlockPos, Integer> entry : set) {
buf.writeBlockPos(entry.getKey());
buf.writeInt(entry.getValue());
}
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 -> {
});
CompoundTag data = new CompoundTag();
data.put("data", tag);
buf.writeNbt(data);
}

/**
* @param buf 缓冲区
*/
@Deprecated
public SimplePowerGrid(@NotNull FriendlyByteBuf buf) {
this.hash = buf.readInt();
this.level = buf.readUtf();
Expand All @@ -358,6 +416,8 @@
grid.producers.forEach(this::addRange);
grid.consumers.forEach(this::addRange);
grid.transmitters.forEach(this::addRange);
this.consume = grid.consume;
this.generate = grid.generate;
}

public void addRange(@NotNull IPowerComponent component) {
Expand All @@ -369,20 +429,27 @@
*/
public VoxelShape getShape() {
return this.ranges.entrySet().stream().map(entry -> Shapes
.box(
-entry.getValue(), -entry.getValue(), -entry.getValue(),
entry.getValue() + 1, entry.getValue() + 1, entry.getValue() + 1
)
.move(
this.offset(entry.getKey()).getX(),
this.offset(entry.getKey()).getY(),
this.offset(entry.getKey()).getZ()
)
.box(
-entry.getValue(), -entry.getValue(), -entry.getValue(),
entry.getValue() + 1, entry.getValue() + 1, entry.getValue() + 1
)
.move(
this.offset(entry.getKey()).getX(),
this.offset(entry.getKey()).getY(),
this.offset(entry.getKey()).getZ()
)
).reduce((v1, v2) -> Shapes.join(v1, v2, BooleanOp.OR)).orElse(Shapes.block());
}

private @NotNull BlockPos offset(@NotNull BlockPos pos) {
return pos.subtract(this.pos);
}

public static List<SimplePowerGrid> findPowerGrid(BlockPos pos) {

Check warning on line 448 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:448:9: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
return PowerGridRenderer.getGrids()
.values().stream()
.filter(it -> it.blocks.stream().anyMatch(it1 -> it1.equals(pos)))
.toList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
Expand Down Expand Up @@ -89,8 +90,9 @@ private void attract(BlockState state, @NotNull Level level, @NotNull BlockPos m
BlockState state1 = level.getBlockState(currentPos);

if (state1.is(BlockTags.ANVIL)) {
level.setBlock(currentPos, state1.getFluidState().createLegacyBlock(), 3);
level.setBlockAndUpdate(magnetPos.below(), state1);
level.setBlockAndUpdate(currentPos, Blocks.AIR.defaultBlockState());

AnimateAscendingBlockEntity.animate(level, currentPos, state1, magnetPos.below());
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,16 @@ public static void init(RegistrateLangProvider provider) {
+ ".base_slot_description",
"Put "
);
provider.add("tooltip.anvilcraft.grid_information.title", "Power Grid Stats:");
provider.add("tooltip.anvilcraft.grid_information.producer_stats", "Power Producer Stats:");
provider.add("tooltip.anvilcraft.grid_information.consumer_stats", "Power Consumer Stats:");
provider.add("tooltip.anvilcraft.grid_information.output_power", " Power Generation: %d");
provider.add("tooltip.anvilcraft.grid_information.input_power", " Power Consumption: %d");
provider.add("tooltip.anvilcraft.grid_information.total_consumed", " Total Consumption: %d");
provider.add("tooltip.anvilcraft.grid_information.total_generated", " Total Generation: %d");
provider.add("tooltip.anvilcraft.grid_information.utilization", " Power Utilization: %s");
provider.add("tooltip.anvilcraft.grid_information.overloaded1", "It appears that this grid is overloaded.");
provider.add("tooltip.anvilcraft.grid_information.overloaded2", "Add more sources or remove the components");
provider.add("tooltip.anvilcraft.grid_information.overloaded3", "with a high stress impact.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void onPlace(
}
if (!level.isEmptyBlock(magnet.below())) return;
level.setBlockAndUpdate(magnet.below(), state);
level.setBlockAndUpdate(anvil, state.getFluidState().createLegacyBlock());
level.setBlockAndUpdate(anvil, Blocks.AIR.defaultBlockState());
AnimateAscendingBlockEntity.animate(level, anvil, state, magnet.below());
}
}
Expand Down
Loading
Loading