Skip to content

Commit

Permalink
Fix entity unable going to minecarft caused by cache minecart collision
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Feb 5, 2024
1 parent 532ae83 commit 0c21b1b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
64 changes: 40 additions & 24 deletions patches/server/0039-Cache-minecart-vehicle-collision-results.patch
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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!
The known issue: not work -> if (!(entity instanceof Player) && !(entity instanceof IronGolem) && !(entity instanceof AbstractMinecart) && !this.isVehicle() && !entity.isPassenger()) {

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
index eb5bd5cfd131042e366872bf599a315d83dc732b..f663c713e496fbc26b893f80b16b0b6a23e20d92 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;
Expand All @@ -21,50 +21,67 @@ index eb5bd5cfd131042e366872bf599a315d83dc732b..a855289c9bd52bea33c1f7a15fa2cbba
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -294,6 +296,21 @@ public abstract class AbstractMinecart extends VehicleEntity {
@@ -294,6 +296,15 @@ 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));
+ }
+ private void checkAndUpdateCache(boolean ride) {
+ if (this.tickCount % 30 == 0) {
+ this.lastCollideCache = ride ? this.level().getEntities((Entity) this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D), EntitySelector.pushableBy(this)) : 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));
@@ -390,14 +401,14 @@ public abstract class AbstractMinecart extends VehicleEntity {
}
// 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));
+ // Leaf start - Cache minecart vehicle collision results
+ if (org.dreeam.leaf.LeafConfig.cacheMinecartCollision) this.checkAndUpdateCache(true);
+ 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 {
- Iterator iterator = list.iterator();
-
- while (iterator.hasNext()) {
- Entity entity = (Entity) iterator.next();
+ System.out.println("这里没有??");

+ for (Entity entity : list) {
if (!(entity instanceof Player) && !(entity instanceof IronGolem) && !(entity instanceof AbstractMinecart) && !this.isVehicle() && !entity.isPassenger()) {
// CraftBukkit start
VehicleEntityCollisionEvent collisionEvent = new VehicleEntityCollisionEvent(vehicle, entity.getBukkitEntity());
@@ -424,11 +435,10 @@ 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();
+ if (org.dreeam.leaf.LeafConfig.cacheMinecartCollision) this.checkAndUpdateCache(false); // Leaf - Cache minecart vehicle collision results
+ List<Entity> list2 = org.dreeam.leaf.LeafConfig.cacheMinecartCollision ? this.lastCollideCache : this.level().getEntities(this, this.getBoundingBox().inflate(0.20000000298023224D, 0.0D, 0.20000000298023224D));; // Leaf - Cache minecart vehicle collision results

+ for (Entity entity1 : list2) {
if (!this.hasPassenger(entity1) && entity1.isPushable() && entity1 instanceof AbstractMinecart) {
// CraftBukkit start
VehicleEntityCollisionEvent collisionEvent = new VehicleEntityCollisionEvent(vehicle, entity1.getBukkitEntity());
@@ -442,6 +452,7 @@ public abstract class AbstractMinecart extends VehicleEntity {
}
}
}
+ // Leaf end

while (iterator1.hasNext()) {
Entity entity1 = (Entity) iterator1.next();
this.updateInWaterStateAndDoFluidPushing();
if (this.isInLava()) {
diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java
index 393c8d887e0de195d6bd95c9c3273b03d7b3eb03..f4b954c8f4fa09a099c9aa95f2165072a891b1f8 100644
index 393c8d887e0de195d6bd95c9c3273b03d7b3eb03..b3b5b5a4f5c6f4c090a80ad7f19c883f85b06d5a 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 {
Expand All @@ -75,13 +92,12 @@ index 393c8d887e0de195d6bd95c9c3273b03d7b3eb03..f4b954c8f4fa09a099c9aa95f2165072
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 {
@@ -265,6 +266,8 @@ 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!");
+ "Cache the minecart collision result to prevent massive stacked minecart lag the server.");
}

public static boolean jadeProtocol = false;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Faster Random for xaeroMapServerID generation


diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java
index f4b954c8f4fa09a099c9aa95f2165072a891b1f8..8db4b1268e7bc18d60cc53f2753a73c2fc3ec242 100644
index b3b5b5a4f5c6f4c090a80ad7f19c883f85b06d5a..490b8916ccb23424b4a8d7fec3f629f5fd191879 100644
--- a/src/main/java/org/dreeam/leaf/LeafConfig.java
+++ b/src/main/java/org/dreeam/leaf/LeafConfig.java
@@ -2,7 +2,7 @@ package org.dreeam.leaf;
Expand All @@ -17,7 +17,7 @@ index f4b954c8f4fa09a099c9aa95f2165072a891b1f8..8db4b1268e7bc18d60cc53f2753a73c2
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.EntityType;
import org.bukkit.Bukkit;
@@ -274,7 +274,7 @@ public class LeafConfig {
@@ -273,7 +273,7 @@ public class LeafConfig {
public static boolean jadeProtocol = false;
public static boolean appleskinProtocol = false;
public static boolean xaeroMapProtocol = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ index 8d3c1897044f9a2bbe1911e1a72dc9a00fb246df..03112a42d1cf3460af5d5ce9d1fdd038
}

diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java
index 8db4b1268e7bc18d60cc53f2753a73c2fc3ec242..64adf80c1d11b631bc772586f649a79387521bcd 100644
index 490b8916ccb23424b4a8d7fec3f629f5fd191879..f6d99128ea36aa3fb7f48abdd48401debb1656d6 100644
--- a/src/main/java/org/dreeam/leaf/LeafConfig.java
+++ b/src/main/java/org/dreeam/leaf/LeafConfig.java
@@ -206,6 +206,7 @@ public class LeafConfig {
Expand All @@ -35,10 +35,10 @@ index 8db4b1268e7bc18d60cc53f2753a73c2fc3ec242..64adf80c1d11b631bc772586f649a793
private static void performance() {
boolean asyncMobSpawning = getBoolean("performance.enable-async-mob-spawning", enableAsyncMobSpawning,
"Whether or not asynchronous mob spawning should be enabled.",
@@ -269,6 +270,7 @@ public class LeafConfig {
@@ -268,6 +269,7 @@ public class LeafConfig {
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!");
"Cache the minecart collision result to prevent massive stacked minecart lag the server.");
+ skipMapItemDataUpdatesIfMapDoesNotHaveCraftMapRenderer = getBoolean("performance.skip-map-item-data-updates-if-map-does-not-have-craftmaprenderer", skipMapItemDataUpdatesIfMapDoesNotHaveCraftMapRenderer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ index 785196e6f4677074890ca965e9add85ccfd0e6e3..c2d9b3110756fa35829f0c01c06331f9

@Override
diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java
index 64adf80c1d11b631bc772586f649a79387521bcd..09a23b3adbe5b6ce8a809b22915f25e9316d54bd 100644
index f6d99128ea36aa3fb7f48abdd48401debb1656d6..f798dcc331e9251ccb1161b251f7f2d5a94fa7a3 100644
--- a/src/main/java/org/dreeam/leaf/LeafConfig.java
+++ b/src/main/java/org/dreeam/leaf/LeafConfig.java
@@ -307,4 +307,11 @@ public class LeafConfig {
@@ -306,4 +306,11 @@ public class LeafConfig {
private static void vanillaEndTeleport() {
useVanillaEndTeleport = getBoolean("use-vanilla-end-teleport", useVanillaEndTeleport, "Vanilla End Gateway Teleport");
}
Expand Down

0 comments on commit 0c21b1b

Please sign in to comment.