Skip to content

Commit

Permalink
Merge pull request #875 from Gu-ZT/releases/1.20.1
Browse files Browse the repository at this point in the history
尝试修复电网的多线程问题
  • Loading branch information
Gu-ZT authored Jun 11, 2024
2 parents 5377432 + 52527cd commit ae6d016
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 27 deletions.
24 changes: 14 additions & 10 deletions common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -26,18 +27,18 @@
@SuppressWarnings("unused")
public class PowerGrid {
public static boolean isServerClosing = false;
public static final Map<Level, Set<PowerGrid>> GRID_MAP = new HashMap<>();
public static final Map<Level, Set<PowerGrid>> GRID_MAP = Collections.synchronizedMap(new HashMap<>());
public static final int GRID_TICK = 20;
@Getter
public boolean remove = false;
@Getter
private int generate = 0; // 发电功率
@Getter
private int consume = 0; // 耗电功率
final Set<IPowerProducer> producers = new HashSet<>(); // 发电机
final Set<IPowerConsumer> consumers = new HashSet<>(); // 用电器
final Set<IPowerStorage> storages = new HashSet<>(); // 储电
final Set<IPowerTransmitter> transmitters = new HashSet<>(); // 中继
final Set<IPowerProducer> producers = Collections.synchronizedSet(new HashSet<>()); // 发电机
final Set<IPowerConsumer> consumers = Collections.synchronizedSet(new HashSet<>()); // 用电器
final Set<IPowerStorage> storages = Collections.synchronizedSet(new HashSet<>()); // 储电
final Set<IPowerTransmitter> transmitters = Collections.synchronizedSet(new HashSet<>()); // 中继
@Getter
private VoxelShape shape = null;
@Getter
Expand Down Expand Up @@ -73,11 +74,13 @@ public boolean isEmpty() {
public static void tickGrid() {
for (Set<PowerGrid> grids : PowerGrid.GRID_MAP.values()) {
Iterator<PowerGrid> iterator = grids.iterator();
Set<PowerGrid> remove = Collections.synchronizedSet(new HashSet<>());
while (iterator.hasNext()) {
PowerGrid grid = iterator.next();
if (grid.isEmpty()) iterator.remove();
if (grid.isEmpty()) remove.add(grid);
grid.tick();
}
grids.removeAll(remove);
}
}

Expand All @@ -97,7 +100,7 @@ protected void tick() {
}
} else {
int need = this.consume - this.generate;
Set<IPowerStorage> storages = new HashSet<>();
Set<IPowerStorage> storages = Collections.synchronizedSet(new HashSet<>());
for (IPowerStorage storage : this.storages) {
need -= storage.getOutputPower();
storages.add(storage);
Expand All @@ -116,7 +119,7 @@ protected void tick() {
}

private void gridTick() {
HashSet<IPowerComponent> components = new HashSet<>();
Set<IPowerComponent> components = Collections.synchronizedSet(new HashSet<>());
components.addAll(this.transmitters);
components.addAll(this.consumers);
components.addAll(this.storages);
Expand Down Expand Up @@ -283,16 +286,18 @@ public static void addComponent(IPowerComponent @NotNull ... components) {
PowerGrid grid = null;
Set<PowerGrid> grids = PowerGrid.getGridSet(component.getCurrentLevel());
Iterator<PowerGrid> iterator = grids.iterator();
Set<PowerGrid> remove = Collections.synchronizedSet(new HashSet<>());
while (iterator.hasNext()) {
PowerGrid grid1 = iterator.next();
if (!grid1.isInRange(component)) continue;
if (grid == null) grid = grid1;
else {
grid.merge(grid1);
iterator.remove();
remove.add(grid1);
new PowerGridRemovePack(grid1).broadcast();
}
}
grids.removeAll(remove);
if (grid == null) grid = new PowerGrid(component.getCurrentLevel());
grid.add(component);
grids.add(grid);
Expand Down Expand Up @@ -321,5 +326,4 @@ public static Set<PowerGrid> getGridSet(Level level) {
public static void clear() {
PowerGrid.GRID_MAP.values().forEach(Collection::clear);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public VoxelShape getShape() {
* 寻找电网
*/
public static List<SimplePowerGrid> findPowerGrid(BlockPos pos) {
return PowerGridRenderer.getGrids()
return PowerGridRenderer.getGridMap()
.values().stream()
.filter(it -> it.blocks.stream().anyMatch(it1 -> it1.equals(pos)))
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,19 @@ public void onPlace(
@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos,
@NotNull BlockState oldState, boolean movedByPiston
) {
level.scheduleTick(pos, this, this.getDelayAfterPlace());
if (state.hasProperty(HALF)) {
level.scheduleTick(pos.subtract(state.getValue(HALF).getOffset()), this, this.getDelayAfterPlace());
}
}

@Override
public @NotNull BlockState updateShape(
@NotNull BlockState state, @NotNull Direction direction, @NotNull BlockState neighborState,
@NotNull LevelAccessor level, @NotNull BlockPos pos, @NotNull BlockPos neighborPos
) {
level.scheduleTick(pos, this, this.getDelayAfterPlace());
if (state.hasProperty(HALF)) {
level.scheduleTick(pos.subtract(state.getValue(HALF).getOffset()), this, this.getDelayAfterPlace());
}
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import dev.dubhe.anvilcraft.api.power.SimplePowerGrid;
import lombok.Getter;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
Expand All @@ -19,8 +18,11 @@

@Environment(EnvType.CLIENT)
public class PowerGridRenderer {
@Getter
private static Map<Integer, SimplePowerGrid> grids = Collections.synchronizedMap(new HashMap<>());
private static final Map<Integer, SimplePowerGrid> GRID_MAP = Collections.synchronizedMap(new HashMap<>());

public static Map<Integer, SimplePowerGrid> getGridMap() {
return PowerGridRenderer.GRID_MAP;
}

/**
* 渲染
Expand All @@ -29,7 +31,7 @@ public static void render(PoseStack poseStack, VertexConsumer consumer, double c
if (Minecraft.getInstance().level == null) return;
RandomSource random = Minecraft.getInstance().level.random;
String level = Minecraft.getInstance().level.dimension().location().toString();
for (SimplePowerGrid grid : PowerGridRenderer.grids.values()) {
for (SimplePowerGrid grid : PowerGridRenderer.GRID_MAP.values()) {
if (!grid.getLevel().equals(level)) continue;
random.setSeed(grid.getHash());
PowerGridRenderer.renderOutline(
Expand All @@ -41,7 +43,7 @@ public static void render(PoseStack poseStack, VertexConsumer consumer, double c
}

public static void clearAllGrid() {
grids.clear();
GRID_MAP.clear();
}

@SuppressWarnings("SameParameterValue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.NotNull;
Expand All @@ -36,10 +37,22 @@ public void accept(Entity oldEntity, Entity newEntity) {
for (InteractionHand value : InteractionHand.values()) {
ItemStack itemStack = o.getItemInHand(value);
if (itemStack.is(Items.ANVIL)
|| itemStack.is(Items.CHIPPED_ANVIL)
|| itemStack.is(Items.DAMAGED_ANVIL)) {
|| itemStack.is(Items.CHIPPED_ANVIL)
|| itemStack.is(Items.DAMAGED_ANVIL)) {
o.setItemInHand(value, ModBlocks.GIANT_ANVIL.asItem().getDefaultInstance());
n.setItemInHand(value, ModBlocks.GIANT_ANVIL.asItem().getDefaultInstance());
if (n instanceof Mob mob) {
mob.setDropChance(
value == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND,
1.0f
);
}
if (o instanceof Mob mob) {
mob.setDropChance(
value == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND,
1.0f
);
}
}
}
}
Expand All @@ -65,16 +78,16 @@ public void accept(Entity oldEntity, Entity newEntity) {
*/
public static TransformOptions fromJson(JsonObject jsonObject) {
return CODEC.decode(JsonOps.INSTANCE, jsonObject)
.getOrThrow(false, s -> {
}).getFirst();
.getOrThrow(false, s -> {
}).getFirst();
}

/**
*
*/
public JsonElement toJson() {
return CODEC.encodeStart(JsonOps.INSTANCE, this)
.getOrThrow(false, s -> {
});
.getOrThrow(false, s -> {
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public void encode(@NotNull FriendlyByteBuf buf) {
@Override
@Environment(EnvType.CLIENT)
public void handler() {
Minecraft.getInstance().execute(() -> PowerGridRenderer.getGrids().remove(this.grid));
Minecraft.getInstance().execute(() -> PowerGridRenderer.getGridMap().remove(this.grid));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public void encode(@NotNull FriendlyByteBuf buf) {
@Override
@Environment(EnvType.CLIENT)
public void handler() {
Minecraft.getInstance().execute(() -> PowerGridRenderer.getGrids().put(this.grid.getHash(), this.grid));
Minecraft.getInstance().execute(() -> PowerGridRenderer.getGridMap().put(this.grid.getHash(), this.grid));
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod_description = The mod is an anvil-centric vanilla survival expansion
contributors = - Phoupraw\n- DancingSnow\n- NightFish\n- BOTXue\n- wdfaESfaef\n- ZhuRuoLing\n- YocyCraft\n- Bi2Nb9O3\n- LemoMew\n- Cjsah
supporters = - BOTXue\n- Fengyuan__\n- Take\n- Moe_Lobster
mod_license = LGPL-3.0 license
mod_version = 1.4.0
mod_version = 1.4.0+pre-release.2
mod_url = https://github.com/Anvil-Dev/AnvilCraft

maven_group=dev.dubhe
Expand Down

0 comments on commit ae6d016

Please sign in to comment.