Files
MiraiMC/patches/server/0053-Optimize-inventory-API-item-handling.patch
2022-06-13 16:14:55 +02:00

66 lines
3.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Phoenix616 <mail@moep.tv>
Date: Sun, 24 Nov 2019 23:00:58 +0100
Subject: [PATCH] Optimize inventory API item handling
Original code by Minebench, licensed under GNU General Public License v3.0
You can find the original code on https://github.com/Minebench/Origami
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
index 30ac442049088200e9ab77a561c59cbc58aaa28f..bdacb0098c93be63693218544b6dc1a2937cdb3b 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventory.java
@@ -273,11 +273,13 @@ public class CraftInventory implements Inventory {
}
private int firstPartial(ItemStack item) {
- ItemStack[] inventory = this.getStorageContents();
- ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
if (item == null) {
return -1;
}
+ // Origami start - Optimize inventory API (moved down from before null check)
+ ItemStack[] inventory = this.getStorageContents();
+ ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
+ // Origami end
for (int i = 0; i < inventory.length; i++) {
ItemStack cItem = inventory[i];
if (cItem != null && cItem.getAmount() < cItem.getMaxStackSize() && cItem.isSimilar(filteredItem)) {
@@ -295,9 +297,10 @@ public class CraftInventory implements Inventory {
/* TODO: some optimization
* - Create a 'firstPartial' with a 'fromIndex'
* - Record the lastPartial per Material
- * - Cache firstEmpty result
+ * - Cache firstEmpty result // Implemented in Origami
*/
+ int firstFree = -2; // Origami - Cache firstEmpty result
for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
while (true) {
@@ -307,7 +310,11 @@ public class CraftInventory implements Inventory {
// Drat! no partial stack
if (firstPartial == -1) {
// Find a free spot!
- int firstFree = this.firstEmpty();
+ // Origami start - Cache firstEmpty result
+ if (firstFree == -2) {
+ firstFree = this.firstEmpty();
+ }
+ // Origami end
if (firstFree == -1) {
// No space at all!
@@ -320,9 +327,11 @@ public class CraftInventory implements Inventory {
stack.setAmount(this.getMaxItemStack());
this.setItem(firstFree, stack);
item.setAmount(item.getAmount() - this.getMaxItemStack());
+ firstFree = -2; // Origami - Cache firstEmpty result
} else {
// Just store it
this.setItem(firstFree, item);
+ firstFree = -2; // Origami - Cache firstEmpty result
break;
}
}