From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: M2ke4U <79621885+MrHua269@users.noreply.github.com> Date: Sun, 26 Nov 2023 16:35:37 +0800 Subject: [PATCH] Pufferfish Reduce projectile chunk loading diff --git a/src/main/java/me/earthme/luminol/LuminolConfig.java b/src/main/java/me/earthme/luminol/LuminolConfig.java index d0c0b4daec59f23a989a8b8f66ea3c704b0e309c..11c1a367fbc25cb63738a00ad93fb0b0b3500e7d 100644 --- a/src/main/java/me/earthme/luminol/LuminolConfig.java +++ b/src/main/java/me/earthme/luminol/LuminolConfig.java @@ -46,6 +46,8 @@ public class LuminolConfig { public static boolean reduceSensorWork = true; public static boolean enableSuffocationOptimization = true; + public static int maxProjectileLoadsPerTick; + public static int maxProjectileLoadsPerProjectile; public static void init() throws IOException { PARENT_FOLDER.mkdir(); @@ -126,6 +128,8 @@ public class LuminolConfig { reduceSensorWork = get("optimizations.reduce_sensor_work",reduceSensorWork,"This optimization is from petal.You can find out more about it on petal's repository"); enableSuffocationOptimization = get("optimizations.optimize_suffocation_check",enableSuffocationOptimization); initEntityTTL(); + maxProjectileLoadsPerTick = get("optimizations.projectile.max-loads-per-tick", maxProjectileLoadsPerTick, "Controls how many chunks are allowed \nto be sync loaded by projectiles in a tick."); + maxProjectileLoadsPerProjectile = get("optimizations.projectile.max-loads-per-projectile", maxProjectileLoadsPerProjectile, "Controls how many chunks a projectile \n can load in its lifetime before it gets \nautomatically removed."); } public static T get(String key,T def){ diff --git a/src/main/java/net/minecraft/world/entity/projectile/Projectile.java b/src/main/java/net/minecraft/world/entity/projectile/Projectile.java index 0c8f90a904c01105ba5fa6a8037150697bc2621e..527168199605c8c606c6dde563e4fe096b6f17ac 100644 --- a/src/main/java/net/minecraft/world/entity/projectile/Projectile.java +++ b/src/main/java/net/minecraft/world/entity/projectile/Projectile.java @@ -4,6 +4,8 @@ import com.google.common.base.MoreObjects; import java.util.Iterator; import java.util.UUID; import javax.annotation.Nullable; + +import io.papermc.paper.threadedregions.TickRegionScheduler; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.Packet; @@ -44,6 +46,36 @@ public abstract class Projectile extends Entity implements TraceableEntity { super(type, world); } + // Pufferfish start + private static long loadedThisTick = 0; + private static long loadedTick; + + private int loadedLifetime = 0; + @Override + public void setPos(double x, double y, double z) { + long currentTick = TickRegionScheduler.getCurrentRegion().getData().getCurrentTick(); + if (loadedTick != currentTick) { + loadedTick = currentTick; + loadedThisTick = 0; + } + int previousX = Mth.floor(this.getX()) >> 4, previousZ = Mth.floor(this.getZ()) >> 4; + int newX = Mth.floor(x) >> 4, newZ = Mth.floor(z) >> 4; + if (previousX != newX || previousZ != newZ) { + boolean isLoaded = ((net.minecraft.server.level.ServerChunkCache) this.level().getChunkSource()).getChunkAtIfLoadedMainThread(newX, newZ) != null; + if (!isLoaded) { + if (Projectile.loadedThisTick > me.earthme.luminol.LuminolConfig.maxProjectileLoadsPerTick) { + if (++this.loadedLifetime > me.earthme.luminol.LuminolConfig.maxProjectileLoadsPerProjectile) { + this.discard(); + } + return; + } + Projectile.loadedThisTick++; + } + } + super.setPos(x, y, z); + } + // Pufferfish end + public void setOwner(@Nullable Entity entity) { if (entity != null) { this.ownerUUID = entity.getUUID();