Skip to content

Commit a1c0b88

Browse files
added the EnumCommandNode
1 parent a5e2d65 commit a1c0b88

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.wizardlybump17.wlib.command.node.object;
2+
3+
import com.wizardlybump17.wlib.command.exception.InputParsingException;
4+
import com.wizardlybump17.wlib.command.executor.CommandNodeExecutor;
5+
import com.wizardlybump17.wlib.command.input.AllowedInputs;
6+
import com.wizardlybump17.wlib.command.node.CommandNode;
7+
import com.wizardlybump17.wlib.command.suggestion.Suggester;
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.util.List;
12+
13+
public class EnumCommandNode<E extends Enum<E>> extends CommandNode<E> {
14+
15+
private final @NotNull Class<E> enumType;
16+
17+
public EnumCommandNode(@NotNull String name, @NotNull List<CommandNode<?>> children, @NotNull AllowedInputs<E> allowedInputs, @Nullable Suggester<E> suggester, @Nullable CommandNodeExecutor<?> executor, @Nullable String permission, @NotNull Class<E> enumType) {
18+
super(name, children, allowedInputs, suggester, executor, permission);
19+
this.enumType = enumType;
20+
}
21+
22+
@Override
23+
public @Nullable E parse(@NotNull String input) throws InputParsingException {
24+
try {
25+
return Enum.valueOf(enumType, input.toUpperCase());
26+
} catch (IllegalArgumentException e) {
27+
throw new InputParsingException("Could not parse as " + enumType.getSimpleName() + ": " + input, e);
28+
}
29+
}
30+
31+
@Override
32+
public @NotNull CommandNode<E> withChildren(@NotNull List<CommandNode<?>> children) {
33+
return new EnumCommandNode<>(getName(), children, getAllowedInputs(), getSuggester(), getExecutor(), getPermission(), enumType);
34+
}
35+
36+
@Override
37+
public @NotNull CommandNode<E> withExecutor(@Nullable CommandNodeExecutor<?> executor) {
38+
return new EnumCommandNode<>(getName(), getChildren(), getAllowedInputs(), getSuggester(), executor, getPermission(), enumType);
39+
}
40+
41+
@Override
42+
public @NotNull CommandNode<E> withPermission(@Nullable String permission) {
43+
return new EnumCommandNode<>(getName(), getChildren(), getAllowedInputs(), getSuggester(), getExecutor(), permission, enumType);
44+
}
45+
46+
public @NotNull Class<E> getEnumType() {
47+
return enumType;
48+
}
49+
}

0 commit comments

Comments
 (0)