9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-23 08:59:23 +00:00

Moonrise: Optimise checkInsideBlocks

This commit is contained in:
Dreeam
2024-10-20 14:01:07 -04:00
parent 743c6f7c38
commit f890915133

View File

@@ -0,0 +1,167 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
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
Retrieve blocks more efficiently
diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java
index efcd9057f008f0b9cf0d22b2b21d1851205841e5..8daa4ca8968cafbd320936c706abe3a2161dd75c 100644
--- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java
+++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/ChunkSystemLevel.java
@@ -19,4 +19,6 @@ public interface ChunkSystemLevel {
public void moonrise$midTickTasks();
+ public boolean moonrise$areChunksLoaded(final int fromX, final int fromZ, final int toX, final int toZ); // Moonrise - Optimise checkInsideBlocks
+
}
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 9ef8919e4cdac61e2e4dd2fe96aed96cb1d5959e..f3190e4b548769f56f07bae06204bb18dbc88ece 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -615,6 +615,23 @@ public class ServerLevel extends Level implements WorldGenLevel, ca.spottedleaf.
this.preciseTime = this.serverLevelData.getDayTime(); // Purpur
}
+ // Moonrise start - Optimise checkInsideBlocks
+ @Override
+ public final boolean moonrise$areChunksLoaded(final int fromX, final int fromZ, final int toX, final int toZ) {
+ final net.minecraft.server.level.ServerChunkCache chunkSource = ((ServerLevel) this).getChunkSource();
+
+ for (int currZ = fromZ; currZ <= toZ; ++currZ) {
+ for (int currX = fromX; currX <= toX; ++currX) {
+ if (!chunkSource.hasChunk(currX, currZ)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+ // Moonrise end - Optimise checkInsideBlocks
+
// Paper start
@Override
public boolean hasChunk(int chunkX, int chunkZ) {
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 39be345524b621e6ae2eec9af9d46da3291c671b..085925162f5099609c9b82ca0103d8dd5d2cb22d 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -1718,6 +1718,83 @@ 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;
+
+ if (!((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel) world).moonrise$areChunksLoaded(minChunkX, minChunkZ, maxChunkX, maxChunkZ)) {
+ return;
+ }
+
+ 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.LevelChunkSection[] sections = chunkSource.getChunk(currChunkX, currChunkZ, net.minecraft.world.level.chunk.status.ChunkStatus.FULL, false).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<BlockState> 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, (Entity) this);
+ this.onInsideBlock(blockState);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ // 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);
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index a40aec3a8adf39a94d62b776e845cfc193084bbb..7f7e1b56f8844bbc54cf0aec1a52c9766e105865 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -236,6 +236,23 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl
return (CraftServer) Bukkit.getServer();
}
+ // Moonrise start - Optimise checkInsideBlocks
+ @Override
+ public boolean moonrise$areChunksLoaded(final int fromX, final int fromZ, final int toX, final int toZ) {
+ final net.minecraft.server.level.ServerChunkCache chunkSource = ((ServerLevel) this).getChunkSource();
+
+ for (int currZ = fromZ; currZ <= toZ; ++currZ) {
+ for (int currX = fromX; currX <= toX; ++currX) {
+ if (!chunkSource.hasChunk(currX, currZ)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+ // Moonrise end - Optimise checkInsideBlocks
+
// Paper start - Use getChunkIfLoadedImmediately
@Override
public boolean hasChunk(int chunkX, int chunkZ) {