Skip to content

Commit e6fe903

Browse files
committed
[WIP]: fix grid chunk loading and unloading
1 parent dd1e6a5 commit e6fe903

4 files changed

Lines changed: 20 additions & 70 deletions

File tree

src/main/java/falseresync/vivatech/common/Vivatech.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public void onInitialize() {
8282
serverGridsLoader.close();
8383
});
8484

85+
ServerLifecycleEvents.AFTER_SAVE.register((server, flush, force) -> {
86+
serverGridsLoader.save();
87+
});
88+
8589
ServerTickEvents.END_WORLD_TICK.register(world -> {
8690
serverGridsLoader.tick(world);
8791
});

src/main/java/falseresync/vivatech/common/power/Grid.java

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package falseresync.vivatech.common.power;
22

3-
import falseresync.vivatech.common.VivatechUtil;
43
import it.unimi.dsi.fastutil.objects.Object2ReferenceRBTreeMap;
54
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
65
import net.minecraft.block.AbstractFireBlock;
@@ -27,7 +26,6 @@ public class Grid {
2726
private final SimpleGraph<GridVertex, GridEdge> graph;
2827
private final Map<BlockPos, Appliance> appliances;
2928
private final Map<ChunkPos, Set<BlockPos>> trackedChunks;
30-
private final Set<ChunkPos> unloadedChunks;
3129
private final GridsManager gridsManager;
3230
private final ServerWorld world;
3331
private final WireType wireType;
@@ -43,7 +41,6 @@ public Grid(GridsManager gridsManager, ServerWorld world, WireType wireType) {
4341
graph = new SimpleGraph<>(GridEdge.class);
4442
appliances = new Object2ReferenceRBTreeMap<>();
4543
trackedChunks = PowerSystem.createChunkPosKeyedMap();
46-
unloadedChunks = PowerSystem.createChunkPosSet();
4744
}
4845

4946
public Grid(GridsManager gridsManager, ServerWorld world, WireType wireType, Set<GridEdge> edges) {
@@ -92,7 +89,7 @@ private void initOrMerge(GridVertex vertex) {
9289
var otherGrid = gridsManager.getGridLookup().get(vertex.pos());
9390
if (otherGrid == null) {
9491
graph.addVertex(vertex);
95-
onVertexAdded(vertex, false);
92+
onVertexAdded(vertex, true);
9693
} else if (otherGrid != this) {
9794
merge(otherGrid.graph);
9895
gridsManager.getGrids().remove(otherGrid);
@@ -102,7 +99,7 @@ private void initOrMerge(GridVertex vertex) {
10299
private void merge(Graph<GridVertex, GridEdge> otherGraph) {
103100
Graphs.addGraph(this.graph, otherGraph);
104101
for (var vertex : otherGraph.vertexSet()) {
105-
onVertexAdded(vertex, true);
102+
onVertexAdded(vertex, false);
106103
}
107104
}
108105

@@ -149,26 +146,17 @@ private void partition() {
149146
} else {
150147
var other = gridsManager.create(wireType);
151148
other.merge(isolatedGraph);
152-
var iterator = unloadedChunks.iterator();
153-
while (iterator.hasNext()) {
154-
var chunkPos = iterator.next();
155-
if (other.tracksChunk(chunkPos)) {
156-
other.unloadedChunks.add(chunkPos);
157-
other.frozen = true;
158-
iterator.remove();
159-
}
160-
}
161149
}
162150
}
163151

164152
gridsManager.getGrids().remove(this);
165153
}
166154

167-
private void onVertexAdded(GridVertex vertex, boolean transferred) {
155+
private void onVertexAdded(GridVertex vertex, boolean shouldInitialize) {
168156
gridsManager.getGridLookup().put(vertex.pos(), this);
169157
if (vertex.appliance() != null) {
170158
if (!appliances.containsValue(vertex.appliance())) {
171-
onApplianceAdded(vertex.appliance(), transferred);
159+
onApplianceAdded(vertex.appliance(), shouldInitialize);
172160
var appliancePos = vertex.appliance().getPos();
173161
trackedChunks.computeIfAbsent(new ChunkPos(appliancePos), key -> new ObjectOpenHashSet<>()).add(appliancePos);
174162
}
@@ -177,7 +165,7 @@ private void onVertexAdded(GridVertex vertex, boolean transferred) {
177165

178166
private void onApplianceAdded(Appliance appliance, boolean shouldInitialize) {
179167
appliances.put(appliance.getPos(), appliance);
180-
if (!shouldInitialize) {
168+
if (shouldInitialize) {
181169
appliance.onGridConnected();
182170
}
183171
}
@@ -225,42 +213,22 @@ private void onWireRemoved(GridEdge edge) {
225213
serverWire.drop(world, wireType);
226214
}
227215

228-
public boolean isFrozen() {
229-
return frozen;
230-
}
231-
232-
public boolean tracksChunk(ChunkPos chunkPos) {
233-
return trackedChunks.containsKey(chunkPos);
234-
}
235-
236-
public void onChunkLoaded(ChunkPos chunkPos) {
237-
unloadedChunks.remove(chunkPos);
238-
if (frozen && unloadedChunks.isEmpty()) {
239-
frozen = false;
240-
refreshApplianceReferences();
241-
appliances.values().forEach(Appliance::onGridUnfrozen);
242-
}
243-
}
244-
245-
private void refreshApplianceReferences() {
246-
for (GridVertex oldVertex : graph.vertexSet()) {
247-
if (oldVertex.appliance() != null) {
248-
var appliance = PowerSystem.APPLIANCE.find(world, oldVertex.appliance().getPos(), null);
249-
VivatechUtil.replaceVertexUndirected(graph, oldVertex, new GridVertex(oldVertex.pos(), appliance));
250-
onApplianceAdded(appliance, true);
216+
private void checkIfFrozen() {
217+
for (ChunkPos chunkPos : trackedChunks.keySet()) {
218+
if (!world.shouldTickBlocksInChunk(chunkPos.toLong())) {
219+
frozen = true;
220+
return;
251221
}
252222
}
223+
frozen = false;
253224
}
254225

255-
public void onChunkUnloaded(ChunkPos chunkPos) {
256-
unloadedChunks.add(chunkPos);
257-
if (!frozen) {
258-
frozen = true;
259-
appliances.values().forEach(Appliance::onGridFrozen);
226+
public void tick() {
227+
checkIfFrozen();
228+
if (frozen) {
229+
return;
260230
}
261-
}
262231

263-
public void tick() {
264232
float generation = 0;
265233
float consumption = 0;
266234
for (var appliance : appliances.values()) {

src/main/java/falseresync/vivatech/common/power/GridsManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public GridsManager(ServerWorld world) {
4242

4343
public void tick() {
4444
for (Grid grid : grids) {
45-
if (!grid.isFrozen()) {
46-
grid.tick();
47-
}
45+
grid.tick();
4846
}
4947
}
5048

src/main/java/falseresync/vivatech/common/power/ServerGridsLoader.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package falseresync.vivatech.common.power;
22

3-
import falseresync.vivatech.common.VivatechUtil;
43
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
5-
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
64
import net.minecraft.nbt.NbtCompound;
75
import net.minecraft.nbt.NbtIo;
86
import net.minecraft.nbt.NbtOps;
@@ -28,24 +26,6 @@ public class ServerGridsLoader {
2826

2927
public ServerGridsLoader(MinecraftServer server) {
3028
this.server = server;
31-
32-
VivatechUtil.CHUNK_STOP_TICKING.register((world, chunkPos) -> {
33-
for (var grid : getGridsManager(world).getGrids()) {
34-
if (grid.tracksChunk(chunkPos)) {
35-
grid.onChunkUnloaded(chunkPos);
36-
}
37-
}
38-
});
39-
40-
VivatechUtil.CHUNK_START_TICKING.register((world, chunkPos) -> {
41-
for (var grid : getGridsManager(world).getGrids()) {
42-
if (grid.tracksChunk(chunkPos)) {
43-
grid.onChunkLoaded(chunkPos);
44-
}
45-
}
46-
});
47-
48-
ServerLifecycleEvents.AFTER_SAVE.register((_ignored, flush, force) -> save());
4929
load();
5030
}
5131

0 commit comments

Comments
 (0)