Skip to content

Commit 87ad894

Browse files
committed
Check maxChunkLoadDistance for block entities
1 parent 58d332b commit 87ad894

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.minecraft.world.level.Level;
2121
import net.minecraft.world.level.block.entity.BlockEntity;
2222
import net.minecraft.world.level.block.state.BlockState;
23+
import net.minecraft.world.level.chunk.ChunkStatus;
2324
import net.minecraft.world.level.chunk.LevelChunk;
2425
import net.minecraft.world.level.chunk.LevelChunkSection;
2526
import net.minecraft.world.level.chunk.PalettedContainer;
@@ -76,29 +77,42 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
7677
ByteArrayOutputStream baos = new ByteArrayOutputStream();
7778
BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
7879

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+
7990
// Save and compress block entities
8091
int count = friendlyByteBuf.readVarInt();
8192
for (int i = 0; i < count; i++) {
8293
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);
84108
if (blockEntity != null) {
85109
CompoundTag tag = blockEntity.saveWithoutMetadata();
86110
blockEntityMap.put(pos, CompressedBlockEntity.compress(tag, baos));
87111
}
88112
}
89113

90-
int playerSectionX = player.getBlockX() >> 4;
91-
int playerSectionZ = player.getBlockZ() >> 4;
92-
93114
Long2ObjectMap<PalettedContainer<BlockState>> sections = new Long2ObjectOpenHashMap<>();
94115

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-
102116
if (maxChunkLoadDistance > 0) {
103117
count = friendlyByteBuf.readVarInt();
104118
for (int i = 0; i < count; i++) {

0 commit comments

Comments
 (0)