# CommandContext

`com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper`

**Since:** 1.4.2

**Cancellable:** No. Calling `event.cancel()` throws `UnsupportedOperationException`.

The parsed context for a client command, containing its arguments and command source. A script
 receives one in a
 [command callback](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/classes/inventory/CommandBuilder.md#com.jsmacrosce.jsmacros.client.api.classes.inventory.CommandBuilder_executes-MethodWrapper-)
 or a
 [suggestion callback](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/classes/inventory/CommandBuilder.md#com.jsmacrosce.jsmacros.client.api.classes.inventory.CommandBuilder_suggest-MethodWrapper-).  
 [getArg(String)](#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getArg-String-) reads a named argument. Command execution dispatches this [BaseEvent](../../../../../../../classes/com/jsmacrosce/jsmacros/core/event/BaseEvent.md)
 before invoking the command callback, so
 [JsMacros.on("CommandContext", ...)](../../../../../../../libraries/com/jsmacrosce/jsmacros/core/library/impl/FJsMacros.md#com.jsmacrosce.jsmacros.core.library.impl.FJsMacros_on-String-MethodWrapper-)
 can observe it. Suggestion callbacks receive a context helper without dispatching this event.  
 ```js
const builder = Chat.getCommandManager().createCommandBuilder("hello");  
builder.greedyStringArg("name");  
builder.executes(JavaWrapper.methodToJava(function (ctx) {
  // getArg hands the argument back already wrapped for its type,  
  // a greedy string comes back as a plain string
  Chat.log(`hello ${ctx.getArg("name")}`);  
  return true;  
}));  
builder.register();  
 ```

## Constructors

### new CommandContextHelper {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_constructor-CommandContext-}

```js
new CommandContextHelper(CommandContext<?> base)
```

Stores the Brigadier context and initializes the event base with the client core; this does
 not dispatch the event or execute the command. The reference is retained without copying or
 validation. A `null` context is accepted, but methods that dereference it will fail.

**Parameters:**
- `base`: the raw Brigadier context to retain; may be `null`

## Methods

### equals {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_equals-Object-}

```js
equals(Object obj): boolean
```

Delegates equality to the underlying context, unwrapping another `CommandContextHelper`
 to its context first; any other object is passed directly to the underlying context's
 `equals`. A `null` underlying context causes `NullPointerException`.

**Parameters:**
- `obj`: another helper or an object to compare through the underlying context

**Returns:** the result of the underlying context's equality comparison

### getArg {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getArg-String-}

```js
getArg(String name): Object
```

Returns a named argument, converting recognized Brigadier argument values to JsMacros
 helpers or script-friendly values. The conversions are:  
 a block argument becomes a
 [BlockStateHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/world/BlockStateHelper.md), an identifier
 becomes its `namespace:path` string, an item becomes an
 [ItemStackHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/inventory/ItemStackHelper.md) of one item, an
 NBT argument becomes the matching
 [NBTElementHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/NBTElementHelper.md), text becomes a
 [TextHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/TextHelper.md), a color becomes a
 [FormattingHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/FormattingHelper.md), an angle becomes a plain
 number, an enchantment becomes an
 [EnchantmentHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/inventory/EnchantmentHelper.md), a particle
 and a status effect each become their identifier string, a position becomes a
 [BlockPosHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/world/BlockPosHelper.md), and an entity
 selector becomes a plain list of
 [EntityHelper](../../../../../../../classes/com/jsmacrosce/jsmacros/client/api/helper/world/entity/EntityHelper.md). An item
 predicate and a block predicate become a Java predicate, which takes the thing being tested
 and answers whether it is accepted, with an item stack for the first and a block position for
 the second. Anything not on that list comes back as the raw brigadier value.  

 Item arguments produce a stack of one. Entity selectors use `findEntities`, so the
 result may be empty or contain multiple entities; unlike `findSingleEntity`, this does
 not fail for zero or multiple matches. Selector permission checks can still throw
 CommandSyntaxException. Angles, coordinates, and selectors use a fake command source
 when the context source is a client suggestion provider. In that provider context, every
 successful name lookup constructs the fake source before conversion. A missing current
 client player therefore causes [NullPointerException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/NullPointerException.html) regardless of argument type.  
 The fake source has no server or server level: selectors that require either can also
 fail with [NullPointerException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/NullPointerException.html), rather than returning an empty list. A non-client
 provider context supplies no fake source, so conversions that dereference it can fail too.  
 Selector permission failures throw CommandSyntaxException; on Minecraft 26.1.2,  
 item-stack conversion can also throw it for an oversized or malformed stack. Other
 unconverted argument types are returned as their raw Brigadier values.  
 A name absent from this context throws [IllegalArgumentException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/IllegalArgumentException.html) before conversion.

**Parameters:**
- `name`: the name the argument was declared with on the command

**Returns:** the argument's value, wrapped as described above

**Since:** 1.4.2

### getChild {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getChild-}

```js
getChild(): CommandContextHelper
```

Returns a wrapper for the child context. If none exists, the wrapper is still non-null but
 [getRaw()](#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getRaw-) returns `null`; other methods on that wrapper will fail.

**Returns:** a wrapper around the context one level down, whose own [getRaw()](#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getRaw-) is
         `null` when there is no child context.

### getInput {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getInput-}

```js
getInput(): String
```

Returns the full input string stored in the underlying Brigadier context, including the
 command and arguments rather than only the portion covered by this context's range.

**Returns:** the full input string stored in this context.

### getRange {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getRange-}

```js
getRange(): StringRange
```

Returns the Brigadier range covered by this context. Its start and end offsets are relative
 to the input returned by [getInput()](#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getInput-).

**Returns:** the range of this context within the command line.

### getRaw {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_getRaw-}

```js
getRaw(): CommandContext<?>
```

Returns the underlying Brigadier context, exposing its raw values rather than JsMacros
 wrappers.

**Returns:** the brigadier context this wraps.

### hashCode {#com.jsmacrosce.jsmacros.client.api.helper.CommandContextHelper_hashCode-}

```js
hashCode(): int
```

Returns the hash code delegated to the underlying Brigadier context.  
 A `null` underlying context causes `NullPointerException`.

**Returns:** the underlying context's hash code
