9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-06 15:51:31 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0027-Reduce-hopper-item-checks.patch
Dreeam 3b162fb788 Move Purpur patches to first
To reduce the difficulty on maintenance and reduce chances to fix conflicts on updating
2025-10-01 18:27:42 -04:00

155 lines
7.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Thu, 24 Nov 2022 23:03:52 +0100
Subject: [PATCH] Reduce hopper item checks
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:
"Improve Hopper Performance"
By: Aikar <aikar@aikar.co>
As part of: EmpireCraft (https://github.com/starlis/empirecraft)
Licensed under: MIT (https://opensource.org/licenses/MIT)
* EmpireCraft description *
Only do an item "suck in" action once per second
diff --git a/net/minecraft/world/entity/item/ItemEntity.java b/net/minecraft/world/entity/item/ItemEntity.java
index d650d4f34fde0682ab76360408f7ff6a7d4b4c3a..7dc97fccfd329253e0f6532d1cc2c06264674e15 100644
--- a/net/minecraft/world/entity/item/ItemEntity.java
+++ b/net/minecraft/world/entity/item/ItemEntity.java
@@ -154,7 +154,13 @@ public class ItemEntity extends Entity implements TraceableEntity {
}
// CraftBukkit end
this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
+ return; // Gale - EMC - reduce hopper item checks
}
+ // Gale start - EMC - reduce hopper item checks
+ if (level().galeConfig().smallOptimizations.reducedIntervals.checkNearbyItem.hopper.minecart.temporaryImmunity.checkForMinecartNearItemWhileInactive) {
+ this.markNearbyHopperCartsAsImmune();
+ }
+ // Gale end - EMC - reduce hopper item checks
}
// Paper end - EAR 2
@@ -238,9 +244,31 @@ public class ItemEntity extends Entity implements TraceableEntity {
}
// CraftBukkit end
this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
+ return; // Gale - EMC - reduce hopper item checks
+ }
+ this.markNearbyHopperCartsAsImmune(); // Gale - EMC - reduce hopper item checks
+ }
+ }
+
+ // Gale start - EMC - reduce hopper item checks
+ private void markNearbyHopperCartsAsImmune() {
+ var config = this.level().galeConfig().smallOptimizations.reducedIntervals.checkNearbyItem.hopper.minecart;
+
+ // No need to mark hopper minecarts as immune if they can pull every tick anyway
+ if (config.interval <= 1) {
+ return;
+ }
+
+ 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) {
+ net.minecraft.world.phys.AABB aabb = this.getBoundingBox().inflate(config.temporaryImmunity.maxItemHorizontalDistance, config.temporaryImmunity.maxItemVerticalDistance, config.temporaryImmunity.maxItemHorizontalDistance);
+ for (Entity entity : this.level().getEntities(this, aabb)) {
+ if (entity instanceof net.minecraft.world.entity.vehicle.MinecartHopper hopper) {
+ hopper.pickupImmunity = net.minecraft.server.MinecraftServer.currentTick + config.temporaryImmunity.duration;
+ }
}
}
}
+ // Gale end - EMC - reduce hopper item checks
@Override
public BlockPos getBlockPosBelowThatAffectsMyMovement() {
diff --git a/net/minecraft/world/entity/vehicle/MinecartHopper.java b/net/minecraft/world/entity/vehicle/MinecartHopper.java
index 41a6ec508a10a49a37539d2f10171d15c233b280..fb099517927f3c2699a348ea7692772c5420b8d1 100644
--- a/net/minecraft/world/entity/vehicle/MinecartHopper.java
+++ b/net/minecraft/world/entity/vehicle/MinecartHopper.java
@@ -23,6 +23,7 @@ public class MinecartHopper extends AbstractMinecartContainer implements Hopper
private static final boolean DEFAULT_ENABLED = true;
private boolean enabled = true;
private boolean consumedItemThisFrame = false;
+ public int pickupImmunity = 0; // Gale - EMC - reduce hopper item checks
public MinecartHopper(EntityType<? extends MinecartHopper> entityType, Level level) {
super(entityType, level);
@@ -151,4 +152,12 @@ public class MinecartHopper extends AbstractMinecartContainer implements Hopper
}
// Paper end
+ // Gale start - EMC - reduce hopper item checks
+ private long tickAttempts = 0;
+ @Override
+ public long getAndIncrementAttemptCounter() {
+ return tickAttempts++;
+ }
+ // Gale end EMC - - reduce hopper item checks
+
}
diff --git a/net/minecraft/world/level/block/entity/Hopper.java b/net/minecraft/world/level/block/entity/Hopper.java
index 484c2ba2752fbf3ad929e46c2f078e906f6f0637..6ced5a7e27703a7cf5a7495dc3a1a290ce0d18eb 100644
--- a/net/minecraft/world/level/block/entity/Hopper.java
+++ b/net/minecraft/world/level/block/entity/Hopper.java
@@ -11,6 +11,8 @@ public interface Hopper extends Container {
return SUCK_AABB;
}
+ long getAndIncrementAttemptCounter(); // Gale - EMC - reduce hopper item checks
+
double getLevelX();
double getLevelY();
diff --git a/net/minecraft/world/level/block/entity/HopperBlockEntity.java b/net/minecraft/world/level/block/entity/HopperBlockEntity.java
index 01ed25d1f895d94485b5fecd98476534cbb26930..c1fcd9e0ab47332ce48e391c6cd1455960340df3 100644
--- a/net/minecraft/world/level/block/entity/HopperBlockEntity.java
+++ b/net/minecraft/world/level/block/entity/HopperBlockEntity.java
@@ -544,7 +544,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
return false;
} else {
boolean flag = hopper.isGridAligned() && blockState.isCollisionShapeFullBlock(level, blockPos) && !blockState.is(BlockTags.DOES_NOT_BLOCK_HOPPERS);
- if (!flag) {
+ if (!flag && shouldSuckIn(level, hopper)) { // Gale - EMC - reduce hopper item checks
for (ItemEntity itemEntity : getItemsAtAndAbove(level, hopper)) {
if (addItem(hopper, itemEntity)) {
return true;
@@ -822,6 +822,34 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
return stack1.getCount() < stack1.getMaxStackSize() && ItemStack.isSameItemSameComponents(stack1, stack2); // Paper - Perf: Optimize Hoppers; used to return true for full itemstacks?!
}
+ // Gale start - EMC - reduce hopper item checks
+ private long tickAttempts = 0;
+ @Override
+ public long getAndIncrementAttemptCounter() {
+ return tickAttempts++;
+ }
+ private static boolean shouldSuckIn(Level level, Hopper hopper) {
+ int suckInterval;
+ if (hopper instanceof net.minecraft.world.entity.vehicle.MinecartHopper minecartHopper) {
+ if (minecartHopper.pickupImmunity > net.minecraft.server.MinecraftServer.currentTick) {
+ return true;
+ }
+
+ suckInterval = level.galeConfig().smallOptimizations.reducedIntervals.checkNearbyItem.hopper.minecart.interval;
+ } else {
+ suckInterval = level.galeConfig().smallOptimizations.reducedIntervals.checkNearbyItem.hopper.interval;
+ }
+
+ if (suckInterval <= 1) {
+ return true;
+ }
+
+ final int hopperId = (int) hopper.getLevelX() + (int) hopper.getLevelY() + (int) hopper.getLevelZ();
+
+ return (hopper.getAndIncrementAttemptCounter() + hopperId) % suckInterval == 0;
+ }
+ // Gale end - EMC - reduce hopper item checks
+
@Override
public double getLevelX() {
return this.worldPosition.getX() + 0.5;