--- a/net/minecraft/world/inventory/Container.java +++ b/net/minecraft/world/inventory/Container.java @@ -29,6 +29,20 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.TileEntity; +// CraftBukkit start +import com.google.common.base.Preconditions; +import java.util.HashMap; +import java.util.Map; +import net.minecraft.network.chat.IChatBaseComponent; +import net.minecraft.network.protocol.game.PacketPlayOutSetSlot; +import org.bukkit.craftbukkit.inventory.CraftInventory; +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.event.Event.Result; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.InventoryView; +// CraftBukkit end + public abstract class Container { public static final int SLOT_CLICKED_OUTSIDE = -999; @@ -43,7 +57,7 @@ public NonNullList slots = NonNullList.a(); private final List dataSlots = Lists.newArrayList(); private ItemStack carried; - private final NonNullList remoteSlots; + public NonNullList remoteSlots; private final IntList remoteDataSlots; private ItemStack remoteCarried; @Nullable @@ -57,6 +71,27 @@ private ContainerSynchronizer synchronizer; private boolean suppressRemoteUpdates; + // CraftBukkit start + public boolean checkReachable = true; + public abstract InventoryView getBukkitView(); + public void transferTo(Container other, org.bukkit.craftbukkit.entity.CraftHumanEntity player) { + InventoryView source = this.getBukkitView(), destination = other.getBukkitView(); + ((CraftInventory) source.getTopInventory()).getInventory().onClose(player); + ((CraftInventory) source.getBottomInventory()).getInventory().onClose(player); + ((CraftInventory) destination.getTopInventory()).getInventory().onOpen(player); + ((CraftInventory) destination.getBottomInventory()).getInventory().onOpen(player); + } + private IChatBaseComponent title; + public final IChatBaseComponent getTitle() { + Preconditions.checkState(this.title != null, "Title not set"); + return this.title; + } + public final void setTitle(IChatBaseComponent title) { + Preconditions.checkState(this.title == null, "Title already set"); + this.title = title; + } + // CraftBukkit end + protected Container(@Nullable Containers containers, int i) { this.carried = ItemStack.EMPTY; this.remoteSlots = NonNullList.a(); @@ -154,6 +189,15 @@ } + // CraftBukkit start + public void broadcastCarriedItem() { + this.remoteCarried = this.getCarried().cloneItemStack(); + if (this.synchronizer != null) { + this.synchronizer.sendCarriedChange(this, this.remoteCarried); + } + } + // CraftBukkit end + public void b(ICrafting icrafting) { this.containerListeners.remove(icrafting); } @@ -349,6 +393,7 @@ l = this.getCarried().getCount(); Iterator iterator = this.quickcraftSlots.iterator(); + Map draggedSlots = new HashMap(); // CraftBukkit - Store slots from drag in map (raw slot id -> new stack) while (iterator.hasNext()) { Slot slot1 = (Slot) iterator.next(); ItemStack itemstack2 = this.getCarried(); @@ -365,12 +410,48 @@ } l -= itemstack3.getCount() - j1; - slot1.set(itemstack3); + // slot1.set(itemstack3); + draggedSlots.put(slot1.index, itemstack3); // CraftBukkit - Put in map instead of setting + } + } + + // CraftBukkit start - InventoryDragEvent + InventoryView view = getBukkitView(); + org.bukkit.inventory.ItemStack newcursor = CraftItemStack.asCraftMirror(itemstack1); + newcursor.setAmount(l); + Map eventmap = new HashMap(); + for (Map.Entry ditem : draggedSlots.entrySet()) { + eventmap.put(ditem.getKey(), CraftItemStack.asBukkitCopy(ditem.getValue())); + } + + // It's essential that we set the cursor to the new value here to prevent item duplication if a plugin closes the inventory. + ItemStack oldCursor = this.getCarried(); + this.setCarried(CraftItemStack.asNMSCopy(newcursor)); + + InventoryDragEvent event = new InventoryDragEvent(view, (newcursor.getType() != org.bukkit.Material.AIR ? newcursor : null), CraftItemStack.asBukkitCopy(oldCursor), this.quickcraftType == 1, eventmap); + entityhuman.level.getServer().getPluginManager().callEvent(event); + + // Whether or not a change was made to the inventory that requires an update. + boolean needsUpdate = event.getResult() != Result.DEFAULT; + + if (event.getResult() != Result.DENY) { + for (Map.Entry dslot : draggedSlots.entrySet()) { + view.setItem(dslot.getKey(), CraftItemStack.asBukkitCopy(dslot.getValue())); + } + // The only time the carried item will be set to null is if the inventory is closed by the server. + // If the inventory is closed by the server, then the cursor items are dropped. This is why we change the cursor early. + if (this.getCarried() != null) { + this.setCarried(CraftItemStack.asNMSCopy(event.getCursor())); + needsUpdate = true; } + } else { + this.setCarried(oldCursor); } - itemstack1.setCount(l); - this.setCarried(itemstack1); + if (needsUpdate && entityhuman instanceof EntityPlayer) { + this.updateInventory(); + } + // CraftBukkit end } this.e(); @@ -388,8 +469,11 @@ if (i == -999) { if (!this.getCarried().isEmpty()) { if (clickaction == ClickAction.PRIMARY) { - entityhuman.drop(this.getCarried(), true); + // CraftBukkit start + ItemStack carried = this.getCarried(); this.setCarried(ItemStack.EMPTY); + entityhuman.drop(carried, true); + // CraftBukkit start } else { entityhuman.drop(this.getCarried().cloneAndSubtract(1), true); } @@ -452,6 +536,15 @@ } slot.d(); + // CraftBukkit start - Make sure the client has the right slot contents + if (entityhuman instanceof EntityPlayer && slot.getMaxStackSize() != 64) { + ((EntityPlayer) entityhuman).connection.sendPacket(new PacketPlayOutSetSlot(this.containerId, slot.index, slot.getItem())); + // Updating a crafting inventory makes the client reset the result slot, have to send it again + if (this.getBukkitView().getType() == InventoryType.WORKBENCH || this.getBukkitView().getType() == InventoryType.CRAFTING) { + ((EntityPlayer) entityhuman).connection.sendPacket(new PacketPlayOutSetSlot(this.containerId, 0, this.getSlot(0).getItem())); + } + } + // CraftBukkit end } } else { Slot slot2; @@ -556,8 +649,11 @@ public void b(EntityHuman entityhuman) { if (!this.getCarried().isEmpty()) { - entityhuman.drop(this.getCarried(), false); + // CraftBukkit start - SPIGOT-4556 + ItemStack carried = this.getCarried(); this.setCarried(ItemStack.EMPTY); + entityhuman.drop(carried, false); + // CraftBukkit end } } @@ -767,6 +863,11 @@ } public ItemStack getCarried() { + // CraftBukkit start + if (this.carried.isEmpty()) { + this.setCarried(ItemStack.EMPTY); + } + // CraftBukkit end return this.carried; }