2021-03-15 23:00:00 +01:00
--- a/net/minecraft/world/inventory/Container.java
+++ b/net/minecraft/world/inventory/Container.java
2022-12-07 17:00:00 +01:00
@@ -33,6 +33,20 @@
2021-03-15 23:00:00 +01:00
import net.minecraft.world.level.block.entity.TileEntity;
2022-02-28 16:00:00 +01:00
import org.slf4j.Logger;
2014-11-25 22:32:16 +01:00
+// CraftBukkit start
2019-04-23 04:00:00 +02:00
+import com.google.common.base.Preconditions;
2014-11-25 22:32:16 +01:00
+import java.util.HashMap;
+import java.util.Map;
2021-03-15 23:00:00 +01:00
+import net.minecraft.network.chat.IChatBaseComponent;
+import net.minecraft.network.protocol.game.PacketPlayOutSetSlot;
2014-11-25 22:32:16 +01:00
+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 {
2022-07-27 20:00:00 +02:00
private static final Logger LOGGER = LogUtils.getLogger();
2022-12-07 17:00:00 +01:00
@@ -63,6 +77,27 @@
2021-06-11 07:00:00 +02:00
private ContainerSynchronizer synchronizer;
private boolean suppressRemoteUpdates;
2015-02-26 23:41:06 +01:00
2014-11-25 22:32:16 +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;
2019-04-24 02:23:12 +02:00
+ public final IChatBaseComponent getTitle() {
2019-04-23 04:00:00 +02:00
+ Preconditions.checkState(this.title != null, "Title not set");
+ return this.title;
+ }
2019-04-24 02:23:12 +02:00
+ 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) {
2021-06-11 07:00:00 +02:00
this.carried = ItemStack.EMPTY;
2021-11-21 23:00:00 +01:00
this.remoteSlots = NonNullList.create();
2022-12-07 17:00:00 +01:00
@@ -164,6 +199,15 @@
2021-06-11 07:00:00 +02:00
}
+ // CraftBukkit start
+ public void broadcastCarriedItem() {
2021-11-21 23:00:00 +01:00
+ this.remoteCarried = this.getCarried().copy();
2021-06-11 07:00:00 +02:00
+ if (this.synchronizer != null) {
+ this.synchronizer.sendCarriedChange(this, this.remoteCarried);
+ }
+ }
+ // CraftBukkit end
+
2021-11-21 23:00:00 +01:00
public void removeSlotListener(ICrafting icrafting) {
2021-06-11 07:00:00 +02:00
this.containerListeners.remove(icrafting);
}
2022-12-07 17:00:00 +01:00
@@ -380,7 +424,7 @@
2021-06-12 12:19:41 +02:00
}
} 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;
2021-11-21 23:00:00 +01:00
this.resetQuickCraft();
this.doClick(k, this.quickcraftType, InventoryClickType.PICKUP, entityhuman);
2022-12-07 17:00:00 +01:00
@@ -391,6 +435,7 @@
2021-06-11 07:00:00 +02:00
l = this.getCarried().getCount();
Iterator iterator = this.quickcraftSlots.iterator();
2014-11-25 22:32:16 +01:00
+ 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();
2021-06-11 07:00:00 +02:00
ItemStack itemstack2 = this.getCarried();
2022-12-07 17:00:00 +01:00
@@ -407,12 +452,48 @@
2014-11-25 22:32:16 +01:00
}
2021-06-11 07:00:00 +02:00
l -= itemstack3.getCount() - j1;
2023-03-14 17:30:00 +01:00
- slot1.setByPlayer(itemstack3);
+ // slot1.setByPlayer(itemstack3);
2021-06-11 07:00:00 +02:00
+ draggedSlots.put(slot1.index, itemstack3); // CraftBukkit - Put in map instead of setting
2021-07-06 16:00:00 +02:00
}
}
- itemstack1.setCount(l);
- this.setCarried(itemstack1);
2014-11-25 22:32:16 +01:00
+ // CraftBukkit start - InventoryDragEvent
+ InventoryView view = getBukkitView();
2021-06-11 07:00:00 +02:00
+ org.bukkit.inventory.ItemStack newcursor = CraftItemStack.asCraftMirror(itemstack1);
+ newcursor.setAmount(l);
2014-11-25 22:32:16 +01:00
+ 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
+
2014-11-25 22:32:16 +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.
2021-06-11 07:00:00 +02:00
+ ItemStack oldCursor = this.getCarried();
+ this.setCarried(CraftItemStack.asNMSCopy(newcursor));
2014-11-25 22:32:16 +01:00
+
2021-06-11 07:00:00 +02:00
+ InventoryDragEvent event = new InventoryDragEvent(view, (newcursor.getType() != org.bukkit.Material.AIR ? newcursor : null), CraftItemStack.asBukkitCopy(oldCursor), this.quickcraftType == 1, eventmap);
2021-06-11 13:33:49 +02:00
+ entityhuman.level.getCraftServer().getPluginManager().callEvent(event);
2014-11-25 22:32:16 +01:00
+
+ // 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.
2021-06-11 07:00:00 +02:00
+ if (this.getCarried() != null) {
+ this.setCarried(CraftItemStack.asNMSCopy(event.getCursor()));
2014-11-25 22:32:16 +01:00
+ needsUpdate = true;
2021-07-06 16:00:00 +02:00
+ }
2014-11-25 22:32:16 +01:00
+ } else {
2021-06-11 07:00:00 +02:00
+ this.setCarried(oldCursor);
2021-07-06 16:00:00 +02:00
+ }
+
2014-11-25 22:32:16 +01:00
+ if (needsUpdate && entityhuman instanceof EntityPlayer) {
2021-11-21 23:00:00 +01:00
+ this.sendAllDataToRemote();
2014-11-25 22:32:16 +01:00
+ }
+ // CraftBukkit end
}
2021-11-21 23:00:00 +01:00
this.resetQuickCraft();
2022-12-07 17:00:00 +01:00
@@ -430,8 +511,11 @@
2016-11-20 00:19:22 +01:00
if (i == -999) {
2021-06-11 07:00:00 +02:00
if (!this.getCarried().isEmpty()) {
if (clickaction == ClickAction.PRIMARY) {
- entityhuman.drop(this.getCarried(), true);
2016-11-20 00:19:22 +01:00
+ // CraftBukkit start
2021-06-11 07:00:00 +02:00
+ ItemStack carried = this.getCarried();
this.setCarried(ItemStack.EMPTY);
2016-11-20 00:19:22 +01:00
+ entityhuman.drop(carried, true);
+ // CraftBukkit start
2021-06-11 07:00:00 +02:00
} else {
2021-11-21 23:00:00 +01:00
entityhuman.drop(this.getCarried().split(1), true);
2016-11-20 00:19:22 +01:00
}
2022-12-07 17:00:00 +01:00
@@ -494,6 +578,15 @@
2021-06-11 07:00:00 +02:00
}
2016-11-20 00:19:22 +01:00
2021-11-21 23:00:00 +01:00
slot.setChanged();
2021-06-11 07:00:00 +02:00
+ // CraftBukkit start - Make sure the client has the right slot contents
+ if (entityhuman instanceof EntityPlayer && slot.getMaxStackSize() != 64) {
2021-11-21 23:00:00 +01:00
+ ((EntityPlayer) entityhuman).connection.send(new PacketPlayOutSetSlot(this.containerId, this.incrementStateId(), slot.index, slot.getItem()));
2021-06-11 07:00:00 +02:00
+ // 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) {
2021-11-21 23:00:00 +01:00
+ ((EntityPlayer) entityhuman).connection.send(new PacketPlayOutSetSlot(this.containerId, this.incrementStateId(), 0, this.getSlot(0).getItem()));
2014-11-25 22:32:16 +01:00
+ }
2021-06-11 07:00:00 +02:00
+ }
+ // CraftBukkit end
2014-11-25 22:32:16 +01:00
}
2021-06-11 07:00:00 +02:00
} else {
Slot slot2;
2022-12-07 17:00:00 +01:00
@@ -607,13 +700,14 @@
2021-07-06 16:00:00 +02:00
ItemStack itemstack = this.getCarried();
if (!itemstack.isEmpty()) {
+ this.setCarried(ItemStack.EMPTY); // CraftBukkit - SPIGOT-4556 - from below
2021-11-21 23:00:00 +01:00
if (entityhuman.isAlive() && !((EntityPlayer) entityhuman).hasDisconnected()) {
entityhuman.getInventory().placeItemBackInInventory(itemstack);
2021-07-06 16:00:00 +02:00
} else {
entityhuman.drop(itemstack, false);
}
2018-12-31 00:13:02 +01:00
2021-07-06 16:00:00 +02:00
- this.setCarried(ItemStack.EMPTY);
+ // this.setCarried(ItemStack.EMPTY); // CraftBukkit - moved up
}
2018-12-31 00:13:02 +01:00
}
2022-12-07 17:00:00 +01:00
@@ -827,6 +921,11 @@
2021-06-11 07:00:00 +02:00
}
public ItemStack getCarried() {
+ // CraftBukkit start
+ if (this.carried.isEmpty()) {
+ this.setCarried(ItemStack.EMPTY);
+ }
+ // CraftBukkit end
return this.carried;
}