From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samsuik Date: Sun, 18 May 2025 20:42:03 +0100 Subject: [PATCH] Optimise block counting for cannon entities diff --git a/ca/spottedleaf/moonrise/patches/collisions/CollisionUtil.java b/ca/spottedleaf/moonrise/patches/collisions/CollisionUtil.java index 54d561e1b4069df7115457accb0763fa7586dddc..29f66ef8355a95b2869988187bf74536343f2ec8 100644 --- a/ca/spottedleaf/moonrise/patches/collisions/CollisionUtil.java +++ b/ca/spottedleaf/moonrise/patches/collisions/CollisionUtil.java @@ -1941,6 +1941,7 @@ public final class CollisionUtil { final CollisionContext collisionShape = new LazyEntityCollisionContext(entity); final boolean useEntityCollisionShape = LazyEntityCollisionContext.useEntityCollisionShape(world, entity); final long gameTime = world.getGameTime(); // Sakura - load chunks on movement + final boolean cannonEntity = entity != null && (entity.isFallingBlock || entity.isPrimedTNT); // Sakura - optimise block counting for cannon entities // special cases: if (minBlockY > maxBlockY) { @@ -2004,15 +2005,19 @@ public final class CollisionUtil { final boolean hasSpecial = !fullBlocks && section.moonrise$hasSpecialCollidingBlocks(); // Sakura - collide with non-solid blocks final int sectionAdjust = !hasSpecial ? 1 : 0; + // Sakura start - optimise block counting for cannon entities + final boolean hasMovingBlocks = section.hasMovingPistonBlocks(); + final int sectionAdjustCE = cannonEntity && !hasMovingBlocks || !cannonEntity && !hasSpecial ? 1 : 0; final PalettedContainer blocks = section.states; - final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) + sectionAdjust : 0; - final int maxXIterate = currChunkX == maxChunkX ? (maxBlockX & 15) - sectionAdjust : 15; - final int minZIterate = currChunkZ == minChunkZ ? (minBlockZ & 15) + sectionAdjust : 0; - final int maxZIterate = currChunkZ == maxChunkZ ? (maxBlockZ & 15) - sectionAdjust : 15; + final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) + sectionAdjustCE : 0; + final int maxXIterate = currChunkX == maxChunkX ? (maxBlockX & 15) - sectionAdjustCE : 15; + final int minZIterate = currChunkZ == minChunkZ ? (minBlockZ & 15) + sectionAdjustCE : 0; + final int maxZIterate = currChunkZ == maxChunkZ ? (maxBlockZ & 15) - sectionAdjustCE : 15; final int minYIterate = currChunkY == minChunkY ? (minBlockY & 15) + sectionAdjust : 0; - final int maxYIterate = currChunkY == maxChunkY ? (maxBlockY & 15) - sectionAdjust : 15; + final int maxYIterate = currChunkY == maxChunkY ? (maxBlockY & 15) - sectionAdjustCE : 15; + // Sakura end - optimise block counting for cannon entities for (int currY = minYIterate; currY <= maxYIterate; ++currY) { final int blockY = currY | (currChunkY << 4); diff --git a/net/minecraft/world/level/Level.java b/net/minecraft/world/level/Level.java index 6804055e9344eb2ea0b2dd6318231963376791c2..3a3d22835df907a85c38cd178c1db9d96554b8e0 100644 --- a/net/minecraft/world/level/Level.java +++ b/net/minecraft/world/level/Level.java @@ -621,6 +621,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl BlockPos selected = null; double selectedDistance = Double.MAX_VALUE; final Vec3 entityPos = entity.position(); + final boolean cannonEntity = entity.isFallingBlock || entity.isPrimedTNT; // Sakura - optimise block counting for cannon entities // special cases: if (minBlockY > maxBlockY) { @@ -663,15 +664,19 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl final boolean hasSpecial = ((ca.spottedleaf.moonrise.patches.block_counting.BlockCountingChunkSection)section).moonrise$hasSpecialCollidingBlocks(); final int sectionAdjust = !hasSpecial ? 1 : 0; + // Sakura start - optimise block counting for cannon entities + final boolean hasMovingBlocks = section.hasMovingPistonBlocks(); + final int sectionAdjustCE = cannonEntity && !hasMovingBlocks || !cannonEntity && !hasSpecial ? 1 : 0; final net.minecraft.world.level.chunk.PalettedContainer blocks = section.states; - final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) + sectionAdjust : 0; - final int maxXIterate = currChunkX == maxChunkX ? (maxBlockX & 15) - sectionAdjust : 15; - final int minZIterate = currChunkZ == minChunkZ ? (minBlockZ & 15) + sectionAdjust : 0; - final int maxZIterate = currChunkZ == maxChunkZ ? (maxBlockZ & 15) - sectionAdjust : 15; + final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) + sectionAdjustCE : 0; + final int maxXIterate = currChunkX == maxChunkX ? (maxBlockX & 15) - sectionAdjustCE : 15; + final int minZIterate = currChunkZ == minChunkZ ? (minBlockZ & 15) + sectionAdjustCE : 0; + final int maxZIterate = currChunkZ == maxChunkZ ? (maxBlockZ & 15) - sectionAdjustCE : 15; final int minYIterate = currChunkY == minChunkY ? (minBlockY & 15) + sectionAdjust : 0; - final int maxYIterate = currChunkY == maxChunkY ? (maxBlockY & 15) - sectionAdjust : 15; + final int maxYIterate = currChunkY == maxChunkY ? (maxBlockY & 15) - sectionAdjustCE : 15; + // Sakura end - optimise block counting for cannon entities for (int currY = minYIterate; currY <= maxYIterate; ++currY) { final int blockY = currY | (currChunkY << 4); diff --git a/net/minecraft/world/level/chunk/LevelChunkSection.java b/net/minecraft/world/level/chunk/LevelChunkSection.java index 029d224557613b46f015785a5bbffe49a6f39ec6..a13771119488202fa0ad27121616f82553be968c 100644 --- a/net/minecraft/world/level/chunk/LevelChunkSection.java +++ b/net/minecraft/world/level/chunk/LevelChunkSection.java @@ -43,6 +43,13 @@ public class LevelChunkSection implements ca.spottedleaf.moonrise.patches.block_ return this.tickingBlocks; } // Paper end - block counting + // Sakura start - optimise block counting for cannon entities + private short movingPistonBlocks; + + public final boolean hasMovingPistonBlocks() { + return this.movingPistonBlocks != 0; + } + // Sakura end - optimise block counting for cannon entities private LevelChunkSection(LevelChunkSection section) { this.nonEmptyBlockCount = section.nonEmptyBlockCount; @@ -113,6 +120,18 @@ public class LevelChunkSection implements ca.spottedleaf.moonrise.patches.block_ } } + // Sakura start - optimise block counting for cannon entities + final boolean isMovingPistonOld = oldState.is(net.minecraft.world.level.block.Blocks.MOVING_PISTON); + final boolean isMovingPistonNew = newState.is(net.minecraft.world.level.block.Blocks.MOVING_PISTON); + if (isMovingPistonOld != isMovingPistonNew) { + if (isMovingPistonOld) { + --this.movingPistonBlocks; + } else { + ++this.movingPistonBlocks; + } + } + // Sakura end - optimise block counting for cannon entities + final boolean oldTicking = oldState.isRandomlyTicking(); final boolean newTicking = newState.isRandomlyTicking(); if (oldTicking != newTicking) { @@ -188,6 +207,7 @@ public class LevelChunkSection implements ca.spottedleaf.moonrise.patches.block_ this.tickingBlockCount = (short)0; this.tickingFluidCount = (short)0; this.specialCollidingBlocks = (short)0; + this.movingPistonBlocks = (short) 0; // Sakura - optimise block counting for cannon entities this.tickingBlocks.clear(); if (this.maybeHas((final BlockState state) -> !state.isAir())) { @@ -216,6 +236,12 @@ public class LevelChunkSection implements ca.spottedleaf.moonrise.patches.block_ continue; } + // Sakura start - optimise block counting for cannon entities + if (state.is(net.minecraft.world.level.block.Blocks.MOVING_PISTON)) { + this.movingPistonBlocks += (short) paletteCount; + } + // Sakura end - optimise block counting for cannon entities + if (ca.spottedleaf.moonrise.patches.collisions.CollisionUtil.isSpecialCollidingBlock(state)) { this.specialCollidingBlocks += (short)paletteCount; }