-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
PrintCommandHelp.java
165 lines (141 loc) · 6.81 KB
/
PrintCommandHelp.java
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
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.command.util;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.formatting.component.CommandListBox;
import com.sk89q.worldedit.util.formatting.component.CommandUsageBox;
import com.sk89q.worldedit.util.formatting.component.InvalidComponentException;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.inject.InjectedValueStore;
import org.enginehub.piston.inject.Key;
import org.enginehub.piston.inject.MapBackedValueStore;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.sk89q.worldedit.internal.command.CommandUtil.byCleanName;
import static com.sk89q.worldedit.internal.command.CommandUtil.getSubCommands;
/**
* Implementation of the //help command.
*/
// Stored in a separate class to prevent import conflicts, and because it's aliased via /we help.
public class PrintCommandHelp {
private static Command detectCommand(CommandManager manager, String command) {
Optional<Command> mapping;
// First try the command as entered
mapping = manager.getCommand(command);
if (mapping.isPresent()) {
return mapping.get();
}
// If tried with slashes, try dropping a slash
if (command.startsWith("/")) {
mapping = manager.getCommand(command.substring(1));
return mapping.orElse(null);
}
// Otherwise, check /command, since that's common
mapping = manager.getCommand("/" + command);
return mapping.orElse(null);
}
public static void help(List<String> commandPath, int page, boolean listSubCommands,
CommandManager manager, Actor actor, String helpRootCommand) throws InvalidComponentException {
if (commandPath.isEmpty()) {
printCommands(page, manager.getAllCommands(), actor, ImmutableList.of(), helpRootCommand);
return;
}
List<Command> visited = new ArrayList<>();
Command currentCommand = detectCommand(manager, commandPath.get(0));
if (currentCommand == null) {
actor.printError(TranslatableComponent.of("worldedit.help.command-not-found", TextComponent.of(commandPath.get(0))));
return;
}
visited.add(currentCommand);
// Drill down to the command
for (int i = 1; i < commandPath.size(); i++) {
String subCommand = commandPath.get(i);
Map<String, Command> subCommands = getSubCommands(currentCommand);
if (subCommands.isEmpty()) {
actor.printError(TranslatableComponent.of("worldedit.help.no-subcommands",
TextComponent.of(toCommandString(visited)), TextComponent.of(subCommand)));
// full help for single command
CommandUsageBox box = new CommandUsageBox(visited, visited.stream()
.map(Command::getName).collect(Collectors.joining(" ")), helpRootCommand);
actor.print(box.create());
return;
}
if (subCommands.containsKey(subCommand)) {
currentCommand = subCommands.get(subCommand);
visited.add(currentCommand);
} else {
actor.printError(TranslatableComponent.of("worldedit.help.subcommand-not-found",
TextComponent.of(subCommand), TextComponent.of(toCommandString(visited))));
// list subcommands for currentCommand
printCommands(page, getSubCommands(Iterables.getLast(visited)).values().stream(), actor, visited, helpRootCommand);
return;
}
}
Map<String, Command> subCommands = getSubCommands(currentCommand);
if (subCommands.isEmpty() || !listSubCommands) {
// Create the message
CommandUsageBox box = new CommandUsageBox(visited, toCommandString(visited), helpRootCommand);
actor.print(box.create());
} else {
printCommands(page, subCommands.values().stream(), actor, visited, helpRootCommand);
}
}
private static String toCommandString(List<Command> visited) {
return "/" + Joiner.on(" ").join(visited.stream().map(Command::getName).iterator());
}
private static void printCommands(int page, Stream<Command> commandStream, Actor actor,
List<Command> commandList, String helpRootCommand) throws InvalidComponentException {
InjectedValueStore store = MapBackedValueStore.create();
store.injectValue(Key.of(Actor.class), context ->
Optional.of(actor));
// Get a list of aliases
List<Command> commands = commandStream
.filter(command -> command.getCondition().satisfied(store))
.sorted(byCleanName())
.toList();
String used = commandList.isEmpty() ? null : toCommandString(commandList);
CommandListBox box = new CommandListBox(
(used == null ? "Help" : "Subcommands: " + used),
helpRootCommand + " -s -p %page%" + (used == null ? "" : " " + used),
helpRootCommand);
if (!actor.isPlayer()) {
box.formatForConsole();
}
for (Command mapping : commands) {
String alias = (commandList.isEmpty() ? "/" : "") + mapping.getName();
String command = Stream.concat(commandList.stream(), Stream.of(mapping))
.map(Command::getName)
.collect(Collectors.joining(" ", "/", ""));
box.appendCommand(alias, mapping.getDescription(), command);
}
actor.print(box.create(page));
}
private PrintCommandHelp() {
}
}