From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Thu, 24 Nov 2022 12:00:55 +0100 Subject: [PATCH] Reduce projectile chunk loading License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) Gale - https://galemc.org This patch is based on the following patch: "Reduce projectile chunk loading" By: Paul Sauve As part of: Airplane (https://github.com/TECHNOVE/Airplane) Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) * Airplane copyright * Airplane Copyright (C) 2020 Technove LLC This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . diff --git a/net/minecraft/world/entity/projectile/Projectile.java b/net/minecraft/world/entity/projectile/Projectile.java index 4487c03183d20a187d391dd124abb7b926508b5b..84c846d2ef4990befb2891631ac5ae16d881401b 100644 --- a/net/minecraft/world/entity/projectile/Projectile.java +++ b/net/minecraft/world/entity/projectile/Projectile.java @@ -53,6 +53,55 @@ public abstract class Projectile extends Entity implements TraceableEntity { super(entityType, level); } + // Gale start - Airplane - reduce projectile chunk loading + private static int chunksLoadedThisTick = 0; + private static int chunksLoadedInTick; + private int chunksLoadedByProjectile = 0; + + @Override + public void setPos(double x, double y, double z) { + int currentTick = net.minecraft.server.MinecraftServer.currentTick; + + if (chunksLoadedInTick != currentTick) { + chunksLoadedInTick = currentTick; + chunksLoadedThisTick = 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()).getChunkAtIfLoadedImmediately(newX, newZ) != null; + + if (!isLoaded) { + var maxProjectileChunkLoadsConfig = this.level().galeConfig().smallOptimizations.maxProjectileChunkLoads; + int maxChunkLoadsPerTick = maxProjectileChunkLoadsConfig.perTick; + + if (maxChunkLoadsPerTick >= 0 && chunksLoadedThisTick > maxChunkLoadsPerTick) { + return; + } + + int maxChunkLoadsPerProjectile = maxProjectileChunkLoadsConfig.perProjectile.max; + + if (maxChunkLoadsPerProjectile >= 0 && this.chunksLoadedByProjectile >= maxChunkLoadsPerProjectile) { + if (maxProjectileChunkLoadsConfig.perProjectile.removeFromWorldAfterReachLimit) { + this.discard(); + } else if (maxProjectileChunkLoadsConfig.perProjectile.resetMovementAfterReachLimit) { + this.setDeltaMovement(0, this.getDeltaMovement().y, 0); + } + + return; + } + + chunksLoadedThisTick++; + this.chunksLoadedByProjectile++; + } + } + + super.setPos(x, y, z); + } + // Gale end - Airplane - reduce projectile chunk loading + public void setOwner(@Nullable Entity owner) { if (owner != null) { this.ownerUUID = owner.getUUID();