Files
OpenAzuriteSpigot/azurite-server/src/main/java/dev/azurite/spigot/optimizations/ModernHopperOptimizations.java
T
2026-11-14 12:58:29 -03:00

170 lines
6.7 KiB
Java

package dev.azurite.spigot.optimizations;
import net.minecraft.server.*;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;
public class ModernHopperOptimizations {
// Paper start - Optimize Hoppers
public static boolean skipPullModeEventFire = false;
public static boolean skipPushModeEventFire = false;
public static boolean skipHopperEvents = false;
public static boolean hopperPush(TileEntityHopper hopper, IInventory iinventory, EnumDirection enumdirection) {
skipPushModeEventFire = skipHopperEvents;
boolean foundItem = false;
for (int i = 0; i < hopper.getSize(); ++i) {
ItemStack item = hopper.getItem(i);
if (item != null && item.count > 0) {
foundItem = true;
ItemStack origItemStack = item;
ItemStack itemstack = origItemStack;
final int origCount = origItemStack.count;
final int moved = Math.min(hopper.getWorld().spigotConfig.hopperAmount, origCount);
origItemStack.count = (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) {
itemstack = callPushMoveEvent(hopper, iinventory, itemstack);
if (itemstack == null) { // cancelled
origItemStack.count = (origCount);
return false;
}
}
final ItemStack itemstack2 = TileEntityHopper.addItem(iinventory, itemstack, enumdirection);
final int remaining = itemstack2 == null ? 0 : itemstack2.count;
if (remaining != moved) {
origItemStack = origItemStack.cloneItemStack();
origItemStack.count = origCount - moved + remaining;
if (origItemStack.count <= 0) origItemStack = null;
hopper.setItem(i, origItemStack);
iinventory.update();
return true;
}
origItemStack.count = (origCount);
}
}
if (foundItem) {
hopper.d(hopper.getWorld().spigotConfig.hopperTransfer);
}
return false;
}
public static boolean hopperPull(IHopper ihopper, IInventory iinventory, ItemStack origItemStack, int i) {
ItemStack itemstack = origItemStack;
final int origCount = origItemStack == null ? 0 : origItemStack.count;
final World world = ihopper.getWorld();
final int moved = Math.min(world.spigotConfig.hopperAmount, origCount);
if (itemstack != null) {
itemstack.count = moved;
if (itemstack.count <= 0) itemstack = null;
}
if (!skipPullModeEventFire) {
itemstack = callPullMoveEvent(ihopper, iinventory, itemstack);
if (itemstack == null) { // cancelled
if (origItemStack != null) origItemStack.count = 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 = TileEntityHopper.addItem(ihopper, itemstack, null);
final int remaining = itemstack2 == null ? 0 : itemstack2.count;
if (origItemStack != null && remaining != moved) {
origItemStack = origItemStack.cloneItemStack();
origItemStack.count = origCount;
if (origItemStack.count <= 0) origItemStack = null;
if (origItemStack != null) {
origItemStack.count = origCount - moved + remaining;
if (origItemStack.count <= 0) origItemStack = null;
}
TileEntityHopper.IGNORE_TILE_UPDATES = true;
iinventory.setItem(i, origItemStack);
TileEntityHopper.IGNORE_TILE_UPDATES = false;
iinventory.update();
return true;
}
if (origItemStack != null) {
origItemStack.count = origCount;
}
cooldownHopper(ihopper); // Cooldown when full
return false;
}
private static ItemStack callPushMoveEvent(TileEntityHopper hopper, IInventory iinventory, ItemStack itemstack) {
Inventory destinationInventory = getInventory(iinventory);
InventoryMoveItemEvent event = new InventoryMoveItemEvent(hopper.getOwner().getInventory(),
CraftItemStack.asCraftMirror(itemstack), destinationInventory, true);
boolean result = event.callEvent();
if (!event.calledGetItem && !event.calledSetItem) {
skipPushModeEventFire = true;
}
if (!result) {
cooldownHopper(hopper);
return null;
}
if (event.calledSetItem) {
return CraftItemStack.asNMSCopy(event.getItem());
} else {
return itemstack;
}
}
private static ItemStack callPullMoveEvent(IHopper hopper, IInventory 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;
}
}
public static Inventory getInventory(IInventory iinventory) {
if (iinventory instanceof InventoryLargeChest) {
return new org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest((InventoryLargeChest) iinventory);
}
return iinventory.getOwner().getInventory();
}
private static void cooldownHopper(IHopper hopper) {
if (hopper instanceof TileEntityHopper) {
((TileEntityHopper) hopper).d(hopper.getWorld().spigotConfig.hopperTransfer);
} else if (hopper instanceof EntityMinecartHopper) {
((EntityMinecartHopper) hopper).m(hopper.getWorld().spigotConfig.hopperTransfer / 2);
}
}
// Paper end
}