From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Thu, 5 Sep 2024 15:42:15 -0700 Subject: [PATCH] Moonrise: Optimise checkInsideBlocks Original license: GPLv3 Original project: https://github.com/Tuinity/Moonrise https://github.com/Tuinity/Moonrise/commit/3fa0ff67a7c165936c5fcef366eb3a14737ab77a This is Dreeam's tiny modified version, should be around 1~2+ times faster. Retrieve blocks more efficiently diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java index 39be345524b621e6ae2eec9af9d46da3291c671b..db0133b8e5e4e26d3f5ca48f87d6d49f8cff0532 100644 --- a/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java @@ -1718,6 +1718,87 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess // CraftBukkit end protected void checkInsideBlocks() { + // Moonrise start - Optimise checkInsideBlocks + if (true) { + final AABB boundingBox = this.getBoundingBox(); + + final int minBlockX = Mth.floor(boundingBox.minX + ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_EPSILON); + final int minBlockY = Mth.floor(boundingBox.minY + ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_EPSILON); + final int minBlockZ = Mth.floor(boundingBox.minZ + ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_EPSILON); + + final int maxBlockX = Mth.floor(boundingBox.maxX - ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_EPSILON); + final int maxBlockY = Mth.floor(boundingBox.maxY - ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_EPSILON); + final int maxBlockZ = Mth.floor(boundingBox.maxZ - ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.COLLISION_EPSILON); + + final int minChunkX = minBlockX >> 4; + final int minChunkY = minBlockY >> 4; + final int minChunkZ = minBlockZ >> 4; + + final int maxChunkX = maxBlockX >> 4; + final int maxChunkY = maxBlockY >> 4; + final int maxChunkZ = maxBlockZ >> 4; + + final Level world = this.level; + + final int minSection = ((ca.spottedleaf.moonrise.patches.collisions.world.CollisionLevel) world).moonrise$getMinSection(); + final net.minecraft.world.level.chunk.ChunkSource chunkSource = world.getChunkSource(); + final BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos(); + + for (int currChunkZ = minChunkZ; currChunkZ <= maxChunkZ; ++currChunkZ) { + for (int currChunkX = minChunkX; currChunkX <= maxChunkX; ++currChunkX) { + final net.minecraft.world.level.chunk.ChunkAccess chunk = chunkSource.getChunk(currChunkX, currChunkZ, net.minecraft.world.level.chunk.status.ChunkStatus.FULL, false); + + if (chunk == null) { + continue; + } + + final net.minecraft.world.level.chunk.LevelChunkSection[] sections = chunk.getSections(); + + for (int currChunkY = minChunkY; currChunkY <= maxChunkY; ++currChunkY) { + final int sectionIdx = currChunkY - minSection; + if (sectionIdx < 0 || sectionIdx >= sections.length) { + continue; + } + final net.minecraft.world.level.chunk.LevelChunkSection section = sections[sectionIdx]; + if (section.hasOnlyAir()) { + // empty + continue; + } + + final net.minecraft.world.level.chunk.PalettedContainer blocks = section.states; + + final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) : 0; + final int maxXIterate = currChunkX == maxChunkX ? (maxBlockX & 15) : 15; + final int minZIterate = currChunkZ == minChunkZ ? (minBlockZ & 15) : 0; + final int maxZIterate = currChunkZ == maxChunkZ ? (maxBlockZ & 15) : 15; + final int minYIterate = currChunkY == minChunkY ? (minBlockY & 15) : 0; + final int maxYIterate = currChunkY == maxChunkY ? (maxBlockY & 15) : 15; + + for (int currY = minYIterate; currY <= maxYIterate; ++currY) { + mutablePos.setY(currY | (currChunkY << 4)); + for (int currZ = minZIterate; currZ <= maxZIterate; ++currZ) { + mutablePos.setZ(currZ | (currChunkZ << 4)); + for (int currX = minXIterate; currX <= maxXIterate; ++currX) { + mutablePos.setX(currX | (currChunkX << 4)); + + final BlockState blockState = blocks.get((currX) | (currZ << 4) | ((currY) << 8)); + + if (!this.isAlive()) { + return; + } + + blockState.entityInside(world, mutablePos, this); + this.onInsideBlock(blockState); + } + } + } + } + } + } + + return; + } + // Moonrise end - Optimise checkInsideBlocks AABB axisalignedbb = this.getBoundingBox(); BlockPos blockposition = BlockPos.containing(axisalignedbb.minX + 1.0E-7D, axisalignedbb.minY + 1.0E-7D, axisalignedbb.minZ + 1.0E-7D); BlockPos blockposition1 = BlockPos.containing(axisalignedbb.maxX - 1.0E-7D, axisalignedbb.maxY - 1.0E-7D, axisalignedbb.maxZ - 1.0E-7D);