-
-
Notifications
You must be signed in to change notification settings - Fork 684
/
Help.java
185 lines (174 loc) · 7.66 KB
/
Help.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites 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.plotsquared.core.command;
import com.plotsquared.core.configuration.caption.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.helpmenu.HelpMenu;
import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
@CommandDeclaration(command = "help",
aliases = "?",
category = CommandCategory.INFO,
usage = "help [category | #]",
permission = "plots.use")
public class Help extends Command {
public Help(Command parent) {
super(parent, true);
}
@Override
public boolean canExecute(PlotPlayer<?> player, boolean message) {
return true;
}
@Override
public CompletableFuture<Boolean> execute(
PlotPlayer<?> player, String[] args,
RunnableVal3<Command, Runnable, Runnable> confirm,
RunnableVal2<Command, CommandResult> whenDone
) {
switch (args.length) {
case 0 -> {
return displayHelp(player, null, 0);
}
case 1 -> {
if (MathMan.isInteger(args[0])) {
try {
return displayHelp(player, null, Integer.parseInt(args[0]));
} catch (NumberFormatException ignored) {
return displayHelp(player, null, 1);
}
} else {
return displayHelp(player, args[0], 1);
}
}
case 2 -> {
if (MathMan.isInteger(args[1])) {
try {
return displayHelp(player, args[0], Integer.parseInt(args[1]));
} catch (NumberFormatException ignored) {
return displayHelp(player, args[0], 1);
}
}
return CompletableFuture.completedFuture(false);
}
default -> sendUsage(player);
}
return CompletableFuture.completedFuture(true);
}
public CompletableFuture<Boolean> displayHelp(
final PlotPlayer<?> player, final String catRaw,
final int page
) {
return CompletableFuture.supplyAsync(() -> {
String cat = catRaw;
CommandCategory catEnum = null;
if (cat != null) {
if (!"all".equalsIgnoreCase(cat)) {
for (CommandCategory c : CommandCategory.values()) {
if (StringMan.isEqualIgnoreCaseToAny(cat, c.name(), c.toString())) {
catEnum = c;
cat = c.name();
break;
}
}
if (catEnum == null) {
cat = null;
}
}
}
if (cat == null && page == 0) {
TextComponent.Builder builder = Component.text();
builder.append(MINI_MESSAGE.deserialize(TranslatableCaption.of("help.help_header").getComponent(player)));
for (CommandCategory c : CommandCategory.values()) {
if (!c.canAccess(player)) {
continue;
}
builder.append(Component.newline()).append(MINI_MESSAGE
.deserialize(
TranslatableCaption.of("help.help_info_item").getComponent(player),
TagResolver.builder()
.tag("command", Tag.inserting(Component.text("/plot help")))
.tag("category", Tag.inserting(Component.text(c.name().toLowerCase())))
.tag("category_desc", Tag.inserting(c.toComponent(player)))
.build()
));
}
builder.append(Component.newline()).append(MINI_MESSAGE
.deserialize(
TranslatableCaption.of("help.help_info_item").getComponent(player),
TagResolver.builder()
.tag("command", Tag.inserting(Component.text("/plot help")))
.tag("category", Tag.inserting(Component.text("all")))
.tag(
"category_desc",
Tag.inserting(TranslatableCaption
.of("help.help_display_all_commands")
.toComponent(player))
)
.build()
));
builder.append(Component.newline()).append(MINI_MESSAGE.deserialize(TranslatableCaption
.of("help.help_footer")
.getComponent(player)));
player.sendMessage(StaticCaption.of(MINI_MESSAGE.serialize(builder.asComponent())));
return true;
}
new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(
page - 1,
getParent().toString(),
player
)
.render();
return true;
});
}
@Override
public Collection<Command> tab(PlotPlayer<?> player, String[] args, boolean space) {
final String argument = args[0].toLowerCase(Locale.ENGLISH);
List<Command> result = new ArrayList<>();
for (final CommandCategory category : CommandCategory.values()) {
if (!category.canAccess(player)) {
continue;
}
String name = category.name().toLowerCase();
if (!name.startsWith(argument)) {
continue;
}
result.add(new Command(null, false, name, "", RequiredType.NONE, null) {
});
}
// add the category "all"
if ("all".startsWith(argument)) {
result.add(new Command(null, false, "all", "", RequiredType.NONE, null) {
});
}
return result;
}
}