-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCloudSpongeCommand.java
More file actions
228 lines (202 loc) · 9 KB
/
CloudSpongeCommand.java
File metadata and controls
228 lines (202 loc) · 9 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.sponge;
import io.leangen.geantyref.GenericTypeReflector;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.internal.CommandNode;
import org.incendo.cloud.parser.aggregate.AggregateParser;
import org.incendo.cloud.parser.standard.LiteralParser;
import org.incendo.cloud.permission.Permission;
import org.incendo.cloud.type.tuple.Pair;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.command.CommandCompletion;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.parameter.ArgumentReader;
import org.spongepowered.api.command.registrar.tree.CommandTreeNode;
import org.spongepowered.api.registry.RegistryHolder;
import static net.kyori.adventure.text.Component.text;
final class CloudSpongeCommand<C> implements Command.Raw {
private final SpongeCommandManager<C> commandManager;
private final String label;
CloudSpongeCommand(
final @NonNull String label,
final @NonNull SpongeCommandManager<C> commandManager
) {
this.label = label;
this.commandManager = commandManager;
}
@Override
public CommandResult process(final @NonNull CommandCause cause, final ArgumentReader.@NonNull Mutable arguments) {
final C cloudSender = this.commandManager.senderMapper().map(cause);
final String input = this.formatCommandForParsing(arguments.input());
this.commandManager.commandExecutor().executeCommand(cloudSender, input);
return CommandResult.success();
}
@Override
public List<CommandCompletion> complete(
final @NonNull CommandCause cause,
final ArgumentReader.@NonNull Mutable arguments
) {
return this.commandManager.suggestionFactory()
.suggestImmediately(this.commandManager.senderMapper().map(cause),
this.formatCommandForSuggestions(arguments.input()))
.list()
.stream()
.map(s -> CommandCompletion.of(s.suggestion(), s.tooltip()))
.collect(Collectors.toList());
}
@Override
public boolean canExecute(final @NonNull CommandCause cause) {
return this.checkAccess(
cause,
this.namedNode().nodeMeta()
.getOrDefault(CommandNode.META_KEY_ACCESS, Collections.emptyMap())
);
}
@Override
public Optional<Component> shortDescription(final CommandCause cause) {
return Optional.of(this.usage(cause));
}
@Override
public Optional<Component> extendedDescription(final CommandCause cause) {
return Optional.of(this.usage(cause));
}
@Override
public Optional<Component> help(final @NonNull CommandCause cause) {
return Optional.of(this.usage(cause));
}
@Override
public Component usage(final CommandCause cause) {
return text(this.commandManager.commandSyntaxFormatter()
.apply(this.commandManager.senderMapper().map(cause), Collections.emptyList(), this.namedNode()));
}
private CommandNode<C> namedNode() {
return this.commandManager.commandTree().getNamedNode(this.label);
}
@Override
public CommandTreeNode.Root commandTree(final RegistryHolder holder) {
final CommandTreeNode<CommandTreeNode.Root> root = CommandTreeNode.root();
final CommandNode<C> cloud = this.namedNode();
if (canExecute(cloud)) {
root.executable();
}
this.addRequirement(cloud, root);
this.addChildren(root, cloud, holder);
return (CommandTreeNode.Root) root;
}
private void addChildren(final CommandTreeNode<?> node, final CommandNode<C> cloud, final RegistryHolder holder) {
for (final CommandNode<C> child : cloud.children()) {
final CommandComponent<C> value = child.component();
final CommandTreeNode.Argument<? extends CommandTreeNode.Argument<?>> treeNode;
if (value.parser() instanceof LiteralParser) {
treeNode = (CommandTreeNode.Argument<? extends CommandTreeNode.Argument<?>>) CommandTreeNode.literal();
} else if (value.parser() instanceof AggregateParser<C, ?> aggregate) {
this.handleAggregate(node, child, aggregate, holder);
continue;
} else {
treeNode = this.commandManager.parserMapper().mapComponent(value, holder);
}
this.addRequirement(child, treeNode);
if (canExecute(child)) {
treeNode.executable();
}
this.addChildren(treeNode, child, holder);
node.child(value.name(), treeNode);
}
}
private void handleAggregate(
final CommandTreeNode<?> node,
final CommandNode<C> child,
final AggregateParser<C, ?> compound,
final RegistryHolder holder
) {
final CommandTreeNode.Argument<? extends CommandTreeNode.Argument<?>> treeNode;
final ArrayDeque<Pair<String, CommandTreeNode.Argument<? extends CommandTreeNode.Argument<?>>>> nodes = new ArrayDeque<>();
for (final CommandComponent<C> component : compound.components()) {
final String name = component.name();
nodes.add(Pair.of(name, this.commandManager.parserMapper().mapParser(component.parser(), holder)));
}
Pair<String, CommandTreeNode.Argument<? extends CommandTreeNode.Argument<?>>> argument = null;
while (!nodes.isEmpty()) {
final Pair<String, CommandTreeNode.Argument<? extends CommandTreeNode.Argument<?>>> prev = argument;
argument = nodes.removeLast();
if (prev != null) {
argument.second().child(prev.first(), prev.second());
} else {
// last node
if (canExecute(child)) {
argument.second().executable();
}
}
this.addRequirement(child, argument.second());
}
treeNode = argument.second();
this.addChildren(treeNode, child, holder);
node.child(compound.components().get(0).toString(), treeNode);
}
private static <C> boolean canExecute(final @NonNull CommandNode<C> node) {
return node.isLeaf()
|| !node.component().required()
|| node.command() != null
|| node.children().stream().noneMatch(c -> c.component().required());
}
private void addRequirement(
final @NonNull CommandNode<C> cloud,
final @NonNull CommandTreeNode<? extends CommandTreeNode<?>> node
) {
final Map<Type, Permission> accessMap =
cloud.nodeMeta().getOrDefault(CommandNode.META_KEY_ACCESS, Collections.emptyMap());
node.requires(cause -> this.checkAccess(cause, accessMap));
}
private boolean checkAccess(final CommandCause cause, final Map<Type, Permission> accessMap) {
final C cloudSender = this.commandManager.senderMapper().map(cause);
for (final Map.Entry<Type, Permission> entry : accessMap.entrySet()) {
if (GenericTypeReflector.isSuperType(entry.getKey(), cloudSender.getClass())) {
if (this.commandManager.testPermission(cloudSender, entry.getValue()).allowed()) {
return true;
}
}
}
return false;
}
private String formatCommandForParsing(final @NonNull String arguments) {
if (arguments.isEmpty()) {
return this.label;
}
return this.label + " " + arguments;
}
private String formatCommandForSuggestions(final @NonNull String arguments) {
return this.label + " " + arguments;
}
}