|
20 | 20 | import net.minecraft.world.level.Level; |
21 | 21 | import net.minecraft.world.level.block.entity.BlockEntity; |
22 | 22 | import net.minecraft.world.level.block.state.BlockState; |
| 23 | +import net.minecraft.world.level.chunk.ChunkStatus; |
23 | 24 | import net.minecraft.world.level.chunk.LevelChunk; |
24 | 25 | import net.minecraft.world.level.chunk.LevelChunkSection; |
25 | 26 | import net.minecraft.world.level.chunk.PalettedContainer; |
@@ -76,29 +77,42 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk |
76 | 77 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
77 | 78 | BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos(); |
78 | 79 |
|
| 80 | + int maxChunkLoadDistance = this.plugin.configuration.getInt("max-chunk-load-distance"); |
| 81 | + |
| 82 | + // Don't allow loading chunks outside render distance for plot worlds |
| 83 | + if (PlotSquaredIntegration.isPlotWorld(level.getWorld())) { |
| 84 | + maxChunkLoadDistance = 0; |
| 85 | + } |
| 86 | + |
| 87 | + int playerSectionX = player.getBlockX() >> 4; |
| 88 | + int playerSectionZ = player.getBlockZ() >> 4; |
| 89 | + |
79 | 90 | // Save and compress block entities |
80 | 91 | int count = friendlyByteBuf.readVarInt(); |
81 | 92 | for (int i = 0; i < count; i++) { |
82 | 93 | long pos = friendlyByteBuf.readLong(); |
83 | | - BlockEntity blockEntity = level.getBlockEntity(mutableBlockPos.set(pos)); |
| 94 | + mutableBlockPos.set(pos); |
| 95 | + |
| 96 | + if (level.isOutsideBuildHeight(mutableBlockPos)) continue; |
| 97 | + |
| 98 | + int chunkX = mutableBlockPos.getX() >> 4; |
| 99 | + int chunkZ = mutableBlockPos.getZ() >> 4; |
| 100 | + |
| 101 | + int distance = Math.abs(playerSectionX - chunkX) + Math.abs(playerSectionZ - chunkZ); |
| 102 | + boolean canLoad = distance <= maxChunkLoadDistance; |
| 103 | + |
| 104 | + LevelChunk chunk = (LevelChunk) level.getChunk(chunkX, chunkZ, ChunkStatus.FULL, canLoad); |
| 105 | + if (chunk == null) continue; |
| 106 | + |
| 107 | + BlockEntity blockEntity = chunk.getBlockEntity(mutableBlockPos, LevelChunk.EntityCreationType.IMMEDIATE); |
84 | 108 | if (blockEntity != null) { |
85 | 109 | CompoundTag tag = blockEntity.saveWithoutMetadata(); |
86 | 110 | blockEntityMap.put(pos, CompressedBlockEntity.compress(tag, baos)); |
87 | 111 | } |
88 | 112 | } |
89 | 113 |
|
90 | | - int playerSectionX = player.getBlockX() >> 4; |
91 | | - int playerSectionZ = player.getBlockZ() >> 4; |
92 | | - |
93 | 114 | Long2ObjectMap<PalettedContainer<BlockState>> sections = new Long2ObjectOpenHashMap<>(); |
94 | 115 |
|
95 | | - int maxChunkLoadDistance = this.plugin.configuration.getInt("max-chunk-load-distance"); |
96 | | - |
97 | | - // Don't allow loading chunks outside render distance for plot worlds |
98 | | - if (PlotSquaredIntegration.isPlotWorld(level.getWorld())) { |
99 | | - maxChunkLoadDistance = 0; |
100 | | - } |
101 | | - |
102 | 116 | if (maxChunkLoadDistance > 0) { |
103 | 117 | count = friendlyByteBuf.readVarInt(); |
104 | 118 | for (int i = 0; i < count; i++) { |
|
0 commit comments