Paper/patches/server/0346-Optimize-Hoppers.patch

483 lines
25 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 27 Apr 2016 22:09:52 -0400
Subject: [PATCH] Optimize Hoppers
* Removes unnecessary extra calls to .update() that are very expensive
* Lots of itemstack cloning removed. Only clone if the item is actually moved
* Return true when a plugin cancels inventory move item event instead of false, as false causes pulls to cycle through all items.
However, pushes do not exhibit the same behavior, so this is not something plugins could of been relying on.
* Add option (Default on) to cooldown hoppers when they fail to move an item due to full inventory
* Skip subsequent InventoryMoveItemEvents if a plugin does not use the item after first event fire for an iteration
* Don't check for Entities with Inventories if the block above us is also occluding (not just Inventoried)
* Remove Streams from Item Suck In and restore restore 1.12 AABB checks which is simpler and no voxel allocations (was doing TWO Item Suck ins)
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index 3e3a48a636c225ec113c1c64f6ffc8a37ad9169e..94336c6a644bf913d366baa71c2372eb67b6e2cd 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
@@ -511,5 +511,17 @@ public class PaperWorldConfig {
2021-06-11 14:02:28 +02:00
private void entitiesTargetWithFollowRange() {
entitiesTargetWithFollowRange = getBoolean("entities-target-with-follow-range", entitiesTargetWithFollowRange);
}
+
+ public boolean cooldownHopperWhenFull = true;
+ public boolean disableHopperMoveEvents = false;
+ public boolean hoppersIgnoreOccludingBlocks = true;
2021-06-11 14:02:28 +02:00
+ private void hopperOptimizations() {
+ cooldownHopperWhenFull = getBoolean("hopper.cooldown-when-full", cooldownHopperWhenFull);
+ log("Cooldown Hoppers when Full: " + (cooldownHopperWhenFull ? "enabled" : "disabled"));
+ disableHopperMoveEvents = getBoolean("hopper.disable-move-event", disableHopperMoveEvents);
+ log("Hopper Move Item Events: " + (disableHopperMoveEvents ? "disabled" : "enabled"));
+ hoppersIgnoreOccludingBlocks = getBoolean("hopper.ignore-occluding-blocks", hoppersIgnoreOccludingBlocks);
+ log("Hopper Ignore Occluding Blocks: " + (hoppersIgnoreOccludingBlocks ? "enabled" : "disabled"));
2021-06-11 14:02:28 +02:00
+ }
}
2021-06-13 21:29:58 +02:00
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 7576047ea9695434ca06ca8fefde0dce68980be8..f71a1401d229b32557f0444ce45cfa47ffa2fde4 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1421,6 +1421,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
while (iterator.hasNext()) {
ServerLevel worldserver = (ServerLevel) iterator.next();
worldserver.hasPhysicsEvent = org.bukkit.event.block.BlockPhysicsEvent.getHandlerList().getRegisteredListeners().length > 0; // Paper
2021-06-13 21:29:58 +02:00
+ net.minecraft.world.level.block.entity.HopperBlockEntity.skipHopperEvents = worldserver.paperConfig.disableHopperMoveEvents || org.bukkit.event.inventory.InventoryMoveItemEvent.getHandlerList().getRegisteredListeners().length == 0; // Paper
2021-06-11 14:02:28 +02:00
this.profiler.push(() -> {
return worldserver + " " + worldserver.dimension().location();
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
2021-12-10 15:24:07 +01:00
index 589a9bd9d3bf3cbf88a6efad5f58970a0a4a56c0..83d872044c3db54352847e08bb333ff7e0aee0b0 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
2021-12-10 15:24:07 +01:00
@@ -603,11 +603,12 @@ public final class ItemStack {
2021-06-11 14:02:28 +02:00
return this.getItem().interactLivingEntity(this, user, entity, hand);
}
- public ItemStack copy() {
- if (this.isEmpty()) {
+ public ItemStack copy() { return cloneItemStack(false); } // Paper
+ public ItemStack cloneItemStack(boolean origItem) { // Paper
+ if (!origItem && this.isEmpty()) { // Paper
return ItemStack.EMPTY;
} else {
- ItemStack itemstack = new ItemStack(this.getItem(), this.count);
+ ItemStack itemstack = new ItemStack(origItem ? this.item : this.getItem(), this.count); // Paper
itemstack.setPopTime(this.getPopTime());
if (this.tag != null) {
diff --git a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
index 6f61fd8224fb4094f38a851300ab55f94523c252..0e37da7227eaba0d089e5bd136eca088ab2b5eb3 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
2021-11-24 06:44:21 +01:00
@@ -70,6 +70,7 @@ public abstract class BlockEntity implements io.papermc.paper.util.KeyedObject {
2021-06-13 21:29:58 +02:00
getMinecraftKey(); // Try to load if it doesn't exists.
return tileEntityKeyString;
2021-06-11 14:02:28 +02:00
}
+ static boolean IGNORE_TILE_UPDATES = false;
// Paper end
@Nullable
2021-11-24 06:44:21 +01:00
@@ -182,6 +183,7 @@ public abstract class BlockEntity implements io.papermc.paper.util.KeyedObject {
2021-06-11 14:02:28 +02:00
public void setChanged() {
if (this.level != null) {
+ if (IGNORE_TILE_UPDATES) return; // Paper
2021-06-13 21:29:58 +02:00
BlockEntity.setChanged(this.level, this.worldPosition, this.blockState);
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/entity/Hopper.java b/src/main/java/net/minecraft/world/level/block/entity/Hopper.java
index a05acf709735b40ca86f978508c63a86065fd405..6a1405a8630e90db3b5a3c9152259ba6f5f0c784 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/Hopper.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/Hopper.java
@@ -14,6 +14,8 @@ public interface Hopper extends Container {
2021-06-13 21:29:58 +02:00
return SUCK;
2021-06-11 14:02:28 +02:00
}
2021-06-13 21:29:58 +02:00
+ default net.minecraft.core.BlockPos getBlockPosition() { return new net.minecraft.core.BlockPos(getLevelX(), getLevelY(), getLevelZ()); } // Paper
+
double getLevelX();
2021-06-11 14:02:28 +02:00
2021-06-13 21:29:58 +02:00
double getLevelY();
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/entity/HopperBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/HopperBlockEntity.java
index f3bc3133b68cabda359e99b74323b94f82bc42e9..31c128eab2a2cb7436e5c1777e9b19affa448ba3 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/HopperBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/HopperBlockEntity.java
2021-06-13 21:29:58 +02:00
@@ -3,7 +3,6 @@ package net.minecraft.world.level.block.entity;
import java.util.Iterator;
import java.util.List;
import java.util.function.BooleanSupplier;
-import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
@@ -33,7 +32,6 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.Shapes;
-import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.entity.HumanEntity;
@@ -191,6 +189,158 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-13 21:29:58 +02:00
2021-06-11 14:02:28 +02:00
return false;
}
+ // Paper start - Optimize Hoppers
+ private static boolean skipPullModeEventFire = false;
+ private static boolean skipPushModeEventFire = false;
+ public static boolean skipHopperEvents = false;
+
2021-06-14 12:18:42 +02:00
+ private static boolean hopperPush(Level level, BlockPos pos, Container destination, Direction enumdirection, HopperBlockEntity hopper) {
2021-06-11 14:02:28 +02:00
+ skipPushModeEventFire = skipHopperEvents;
+ boolean foundItem = false;
2021-06-14 12:18:42 +02:00
+ for (int i = 0; i < hopper.getContainerSize(); ++i) {
+ ItemStack item = hopper.getItem(i);
2021-06-11 14:02:28 +02:00
+ if (!item.isEmpty()) {
+ foundItem = true;
+ ItemStack origItemStack = item;
+ ItemStack itemstack = origItemStack;
+
+ final int origCount = origItemStack.getCount();
+ final int moved = Math.min(level.spigotConfig.hopperAmount, origCount);
+ origItemStack.setCount(moved);
+
+ // We only need to fire the event once to give protection plugins a chance to cancel this event
+ // Because nothing uses getItem, every event call should end up the same result.
+ if (!skipPushModeEventFire) {
2021-06-14 12:18:42 +02:00
+ itemstack = callPushMoveEvent(destination, itemstack, hopper);
2021-06-11 14:02:28 +02:00
+ if (itemstack == null) { // cancelled
+ origItemStack.setCount(origCount);
+ return false;
+ }
+ }
2021-06-14 12:18:42 +02:00
+ final ItemStack itemstack2 = addItem(hopper, destination, itemstack, enumdirection);
2021-06-11 14:02:28 +02:00
+ final int remaining = itemstack2.getCount();
+ if (remaining != moved) {
+ origItemStack = origItemStack.cloneItemStack(true);
+ origItemStack.setCount(origCount);
+ if (!origItemStack.isEmpty()) {
+ origItemStack.setCount(origCount - moved + remaining);
+ }
2021-06-13 21:29:58 +02:00
+ hopper.setItem(i, origItemStack);
2021-06-14 12:18:42 +02:00
+ destination.setChanged();
2021-06-11 14:02:28 +02:00
+ return true;
+ }
+ origItemStack.setCount(origCount);
+ }
+ }
+ if (foundItem && level.paperConfig.cooldownHopperWhenFull) { // Inventory was full - cooldown
2021-06-13 21:29:58 +02:00
+ hopper.setCooldown(level.spigotConfig.hopperTransfer);
2021-06-11 14:02:28 +02:00
+ }
+ return false;
+ }
+
+ private static boolean hopperPull(Level level, Hopper ihopper, Container iinventory, ItemStack origItemStack, int i) {
2021-06-11 14:02:28 +02:00
+ ItemStack itemstack = origItemStack;
+ final int origCount = origItemStack.getCount();
+ final int moved = Math.min(level.spigotConfig.hopperAmount, origCount);
2021-06-11 14:02:28 +02:00
+ itemstack.setCount(moved);
+
+ if (!skipPullModeEventFire) {
+ itemstack = callPullMoveEvent(ihopper, iinventory, itemstack);
+ if (itemstack == null) { // cancelled
+ origItemStack.setCount(origCount);
+ // Drastically improve performance by returning true.
+ // No plugin could of relied on the behavior of false as the other call
+ // site for IMIE did not exhibit the same behavior
+ return true;
+ }
+ }
+
+ final ItemStack itemstack2 = addItem(iinventory, ihopper, itemstack, null);
+ final int remaining = itemstack2.getCount();
+ if (remaining != moved) {
+ origItemStack = origItemStack.cloneItemStack(true);
+ origItemStack.setCount(origCount);
+ if (!origItemStack.isEmpty()) {
+ origItemStack.setCount(origCount - moved + remaining);
+ }
+ IGNORE_TILE_UPDATES = true;
+ iinventory.setItem(i, origItemStack);
+ IGNORE_TILE_UPDATES = false;
+ iinventory.setChanged();
+ return true;
+ }
+ origItemStack.setCount(origCount);
+
+ if (level.paperConfig.cooldownHopperWhenFull) {
2021-06-11 14:02:28 +02:00
+ cooldownHopper(ihopper);
+ }
+
+ return false;
+ }
+
2021-06-13 21:29:58 +02:00
+ private static ItemStack callPushMoveEvent(Container iinventory, ItemStack itemstack, HopperBlockEntity hopper) {
2021-06-11 14:02:28 +02:00
+ Inventory destinationInventory = getInventory(iinventory);
2021-06-13 21:29:58 +02:00
+ InventoryMoveItemEvent event = new InventoryMoveItemEvent(hopper.getOwner(false).getInventory(),
2021-06-11 14:02:28 +02:00
+ CraftItemStack.asCraftMirror(itemstack), destinationInventory, true);
+ boolean result = event.callEvent();
+ if (!event.calledGetItem && !event.calledSetItem) {
+ skipPushModeEventFire = true;
+ }
+ if (!result) {
2021-06-13 21:29:58 +02:00
+ cooldownHopper(hopper);
2021-06-11 14:02:28 +02:00
+ return null;
+ }
+
+ if (event.calledSetItem) {
+ return CraftItemStack.asNMSCopy(event.getItem());
+ } else {
+ return itemstack;
+ }
+ }
+
+ private static ItemStack callPullMoveEvent(Hopper hopper, Container iinventory, ItemStack itemstack) {
+ Inventory sourceInventory = getInventory(iinventory);
+ Inventory destination = getInventory(hopper);
+
+ InventoryMoveItemEvent event = new InventoryMoveItemEvent(sourceInventory,
+ // Mirror is safe as we no plugins ever use this item
+ CraftItemStack.asCraftMirror(itemstack), destination, false);
+ boolean result = event.callEvent();
+ if (!event.calledGetItem && !event.calledSetItem) {
+ skipPullModeEventFire = true;
+ }
+ if (!result) {
+ cooldownHopper(hopper);
+ return null;
+ }
+
+ if (event.calledSetItem) {
+ return CraftItemStack.asNMSCopy(event.getItem());
+ } else {
+ return itemstack;
+ }
+ }
+
+ private static Inventory getInventory(Container iinventory) {
+ Inventory sourceInventory;// Have to special case large chests as they work oddly
+ if (iinventory instanceof CompoundContainer) {
+ sourceInventory = new org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest((CompoundContainer) iinventory);
+ } else if (iinventory instanceof BlockEntity) {
+ sourceInventory = ((BlockEntity) iinventory).getOwner(false).getInventory();
+ } else {
+ sourceInventory = iinventory.getOwner().getInventory();
+ }
+ return sourceInventory;
+ }
+
+ private static void cooldownHopper(Hopper hopper) {
+ if (hopper instanceof HopperBlockEntity blockEntity) {
+ blockEntity.setCooldown(blockEntity.getLevel().spigotConfig.hopperTransfer);
+ } else if (hopper instanceof MinecartHopper blockEntity) {
+ blockEntity.setCooldown(blockEntity.getLevel().spigotConfig.hopperTransfer / 2);
2021-06-11 14:02:28 +02:00
+ }
+ }
+ // Paper end
2021-11-24 06:44:21 +01:00
private static boolean ejectItems(Level world, BlockPos blockposition, BlockState iblockdata, Container iinventory, HopperBlockEntity hopper) { // CraftBukkit
2021-06-13 21:29:58 +02:00
Container iinventory1 = HopperBlockEntity.getAttachedContainer(world, blockposition, iblockdata);
@@ -203,6 +353,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-13 21:29:58 +02:00
if (HopperBlockEntity.isFullContainer(iinventory1, enumdirection)) {
2021-06-11 14:02:28 +02:00
return false;
} else {
2021-06-14 12:18:42 +02:00
+ return hopperPush(world, blockposition, iinventory1, enumdirection, hopper); /* // Paper - disable rest
2021-06-13 21:29:58 +02:00
for (int i = 0; i < iinventory.getContainerSize(); ++i) {
if (!iinventory.getItem(i).isEmpty()) {
ItemStack itemstack = iinventory.getItem(i).copy();
@@ -240,7 +391,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-11 14:02:28 +02:00
}
}
- return false;
+ return false;*/ // Paper - end commenting out replaced block for Hopper Optimizations
}
}
}
@@ -250,27 +401,68 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-11 14:02:28 +02:00
}
2021-06-13 21:29:58 +02:00
private static boolean isFullContainer(Container inventory, Direction direction) {
- return HopperBlockEntity.getSlots(inventory, direction).allMatch((i) -> {
- ItemStack itemstack = inventory.getItem(i);
-
- return itemstack.getCount() >= itemstack.getMaxStackSize();
- });
+ return allMatch(inventory, direction, STACK_SIZE_TEST); // Paper - no streams
}
private static boolean isEmptyContainer(Container inv, Direction facing) {
- return HopperBlockEntity.getSlots(inv, facing).allMatch((i) -> {
- return inv.getItem(i).isEmpty();
- });
+ // Paper start
+ return allMatch(inv, facing, IS_EMPTY_TEST);
+ }
2021-06-11 14:02:28 +02:00
+ private static boolean allMatch(Container iinventory, Direction enumdirection, java.util.function.BiPredicate<ItemStack, Integer> test) {
+ if (iinventory instanceof WorldlyContainer) {
+ for (int i : ((WorldlyContainer) iinventory).getSlotsForFace(enumdirection)) {
+ if (!test.test(iinventory.getItem(i), i)) {
+ return false;
+ }
+ }
+ } else {
+ int size = iinventory.getContainerSize();
+ for (int i = 0; i < size; i++) {
+ if (!test.test(iinventory.getItem(i), i)) {
+ return false;
+ }
+ }
+ }
+ return true;
2021-06-13 21:29:58 +02:00
}
2021-06-11 14:02:28 +02:00
+ private static boolean anyMatch(Container iinventory, Direction enumdirection, java.util.function.BiPredicate<ItemStack, Integer> test) {
+ if (iinventory instanceof WorldlyContainer) {
+ for (int i : ((WorldlyContainer) iinventory).getSlotsForFace(enumdirection)) {
+ if (test.test(iinventory.getItem(i), i)) {
+ return true;
+ }
+ }
+ } else {
+ int size = iinventory.getContainerSize();
+ for (int i = 0; i < size; i++) {
+ if (test.test(iinventory.getItem(i), i)) {
+ return true;
+ }
+ }
+ }
+ return true;
+ }
+ private static final java.util.function.BiPredicate<ItemStack, Integer> STACK_SIZE_TEST = (itemstack, i) -> itemstack.getCount() >= itemstack.getMaxStackSize();
+ private static final java.util.function.BiPredicate<ItemStack, Integer> IS_EMPTY_TEST = (itemstack, i) -> itemstack.isEmpty();
+ // Paper end
+
2021-06-13 21:29:58 +02:00
public static boolean suckInItems(Level world, Hopper hopper) {
Container iinventory = HopperBlockEntity.getSourceContainer(world, hopper);
2021-06-11 14:02:28 +02:00
if (iinventory != null) {
Direction enumdirection = Direction.DOWN;
2021-06-13 21:29:58 +02:00
- return HopperBlockEntity.isEmptyContainer(iinventory, enumdirection) ? false : HopperBlockEntity.getSlots(iinventory, enumdirection).anyMatch((i) -> {
- return HopperBlockEntity.a(hopper, iinventory, i, enumdirection, world); // Spigot
2021-06-11 14:02:28 +02:00
+ // Paper start - optimize hoppers and remove streams
+ skipPullModeEventFire = skipHopperEvents;
2021-06-13 21:29:58 +02:00
+ return !HopperBlockEntity.isEmptyContainer(iinventory, enumdirection) && anyMatch(iinventory, enumdirection, (item, i) -> {
2021-06-11 14:02:28 +02:00
+ // Logic copied from below to avoid extra getItem calls
2021-06-13 21:29:58 +02:00
+ if (!item.isEmpty() && canTakeItemFromContainer(iinventory, item, i, enumdirection)) {
+ return hopperPull(world, hopper, iinventory, item, i);
2021-06-11 14:02:28 +02:00
+ } else {
+ return false;
+ }
2021-06-13 21:29:58 +02:00
+ // Paper end
2021-06-11 14:02:28 +02:00
});
} else {
2021-06-13 21:29:58 +02:00
Iterator iterator = HopperBlockEntity.getItemsAtAndAbove(world, hopper).iterator();
@@ -289,10 +481,12 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-11 14:02:28 +02:00
}
}
2021-06-13 21:29:58 +02:00
+ // Paper - method unused as logic is inlined above
private static boolean a(Hopper ihopper, Container iinventory, int i, Direction enumdirection, Level world) { // Spigot
ItemStack itemstack = iinventory.getItem(i);
2021-06-11 14:02:28 +02:00
2021-06-13 21:29:58 +02:00
- if (!itemstack.isEmpty() && HopperBlockEntity.canTakeItemFromContainer(iinventory, itemstack, i, enumdirection)) {
+ if (!itemstack.isEmpty() && HopperBlockEntity.canTakeItemFromContainer(iinventory, itemstack, i, enumdirection)) { // If this logic changes, update above. this is left inused incase reflective plugins
+ return hopperPull(world, ihopper, iinventory, itemstack, i); /* // Paper - disable rest
2021-06-13 21:29:58 +02:00
ItemStack itemstack1 = itemstack.copy();
2021-11-24 06:44:21 +01:00
// ItemStack itemstack2 = addItem(iinventory, ihopper, iinventory.removeItem(i, 1), (EnumDirection) null);
2021-06-11 14:02:28 +02:00
// CraftBukkit start - Call event on collection of items from inventories into the hopper
@@ -329,7 +523,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-11 14:02:28 +02:00
}
2021-06-13 21:29:58 +02:00
itemstack1.shrink(origCount - itemstack2.getCount()); // Spigot
- iinventory.setItem(i, itemstack1);
2021-06-11 14:02:28 +02:00
+ iinventory.setItem(i, itemstack1);*/ // Paper - end commenting out replaced block for Hopper Optimizations
}
return false;
@@ -338,7 +532,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-11 14:02:28 +02:00
public static boolean addItem(Container inventory, ItemEntity itemEntity) {
boolean flag = false;
// CraftBukkit start
- InventoryPickupItemEvent event = new InventoryPickupItemEvent(inventory.getOwner().getInventory(), (org.bukkit.entity.Item) itemEntity.getBukkitEntity());
+ InventoryPickupItemEvent event = new InventoryPickupItemEvent(getInventory(inventory), (org.bukkit.entity.Item) itemEntity.getBukkitEntity()); // Paper - use getInventory() to avoid snapshot creation
itemEntity.level.getCraftServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return false;
@@ -397,7 +591,9 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
stack = stack.split(to.getMaxStackSize());
}
// Spigot end
2021-06-11 14:02:28 +02:00
+ IGNORE_TILE_UPDATES = true; // Paper
to.setItem(slot, stack);
+ IGNORE_TILE_UPDATES = false; // Paper
stack = ItemStack.EMPTY;
flag = true;
2021-06-13 21:29:58 +02:00
} else if (HopperBlockEntity.canMergeItems(itemstack1, stack)) {
@@ -448,18 +644,23 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-11 14:02:28 +02:00
}
2021-06-13 21:29:58 +02:00
public static List<ItemEntity> getItemsAtAndAbove(Level world, Hopper hopper) {
- return (List) hopper.getSuckShape().toAabbs().stream().flatMap((axisalignedbb) -> {
- return world.getEntitiesOfClass(ItemEntity.class, axisalignedbb.move(hopper.getLevelX() - 0.5D, hopper.getLevelY() - 0.5D, hopper.getLevelZ() - 0.5D), EntitySelector.ENTITY_STILL_ALIVE).stream();
2021-06-11 14:02:28 +02:00
- }).collect(Collectors.toList());
+ // Paper start - Optimize item suck in. remove streams, restore 1.12 checks. Seriously checking the bowl?!
2021-06-13 21:29:58 +02:00
+ double d0 = hopper.getLevelX();
+ double d1 = hopper.getLevelY();
+ double d2 = hopper.getLevelZ();
2021-06-11 14:02:28 +02:00
+ AABB bb = new AABB(d0 - 0.5D, d1, d2 - 0.5D, d0 + 0.5D, d1 + 1.5D, d2 + 0.5D);
2021-06-13 21:29:58 +02:00
+ return world.getEntitiesOfClass(ItemEntity.class, bb, Entity::isAlive);
2021-06-11 14:02:28 +02:00
+ // Paper end
}
@Nullable
2021-06-13 21:29:58 +02:00
public static Container getContainerAt(Level world, BlockPos pos) {
- return HopperBlockEntity.getContainerAt(world, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D);
+ return HopperBlockEntity.getContainerAt(world, (double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D, true); // Paper
2021-06-11 14:02:28 +02:00
}
2021-06-13 21:29:58 +02:00
+ public static Container getContainerAt(Level world, double x, double y, double z) { return getContainerAt(world, x, y, z, false); } // Paper - overload to default false
2021-06-11 14:02:28 +02:00
@Nullable
2021-06-13 21:29:58 +02:00
- private static Container getContainerAt(Level world, double x, double y, double z) {
+ private static Container getContainerAt(Level world, double x, double y, double z, boolean optimizeEntities) {
2021-06-11 14:02:28 +02:00
Object object = null;
2021-06-13 21:29:58 +02:00
BlockPos blockposition = new BlockPos(x, y, z);
2021-06-11 14:02:28 +02:00
if ( !world.hasChunkAt( blockposition ) ) return null; // Spigot
@@ -479,7 +680,7 @@ public class HopperBlockEntity extends RandomizableContainerBlockEntity implemen
2021-06-11 14:02:28 +02:00
}
}
- if (object == null) {
+ if (object == null && (!optimizeEntities || !world.paperConfig.hoppersIgnoreOccludingBlocks || !org.bukkit.craftbukkit.util.CraftMagicNumbers.getMaterial(block).isOccluding())) { // Paper
2021-06-13 21:29:58 +02:00
List<Entity> list = world.getEntities((Entity) null, new AABB(x - 0.5D, y - 0.5D, z - 0.5D, x + 0.5D, y + 0.5D, z + 0.5D), EntitySelector.CONTAINER_ENTITY_SELECTOR);
2021-06-11 14:02:28 +02:00
if (!list.isEmpty()) {
diff --git a/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java
2021-06-13 21:29:58 +02:00
index f23fff80d07ac7d06715efe67cb49ebbe704967b..ed3518fe7c841d9e1a9c97626acaa3d765a6d76f 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/RandomizableContainerBlockEntity.java
2021-06-13 21:29:58 +02:00
@@ -95,12 +95,19 @@ public abstract class RandomizableContainerBlockEntity extends BaseContainerBloc
2021-06-11 14:02:28 +02:00
@Override
public boolean isEmpty() {
2021-06-13 21:29:58 +02:00
this.unpackLootTable((Player)null);
2021-06-11 14:02:28 +02:00
- return this.getItems().stream().allMatch(ItemStack::isEmpty);
+ // Paper start
+ for (ItemStack itemStack : this.getItems()) {
+ if (!itemStack.isEmpty()) {
+ return false;
+ }
+ }
+ // Paper end
+ return true;
}
@Override
public ItemStack getItem(int slot) {
2021-06-13 21:29:58 +02:00
- this.unpackLootTable((Player)null);
2021-06-11 14:02:28 +02:00
+ if (slot == 0) this.unpackLootTable((Player) null); // Paper
2021-06-13 21:29:58 +02:00
return this.getItems().get(slot);
2021-06-11 14:02:28 +02:00
}