Bukkit/CraftBukkit — pull request #1249 by ase34 (2013-09-28 08:34:18+00:00)
Title: [C] Fix moving items in hotbar when cancelling PlayerDropItemEvent - Fixes BUKKIT-3313

**The issue:**
As described in the [leaky ticket](https://bukkit.atlassian.net/browse/BUKKIT-3313), cancelling the [PlayerDropItemEvent][item-drop-event] when a player drops an item by pressing the keys `Q` or `CTRL-Q` causes the dropped item to be moved to the first available slot. This PR replaces the current method of simply giving back the dropped items to the player (Inventory.addItem(ItemStack…)) (which will then be added in the first slots they fit) in case of dropping by `Q` or `CTRL-Q` to adding the dropped items into the currently selected slot.

**Justification for this PR:**
With this fix, plugin programmers do not need to worry about moving the item in the correct slot anymore since that will be then made automatically.

**PR Breakdown:**
The actions which call the event keep the same, including dropping by `Q` or `CTRL-Q` and moving items on the cursor out of the inventory window. However, the behavior changes when the event was canceled, but it is *always* guaranteed that no items will be dropped, and that no items will be removed from the inventory when the event was canceled:

* If (and *only* if) they are dropped from the hotbar, it is guaranteed that they will stay at their original positions.
* However, items that are not dropped by pressing `Q` or `CTRL-Q`, for example moving a stack of items out of the inventory window, will be added the same way as previous: simply filling the inventory slots (`Inventory.addItem(ItemStack...)`).

**Testing Results and Materials:**
This PR was tested by cancelling [PlayerDropItemEvent][item-drop-event] and [InventoryClickEvent][inventory-click-event]:

```java
@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent ev) {
ev.setcanceled(true);
}

@EventHandler
public void onInventoryClick(InventoryClickEvent ev) {
ev.setcanceled(ev.getSlotType() == SlotType.OUTSIDE);
}
```

The tests, done with the same [inventory](http://s7.directupload.net/images/130917/32p84ngl.png) in survival gamemode:

* **Pressing `Q` while selecting slot *A*:** Nothing changes, all items stay at their positions.
* **Pressing `CTRL-Q` while selecting slot *A*:** As above, no changes.
* **In the inventory window, select *B* and click outside of the inventory window:** The item keeps at the cursor.
* **In the inventory window, select *A* and click outside of the inventory window:** As above, the item keeps at the cursor.
* **In the inventory window, select *A* so the stack is on the cursor and then close the window:** The stack moves to the left slot of the hotbar.

**Relevant PR(s):**
CB-984 - - Basically does the same thing, but it's not updated to the latest code base and has a bigger diff

**JIRA Ticket:**
BUKKIT-3313 -

[inventory-click-event]: http://jd.bukkit.org/rb/apidocs/org/bukkit/event/inventory/InventoryClickEvent.html
[item-drop-event]: http://jd.bukkit.org/rb/apidocs/org/bukkit/event/player/PlayerDropItemEvent.html