9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-28 19:39:17 +00:00
Files
Leaf/patches/server/0049-Reduce-items-finding-hopper-nearby-check.patch
2024-02-03 11:17:55 -05:00

108 lines
5.3 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Mon, 15 Jan 2024 10:53:10 -0500
Subject: [PATCH] Reduce items finding hopper nearby check
This patch add a toggle for items checking MinecraftHopper nearby,
and add a time throttler and MinecraftHopper pre-cehck for items checking
But still recommend to turn-off `checkForMinecartNearItemWhileActive`
Since `Reduce-hopper-item-checks.patch` will cause lag under massive dropped items
diff --git a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
index 414948ac6ad0b6919bedb119f4942a2881211825..397199aa5e9290287adc64abd3bb333bb96cec83 100644
--- a/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
+++ b/src/main/java/net/minecraft/world/entity/item/ItemEntity.java
@@ -1,7 +1,9 @@
package net.minecraft.world.entity.item;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import javax.annotation.Nullable;
@@ -237,7 +239,9 @@ public class ItemEntity extends Entity implements TraceableEntity {
this.discard();
return; // Gale - EMC - reduce hopper item checks
}
- this.markNearbyHopperCartsAsImmune(); // Gale - EMC - reduce hopper item checks
+ if (level().galeConfig().smallOptimizations.reducedIntervals.checkNearbyItem.hopper.minecart.temporaryImmunity.checkForMinecartNearItemWhileActive) { // Leaf - Reduce items finding hopper nearby check
+ this.markNearbyHopperCartsAsImmune(); // Gale - EMC - reduce hopper item checks
+ }
}
}
@@ -249,17 +253,53 @@ public class ItemEntity extends Entity implements TraceableEntity {
if (config.interval <= 1) {
return;
}
+
+ // Leaf start - Reduce items finding hopper nearby check
+ BlockPos pos = new BlockPos(this.getBlockX(), this.getBlockY(), this.getBlockZ());
+ if (this.shouldCheckItem(pos)) {
+ cachedResults.putIfAbsent(pos, true);
+
if (config.temporaryImmunity.duration > 0 && this.isAlive() && this.onGround && !this.isRemoved() && (config.temporaryImmunity.nearbyItemMaxAge == -1 || this.age <= config.temporaryImmunity.nearbyItemMaxAge) && this.age % Math.max(1, config.temporaryImmunity.checkForMinecartNearItemInterval) == 0 && config.temporaryImmunity.maxItemHorizontalDistance >= 0 && config.temporaryImmunity.maxItemVerticalDistance >= 0) {
AABB aabb = this.getBoundingBox().inflate(config.temporaryImmunity.maxItemHorizontalDistance, config.temporaryImmunity.maxItemVerticalDistance, config.temporaryImmunity.maxItemHorizontalDistance);
+ if (this.level().getEntities(this, aabb).stream().noneMatch(entity -> entity instanceof MinecartHopper)) {
+ return;
+ }
+
for (Entity entity : this.level().getEntities(this, aabb)) {
if (entity instanceof MinecartHopper) {
((MinecartHopper) entity).pickupImmunity = MinecraftServer.currentTick + config.temporaryImmunity.duration;
}
}
}
+ }// Leaf end
}
// Gale end - EMC - reduce hopper item checks
+ // Leaf start - Reduce items finding hopper nearby check
+ private Map<BlockPos, Boolean> cachedResults = new HashMap<>();
+ private long startTime = System.nanoTime();
+
+ private boolean shouldCheckItem(BlockPos pos) {
+ long now = System.nanoTime();
+ long timeElapsed = (now - startTime) / 1000000L;
+
+ // Throttle
+ if (timeElapsed < 20000L) {
+ return false;
+ }
+
+ // Check in cache
+ if (cachedResults.containsKey(pos)) {
+ return cachedResults.get(pos);
+ }
+
+ // Reset timer
+ startTime = now;
+
+ return true;
+ }
+ // Leaf end
+
// Spigot start - copied from above
@Override
public void inactiveTick() {
diff --git a/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java b/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java
index 1b1bf657993c2a96d7168e3b1161872839a6e67a..32e4fda2807aa68ba07e2c40881a451848bebd77 100644
--- a/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java
+++ b/src/main/java/org/galemc/gale/configuration/GaleWorldConfiguration.java
@@ -76,10 +76,11 @@ public class GaleWorldConfiguration extends ConfigurationPart {
public TemporaryImmunity temporaryImmunity;
public class TemporaryImmunity extends ConfigurationPart {
+ public boolean checkForMinecartNearItemWhileActive = false; // Leaf - Make it configurable and reorder code
+ public boolean checkForMinecartNearItemWhileInactive = true;
public int duration = 100;
public int nearbyItemMaxAge = 1200;
public int checkForMinecartNearItemInterval = 20;
- public boolean checkForMinecartNearItemWhileInactive = true;
public double maxItemHorizontalDistance = 24.0;
public double maxItemVerticalDistance = 4.0;
}