Skip to content

Commit

Permalink
Merge pull request #1005 from dmzz-yyhyy/grid_fix_twice
Browse files Browse the repository at this point in the history
再次修复电网tick
  • Loading branch information
Gu-ZT authored Jul 4, 2024
2 parents 615fbc1 + ae5353c commit df77a57
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions common/src/main/java/dev/dubhe/anvilcraft/api/power/PowerGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -27,7 +26,7 @@
@SuppressWarnings("unused")
public class PowerGrid {
public static boolean isServerClosing = false;
public static final Map<Level, Set<PowerGrid>> GRID_MAP = Collections.synchronizedMap(new HashMap<>());
private static final Map<Level, Set<PowerGrid>> GRID_MAP = Collections.synchronizedMap(new HashMap<>());
public static final int GRID_TICK = 20;
@Getter
public boolean remove = false;
Expand Down Expand Up @@ -73,13 +72,11 @@ 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() || grid.isRemove()) remove.add(grid);
grid.tick();
}
grids.forEach(powerGrid -> {
if (powerGrid.isEmpty() || powerGrid.isRemove()) remove.add(powerGrid);
powerGrid.tick();
});
grids.removeAll(remove);
}
}
Expand Down Expand Up @@ -280,27 +277,27 @@ public boolean isInRange(@NotNull IPowerComponent component) {
* @param components 元件
*/
public static void addComponent(IPowerComponent @NotNull ... components) {

for (IPowerComponent component : components) {
if (component.getComponentType() == PowerComponentType.INVALID) continue;
PowerGrid grid = null;
final 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.isRemove() || !grid1.isInRange(component)) continue;
if (grid == null) grid = grid1;
grids.forEach(powerGrid -> {
if (powerGrid.isRemove() || !powerGrid.isInRange(component)) return;
if (grid[0] == null) grid[0] = powerGrid;
else {
grid.merge(grid1);
remove.add(grid1);
new PowerGridRemovePack(grid1).broadcast();
grid[0].merge(powerGrid);
remove.add(powerGrid);
new PowerGridRemovePack(powerGrid).broadcast();
}
}
});
grids.removeAll(remove);
if (grid == null) grid = new PowerGrid(component.getCurrentLevel());
grid.add(component);
grids.add(grid);
if (grid[0] == null) grid[0] = new PowerGrid(component.getCurrentLevel());
grid[0].add(component);
grids.add(grid[0]);
}

}

/**
Expand All @@ -313,7 +310,7 @@ public static Set<PowerGrid> getGridSet(Level level) {
if (PowerGrid.GRID_MAP.containsKey(level)) {
return PowerGrid.GRID_MAP.get(level);
} else {
Set<PowerGrid> grids = new HashSet<>();
Set<PowerGrid> grids = Collections.synchronizedSet(new HashSet<>());
PowerGrid.GRID_MAP.put(level, grids);
return grids;
}
Expand Down

0 comments on commit df77a57

Please sign in to comment.