Skip to content

Commit

Permalink
微调
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Apr 17, 2024
1 parent 558d110 commit aa5a132
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
55 changes: 42 additions & 13 deletions common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.api.power;

import dev.dubhe.anvilcraft.AnvilCraft;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -11,6 +12,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/**
Expand All @@ -19,7 +21,9 @@
@SuppressWarnings("unused")
public class PowerGrid {
public static boolean isServerClosing = false;
public static final Set<PowerGrid> GRID_SET = Collections.synchronizedSet(new HashSet<>());
@Getter
private static Set<PowerGrid> gridSetClient = Collections.synchronizedSet(new HashSet<>());
public static final Set<PowerGrid> GRID_SET = new HashSet<>();
public static final int GRID_TICK = 40;
public static int cooldown = 0;
@Getter
Expand All @@ -35,6 +39,20 @@ public class PowerGrid {
@Getter
private BlockPos pos = null;

/**
* @return 获取电网中的元件数量
*/
public int getComponentCount() {
return this.transmitters.size() + this.producers.size() + this.consumers.size() + this.storages.size();
}

/**
* @return 该电网是否为空电网
*/
public boolean isEmpty() {
return this.getComponentCount() <= 0;
}

/**
* 总电力刻
*/
Expand All @@ -43,10 +61,14 @@ public static void tickGrid() {
cooldown--;
return;
}
for (PowerGrid grid : PowerGrid.GRID_SET) {
Iterator<PowerGrid> iterator = PowerGrid.GRID_SET.iterator();
while (iterator.hasNext()) {
PowerGrid grid = iterator.next();
if (grid.isEmpty()) iterator.remove();
grid.tick();
}
cooldown = GRID_TICK;
gridSetClient = Set.copyOf(GRID_SET);
}

/**
Expand Down Expand Up @@ -154,11 +176,15 @@ private void addRange(IPowerComponent component) {
* @param components 元件
*/
public static void removeComponent(IPowerComponent @NotNull ... components) {
if (PowerGrid.isServerClosing) return;
for (IPowerComponent component : components) {
PowerGrid grid = component.getGrid();
if (grid == null) return;
grid.remove(component);
try {
if (PowerGrid.isServerClosing) return;
for (IPowerComponent component : components) {
PowerGrid grid = component.getGrid();
if (grid == null) return;
grid.remove(component);
}
} catch (Exception e) {
AnvilCraft.LOGGER.error(e.getMessage(), e);
}
}

Expand All @@ -168,15 +194,15 @@ public static void removeComponent(IPowerComponent @NotNull ... components) {
* @param components 电力元件
*/
public void remove(IPowerComponent @NotNull ... components) {
Set<IPowerComponent> set = new HashSet<>();
Set<IPowerComponent> set = new LinkedHashSet<>();
this.transmitters.stream().filter(this::clearGrid).forEach(set::add);
this.transmitters.clear();
this.storages.stream().filter(this::clearGrid).forEach(set::add);
this.storages.clear();
this.producers.stream().filter(this::clearGrid).forEach(set::add);
this.producers.clear();
this.consumers.stream().filter(this::clearGrid).forEach(set::add);
this.consumers.clear();
this.transmitters.stream().filter(this::clearGrid).forEach(set::add);
this.transmitters.clear();
for (IPowerComponent component : components) {
set.remove(component);
}
Expand All @@ -195,9 +221,13 @@ private boolean clearGrid(@NotNull IPowerComponent component) {
* @param grid 电网
*/
public void merge(@NotNull PowerGrid grid) {
grid.producers.forEach(producer -> producer.setGrid(this));
this.producers.addAll(grid.producers);
grid.consumers.forEach(producer -> producer.setGrid(this));
this.consumers.addAll(grid.consumers);
grid.storages.forEach(producer -> producer.setGrid(this));
this.storages.addAll(grid.storages);
grid.transmitters.forEach(producer -> producer.setGrid(this));
this.transmitters.addAll(grid.transmitters);
}

Expand All @@ -206,10 +236,9 @@ public void merge(@NotNull PowerGrid grid) {
* @return 元件是否在电网范围内
*/
public boolean isInRange(@NotNull IPowerComponent component) {
BlockPos vec3 = component.getPos();
BlockPos center = this.getPos();
BlockPos vec3 = component.getPos().subtract(this.getPos());
VoxelShape range = Shapes.join(
this.range.move(center.getX(), center.getY(), center.getZ()),
this.range,
component.getRange().move(vec3.getX(), vec3.getY(), vec3.getZ()),
BooleanOp.AND
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ private TransmissionPoleBlockEntity(BlockEntityType<?> type, BlockPos pos, Block
@Override
public @NotNull PowerComponentType getComponentType() {
if (this.getLevel() == null) return PowerComponentType.INVALID;
BlockState state = this.getLevel().getBlockState(this.getPos());
if (!state.is(ModBlocks.TRANSMISSION_POLE.get())) return PowerComponentType.INVALID;
if (state.getValue(TransmissionPoleBlock.HALF) != Half.TOP) return PowerComponentType.INVALID;
if (!this.getBlockState().is(ModBlocks.TRANSMISSION_POLE.get())) return PowerComponentType.INVALID;
if (this.getBlockState().getValue(TransmissionPoleBlock.HALF) != Half.TOP) return PowerComponentType.INVALID;
return PowerComponentType.TRANSMITTER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PowerGridRenderer {
public static void render(PoseStack poseStack, VertexConsumer consumer, double camX, double camY, double camZ) {
if (Minecraft.getInstance().level == null) return;
RandomSource random = Minecraft.getInstance().level.random;
for (PowerGrid grid : PowerGrid.GRID_SET) {
for (PowerGrid grid : PowerGrid.getGridSetClient()) {
random.setSeed(grid.hashCode());
PowerGridRenderer.renderOutline(
poseStack, consumer, camX, camY, camZ,
Expand Down

0 comments on commit aa5a132

Please sign in to comment.