Improve events for new inventory features. Adds BUKKIT-3859

This commit brings the InventoryClickEvent up to date with the new Minecraft
changes in 1.5.

InventoryDragEvent (thanks to @YLivay for his PR) is added to represent the
new "dragging" or "painting" functionality, where if you hold an itemstack and
click-drag over several slots, the items will be split evenly (left click) or
1 each (right click).

The ClickType enum is used to represent what the client did to trigger the
event.

The InventoryAction enum is reserved for future expansion, but will be used to
indicate the approximate result of the action.

Additionally, handling of creative inventory editing is improved with the new
InventoryCreativeEvent, and handling of numberkey presses is also improved
within InventoryClickEvent and CraftItemEvent.

Also, cancelling a creative click now displays properly on the client.

Adresses BUKKIT-3692, BUKKIT-4035, BUKKIT-3859 (new 1.5 events),
BUKKIT-2659, BUKKIT-3043, BUKKIT-2659, and BUKKIT-2897 (creative click events).
This commit is contained in:
riking
2013-04-13 18:16:25 -07:00
committed by Nate Mortensen
parent 56dbde3c5b
commit 991218a339
2 changed files with 323 additions and 69 deletions

View File

@@ -7,7 +7,12 @@ import java.util.List;
import java.util.Set;
// CraftBukkit start
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.inventory.InventoryView;
// CraftBukkit end
@@ -18,7 +23,7 @@ public abstract class Container {
public int windowId = 0;
private short a = 0;
private int f = -1;
private int g = 0;
public int g = 0; // CraftBukkit - private -> public
private final Set h = new HashSet();
protected List listeners = new ArrayList();
private Set i = new HashSet();
@@ -140,6 +145,7 @@ public abstract class Container {
l = playerinventory.getCarried().count;
Iterator iterator = this.h.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();
@@ -157,16 +163,46 @@ public abstract class Container {
}
l -= itemstack2.count - j1;
slot1.set(itemstack2);
draggedSlots.put(slot1.g, itemstack2); // CraftBukkit - Put in map instead of setting, Should be Slot.rawSlotIndex
}
}
itemstack1.count = l;
if (itemstack1.count <= 0) {
itemstack1 = null;
// 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()));
}
playerinventory.setCarried(itemstack1);
// 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.f == 1, eventmap); // Should be dragButton
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;
}
}
if (needsUpdate && entityhuman instanceof EntityPlayer) {
((EntityPlayer) entityhuman).updateInventory(this);
}
// CraftBukkit end
}
this.d();