Fix InventoryUtils#addItemCorrectly for crafting inventory

This commit is contained in:
NichtStudioCode 2022-09-30 15:25:35 +02:00
parent 522326fbae
commit fab3666bde

@ -7,6 +7,7 @@ import org.bukkit.Location;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -53,7 +54,13 @@ public class InventoryUtils {
@Nullable @Nullable
public static ItemStack getFirstPartialStack(Inventory inventory, @NotNull ItemStack type) { public static ItemStack getFirstPartialStack(Inventory inventory, @NotNull ItemStack type) {
int maxStackSize = stackSizeProvider.getMaxStackSize(type); int maxStackSize = stackSizeProvider.getMaxStackSize(type);
for (ItemStack item : inventory.getStorageContents()) {
ItemStack[] storageContents = inventory.getStorageContents();
for (int i = 0; i < storageContents.length; i++) {
if (isInvalidSlot(inventory, i))
continue;
ItemStack item = storageContents[i];
if (type.isSimilar(item)) { if (type.isSimilar(item)) {
int amount = item.getAmount(); int amount = item.getAmount();
if (amount < maxStackSize) if (amount < maxStackSize)
@ -67,13 +74,26 @@ public class InventoryUtils {
public static int getFirstEmptySlot(Inventory inventory) { public static int getFirstEmptySlot(Inventory inventory) {
ItemStack[] storageContents = inventory.getStorageContents(); ItemStack[] storageContents = inventory.getStorageContents();
for (int i = 0; i < storageContents.length; i++) { for (int i = 0; i < storageContents.length; i++) {
if (storageContents[i] == null) if (isInvalidSlot(inventory, i))
continue;
ItemStack item = storageContents[i];
if (item == null || item.getType().isAir())
return i; return i;
} }
return -1; return -1;
} }
private static boolean isInvalidSlot(Inventory inventory, int slot) {
if (inventory instanceof CraftingInventory) {
// craft result slot
return slot == 0;
}
return false;
}
public static Inventory createMatchingInventory(GUI gui, String title) { public static Inventory createMatchingInventory(GUI gui, String title) {
InventoryType type; InventoryType type;