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/src/main/java/net/minecraft/world/entity/projectile/Projectile.java b/src/main/java/net/minecraft/world/entity/projectile/Projectile.java index 713c11d6547cb02ac4b6a02aec07a8ba68019f3f..ea23e771ab2b77e8001d0eaaf834423353ef70c2 100644 --- a/src/main/java/net/minecraft/world/entity/projectile/Projectile.java +++ b/src/main/java/net/minecraft/world/entity/projectile/Projectile.java @@ -42,6 +42,44 @@ public abstract class Projectile extends Entity { super(type, world); } + // 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()).getChunkAtIfLoadedMainThread(newX, newZ) != null; + if (!isLoaded) { + int maxChunkLoadsPerTick = this.level.galeConfig().smallOptimizations.maxProjectileChunkLoads.perTick; + if (maxChunkLoadsPerTick >= 0 && chunksLoadedThisTick > maxChunkLoadsPerTick) { + return; + } + int maxChunkLoadsPerProjectile = this.level.galeConfig().smallOptimizations.maxProjectileChunkLoads.perProjectile.max; + if (maxChunkLoadsPerProjectile >= 0 && this.chunksLoadedByProjectile >= maxChunkLoadsPerProjectile) { + if (this.level.galeConfig().smallOptimizations.maxProjectileChunkLoads.perProjectile.removeFromWorldAfterReachLimit) { + this.discard(); + } else if (this.level.galeConfig().smallOptimizations.maxProjectileChunkLoads.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 entity) { if (entity != null) { this.ownerUUID = entity.getUUID(); diff --git a/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java b/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java index b82bb95b524c95cdefb81abef906eded0717e9a1..a83675d8d2f94a8e73cc2c7fa11d0aaf6f78be2c 100644 --- a/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java +++ b/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java @@ -33,7 +33,55 @@ public class GaleWorldConfiguration extends ConfigurationPart { public SmallOptimizations smallOptimizations; public class SmallOptimizations extends ConfigurationPart { - public int dummyValue = 0; + // Gale start - Airplane - reduce projectile chunk loading + public MaxProjectileChunkLoads maxProjectileChunkLoads; + public class MaxProjectileChunkLoads extends ConfigurationPart { + + /** + * The maximum number of chunks that can be synchronously loaded by all projectiles in one world in a tick. + * Any value < 0 means no maximum. + *
    + *
  • Default: 10
  • + *
  • Vanilla: -1
  • + *
+ */ + public int perTick = 10; + + public PerProjectile perProjectile; + public class PerProjectile extends ConfigurationPart { + + /** + * The maximum number of chunks that can be synchronously loaded by a projectile throughout its lifetime. + * Any value < 0 means no maximum. + *
    + *
  • Default: 10
  • + *
  • Vanilla: -1
  • + *
+ */ + public int max = 10; + + /** + * Whether to set the planar velocity of projectiles that cross the {@link #max} threshold + * to zero, so that they stop attempting to cross chunk boundaries. + * This has no effect if {@link #removeFromWorldAfterReachLimit} is true. + *
    + *
  • Default: false
  • + *
+ */ + public boolean resetMovementAfterReachLimit = false; + + /** + * Whether to remove projectiles that cross the {@link #max} threshold from the world entirely. + *
    + *
  • Default: false
  • + *
+ */ + public boolean removeFromWorldAfterReachLimit = false; + + } + + } + // Gale end - Airplane - reduce projectile chunk loading }