Paper/nms-patches/Container.patch

152 lines
7.7 KiB
Diff
Raw Normal View History

2015-05-25 12:37:24 +02:00
--- a/net/minecraft/server/Container.java
+++ b/net/minecraft/server/Container.java
2019-04-23 04:00:00 +02:00
@@ -7,6 +7,18 @@
import java.util.Set;
2016-05-10 13:47:39 +02:00
import javax.annotation.Nullable;
+// CraftBukkit start
2019-04-23 04:00:00 +02:00
+import com.google.common.base.Preconditions;
+import java.util.HashMap;
+import java.util.Map;
+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 {
2017-09-18 12:00:00 +02:00
public NonNullList<ItemStack> items = NonNullList.a();
2019-04-23 04:00:00 +02:00
@@ -21,6 +33,27 @@
private final List<ICrafting> listeners = Lists.newArrayList();
private final Set<EntityHuman> k = Sets.newHashSet();
2015-02-26 23:41:06 +01:00
+ // 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);
+ }
2019-04-23 04:00:00 +02:00
+ private IChatBaseComponent title;
+ public final IChatBaseComponent getTitle() {
2019-04-23 04:00:00 +02:00
+ Preconditions.checkState(this.title != null, "Title not set");
+ return this.title;
+ }
+ public final void setTitle(IChatBaseComponent title) {
2019-04-23 04:00:00 +02:00
+ Preconditions.checkState(this.title == null, "Title already set");
+ this.title = title;
+ }
2015-02-26 23:41:06 +01:00
+ // CraftBukkit end
+
2019-04-23 04:00:00 +02:00
protected Container(@Nullable Containers<?> containers, int i) {
this.e = containers;
this.windowId = i;
2020-06-25 02:00:00 +02:00
@@ -202,6 +235,7 @@
2018-12-06 00:00:00 +01:00
k = playerinventory.getCarried().getCount();
2019-04-23 04:00:00 +02:00
Iterator iterator = this.i.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();
2016-11-17 02:41:03 +01:00
ItemStack itemstack3 = playerinventory.getCarried();
2020-06-25 02:00:00 +02:00
@@ -217,12 +251,48 @@
}
2018-12-06 00:00:00 +01:00
k -= itemstack4.getCount() - j1;
2016-11-17 02:41:03 +01:00
- slot1.set(itemstack4);
+ // slot1.set(itemstack4);
+ draggedSlots.put(slot1.rawSlotIndex, itemstack4); // CraftBukkit - Put in map instead of setting
2017-05-14 04:00:00 +02:00
}
}
2018-12-06 00:00:00 +01:00
- itemstack2.setCount(k);
2017-05-14 04:00:00 +02:00
- playerinventory.setCarried(itemstack2);
+ // CraftBukkit start - InventoryDragEvent
+ InventoryView view = getBukkitView();
2016-11-17 02:41:03 +01:00
+ org.bukkit.inventory.ItemStack newcursor = CraftItemStack.asCraftMirror(itemstack2);
2018-12-06 00:00:00 +01:00
+ newcursor.setAmount(k);
+ 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()));
+ }
2015-02-26 23:41:06 +01:00
+
+ // 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 = playerinventory.getCarried();
+ playerinventory.setCarried(CraftItemStack.asNMSCopy(newcursor));
+
+ InventoryDragEvent event = new InventoryDragEvent(view, (newcursor.getType() != org.bukkit.Material.AIR ? newcursor : null), CraftItemStack.asBukkitCopy(oldCursor), this.dragType == 1, eventmap);
+ entityhuman.world.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<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 (playerinventory.getCarried() != null) {
+ playerinventory.setCarried(CraftItemStack.asNMSCopy(event.getCursor()));
+ needsUpdate = true;
2017-05-14 04:00:00 +02:00
+ }
+ } else {
+ playerinventory.setCarried(oldCursor);
2017-05-14 04:00:00 +02:00
+ }
+
+ if (needsUpdate && entityhuman instanceof EntityPlayer) {
+ ((EntityPlayer) entityhuman).updateInventory(this);
+ }
+ // CraftBukkit end
}
2019-04-23 04:00:00 +02:00
this.d();
2020-06-25 02:00:00 +02:00
@@ -239,8 +309,11 @@
if (i == -999) {
if (!playerinventory.getCarried().isEmpty()) {
if (j == 0) {
- entityhuman.drop(playerinventory.getCarried(), true);
+ // CraftBukkit start
+ ItemStack carried = playerinventory.getCarried();
2020-06-25 02:00:00 +02:00
playerinventory.setCarried(ItemStack.b);
+ entityhuman.drop(carried, true);
+ // CraftBukkit start
}
if (j == 1) {
2020-06-25 02:00:00 +02:00
@@ -328,6 +401,15 @@
}
2019-04-23 04:00:00 +02:00
slot2.d();
+ // CraftBukkit start - Make sure the client has the right slot contents
+ if (entityhuman instanceof EntityPlayer && slot2.getMaxStackSize() != 64) {
+ ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutSetSlot(this.windowId, slot2.rawSlotIndex, slot2.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).playerConnection.sendPacket(new PacketPlayOutSetSlot(this.windowId, 0, this.getSlot(0).getItem()));
+ }
+ }
2015-02-26 23:41:06 +01:00
+ // CraftBukkit end
}
}
2020-06-25 02:00:00 +02:00
} else if (inventoryclicktype == InventoryClickType.SWAP) {
@@ -430,8 +512,11 @@
PlayerInventory playerinventory = entityhuman.inventory;
if (!playerinventory.getCarried().isEmpty()) {
- entityhuman.drop(playerinventory.getCarried(), false);
+ // CraftBukkit start - SPIGOT-4556
+ ItemStack carried = playerinventory.getCarried();
2020-06-25 02:00:00 +02:00
playerinventory.setCarried(ItemStack.b);
+ entityhuman.drop(carried, false);
+ // CraftBukkit end
}
}