Skip to content

Commit

Permalink
Improve events for new inventory features. Adds BUKKIT-3859
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
riking authored and natemort committed Jun 3, 2013
1 parent faea684 commit cd0205e
Show file tree
Hide file tree
Showing 8 changed files with 648 additions and 60 deletions.
115 changes: 115 additions & 0 deletions src/main/java/org/bukkit/event/inventory/ClickType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.bukkit.event.inventory;

/**
* What the client did to trigger this action (not the result).
*/
public enum ClickType {
/**
* The left (or primary) mouse button.
*/
LEFT,
/**
* Holding shift while pressing the left mouse button.
*/
SHIFT_LEFT,
/**
* The right mouse button.
*/
RIGHT,
/**
* Holding shift while pressing the right mouse button.
*/
SHIFT_RIGHT,
/**
* Clicking the left mouse button on the grey area around the
* inventory.
*/
WINDOW_BORDER_LEFT,
/**
* Clicking the right mouse button on the grey area around the
* inventory.
*/
WINDOW_BORDER_RIGHT,
/**
* The middle mouse button, or a "scrollwheel click".
*/
MIDDLE,
/**
* One of the number keys 1-9, correspond to slots on the hotbar.
*/
NUMBER_KEY,
/**
* Pressing the left mouse button twice in quick succession.
*/
DOUBLE_CLICK,
/**
* The "Drop" key (defaults to Q).
*/
DROP,
/**
* Holding Ctrl while pressing the "Drop" key (defaults to Q).
*/
CONTROL_DROP,
/**
* Any action done with the Creative inventory open.
*/
CREATIVE,
/**
* A type of inventory manipulation not yet recognized by Bukkit.
* This is only for transitional purposes on a new Minecraft update,
* and should never be relied upon.
* <p>
* Any ClickType.UNKNOWN is called on a best-effort basis.
*/
UNKNOWN,
;

/**
* Gets whether this ClickType represents the pressing of a key on a
* keyboard.
*
* @return true if this ClickType represents the pressing of a key
*/
public boolean isKeyboardClick() {
return (this == ClickType.NUMBER_KEY) || (this == ClickType.DROP) || (this == ClickType.CONTROL_DROP);
}

/**
* Gets whether this ClickType represents an action that can only be
* performed by a Player in creative mode.
*
* @return true if this action requires Creative mode
*/
public boolean isCreativeAction() {
// Why use middle click?
return (this == ClickType.MIDDLE) || (this == ClickType.CREATIVE);
}

/**
* Gets whether this ClickType represents a right click.
*
* @return true if this ClickType represents a right click
*/
public boolean isRightClick() {
return (this == ClickType.RIGHT) || (this == ClickType.SHIFT_RIGHT);
}

/**
* Gets whether this ClickType represents a left click.
*
* @return true if this ClickType represents a left click
*/
public boolean isLeftClick() {
return (this == ClickType.LEFT) || (this == ClickType.SHIFT_LEFT) || (this == ClickType.DOUBLE_CLICK) || (this == ClickType.CREATIVE);
}

/**
* Gets whether this ClickType indicates that the shift key was pressed
* down when the click was made.
*
* @return true if the action uses Shift.
*/
public boolean isShiftClick() {
return (this == ClickType.SHIFT_LEFT) || (this == ClickType.SHIFT_RIGHT) || (this == ClickType.CONTROL_DROP);
}
}
12 changes: 11 additions & 1 deletion src/main/java/org/bukkit/event/inventory/CraftItemEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@
public class CraftItemEvent extends InventoryClickEvent {
private Recipe recipe;

@Deprecated
public CraftItemEvent(Recipe recipe, InventoryView what, SlotType type, int slot, boolean right, boolean shift) {
super(what, type, slot, right, shift);
this(recipe, what, type, slot, right ? (shift ? ClickType.SHIFT_RIGHT : ClickType.RIGHT) : (shift ? ClickType.SHIFT_LEFT : ClickType.LEFT), InventoryAction.PICKUP_ALL);
}

public CraftItemEvent(Recipe recipe, InventoryView what, SlotType type, int slot, ClickType click, InventoryAction action) {
super(what, type, slot, click, action);
this.recipe = recipe;
}

public CraftItemEvent(Recipe recipe, InventoryView what, SlotType type, int slot, ClickType click, InventoryAction action, int key) {
super(what, type, slot, click, action, key);
this.recipe = recipe;
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/bukkit/event/inventory/DragType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.bukkit.event.inventory;

/**
* Represents the effect of a drag that will be applied to an Inventory in an
* InventoryDragEvent.
*/
public enum DragType {
/**
* One item from the cursor is placed in each selected slot.
*/
SINGLE,
/**
* The cursor is split evenly across all selected slots, not to
* exceed the Material's max stack size, with the remainder going to
* the cursor.
*/
EVEN,
}
90 changes: 90 additions & 0 deletions src/main/java/org/bukkit/event/inventory/InventoryAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.bukkit.event.inventory;

/**
* An estimation of what the result will be.
*/
public enum InventoryAction {
/**
* Nothing will happen from the click.
* There may be cases where nothing will happen and this is value is
* not provided, but it is guaranteed that this value is accurate
* when given.
*/
NOTHING,
/**
* All of the items on the clicked slot are moved to the cursor.
*/
PICKUP_ALL,
/**
* Some of the items on the clicked slot are moved to the cursor.
*/
PICKUP_SOME,
/**
* Half of the items on the clicked slot are moved to the cursor.
*/
PICKUP_HALF,
/**
* One of the items on the clicked slot are moved to the cursor.
*/
PICKUP_ONE,
/**
* All of the items on the cursor are moved to the clicked slot.
*/
PLACE_ALL,
/**
* Some of the items from the cursor are moved to the clicked slot
* (usually up to the max stack size).
*/
PLACE_SOME,
/**
* A single item from the cursor is moved to the clicked slot.
*/
PLACE_ONE,
/**
* The clicked item and the cursor are exchanged.
*/
SWAP_WITH_CURSOR,
/**
* The entire cursor item is dropped.
*/
DROP_ALL_CURSOR,
/**
* One item is dropped from the cursor.
*/
DROP_ONE_CURSOR,
/**
* The entire clicked slot is dropped.
*/
DROP_ALL_SLOT,
/**
* One item is dropped from the clicked slot.
*/
DROP_ONE_SLOT,
/**
* The item is moved to the opposite inventory if a space is found.
*/
MOVE_TO_OTHER_INVENTORY,
/**
* The clicked item is moved to the hotbar, and the item currently
* there is re-added to the player's inventory.
*/
HOTBAR_MOVE_AND_READD,
/**
* The clicked slot and the picked hotbar slot are swapped.
*/
HOTBAR_SWAP,
/**
* A max-size stack of the clicked item is put on the cursor.
*/
CLONE_STACK,
/**
* The inventory is searched for the same material, and they are put
* on the cursor up to {@link org.bukkit.Material#getMaxStackSize()}.
*/
COLLECT_TO_CURSOR,
/**
* An unrecognized ClickType.
*/
UNKNOWN,
;
}
Loading

0 comments on commit cd0205e

Please sign in to comment.