Paper/paper-server/nms-patches/net/minecraft/world/inventory/Container.patch
CraftBukkit/Spigot 47c7562765 Restore 1.16.5 behaviour of InventoryDragEvent being called even when a single item is 'dragged' to its own slot
Ideally this would now be an InventoryClickEvent instead, but that is not so easy with the current structure.
See https://www.spigotmc.org/threads/510208/page-9#post-4185501 for further info.

By: md_5 <git@md-5.net>
2021-06-12 20:19:41 +10:00

200 lines
9.3 KiB
Diff

--- 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<Slot> slots = NonNullList.a();
private final List<ContainerProperty> dataSlots = Lists.newArrayList();
private ItemStack carried;
- private final NonNullList<ItemStack> remoteSlots;
+ public NonNullList<ItemStack> 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);
}
@@ -338,7 +382,7 @@
}
} else if (this.quickcraftStatus == 2) {
if (!this.quickcraftSlots.isEmpty()) {
- if (this.quickcraftSlots.size() == 1) {
+ if (false && this.quickcraftSlots.size() == 1) { // CraftBukkit - treat everything as a drag since we are unable to easily call InventoryClickEvent instead
k = ((Slot) this.quickcraftSlots.iterator().next()).index;
this.e();
this.b(k, this.quickcraftType, InventoryClickType.PICKUP, entityhuman);
@@ -349,6 +393,7 @@
l = this.getCarried().getCount();
Iterator iterator = this.quickcraftSlots.iterator();
+ Map<Integer, ItemStack> draggedSlots = new HashMap<Integer, ItemStack>(); // 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<Integer, org.bukkit.inventory.ItemStack> eventmap = new HashMap<Integer, org.bukkit.inventory.ItemStack>();
+ for (Map.Entry<Integer, ItemStack> 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.getCraftServer().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<Integer, ItemStack> 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;
}