public final class CommandContext extends Object
Modifier and Type | Class and Description |
---|---|
class |
CommandContext.Snapshot
A snapshot of a
CommandContext . |
Modifier and Type | Field and Description |
---|---|
static String |
TAB_COMPLETION
The argument key to indicate that a tab completion is taking place.
|
static String |
TARGET_BLOCK_ARG
The argument key for a target block position that may be present
during tab completion, of type
Location<World> . |
Constructor and Description |
---|
CommandContext()
Create a new empty CommandContext.
|
Modifier and Type | Method and Description |
---|---|
void |
addFlag(String key)
Defines the flag as being present when parsing this context.
|
void |
addFlag(Text key)
Defines the flag as being present when parsing this context.
|
void |
applySnapshot(CommandContext.Snapshot snapshot)
Resets a
CommandContext to a previous state using a previously
created CommandContext.Snapshot . |
void |
checkPermission(CommandSource commander,
String permission)
Perform a permissions check, throwing an exception if the required
permissions are not present.
|
CommandContext.Snapshot |
createSnapshot()
Gets a snapshot of the data inside this context to allow it to be
restored later.
|
<T> Collection<T> |
getAll(String key)
Gets all values for the given argument.
|
<T> Collection<T> |
getAll(Text key)
Gets all values for the given argument.
|
<T> Optional<T> |
getOne(String key)
Gets the value for the given key if the key has only one value.
|
<T> Optional<T> |
getOne(Text key)
Gets the value for the given key if the key has only one value.
|
boolean |
hasAny(String key)
Returns whether this context has any value for the given argument key.
|
boolean |
hasAny(Text key)
Returns whether this context has any value for the given argument key.
|
boolean |
hasFlag(String key)
Returns whether the given flag has been defined in this context.
|
boolean |
hasFlag(Text key)
Returns whether the given flag has been defined in this context.
|
void |
putArg(String key,
Object value)
Insert an argument into this context.
|
void |
putArg(Text key,
Object value)
Insert an argument into this context.
|
<T> T |
requireOne(String key)
Gets the value for the given key if the key has only one value, throws an
exception otherwise.
|
<T> T |
requireOne(Text key)
Gets the value for the given key if the key has only one value, throws an
exception otherwise.
|
public static final String TARGET_BLOCK_ARG
Location<World>
.public static final String TAB_COMPLETION
public <T> Collection<T> getAll(String key)
T
- the type of value to getkey
- The key to get values forpublic <T> Collection<T> getAll(Text key)
T
- the type of value to getkey
- The key to get values forpublic <T> Optional<T> getOne(String key)
An empty Optional
indicates that there are either zero or more
than one values for the given key. Use hasAny(Text)
to verify
which.
T
- the expected type of the argumentkey
- the key to getpublic <T> Optional<T> getOne(Text key)
An empty Optional
indicates that there are either zero or more
than one values for the given key. Use hasAny(Text)
to verify
which.
T
- the expected type of the argumentkey
- the key to getpublic <T> T requireOne(String key) throws NoSuchElementException, IllegalArgumentException, ClassCastException
T
- the expected type of the argumentkey
- the key to getNoSuchElementException
- if there is no element with the
associated keyIllegalArgumentException
- if there are more than one element
associated with the key (thus, the argument is illegal in this
context)ClassCastException
- if the element type is not what is expected
by the callerpublic <T> T requireOne(Text key) throws NoSuchElementException, IllegalArgumentException, ClassCastException
T
- the expected type of the argumentkey
- the key to getNoSuchElementException
- if there is no element with the
associated keyIllegalArgumentException
- if there are more than one element
associated with the key (thus, the argument is illegal in this
context)ClassCastException
- if the element type is not what is expectedpublic void putArg(String key, Object value)
key
- the key to store the arg undervalue
- the value for this argumentpublic void putArg(Text key, Object value)
key
- the key to store the arg undervalue
- the value for this argumentpublic void addFlag(String key)
key
- the key for the flag definedpublic void addFlag(Text key)
key
- the key for the flag definedpublic void checkPermission(CommandSource commander, String permission) throws CommandException
commander
- the source to check againstpermission
- The permission to checkCommandException
- if the source does not have permissionpublic boolean hasAny(String key)
key
- The key to look uppublic boolean hasAny(Text key)
key
- The key to look uppublic boolean hasFlag(String key)
key
- The key to look uppublic boolean hasFlag(Text key)
key
- The key to look uppublic CommandContext.Snapshot createSnapshot()
This is only guaranteed to create a shallow copy of the backing store. If any value is mutable, any changes to that value will be reflected in this snapshot. It is therefore not recommended that you keep this snapshot around for longer than is necessary.
CommandContext.Snapshot
containing the current state of the
CommandContext
public void applySnapshot(CommandContext.Snapshot snapshot)
CommandContext
to a previous state using a previously
created CommandContext.Snapshot
.snapshot
- The CommandContext.Snapshot
to restore this context with