State
Each element in a command hierarchy, when parsing begins, has a state, which can be altered while the parsing process is ongoing.
Lanat allows you to parse a command hierarchy more than once. After the first parse, all states are reset to their initial values so that the next parse can be done without any interference from the previous one.
Resetting states on custom elements
Even if parsing multiple times is not common, we should always ensure that states are properly reset.
Elements that have the ability to reset their states (such as argument types) implement the Resettable
interface. This interface has a single method, resetState()
, which resets the state of the element.
Example
Let's take a look at a simple example of a custom argument type.
class MyType implements ArgumentType<String> { private int times = 0; @Override public String parseValues(String[] args) { return args[0] + " " + ++times; } @Override public Range getUsageCountBounds() { return Range.AT_LEAST_ONE; } }An issue arises when we parse the command multiple times. The
times
field will not be reset to its initial value, which is zero. In order to fix this, we need to implement our ownresetState()
:@Override public void resetState() { super.resetState(); this.times = 0; }