11package falseresync .vivatech .common .power ;
22
3- import falseresync .vivatech .common .VivatechUtil ;
43import it .unimi .dsi .fastutil .objects .Object2ReferenceRBTreeMap ;
54import it .unimi .dsi .fastutil .objects .ObjectOpenHashSet ;
65import 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 ()) {
0 commit comments