1515import net .minecraft .core .BlockPos ;
1616import net .minecraft .core .Direction ;
1717import net .minecraft .nbt .CompoundTag ;
18- import net .minecraft .network .Connection ;
19- import net .minecraft .network .protocol .game .ClientboundBlockEntityDataPacket ;
2018import net .minecraft .server .MinecraftServer ;
2119import net .minecraft .server .level .ServerLevel ;
2220import net .minecraft .world .level .ChunkPos ;
3331
3432public class CompactMachineBlockEntity extends BlockEntity implements ICapabilityProvider {
3533 public int machineId = -1 ;
36- private final boolean initialized = false ;
3734 public long nextSpawnTick = 0 ;
3835
3936 protected UUID owner ;
4037 protected String schema ;
4138 protected boolean locked = false ;
39+ private ChunkPos roomChunk ;
4240 private LazyOptional <IMachineRoom > room = LazyOptional .empty ();
4341
4442 public CompactMachineBlockEntity (BlockPos pos , BlockState state ) {
@@ -51,8 +49,7 @@ public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable D
5149 if (cap == Capabilities .ROOM ) return room .cast ();
5250
5351 if (level instanceof ServerLevel sl ) {
54- return room .map (r -> {
55- var roomId = r .getChunk ();
52+ return getInternalChunkPos ().map (roomId -> {
5653 try {
5754 final var serv = sl .getServer ();
5855
@@ -165,6 +162,9 @@ public CompoundTag getUpdateTag() {
165162
166163 public Optional <ChunkPos > getInternalChunkPos () {
167164 if (level instanceof ServerLevel ) {
165+ if (roomChunk != null )
166+ return Optional .of (roomChunk );
167+
168168 MinecraftServer serv = level .getServer ();
169169 if (serv == null )
170170 return Optional .empty ();
@@ -173,7 +173,9 @@ public Optional<ChunkPos> getInternalChunkPos() {
173173 if (connections == null )
174174 return Optional .empty ();
175175
176- return connections .getConnectedRoom (this .machineId );
176+ var chunk = connections .getConnectedRoom (this .machineId );
177+ chunk .ifPresent (c -> this .roomChunk = c );
178+ return chunk ;
177179 }
178180
179181 return Optional .empty ();
@@ -194,16 +196,6 @@ public void handleUpdateTag(CompoundTag tag) {
194196 owner = tag .getUUID ("owner" );
195197 }
196198
197- @ Override
198- public void onDataPacket (Connection net , ClientboundBlockEntityDataPacket pkt ) {
199- BlockState state = null ;
200- if (this .level != null ) {
201- state = this .level .getBlockState (worldPosition );
202- }
203-
204- super .onDataPacket (net , pkt );
205- }
206-
207199 public Optional <UUID > getOwnerUUID () {
208200 return Optional .ofNullable (this .owner );
209201 }
@@ -215,6 +207,7 @@ public void setOwner(UUID owner) {
215207 public void setMachineId (int id ) {
216208 this .machineId = id ;
217209 this .room .invalidate ();
210+ this .roomChunk = null ;
218211 this .setChanged ();
219212 }
220213
0 commit comments