1212import dev .compactmods .crafting .api .field .MiniaturizationFieldSize ;
1313import dev .compactmods .crafting .api .recipe .IMiniaturizationRecipe ;
1414import dev .compactmods .crafting .crafting .CraftingHelper ;
15+ import dev .compactmods .crafting .events .WorldEventHandler ;
1516import dev .compactmods .crafting .network .FieldActivatedPacket ;
1617import dev .compactmods .crafting .network .FieldDeactivatedPacket ;
1718import dev .compactmods .crafting .network .FieldRecipeChangedPacket ;
2223import dev .compactmods .crafting .recipes .blocks .RecipeBlocks ;
2324import dev .compactmods .crafting .server .ServerConfig ;
2425import dev .compactmods .crafting .util .BlockSpaceUtil ;
26+ import io .reactivex .rxjava3 .disposables .Disposable ;
2527import net .minecraft .block .Blocks ;
2628import net .minecraft .entity .item .ItemEntity ;
2729import net .minecraft .item .Item ;
3537import net .minecraft .util .ResourceLocation ;
3638import net .minecraft .util .math .AxisAlignedBB ;
3739import net .minecraft .util .math .BlockPos ;
40+ import net .minecraft .util .math .ChunkPos ;
3841import net .minecraft .util .math .vector .Vector3i ;
3942import net .minecraft .world .IServerWorld ;
4043import net .minecraft .world .IWorld ;
4144import net .minecraft .world .World ;
45+ import net .minecraft .world .chunk .Chunk ;
4246import net .minecraft .world .gen .feature .template .PlacementSettings ;
4347import net .minecraft .world .gen .feature .template .Template ;
4448import net .minecraft .world .server .ServerWorld ;
@@ -68,13 +72,60 @@ public class MiniaturizationField implements IMiniaturizationField {
6872 private LazyOptional <IMiniaturizationField > lazyReference = LazyOptional .empty ();
6973 private boolean disabled = false ;
7074
75+ private static Disposable CHUNK_LISTENER ;
76+
7177 public MiniaturizationField () {
7278 }
7379
7480 private MiniaturizationField (MiniaturizationFieldSize size , BlockPos center ) {
7581 this .center = center ;
7682 this .size = size ;
7783 this .craftingState = EnumCraftingState .NOT_MATCHED ;
84+
85+ setupChunkListener ();
86+ }
87+
88+ public MiniaturizationField (CompoundNBT nbt ) {
89+ this .craftingState = EnumCraftingState .valueOf (nbt .getString ("state" ));
90+
91+ this .center = NBTUtil .readBlockPos (nbt .getCompound ("center" ));
92+ this .size = MiniaturizationFieldSize .valueOf (nbt .getString ("size" ));
93+
94+ setupChunkListener ();
95+
96+ // temp load recipe
97+ if (nbt .contains ("recipe" )) {
98+ this .recipeId = new ResourceLocation (nbt .getString ("recipe" ));
99+ this .craftingProgress = nbt .getInt ("progress" );
100+ }
101+
102+ if (nbt .contains ("matchedBlocks" )) {
103+ Template t = new Template ();
104+ t .load (nbt .getCompound ("matchedBlocks" ));
105+ this .matchedBlocks = t ;
106+ } else {
107+ this .matchedBlocks = null ;
108+ }
109+
110+ this .disabled = nbt .contains ("disabled" ) && nbt .getBoolean ("disabled" );
111+ }
112+
113+ private void setupChunkListener () {
114+ // add projector and central chunks
115+ final Set <ChunkPos > insideChunks = getProjectorPositions ().map (ChunkPos ::new ).distinct ().collect (Collectors .toSet ());
116+ insideChunks .add (new ChunkPos (center ));
117+
118+ CHUNK_LISTENER = WorldEventHandler .CHUNK_CHANGES .filter (ce -> {
119+ boolean sameLevel = ((Chunk ) ce .getChunk ()).getLevel ().dimension ().equals (level .dimension ());
120+ boolean watchedChunk = insideChunks .contains (ce .getChunk ().getPos ());
121+ return sameLevel && watchedChunk ;
122+ }).subscribe ((changed ) -> this .checkLoaded ());
123+ }
124+
125+ @ Override
126+ public void dispose () {
127+ if (!CHUNK_LISTENER .isDisposed ())
128+ CHUNK_LISTENER .dispose ();
78129 }
79130
80131 public static MiniaturizationField fromSizeAndCenter (MiniaturizationFieldSize fieldSize , BlockPos center ) {
@@ -368,7 +419,7 @@ public boolean isLoaded() {
368419 }
369420
370421 public void checkLoaded () {
371- CompactCrafting .LOGGER .trace ("Checking loaded state." );
422+ CompactCrafting .LOGGER .debug ("Checking loaded state." );
372423 this .loaded = level .isAreaLoaded (center , size .getProjectorDistance () + 3 );
373424
374425 if (loaded ) {
@@ -418,27 +469,6 @@ public CompoundNBT serverData() {
418469 return nbt ;
419470 }
420471
421- @ Override
422- public void loadServerData (CompoundNBT nbt ) {
423- this .craftingState = EnumCraftingState .valueOf (nbt .getString ("state" ));
424-
425- // temp load recipe
426- if (nbt .contains ("recipe" )) {
427- this .recipeId = new ResourceLocation (nbt .getString ("recipe" ));
428- this .craftingProgress = nbt .getInt ("progress" );
429- }
430-
431- if (nbt .contains ("matchedBlocks" )) {
432- Template t = new Template ();
433- t .load (nbt .getCompound ("matchedBlocks" ));
434- this .matchedBlocks = t ;
435- } else {
436- this .matchedBlocks = null ;
437- }
438-
439- this .disabled = nbt .contains ("disabled" ) && nbt .getBoolean ("disabled" );
440- }
441-
442472 @ Override
443473 public void setProgress (int progress ) {
444474 this .craftingProgress = progress ;
@@ -507,7 +537,7 @@ public void setRef(LazyOptional<IMiniaturizationField> lazyReference) {
507537 @ Override
508538 public void disable () {
509539 this .disabled = true ;
510- if (this .craftingState != EnumCraftingState .NOT_MATCHED )
540+ if (this .craftingState != EnumCraftingState .NOT_MATCHED )
511541 handleDestabilize ();
512542
513543 getProjectorPositions ().forEach (proj -> {
@@ -526,7 +556,7 @@ public void enable() {
526556 getProjectorPositions ().forEach (proj -> {
527557 FieldProjectorBlock .activateProjector (level , proj , this .size );
528558 TileEntity projTile = level .getBlockEntity (proj );
529- if (projTile instanceof FieldProjectorTile ) {
559+ if (projTile instanceof FieldProjectorTile ) {
530560 ((FieldProjectorTile ) projTile ).setFieldRef (lazyReference );
531561 }
532562 });
0 commit comments