# ClickSlot

`com.jsmacrosce.jsmacros.client.api.event.impl.inventory.EventClickSlot`

**Since:** 1.6.4

**Cancellable:** Yes. Call `event.cancel()` in a handler before releasing the event lock.

Fires when the local player clicks a slot in an open container screen, which includes the
player's own inventory and the creative inventory. It is raised for every click the screen
handles, whether the click ends up moving items or not, and the fields describe the click
rather than its result.  

Cancelling stops the click. The event is raised before the click is handed to the game mode,  
and cancelling makes the mixin abort before that call happens, so the click packet is never
sent and no slot changes. This is also why
[EventDropSlot](EventDropSlot.md) is not raised for a cancelled click: the drop event is only reached once
the click itself has been allowed through.  


For drops on their own, see [EventDropSlot](EventDropSlot.md). For a single slot changing because the
server sent an update, see [EventSlotUpdate](EventSlotUpdate.md).  
```js
JsMacros.on("ClickSlot", true, JavaWrapper.methodToJava(function (event) {
  if (event.mode === 1) {
    // mode 1 is the shift-click quick move, refuse it entirely
    event.cancel();  
    return;  
  }
  if (event.slot === -999) {
    // -999 is the out-of-window slot, nothing lives there to protect
    return;  
  }
  const stack = event.getInventory().getSlot(event.slot);  
  if (stack.getItemID() === "minecraft:diamond_sword") {
    Chat.log("diamond swords are not allowed to move out of this chest");  
    event.cancel();  
  }
}))
```

## Constructors

### new EventClickSlot {#com.jsmacrosce.jsmacros.client.api.event.impl.inventory.EventClickSlot_constructor-AbstractContainerScreen-int-int-int-}

```js
new EventClickSlot(AbstractContainerScreen<?> screen, int mode, int button, int slot)
```

Stores the screen reference and click values unchanged, and initializes the event with the client core.  
A `null` screen is accepted and is not dereferenced here.

**Parameters:**
- `screen`: source container screen; may be `null`
- `mode`: numeric click-action ID; no click-type range is enforced
- `button`: action button or key number
- `slot`: source inventory-menu index; the out-of-window convention is not validated

## Fields

### button {#com.jsmacrosce.jsmacros.client.api.event.impl.inventory.EventClickSlot_button}

```js
button: int
```

Which button or key produced the click, as a number.  

For a pick up or a throw this is the mouse button: `0` for the left button, or for
dropping a single item, and `1` for the right button, or for dropping the whole
stack. For a swap it is the hotbar slot that was pressed, `0` through `8`, or
`40` for the offhand key. The creative inventory also uses `9` and `10`
for its two extra buttons.

### mode {#com.jsmacrosce.jsmacros.client.api.event.impl.inventory.EventClickSlot_mode}

```js
mode: int
```

Numeric id of the game's click action.  

`0` is a plain pick up or place, `1` is the shift-click quick move, `2` is
a hotbar swap, `3` is the creative middle-click clone, `4` is a throw or drop,  
`5` is a right-click quick craft, and `6` is a double-click pick up all, which
sweeps everything of that item type into the cursor.  

These ids correspond to the `ClickType` action ids on Minecraft 1.21.8.

### slot {#com.jsmacrosce.jsmacros.client.api.event.impl.inventory.EventClickSlot_slot}

```js
slot: int
```

the index of the clicked slot in the open screen's menu, the same numbering
[getSlot()](../../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/classes/inventory/Inventory.md#com.jsmacrosce.jsmacros.client.api.classes.inventory.Inventory_getSlot-int-) uses.  

This is `-999`, the game's out-of-window slot, when the click landed outside the
container window. That case is also reported as a drop by
[EventDropSlot](EventDropSlot.md).

## Methods

### getInventory {#com.jsmacrosce.jsmacros.client.api.event.impl.inventory.EventClickSlot_getInventory-}

```js
getInventory(): Inventory<?>
```

Returns a fresh [Inventory](../../../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/classes/inventory/Inventory.md) wrapper for the menu of the screen where the click
occurred. The wrapper refers to that same menu; `-999` is not a menu slot.

**Returns:** the inventory of the screen this click happened in

### toString {#com.jsmacrosce.jsmacros.client.api.event.impl.inventory.EventClickSlot_toString-}

```js
toString(): String
```

Formats a diagnostic string using the event name, stored slot, and screen name.  
The result has the form `%s:{"slot": %d, "screen": "%s"}`; a null screen name appears as the quoted text `"null"`.

**Returns:** formatted diagnostic string; this is not JSON serialization
