Skip to content

Commit f3924a2

Browse files
Partial fixes for chunk loading, made machines autoload if they have a tunnel. (#580)
* Updates chunk loading upgrade, made machines autoload if they have a tunnel.
1 parent 543a06d commit f3924a2

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/main/java/dev/compactmods/machines/machine/CompactMachineBlockEntity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import dev.compactmods.machines.machine.graph.CompactMachineNode;
1010
import dev.compactmods.machines.machine.graph.legacy.LegacyMachineConnections;
1111
import dev.compactmods.machines.room.graph.CompactMachineRoomNode;
12+
import dev.compactmods.machines.tunnel.TunnelHelper;
1213
import dev.compactmods.machines.tunnel.TunnelWallEntity;
1314
import dev.compactmods.machines.tunnel.graph.TunnelConnectionGraph;
1415
import dev.compactmods.machines.tunnel.graph.TunnelNode;
@@ -237,6 +238,7 @@ public void syncConnectedRoom() {
237238
.ifPresent(roomNode -> this.roomNode = new WeakReference<>(roomNode));
238239

239240
this.setChanged();
241+
forceLoadIfHasTunnels();
240242
}
241243
}
242244

@@ -259,6 +261,18 @@ public void disconnect() {
259261
}
260262
}
261263

264+
public void forceLoadIfHasTunnels() {
265+
if(level == null || roomChunk == null) return;
266+
267+
if (level instanceof ServerLevel sl) {
268+
final var compactDim = CompactDimension.forServer(sl.getServer());
269+
if (compactDim == null) return;
270+
final var tunnelGraph = TunnelConnectionGraph.forRoom(compactDim, roomChunk);
271+
TunnelHelper.setChunkMode(compactDim, roomChunk, tunnelGraph.tunnels().findAny().isPresent());
272+
}
273+
}
274+
275+
262276
public Stream<BlockPos> getTunnels(Direction dir) {
263277
if(level == null || roomChunk == null) return Stream.empty();
264278

src/main/java/dev/compactmods/machines/tunnel/TunnelHelper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package dev.compactmods.machines.tunnel;
22

3+
import dev.compactmods.machines.api.core.Constants;
34
import net.minecraft.core.Direction;
5+
import net.minecraft.server.level.ServerLevel;
6+
import net.minecraft.server.level.TicketType;
7+
import net.minecraft.world.level.ChunkPos;
48

59
import javax.annotation.Nonnull;
610
import java.util.Collection;
11+
import java.util.Comparator;
712
import java.util.Optional;
813
import java.util.Set;
914
import java.util.stream.Stream;
1015

1116
public class TunnelHelper {
17+
private static final TicketType<ChunkPos> CM4_TUUNEL_LOAD_TYPE = TicketType.create(Constants.MOD_ID + ":tunnels", Comparator.comparingLong(ChunkPos::toLong));
1218

1319
@Nonnull
1420
public static Direction getNextDirection(Direction in) {
@@ -33,4 +39,14 @@ public static Optional<Direction> getNextDirection(Direction current, Set<Direct
3339

3440
return stream.skip(found + 1).filter(dir -> !used.contains(dir)).findFirst();
3541
}
42+
43+
public static void setChunkMode(ServerLevel level, ChunkPos room, boolean forceLoad) {
44+
final var chunks = level.getChunkSource();
45+
level.setChunkForced(room.x, room.z, forceLoad);
46+
if (forceLoad)
47+
chunks.addRegionTicket(CM4_TUUNEL_LOAD_TYPE, room, 2, room);
48+
else
49+
chunks.removeRegionTicket(CM4_TUUNEL_LOAD_TYPE, room, 2, room);
50+
chunks.save(true);
51+
}
3652
}

src/main/java/dev/compactmods/machines/upgrade/ChunkloadUpgrade.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dev.compactmods.machines.api.room.upgrade.ILevelLoadedUpgradeListener;
77
import dev.compactmods.machines.api.room.upgrade.RoomUpgrade;
88
import net.minecraft.resources.ResourceLocation;
9+
import net.minecraft.server.commands.ForceLoadCommand;
910
import net.minecraft.server.level.ServerLevel;
1011
import net.minecraft.server.level.TicketType;
1112
import net.minecraft.world.level.ChunkPos;
@@ -50,14 +51,14 @@ private void forceLoad(ServerLevel level, ChunkPos room) {
5051
final var chunks = level.getChunkSource();
5152
level.setChunkForced(room.x, room.z, true);
5253
chunks.addRegionTicket(CM4_LOAD_TYPE, room, 2, room);
53-
chunks.save(false);
54+
chunks.save(true);
5455
}
5556

5657
private void normalLoad(ServerLevel level, ChunkPos room) {
5758
final var chunks = level.getChunkSource();
5859
level.setChunkForced(room.x, room.z, false);
5960
chunks.removeRegionTicket(CM4_LOAD_TYPE, room, 2, room);
60-
chunks.save(false);
61+
chunks.save(true);
6162
}
6263
}
6364

0 commit comments

Comments
 (0)