9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-23 08:59:23 +00:00
Files
Leaf/patches/server/0039-Cache-minecart-vehicle-collision-results.patch
2024-02-03 11:03:22 -05:00

88 lines
5.4 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrHua269 <wangxyper@163.com>
Date: Wed, 23 Aug 2023 14:21:16 -0400
Subject: [PATCH] Cache minecart vehicle collision results
Co-authored-by: MrHua269 <wangxyper@163.com>
Cache minecart vehicle collision results to prevent lag causing by massive stacked minecart
The known issue: entity can't enter the minecart after enabling this!
diff --git a/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java b/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java
index eb5bd5cfd131042e366872bf599a315d83dc732b..a855289c9bd52bea33c1f7a15fa2cbba90855713 100644
--- a/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java
+++ b/src/main/java/net/minecraft/world/entity/vehicle/AbstractMinecart.java
@@ -5,6 +5,8 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.UnmodifiableIterator;
import com.mojang.datafixers.util.Pair;
+
+import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -294,6 +296,21 @@ public abstract class AbstractMinecart extends VehicleEntity {
return this.flipped ? this.getDirection().getOpposite().getClockWise() : this.getDirection().getClockWise();
}
+ // Leaf start - Cache minecart vehicle collision results
+ private List<Entity> lastCollideCache = new ArrayList<>();
+ private List<Entity> lastCollideCache2 = new ArrayList<>();
+
+ private void checkAndUpdateCache() {
+ if (this.getId() + this.tickCount % 10 == 0) {
+ if (this.getMinecartType() == AbstractMinecart.Type.RIDEABLE && this.getDeltaMovement().horizontalDistanceSqr() > 0.01D) {
+ this.lastCollideCache = this.level().getEntities((Entity) this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D), EntitySelector.pushableBy(this));
+ } else {
+ this.lastCollideCache2 = this.level().getEntities(this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D));
+ }
+ }
+ }
+ // Leaf end
+
@Override
public void tick() {
// Purpur start
@@ -389,8 +406,9 @@ public abstract class AbstractMinecart extends VehicleEntity {
this.level().getCraftServer().getPluginManager().callEvent(new org.bukkit.event.vehicle.VehicleMoveEvent(vehicle, from, to));
}
// CraftBukkit end
+ if (org.dreeam.leaf.LeafConfig.cacheMinecartCollision) this.checkAndUpdateCache(); // Leaf - Cache minecart vehicle collision results
if (this.getMinecartType() == AbstractMinecart.Type.RIDEABLE && this.getDeltaMovement().horizontalDistanceSqr() > 0.01D) {
- List<Entity> list = this.level().getEntities((Entity) this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D), EntitySelector.pushableBy(this));
+ List<Entity> list = org.dreeam.leaf.LeafConfig.cacheMinecartCollision ? this.lastCollideCache : this.level().getEntities((Entity) this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D), EntitySelector.pushableBy(this));; // Leaf - Cache minecart vehicle collision results
if (!list.isEmpty()) {
Iterator iterator = list.iterator();
@@ -424,7 +442,7 @@ public abstract class AbstractMinecart extends VehicleEntity {
}
}
} else {
- Iterator iterator1 = this.level().getEntities(this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D)).iterator();
+ Iterator iterator1 = org.dreeam.leaf.LeafConfig.cacheMinecartCollision ? this.lastCollideCache2.iterator() : this.level().getEntities(this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D)).iterator();; // Leaf - Cache minecart vehicle collision results
while (iterator1.hasNext()) {
Entity entity1 = (Entity) iterator1.next();
diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java
index 393c8d887e0de195d6bd95c9c3273b03d7b3eb03..f4b954c8f4fa09a099c9aa95f2165072a891b1f8 100644
--- a/src/main/java/org/dreeam/leaf/LeafConfig.java
+++ b/src/main/java/org/dreeam/leaf/LeafConfig.java
@@ -205,6 +205,7 @@ public class LeafConfig {
public static boolean asyncPathfinding = false;
public static int asyncPathfindingMaxThreads = 0;
public static int asyncPathfindingKeepalive = 60;
+ public static boolean cacheMinecartCollision = false;
private static void performance() {
boolean asyncMobSpawning = getBoolean("performance.enable-async-mob-spawning", enableAsyncMobSpawning,
"Whether or not asynchronous mob spawning should be enabled.",
@@ -265,6 +266,9 @@ public class LeafConfig {
asyncPathfindingMaxThreads = 0;
else
Bukkit.getLogger().log(Level.INFO, "Using " + asyncPathfindingMaxThreads + " threads for Async Pathfinding");
+ cacheMinecartCollision = getBoolean("performance.cache-minecart-collision", cacheMinecartCollision,
+ "Cache the minecart collision result to prevent massive stacked minecart lag the server.",
+ "The known issue: entity can't enter the minecart after enabling this!");
}
public static boolean jadeProtocol = false;