--- 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;
@@ -18,13 +19,21 @@
 import net.minecraft.world.ContainerHelper;
 import net.minecraft.world.Nameable;
 import net.minecraft.world.damagesource.DamageSource;
+import net.minecraft.world.entity.Entity;
 import net.minecraft.world.entity.EquipmentSlot;
 import net.minecraft.world.item.ArmorItem;
 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.v1_20_R2.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;
@@ -32,20 +41,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() {
@@ -56,9 +117,31 @@
         return !existingStack.isEmpty() && ItemStack.isSameItemSameTags(existingStack, stack) && existingStack.isStackable() && existingStack.getCount() < existingStack.getMaxStackSize() && existingStack.getCount() < this.getMaxStackSize();
     }
 
+    // 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 +151,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));
                     }
                 }
 
@@ -89,10 +174,17 @@
     }
 
     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
+        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) {
@@ -100,8 +192,8 @@
     }
 
     public int findSlotMatchingItem(ItemStack stack) {
-        for(int i = 0; i < this.items.size(); ++i) {
-            if (!this.items.get(i).isEmpty() && ItemStack.isSameItemSameTags(stack, this.items.get(i))) {
+        for (int i = 0; i < this.items.size(); ++i) {
+            if (!((ItemStack) this.items.get(i)).isEmpty() && ItemStack.isSameItemSameTags(stack, (ItemStack) this.items.get(i))) {
                 return i;
             }
         }
@@ -110,9 +202,10 @@
     }
 
     public int findSlotMatchingUnusedItem(ItemStack stack) {
-        for(int i = 0; i < this.items.size(); ++i) {
-            ItemStack itemStack = this.items.get(i);
-            if (!this.items.get(i).isEmpty() && ItemStack.isSameItemSameTags(stack, this.items.get(i)) && !this.items.get(i).isDamaged() && !itemStack.isEnchanted() && !itemStack.hasCustomHoverName()) {
+        for (int i = 0; i < this.items.size(); ++i) {
+            ItemStack itemstack1 = (ItemStack) this.items.get(i);
+
+            if (!((ItemStack) this.items.get(i)).isEmpty() && ItemStack.isSameItemSameTags(stack, (ItemStack) this.items.get(i)) && !((ItemStack) this.items.get(i)).isDamaged() && !itemstack1.isEnchanted() && !itemstack1.hasCustomHoverName()) {
                 return i;
             }
         }
@@ -121,17 +214,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;
             }
         }
 
@@ -139,33 +235,37 @@
     }
 
     public void swapPaint(double scrollAmount) {
-        int i = (int)Math.signum(scrollAmount);
+        int i = (int) Math.signum(scrollAmount);
 
-        for(this.selected -= i; this.selected < 0; this.selected += 9) {
+        for (this.selected -= i; this.selected < 0; this.selected += 9) {
+            ;
         }
 
-        while(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()) {
+        byte b0 = 0;
+        boolean flag = maxCount == 0;
+        int j = b0 + ContainerHelper.clearOrCountMatchingItems((Container) this, shouldRemove, maxCount - b0, 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();
         }
@@ -175,33 +275,35 @@
 
     private int addResource(int slot, ItemStack stack) {
         Item item = stack.getItem();
-        int i = stack.getCount();
-        ItemStack itemStack = this.getItem(slot);
-        if (itemStack.isEmpty()) {
-            itemStack = new ItemStack(item, 0);
+        int j = stack.getCount();
+        ItemStack itemstack1 = this.getItem(slot);
+
+        if (itemstack1.isEmpty()) {
+            itemstack1 = new ItemStack(item, 0);
             if (stack.hasTag()) {
-                itemStack.setTag(stack.getTag().copy());
+                itemstack1.setTag(stack.getTag().copy());
             }
 
-            this.setItem(slot, itemStack);
+            this.setItem(slot, itemstack1);
         }
 
-        int j = i;
-        if (i > itemStack.getMaxStackSize() - itemStack.getCount()) {
-            j = itemStack.getMaxStackSize() - itemStack.getCount();
+        int k = j;
+
+        if (j > itemstack1.getMaxStackSize() - itemstack1.getCount()) {
+            k = itemstack1.getMaxStackSize() - itemstack1.getCount();
         }
 
-        if (j > this.getMaxStackSize() - itemStack.getCount()) {
-            j = this.getMaxStackSize() - itemStack.getCount();
+        if (k > this.getMaxStackSize() - itemstack1.getCount()) {
+            k = this.getMaxStackSize() - itemstack1.getCount();
         }
 
-        if (j == 0) {
-            return i;
+        if (k == 0) {
+            return j;
         } else {
-            i -= j;
-            itemStack.grow(j);
-            itemStack.setPopTime(5);
-            return i;
+            j -= k;
+            itemstack1.grow(k);
+            itemstack1.setPopTime(5);
+            return j;
         }
     }
 
@@ -211,8 +313,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;
                 }
             }
@@ -222,10 +324,14 @@
     }
 
     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);
                 }
             }
         }
@@ -248,7 +354,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.getAbilities().instabuild) {
                         stack.setCount(0);
@@ -257,32 +363,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.getAbilities().instabuild) {
+                    if (stack.getCount() == j && this.player.getAbilities().instabuild) {
                         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", () -> {
+            } 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);
+                throw new ReportedException(crashreport);
             }
         }
     }
@@ -292,17 +400,19 @@
     }
 
     public void placeItemBackInInventory(ItemStack stack, boolean notifiesClient) {
-        while(true) {
+        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)));
+                        ((ServerPlayer) this.player).connection.send(new ClientboundContainerSetSlotPacket(-2, 0, i, this.getItem(i)));
                     }
                     continue;
                 }
@@ -318,23 +428,28 @@
     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;
                 }
             }
@@ -344,21 +459,23 @@
 
     @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;
         }
@@ -366,52 +483,56 @@
 
     @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);
-                this.items.get(i).save(compoundTag);
-                nbtList.add(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);
+                ((ItemStack) this.items.get(i)).save(nbttagcompound);
+                nbtList.add(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));
-                this.armor.get(j).save(compoundTag2);
-                nbtList.add(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));
+                ((ItemStack) this.armor.get(i)).save(nbttagcompound);
+                nbtList.add(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));
-                this.offhand.get(k).save(compoundTag3);
-                nbtList.add(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));
+                ((ItemStack) this.offhand.get(i)).save(nbttagcompound);
+                nbtList.add(nbttagcompound);
             }
         }
 
@@ -423,17 +544,18 @@
         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.of(compoundTag);
-            if (!itemStack.isEmpty()) {
+        for (int i = 0; i < nbtList.size(); ++i) {
+            CompoundTag nbttagcompound = nbtList.getCompound(i);
+            int j = nbttagcompound.getByte("Slot") & 255;
+            ItemStack itemstack = ItemStack.of(nbttagcompound);
+
+            if (!itemstack.isEmpty()) {
                 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);
                 }
             }
         }
@@ -447,41 +569,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
@@ -490,21 +627,26 @@
     }
 
     public ItemStack getArmor(int slot) {
-        return this.armor.get(slot);
+        return (ItemStack) this.armor.get(slot);
     }
 
     public void hurtArmor(DamageSource damageSource, float amount, int[] slots) {
-        if (!(amount <= 0.0F)) {
+        if (amount > 0.0F) {
             amount /= 4.0F;
             if (amount < 1.0F) {
                 amount = 1.0F;
             }
 
-            for(int i : slots) {
-                ItemStack itemStack = this.armor.get(i);
-                if ((!damageSource.is(DamageTypeTags.IS_FIRE) || !itemStack.getItem().isFireResistant()) && itemStack.getItem() instanceof ArmorItem) {
-                    itemStack.hurtAndBreak((int)amount, this.player, (player) -> {
-                        player.broadcastBreakEvent(EquipmentSlot.byTypeAndIndex(EquipmentSlot.Type.ARMOR, i));
+            int[] aint1 = slots;
+            int i = slots.length;
+
+            for (int j = 0; j < i; ++j) {
+                int k = aint1[j];
+                ItemStack itemstack = (ItemStack) this.armor.get(k);
+
+                if ((!damageSource.is(DamageTypeTags.IS_FIRE) || !itemstack.getItem().isFireResistant()) && itemstack.getItem() instanceof ArmorItem) {
+                    itemstack.hurtAndBreak((int) amount, this.player, (entityhuman) -> {
+                        entityhuman.broadcastBreakEvent(EquipmentSlot.byTypeAndIndex(EquipmentSlot.Type.ARMOR, k));
                     });
                 }
             }
@@ -513,11 +655,16 @@
     }
 
     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);
                 }
             }
@@ -536,17 +683,20 @@
 
     @Override
     public boolean stillValid(Player player) {
-        if (this.player.isRemoved()) {
-            return false;
-        } else {
-            return !(player.distanceToSqr(this.player) > 64.0D);
-        }
+        return this.player.isRemoved() ? false : player.distanceToSqr((Entity) this.player) <= 64.0D;
     }
 
     public boolean contains(ItemStack stack) {
-        for(List<ItemStack> list : this.compartments) {
-            for(ItemStack itemStack : list) {
-                if (!itemStack.isEmpty() && ItemStack.isSameItemSameTags(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.isSameItemSameTags(itemstack1, stack)) {
                     return true;
                 }
             }
@@ -556,9 +706,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;
                 }
             }
@@ -568,7 +725,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));
         }
 
@@ -577,21 +734,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);
     }
 }
