--- a/net/minecraft/world/entity/player/Inventory.java
+++ b/net/minecraft/world/entity/player/Inventory.java
@@ -1,6 +1,7 @@
 package net.minecraft.world.entity.player;
 
 import com.google.common.collect.ImmutableList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.function.Predicate;
 import net.minecraft.CrashReport;
@@ -17,11 +18,19 @@
 import net.minecraft.world.Container;
 import net.minecraft.world.ContainerHelper;
 import net.minecraft.world.Nameable;
+import net.minecraft.world.entity.Entity;
 import net.minecraft.world.item.Item;
 import net.minecraft.world.item.ItemStack;
 import net.minecraft.world.level.block.state.BlockState;
+// CraftBukkit start
+import java.util.ArrayList;
+import org.bukkit.Location;
+import org.bukkit.craftbukkit.entity.CraftHumanEntity;
+import org.bukkit.entity.HumanEntity;
+// CraftBukkit end
 
 public class Inventory implements Container, Nameable {
+
     public static final int POP_TIME_DURATION = 5;
     public static final int INVENTORY_SIZE = 36;
     private static final int SELECTION_SIZE = 9;
@@ -29,20 +38,72 @@
     public static final int NOT_FOUND_INDEX = -1;
     public static final int[] ALL_ARMOR_SLOTS = new int[]{0, 1, 2, 3};
     public static final int[] HELMET_SLOT_ONLY = new int[]{3};
-    public final NonNullList<ItemStack> items = NonNullList.withSize(36, ItemStack.EMPTY);
-    public final NonNullList<ItemStack> armor = NonNullList.withSize(4, ItemStack.EMPTY);
-    public final NonNullList<ItemStack> offhand = NonNullList.withSize(1, ItemStack.EMPTY);
-    public final List<NonNullList<ItemStack>> compartments = ImmutableList.of(this.items, this.armor, this.offhand);
+    public final NonNullList<ItemStack> items;
+    public final NonNullList<ItemStack> armor;
+    public final NonNullList<ItemStack> offhand;
+    public final List<NonNullList<ItemStack>> compartments;
     public int selected;
     public final Player player;
     private int timesChanged;
 
+    // CraftBukkit start - add fields and methods
+    public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
+    private int maxStack = MAX_STACK;
+
+    public List<ItemStack> getContents() {
+        List<ItemStack> combined = new ArrayList<ItemStack>(this.items.size() + this.armor.size() + this.offhand.size());
+        for (List<net.minecraft.world.item.ItemStack> sub : this.compartments) {
+            combined.addAll(sub);
+        }
+
+        return combined;
+    }
+
+    public List<ItemStack> getArmorContents() {
+        return this.armor;
+    }
+
+    public void onOpen(CraftHumanEntity who) {
+        this.transaction.add(who);
+    }
+
+    public void onClose(CraftHumanEntity who) {
+        this.transaction.remove(who);
+    }
+
+    public List<HumanEntity> getViewers() {
+        return this.transaction;
+    }
+
+    public org.bukkit.inventory.InventoryHolder getOwner() {
+        return this.player.getBukkitEntity();
+    }
+
+    @Override
+    public int getMaxStackSize() {
+        return this.maxStack;
+    }
+
+    public void setMaxStackSize(int size) {
+        this.maxStack = size;
+    }
+
+    @Override
+    public Location getLocation() {
+        return this.player.getBukkitEntity().getLocation();
+    }
+    // CraftBukkit end
+
     public Inventory(Player player) {
+        this.items = NonNullList.withSize(36, ItemStack.EMPTY);
+        this.armor = NonNullList.withSize(4, ItemStack.EMPTY);
+        this.offhand = NonNullList.withSize(1, ItemStack.EMPTY);
+        this.compartments = ImmutableList.of(this.items, this.armor, this.offhand);
         this.player = player;
     }
 
     public ItemStack getSelected() {
-        return isHotbarSlot(this.selected) ? this.items.get(this.selected) : ItemStack.EMPTY;
+        return Inventory.isHotbarSlot(this.selected) ? (ItemStack) this.items.get(this.selected) : ItemStack.EMPTY;
     }
 
     public static int getSelectionSize() {
@@ -50,15 +111,34 @@
     }
 
     private boolean hasRemainingSpaceForItem(ItemStack existingStack, ItemStack stack) {
-        return !existingStack.isEmpty()
-            && ItemStack.isSameItemSameComponents(existingStack, stack)
-            && existingStack.isStackable()
-            && existingStack.getCount() < this.getMaxStackSize(existingStack);
+        return !existingStack.isEmpty() && existingStack.isStackable() && existingStack.getCount() < this.getMaxStackSize(existingStack) && ItemStack.isSameItemSameComponents(existingStack, stack); // Paper - check if itemstack is stackable first
+    }
+
+    // CraftBukkit start - Watch method above! :D
+    public int canHold(ItemStack itemstack) {
+        int remains = itemstack.getCount();
+        for (int i = 0; i < this.items.size(); ++i) {
+            ItemStack itemstack1 = this.getItem(i);
+            if (itemstack1.isEmpty()) return itemstack.getCount();
+
+            if (this.hasRemainingSpaceForItem(itemstack1, itemstack)) {
+                remains -= (itemstack1.getMaxStackSize() < this.getMaxStackSize() ? itemstack1.getMaxStackSize() : this.getMaxStackSize()) - itemstack1.getCount();
+            }
+            if (remains <= 0) return itemstack.getCount();
+        }
+        ItemStack offhandItemStack = this.getItem(this.items.size() + this.armor.size());
+        if (this.hasRemainingSpaceForItem(offhandItemStack, itemstack)) {
+            remains -= (offhandItemStack.getMaxStackSize() < this.getMaxStackSize() ? offhandItemStack.getMaxStackSize() : this.getMaxStackSize()) - offhandItemStack.getCount();
+        }
+        if (remains <= 0) return itemstack.getCount();
+
+        return itemstack.getCount() - remains;
     }
+    // CraftBukkit end
 
     public int getFreeSlot() {
-        for (int i = 0; i < this.items.size(); i++) {
-            if (this.items.get(i).isEmpty()) {
+        for (int i = 0; i < this.items.size(); ++i) {
+            if (((ItemStack) this.items.get(i)).isEmpty()) {
                 return i;
             }
         }
@@ -68,15 +148,17 @@
 
     public void setPickedItem(ItemStack stack) {
         int i = this.findSlotMatchingItem(stack);
-        if (isHotbarSlot(i)) {
+
+        if (Inventory.isHotbarSlot(i)) {
             this.selected = i;
         } else {
             if (i == -1) {
                 this.selected = this.getSuitableHotbarSlot();
-                if (!this.items.get(this.selected).isEmpty()) {
+                if (!((ItemStack) this.items.get(this.selected)).isEmpty()) {
                     int j = this.getFreeSlot();
+
                     if (j != -1) {
-                        this.items.set(j, this.items.get(this.selected));
+                        this.items.set(j, (ItemStack) this.items.get(this.selected));
                     }
                 }
 
@@ -84,14 +166,22 @@
             } else {
                 this.pickSlot(i);
             }
+
         }
     }
 
     public void pickSlot(int slot) {
-        this.selected = this.getSuitableHotbarSlot();
-        ItemStack itemStack = this.items.get(this.selected);
-        this.items.set(this.selected, this.items.get(slot));
-        this.items.set(slot, itemStack);
+        // Paper start - Add PlayerPickItemEvent
+        pickSlot(slot, this.getSuitableHotbarSlot());
+    }
+
+    public void pickSlot(int slot, int targetSlot) {
+        this.selected = targetSlot;
+        // Paper end - Add PlayerPickItemEvent
+        ItemStack itemstack = (ItemStack) this.items.get(this.selected);
+
+        this.items.set(this.selected, (ItemStack) this.items.get(slot));
+        this.items.set(slot, itemstack);
     }
 
     public static boolean isHotbarSlot(int slot) {
@@ -99,8 +189,8 @@
     }
 
     public int findSlotMatchingItem(ItemStack stack) {
-        for (int i = 0; i < this.items.size(); i++) {
-            if (!this.items.get(i).isEmpty() && ItemStack.isSameItemSameComponents(stack, this.items.get(i))) {
+        for (int i = 0; i < this.items.size(); ++i) {
+            if (!((ItemStack) this.items.get(i)).isEmpty() && ItemStack.isSameItemSameComponents(stack, (ItemStack) this.items.get(i))) {
                 return i;
             }
         }
@@ -109,13 +199,10 @@
     }
 
     public int findSlotMatchingUnusedItem(ItemStack stack) {
-        for (int i = 0; i < this.items.size(); i++) {
-            ItemStack itemStack = this.items.get(i);
-            if (!itemStack.isEmpty()
-                && ItemStack.isSameItemSameComponents(stack, itemStack)
-                && !itemStack.isDamaged()
-                && !itemStack.isEnchanted()
-                && !itemStack.has(DataComponents.CUSTOM_NAME)) {
+        for (int i = 0; i < this.items.size(); ++i) {
+            ItemStack itemstack1 = (ItemStack) this.items.get(i);
+
+            if (!itemstack1.isEmpty() && ItemStack.isSameItemSameComponents(stack, itemstack1) && !itemstack1.isDamaged() && !itemstack1.isEnchanted() && !itemstack1.has(DataComponents.CUSTOM_NAME)) {
                 return i;
             }
         }
@@ -124,17 +211,20 @@
     }
 
     public int getSuitableHotbarSlot() {
-        for (int i = 0; i < 9; i++) {
-            int j = (this.selected + i) % 9;
-            if (this.items.get(j).isEmpty()) {
-                return j;
+        int i;
+        int j;
+
+        for (j = 0; j < 9; ++j) {
+            i = (this.selected + j) % 9;
+            if (((ItemStack) this.items.get(i)).isEmpty()) {
+                return i;
             }
         }
 
-        for (int k = 0; k < 9; k++) {
-            int l = (this.selected + k) % 9;
-            if (!this.items.get(l).isEnchanted()) {
-                return l;
+        for (j = 0; j < 9; ++j) {
+            i = (this.selected + j) % 9;
+            if (!((ItemStack) this.items.get(i)).isEnchanted()) {
+                return i;
             }
         }
 
@@ -142,34 +232,37 @@
     }
 
     public void swapPaint(double scrollAmount) {
-        int i = (int)Math.signum(scrollAmount);
-        this.selected -= i;
+        int i = (int) Math.signum(scrollAmount);
 
-        while (this.selected < 0) {
-            this.selected += 9;
+        for (this.selected -= i; this.selected < 0; this.selected += 9) {
+            ;
         }
 
         while (this.selected >= 9) {
             this.selected -= 9;
         }
+
     }
 
     public int clearOrCountMatchingItems(Predicate<ItemStack> shouldRemove, int maxCount, Container craftingInventory) {
-        int i = 0;
-        boolean bl = maxCount == 0;
-        i += ContainerHelper.clearOrCountMatchingItems(this, shouldRemove, maxCount - i, bl);
-        i += ContainerHelper.clearOrCountMatchingItems(craftingInventory, shouldRemove, maxCount - i, bl);
-        ItemStack itemStack = this.player.containerMenu.getCarried();
-        i += ContainerHelper.clearOrCountMatchingItems(itemStack, shouldRemove, maxCount - i, bl);
-        if (itemStack.isEmpty()) {
+        int j = 0;
+        boolean flag = maxCount == 0;
+
+        j += ContainerHelper.clearOrCountMatchingItems((Container) this, shouldRemove, maxCount - j, flag);
+        j += ContainerHelper.clearOrCountMatchingItems(craftingInventory, shouldRemove, maxCount - j, flag);
+        ItemStack itemstack = this.player.containerMenu.getCarried();
+
+        j += ContainerHelper.clearOrCountMatchingItems(itemstack, shouldRemove, maxCount - j, flag);
+        if (itemstack.isEmpty()) {
             this.player.containerMenu.setCarried(ItemStack.EMPTY);
         }
 
-        return i;
+        return j;
     }
 
     private int addResource(ItemStack stack) {
         int i = this.getSlotWithRemainingSpace(stack);
+
         if (i == -1) {
             i = this.getFreeSlot();
         }
@@ -178,22 +271,24 @@
     }
 
     private int addResource(int slot, ItemStack stack) {
-        int i = stack.getCount();
-        ItemStack itemStack = this.getItem(slot);
-        if (itemStack.isEmpty()) {
-            itemStack = stack.copyWithCount(0);
-            this.setItem(slot, itemStack);
+        int j = stack.getCount();
+        ItemStack itemstack1 = this.getItem(slot);
+
+        if (itemstack1.isEmpty()) {
+            itemstack1 = stack.copyWithCount(0);
+            this.setItem(slot, itemstack1);
         }
 
-        int j = this.getMaxStackSize(itemStack) - itemStack.getCount();
-        int k = Math.min(i, j);
-        if (k == 0) {
-            return i;
+        int k = this.getMaxStackSize(itemstack1) - itemstack1.getCount();
+        int l = Math.min(j, k);
+
+        if (l == 0) {
+            return j;
         } else {
-            i -= k;
-            itemStack.grow(k);
-            itemStack.setPopTime(5);
-            return i;
+            j -= l;
+            itemstack1.grow(l);
+            itemstack1.setPopTime(5);
+            return j;
         }
     }
 
@@ -203,8 +298,8 @@
         } else if (this.hasRemainingSpaceForItem(this.getItem(40), stack)) {
             return 40;
         } else {
-            for (int i = 0; i < this.items.size(); i++) {
-                if (this.hasRemainingSpaceForItem(this.items.get(i), stack)) {
+            for (int i = 0; i < this.items.size(); ++i) {
+                if (this.hasRemainingSpaceForItem((ItemStack) this.items.get(i), stack)) {
                     return i;
                 }
             }
@@ -214,13 +309,18 @@
     }
 
     public void tick() {
-        for (NonNullList<ItemStack> nonNullList : this.compartments) {
-            for (int i = 0; i < nonNullList.size(); i++) {
-                if (!nonNullList.get(i).isEmpty()) {
-                    nonNullList.get(i).inventoryTick(this.player.level(), this.player, i, this.selected == i);
+        Iterator iterator = this.compartments.iterator();
+
+        while (iterator.hasNext()) {
+            NonNullList<ItemStack> nonnulllist = (NonNullList) iterator.next();
+
+            for (int i = 0; i < nonnulllist.size(); ++i) {
+                if (!((ItemStack) nonnulllist.get(i)).isEmpty()) {
+                    ((ItemStack) nonnulllist.get(i)).inventoryTick(this.player.level(), this.player, i, this.selected == i);
                 }
             }
         }
+
     }
 
     public boolean add(ItemStack stack) {
@@ -239,7 +339,7 @@
 
                     if (slot >= 0) {
                         this.items.set(slot, stack.copyAndClear());
-                        this.items.get(slot).setPopTime(5);
+                        ((ItemStack) this.items.get(slot)).setPopTime(5);
                         return true;
                     } else if (this.player.hasInfiniteMaterials()) {
                         stack.setCount(0);
@@ -248,30 +348,34 @@
                         return false;
                     }
                 } else {
-                    int i;
+                    int j;
+
                     do {
-                        i = stack.getCount();
+                        j = stack.getCount();
                         if (slot == -1) {
                             stack.setCount(this.addResource(stack));
                         } else {
                             stack.setCount(this.addResource(slot, stack));
                         }
-                    } while (!stack.isEmpty() && stack.getCount() < i);
+                    } while (!stack.isEmpty() && stack.getCount() < j);
 
-                    if (stack.getCount() == i && this.player.hasInfiniteMaterials()) {
+                    if (stack.getCount() == j && this.player.hasInfiniteMaterials()) {
                         stack.setCount(0);
                         return true;
                     } else {
-                        return stack.getCount() < i;
+                        return stack.getCount() < j;
                     }
                 }
-            } catch (Throwable var6) {
-                CrashReport crashReport = CrashReport.forThrowable(var6, "Adding item to inventory");
-                CrashReportCategory crashReportCategory = crashReport.addCategory("Item being added");
-                crashReportCategory.setDetail("Item ID", Item.getId(stack.getItem()));
-                crashReportCategory.setDetail("Item data", stack.getDamageValue());
-                crashReportCategory.setDetail("Item name", () -> stack.getHoverName().getString());
-                throw new ReportedException(crashReport);
+            } catch (Throwable throwable) {
+                CrashReport crashreport = CrashReport.forThrowable(throwable, "Adding item to inventory");
+                CrashReportCategory crashreportsystemdetails = crashreport.addCategory("Item being added");
+
+                crashreportsystemdetails.setDetail("Item ID", (Object) Item.getId(stack.getItem()));
+                crashreportsystemdetails.setDetail("Item data", (Object) stack.getDamageValue());
+                crashreportsystemdetails.setDetail("Item name", () -> {
+                    return stack.getHoverName().getString();
+                });
+                throw new ReportedException(crashreport);
             }
         }
     }
@@ -281,21 +385,27 @@
     }
 
     public void placeItemBackInInventory(ItemStack stack, boolean notifiesClient) {
-        while (!stack.isEmpty()) {
-            int i = this.getSlotWithRemainingSpace(stack);
-            if (i == -1) {
-                i = this.getFreeSlot();
-            }
+        while (true) {
+            if (!stack.isEmpty()) {
+                int i = this.getSlotWithRemainingSpace(stack);
+
+                if (i == -1) {
+                    i = this.getFreeSlot();
+                }
+
+                if (i != -1) {
+                    int j = stack.getMaxStackSize() - this.getItem(i).getCount();
+
+                    if (this.add(i, stack.split(j)) && notifiesClient && this.player instanceof ServerPlayer) {
+                        ((ServerPlayer) this.player).connection.send(new ClientboundContainerSetSlotPacket(-2, 0, i, this.getItem(i)));
+                    }
+                    continue;
+                }
 
-            if (i == -1) {
                 this.player.drop(stack, false);
-                break;
             }
 
-            int j = stack.getMaxStackSize() - this.getItem(i).getCount();
-            if (this.add(i, stack.split(j)) && notifiesClient && this.player instanceof ServerPlayer) {
-                ((ServerPlayer)this.player).connection.send(new ClientboundContainerSetSlotPacket(-2, 0, i, this.getItem(i)));
-            }
+            return;
         }
     }
 
@@ -303,46 +413,54 @@
     public ItemStack removeItem(int slot, int amount) {
         List<ItemStack> list = null;
 
-        for (NonNullList<ItemStack> nonNullList : this.compartments) {
-            if (slot < nonNullList.size()) {
-                list = nonNullList;
+        NonNullList nonnulllist;
+
+        for (Iterator iterator = this.compartments.iterator(); iterator.hasNext(); slot -= nonnulllist.size()) {
+            nonnulllist = (NonNullList) iterator.next();
+            if (slot < nonnulllist.size()) {
+                list = nonnulllist;
                 break;
             }
-
-            slot -= nonNullList.size();
         }
 
-        return list != null && !list.get(slot).isEmpty() ? ContainerHelper.removeItem(list, slot, amount) : ItemStack.EMPTY;
+        return list != null && !((ItemStack) list.get(slot)).isEmpty() ? ContainerHelper.removeItem(list, slot, amount) : ItemStack.EMPTY;
     }
 
     public void removeItem(ItemStack stack) {
-        for (NonNullList<ItemStack> nonNullList : this.compartments) {
-            for (int i = 0; i < nonNullList.size(); i++) {
-                if (nonNullList.get(i) == stack) {
-                    nonNullList.set(i, ItemStack.EMPTY);
+        Iterator iterator = this.compartments.iterator();
+
+        while (iterator.hasNext()) {
+            NonNullList<ItemStack> nonnulllist = (NonNullList) iterator.next();
+
+            for (int i = 0; i < nonnulllist.size(); ++i) {
+                if (nonnulllist.get(i) == stack) {
+                    nonnulllist.set(i, ItemStack.EMPTY);
                     break;
                 }
             }
         }
+
     }
 
     @Override
     public ItemStack removeItemNoUpdate(int slot) {
-        NonNullList<ItemStack> nonNullList = null;
+        NonNullList<ItemStack> nonnulllist = null;
 
-        for (NonNullList<ItemStack> nonNullList2 : this.compartments) {
-            if (slot < nonNullList2.size()) {
-                nonNullList = nonNullList2;
+        NonNullList nonnulllist1;
+
+        for (Iterator iterator = this.compartments.iterator(); iterator.hasNext(); slot -= nonnulllist1.size()) {
+            nonnulllist1 = (NonNullList) iterator.next();
+            if (slot < nonnulllist1.size()) {
+                nonnulllist = nonnulllist1;
                 break;
             }
-
-            slot -= nonNullList2.size();
         }
 
-        if (nonNullList != null && !nonNullList.get(slot).isEmpty()) {
-            ItemStack itemStack = nonNullList.get(slot);
-            nonNullList.set(slot, ItemStack.EMPTY);
-            return itemStack;
+        if (nonnulllist != null && !((ItemStack) nonnulllist.get(slot)).isEmpty()) {
+            ItemStack itemstack = (ItemStack) nonnulllist.get(slot);
+
+            nonnulllist.set(slot, ItemStack.EMPTY);
+            return itemstack;
         } else {
             return ItemStack.EMPTY;
         }
@@ -350,48 +468,53 @@
 
     @Override
     public void setItem(int slot, ItemStack stack) {
-        NonNullList<ItemStack> nonNullList = null;
+        NonNullList<ItemStack> nonnulllist = null;
 
-        for (NonNullList<ItemStack> nonNullList2 : this.compartments) {
-            if (slot < nonNullList2.size()) {
-                nonNullList = nonNullList2;
+        NonNullList nonnulllist1;
+
+        for (Iterator iterator = this.compartments.iterator(); iterator.hasNext(); slot -= nonnulllist1.size()) {
+            nonnulllist1 = (NonNullList) iterator.next();
+            if (slot < nonnulllist1.size()) {
+                nonnulllist = nonnulllist1;
                 break;
             }
-
-            slot -= nonNullList2.size();
         }
 
-        if (nonNullList != null) {
-            nonNullList.set(slot, stack);
+        if (nonnulllist != null) {
+            nonnulllist.set(slot, stack);
         }
+
     }
 
     public float getDestroySpeed(BlockState block) {
-        return this.items.get(this.selected).getDestroySpeed(block);
+        return ((ItemStack) this.items.get(this.selected)).getDestroySpeed(block);
     }
 
     public ListTag save(ListTag nbtList) {
-        for (int i = 0; i < this.items.size(); i++) {
-            if (!this.items.get(i).isEmpty()) {
-                CompoundTag compoundTag = new CompoundTag();
-                compoundTag.putByte("Slot", (byte)i);
-                nbtList.add(this.items.get(i).save(this.player.registryAccess(), compoundTag));
+        CompoundTag nbttagcompound;
+        int i;
+
+        for (i = 0; i < this.items.size(); ++i) {
+            if (!((ItemStack) this.items.get(i)).isEmpty()) {
+                nbttagcompound = new CompoundTag();
+                nbttagcompound.putByte("Slot", (byte) i);
+                nbtList.add(((ItemStack) this.items.get(i)).save(this.player.registryAccess(), nbttagcompound));
             }
         }
 
-        for (int j = 0; j < this.armor.size(); j++) {
-            if (!this.armor.get(j).isEmpty()) {
-                CompoundTag compoundTag2 = new CompoundTag();
-                compoundTag2.putByte("Slot", (byte)(j + 100));
-                nbtList.add(this.armor.get(j).save(this.player.registryAccess(), compoundTag2));
+        for (i = 0; i < this.armor.size(); ++i) {
+            if (!((ItemStack) this.armor.get(i)).isEmpty()) {
+                nbttagcompound = new CompoundTag();
+                nbttagcompound.putByte("Slot", (byte) (i + 100));
+                nbtList.add(((ItemStack) this.armor.get(i)).save(this.player.registryAccess(), nbttagcompound));
             }
         }
 
-        for (int k = 0; k < this.offhand.size(); k++) {
-            if (!this.offhand.get(k).isEmpty()) {
-                CompoundTag compoundTag3 = new CompoundTag();
-                compoundTag3.putByte("Slot", (byte)(k + 150));
-                nbtList.add(this.offhand.get(k).save(this.player.registryAccess(), compoundTag3));
+        for (i = 0; i < this.offhand.size(); ++i) {
+            if (!((ItemStack) this.offhand.get(i)).isEmpty()) {
+                nbttagcompound = new CompoundTag();
+                nbttagcompound.putByte("Slot", (byte) (i + 150));
+                nbtList.add(((ItemStack) this.offhand.get(i)).save(this.player.registryAccess(), nbttagcompound));
             }
         }
 
@@ -403,18 +526,20 @@
         this.armor.clear();
         this.offhand.clear();
 
-        for (int i = 0; i < nbtList.size(); i++) {
-            CompoundTag compoundTag = nbtList.getCompound(i);
-            int j = compoundTag.getByte("Slot") & 255;
-            ItemStack itemStack = ItemStack.parse(this.player.registryAccess(), compoundTag).orElse(ItemStack.EMPTY);
+        for (int i = 0; i < nbtList.size(); ++i) {
+            CompoundTag nbttagcompound = nbtList.getCompound(i);
+            int j = nbttagcompound.getByte("Slot") & 255;
+            ItemStack itemstack = (ItemStack) ItemStack.parse(this.player.registryAccess(), nbttagcompound).orElse(ItemStack.EMPTY);
+
             if (j >= 0 && j < this.items.size()) {
-                this.items.set(j, itemStack);
+                this.items.set(j, itemstack);
             } else if (j >= 100 && j < this.armor.size() + 100) {
-                this.armor.set(j - 100, itemStack);
+                this.armor.set(j - 100, itemstack);
             } else if (j >= 150 && j < this.offhand.size() + 150) {
-                this.offhand.set(j - 150, itemStack);
+                this.offhand.set(j - 150, itemstack);
             }
         }
+
     }
 
     @Override
@@ -424,41 +549,56 @@
 
     @Override
     public boolean isEmpty() {
-        for (ItemStack itemStack : this.items) {
-            if (!itemStack.isEmpty()) {
-                return false;
-            }
-        }
+        Iterator iterator = this.items.iterator();
 
-        for (ItemStack itemStack2 : this.armor) {
-            if (!itemStack2.isEmpty()) {
-                return false;
-            }
-        }
+        ItemStack itemstack;
+
+        do {
+            if (!iterator.hasNext()) {
+                iterator = this.armor.iterator();
+
+                do {
+                    if (!iterator.hasNext()) {
+                        iterator = this.offhand.iterator();
+
+                        do {
+                            if (!iterator.hasNext()) {
+                                return true;
+                            }
+
+                            itemstack = (ItemStack) iterator.next();
+                        } while (itemstack.isEmpty());
+
+                        return false;
+                    }
+
+                    itemstack = (ItemStack) iterator.next();
+                } while (itemstack.isEmpty());
 
-        for (ItemStack itemStack3 : this.offhand) {
-            if (!itemStack3.isEmpty()) {
                 return false;
             }
-        }
 
-        return true;
+            itemstack = (ItemStack) iterator.next();
+        } while (itemstack.isEmpty());
+
+        return false;
     }
 
     @Override
     public ItemStack getItem(int slot) {
         List<ItemStack> list = null;
 
-        for (NonNullList<ItemStack> nonNullList : this.compartments) {
-            if (slot < nonNullList.size()) {
-                list = nonNullList;
+        NonNullList nonnulllist;
+
+        for (Iterator iterator = this.compartments.iterator(); iterator.hasNext(); slot -= nonnulllist.size()) {
+            nonnulllist = (NonNullList) iterator.next();
+            if (slot < nonnulllist.size()) {
+                list = nonnulllist;
                 break;
             }
-
-            slot -= nonNullList.size();
         }
 
-        return list == null ? ItemStack.EMPTY : list.get(slot);
+        return list == null ? ItemStack.EMPTY : (ItemStack) list.get(slot);
     }
 
     @Override
@@ -467,24 +607,30 @@
     }
 
     public ItemStack getArmor(int slot) {
-        return this.armor.get(slot);
+        return (ItemStack) this.armor.get(slot);
     }
 
     public void dropAll() {
-        for (List<ItemStack> list : this.compartments) {
-            for (int i = 0; i < list.size(); i++) {
-                ItemStack itemStack = list.get(i);
-                if (!itemStack.isEmpty()) {
-                    this.player.drop(itemStack, true, false);
+        Iterator iterator = this.compartments.iterator();
+
+        while (iterator.hasNext()) {
+            List<ItemStack> list = (List) iterator.next();
+
+            for (int i = 0; i < list.size(); ++i) {
+                ItemStack itemstack = (ItemStack) list.get(i);
+
+                if (!itemstack.isEmpty()) {
+                    this.player.drop(itemstack, true, false);
                     list.set(i, ItemStack.EMPTY);
                 }
             }
         }
+
     }
 
     @Override
     public void setChanged() {
-        this.timesChanged++;
+        ++this.timesChanged;
     }
 
     public int getTimesChanged() {
@@ -493,13 +639,20 @@
 
     @Override
     public boolean stillValid(Player player) {
-        return player.canInteractWithEntity(this.player, 4.0);
+        return player.canInteractWithEntity((Entity) this.player, 4.0D);
     }
 
     public boolean contains(ItemStack stack) {
-        for (List<ItemStack> list : this.compartments) {
-            for (ItemStack itemStack : list) {
-                if (!itemStack.isEmpty() && ItemStack.isSameItemSameComponents(itemStack, stack)) {
+        Iterator iterator = this.compartments.iterator();
+
+        while (iterator.hasNext()) {
+            List<ItemStack> list = (List) iterator.next();
+            Iterator iterator1 = list.iterator();
+
+            while (iterator1.hasNext()) {
+                ItemStack itemstack1 = (ItemStack) iterator1.next();
+
+                if (!itemstack1.isEmpty() && ItemStack.isSameItemSameComponents(itemstack1, stack)) {
                     return true;
                 }
             }
@@ -509,9 +662,16 @@
     }
 
     public boolean contains(TagKey<Item> tag) {
-        for (List<ItemStack> list : this.compartments) {
-            for (ItemStack itemStack : list) {
-                if (!itemStack.isEmpty() && itemStack.is(tag)) {
+        Iterator iterator = this.compartments.iterator();
+
+        while (iterator.hasNext()) {
+            List<ItemStack> list = (List) iterator.next();
+            Iterator iterator1 = list.iterator();
+
+            while (iterator1.hasNext()) {
+                ItemStack itemstack = (ItemStack) iterator1.next();
+
+                if (!itemstack.isEmpty() && itemstack.is(tag)) {
                     return true;
                 }
             }
@@ -521,9 +681,16 @@
     }
 
     public boolean contains(Predicate<ItemStack> predicate) {
-        for (List<ItemStack> list : this.compartments) {
-            for (ItemStack itemStack : list) {
-                if (predicate.test(itemStack)) {
+        Iterator iterator = this.compartments.iterator();
+
+        while (iterator.hasNext()) {
+            List<ItemStack> list = (List) iterator.next();
+            Iterator iterator1 = list.iterator();
+
+            while (iterator1.hasNext()) {
+                ItemStack itemstack = (ItemStack) iterator1.next();
+
+                if (predicate.test(itemstack)) {
                     return true;
                 }
             }
@@ -533,7 +700,7 @@
     }
 
     public void replaceWith(Inventory other) {
-        for (int i = 0; i < this.getContainerSize(); i++) {
+        for (int i = 0; i < this.getContainerSize(); ++i) {
             this.setItem(i, other.getItem(i));
         }
 
@@ -542,19 +709,30 @@
 
     @Override
     public void clearContent() {
-        for (List<ItemStack> list : this.compartments) {
+        Iterator iterator = this.compartments.iterator();
+
+        while (iterator.hasNext()) {
+            List<ItemStack> list = (List) iterator.next();
+
             list.clear();
         }
+
     }
 
     public void fillStackedContents(StackedContents finder) {
-        for (ItemStack itemStack : this.items) {
-            finder.accountSimpleStack(itemStack);
+        Iterator iterator = this.items.iterator();
+
+        while (iterator.hasNext()) {
+            ItemStack itemstack = (ItemStack) iterator.next();
+
+            finder.accountSimpleStack(itemstack);
         }
+
     }
 
     public ItemStack removeFromSelected(boolean entireStack) {
-        ItemStack itemStack = this.getSelected();
-        return itemStack.isEmpty() ? ItemStack.EMPTY : this.removeItem(this.selected, entireStack ? itemStack.getCount() : 1);
+        ItemStack itemstack = this.getSelected();
+
+        return itemstack.isEmpty() ? ItemStack.EMPTY : this.removeItem(this.selected, entireStack ? itemstack.getCount() : 1);
     }
 }
