Skip to content

Commit d9ae165

Browse files
committed
WIP - cleaning up machine block chunkloading
1 parent f75f3a2 commit d9ae165

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/main/java/dev/compactmods/machines/block/CompactMachineTile.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,43 @@ public CompactMachineTile(BlockPos pos, BlockState state) {
4545
@NotNull
4646
@Override
4747
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
48-
if(cap == Capabilities.ROOM) return room.cast();
49-
if(cap == Capabilities.ROOM_CAPS) return caps.cast();
48+
if (cap == Capabilities.ROOM) return room.cast();
49+
if (cap == Capabilities.ROOM_CAPS) return caps.cast();
5050

5151
return caps.lazyMap(c -> c.getCapability(cap, side))
5252
.orElse(super.getCapability(cap, side));
5353
}
5454

55+
@Nullable
56+
private IMachineRoom getRoom() {
57+
if (level instanceof ServerLevel sl) {
58+
return getInternalChunkPos().map(c -> {
59+
var inChunk = sl.getChunk(c.x, c.z);
60+
return inChunk.getCapability(Capabilities.ROOM).orElseThrow(RuntimeException::new);
61+
}).orElse(null);
62+
}
63+
64+
return null;
65+
}
66+
67+
@Nullable
68+
private IRoomCapabilities getRoomCapabilities() {
69+
if (level instanceof ServerLevel sl) {
70+
return getInternalChunkPos().map(c -> {
71+
var inChunk = sl.getChunk(c.x, c.z);
72+
return inChunk.getCapability(Capabilities.ROOM_CAPS).orElseThrow(RuntimeException::new);
73+
}).orElse(null);
74+
}
75+
76+
return null;
77+
}
78+
5579
@Override
5680
public void onLoad() {
5781
super.onLoad();
5882

59-
if(level instanceof ServerLevel sl) {
60-
getInternalChunkPos().ifPresent(c -> {
61-
var inChunk = sl.getChunk(c.x, c.z);
62-
this.room = inChunk.getCapability(Capabilities.ROOM);
63-
this.caps = inChunk.getCapability(Capabilities.ROOM_CAPS);
64-
});
65-
}
83+
this.room = LazyOptional.of(this::getRoom);
84+
this.caps = LazyOptional.of(this::getRoomCapabilities);
6685
}
6786

6887
@Override
@@ -173,6 +192,8 @@ public void setOwner(UUID owner) {
173192

174193
public void setMachineId(int id) {
175194
this.machineId = id;
195+
this.room.invalidate();
196+
this.caps.invalidate();
176197
this.setChanged();
177198
}
178199

0 commit comments

Comments
 (0)