PaperMC/paper-server/patches/sources/net/minecraft/world/SimpleContainer.java.patch
2022-01-24 00:09:02 -08:00

103 lines
3.6 KiB
Diff

--- a/net/minecraft/world/SimpleContainer.java
+++ b/net/minecraft/world/SimpleContainer.java
@@ -14,18 +14,98 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
+// CraftBukkit start
+import org.bukkit.Location;
+import org.bukkit.craftbukkit.entity.CraftHumanEntity;
+import org.bukkit.entity.HumanEntity;
+// CraftBukkit end
+
public class SimpleContainer implements Container, StackedContentsCompatible {
private final int size;
public final NonNullList<ItemStack> items;
@Nullable
private List<ContainerListener> listeners;
+
+ // CraftBukkit start - add fields and methods
+ public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
+ private int maxStack = MAX_STACK;
+ protected @Nullable org.bukkit.inventory.InventoryHolder bukkitOwner; // Paper - annotation
+
+ public List<ItemStack> getContents() {
+ return this.items;
+ }
+
+ 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;
+ }
+
+ @Override
+ public int getMaxStackSize() {
+ return this.maxStack;
+ }
+
+ public void setMaxStackSize(int i) {
+ this.maxStack = i;
+ }
+
+ public org.bukkit.inventory.InventoryHolder getOwner() {
+ // Paper start - Add missing InventoryHolders
+ if (this.bukkitOwner == null && this.bukkitOwnerCreator != null) {
+ this.bukkitOwner = this.bukkitOwnerCreator.get();
+ }
+ // Paper end - Add missing InventoryHolders
+ return this.bukkitOwner;
+ }
+ @Override
+ public Location getLocation() {
+ // Paper start - Fix inventories returning null Locations
+ // When the block inventory does not have a tile state that implements getLocation, e. g. composters
+ if (this.bukkitOwner instanceof org.bukkit.inventory.BlockInventoryHolder blockInventoryHolder) {
+ return blockInventoryHolder.getBlock().getLocation();
+ }
+ // When the bukkit owner is a bukkit entity, but does not implement Container itself, e. g. horses
+ if (this.bukkitOwner instanceof org.bukkit.entity.Entity entity) {
+ return entity.getLocation();
+ }
+ // Paper end - Fix inventories returning null Locations
+ return null;
+ }
+
+ public SimpleContainer(SimpleContainer original) {
+ this(original.size);
+ for (int slot = 0; slot < original.size; slot++) {
+ this.items.set(slot, original.items.get(slot).copy());
+ }
+ }
+
public SimpleContainer(int size) {
- this.size = size;
- this.items = NonNullList.withSize(size, ItemStack.EMPTY);
+ this(size, null);
+ }
+ // Paper start - Add missing InventoryHolders
+ private @Nullable java.util.function.Supplier<? extends org.bukkit.inventory.InventoryHolder> bukkitOwnerCreator;
+ public SimpleContainer(java.util.function.Supplier<? extends org.bukkit.inventory.InventoryHolder> bukkitOwnerCreator, int size) {
+ this(size);
+ this.bukkitOwnerCreator = bukkitOwnerCreator;
}
+ // Paper end - Add missing InventoryHolders
+ public SimpleContainer(int i, org.bukkit.inventory.InventoryHolder owner) {
+ this.bukkitOwner = owner;
+ // CraftBukkit end
+ this.size = i;
+ this.items = NonNullList.withSize(i, ItemStack.EMPTY);
+ }
+
public SimpleContainer(ItemStack... items) {
this.size = items.length;
this.items = NonNullList.of(ItemStack.EMPTY, items);