-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSwitchPart.java
More file actions
115 lines (84 loc) · 2.99 KB
/
SwitchPart.java
File metadata and controls
115 lines (84 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package me.fixeddev.commandflow.part.defaults;
import me.fixeddev.commandflow.CommandContext;
import me.fixeddev.commandflow.exception.ArgumentParseException;
import me.fixeddev.commandflow.part.CommandPart;
import me.fixeddev.commandflow.stack.ArgumentStack;
import me.fixeddev.commandflow.stack.StackSnapshot;
import net.kyori.text.Component;
import net.kyori.text.TextComponent;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class SwitchPart implements CommandPart {
private final String name;
private final String shortName;
private final boolean allowFullName;
public SwitchPart(String name, String shortName, boolean allowFullName) {
this.name = name;
this.shortName = shortName;
this.allowFullName = allowFullName;
}
public SwitchPart(String shortName) {
this.name = shortName;
this.shortName = shortName;
this.allowFullName = false;
}
@Override
public String getName() {
return name;
}
@Override
public @Nullable Component getLineRepresentation() {
TextComponent.Builder builder = TextComponent.builder("[");
builder.append("-" + shortName);
builder.append("]");
return builder.build();
}
@Override
public void parse(CommandContext context, ArgumentStack stack, CommandPart parent) throws ArgumentParseException {
StackSnapshot snapshot = stack.getSnapshot();
boolean found = false;
while (stack.hasNext()) {
String arg = stack.next();
if (!arg.startsWith("-")) {
continue;
}
if (arg.equals("--" + name) && allowFullName) {
stack.remove();
context.setValue(this, true);
found = true;
break;
}
if (arg.equals("-" + shortName)) {
stack.remove();
context.setValue(this, true);
found = true;
break;
}
}
if (!found) {
context.setValue(this, false);
}
stack.applySnapshot(snapshot, false);
}
@Override
public List<String> getSuggestions(CommandContext commandContext, ArgumentStack stack) {
String nextArgument = stack.hasNext() ? stack.next() : "";
List<String> suggestions = new ArrayList<>();
if (!nextArgument.startsWith("-")) {
return suggestions;
}
nextArgument = nextArgument.lastIndexOf('-') != 0 ? nextArgument.substring(2) : nextArgument.substring(1);
if (allowFullName && name.startsWith(nextArgument)) {
suggestions.add("--" + name);
}
if (shortName.startsWith(nextArgument)) {
suggestions.add("-" + shortName);
}
if(nextArgument.equals(shortName) || nextArgument.equals(name)){
suggestions.clear();
return suggestions;
}
return suggestions;
}
}