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
|
/*
* Copyright (C) 2021 - 2023 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.elytrium.limboauth.command;
import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.Settings;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class LimboAuthCommand implements SimpleCommand {
private static final List<Component> HELP_MESSAGE = List.of(
Component.text("This server is using LimboAuth and LimboAPI.", NamedTextColor.YELLOW),
Component.text("(C) 2021 - 2023 Elytrium", NamedTextColor.YELLOW),
Component.text("https://elytrium.net/github/", NamedTextColor.GREEN),
Component.empty()
);
private static final Component AVAILABLE_SUBCOMMANDS_MESSAGE = Component.text("Available subcommands:", NamedTextColor.WHITE);
private static final Component NO_AVAILABLE_SUBCOMMANDS_MESSAGE = Component.text("There is no available subcommands for you.", NamedTextColor.WHITE);
private final LimboAuth plugin;
public LimboAuthCommand(LimboAuth plugin) {
this.plugin = plugin;
}
@Override
public List<String> suggest(SimpleCommand.Invocation invocation) {
CommandSource source = invocation.source();
String[] args = invocation.arguments();
if (args.length == 0) {
return Arrays.stream(Subcommand.values())
.filter(command -> command.hasPermission(source))
.map(Subcommand::getCommand)
.collect(Collectors.toList());
} else if (args.length == 1) {
String argument = args[0];
return Arrays.stream(Subcommand.values())
.filter(command -> command.hasPermission(source))
.map(Subcommand::getCommand)
.filter(str -> str.regionMatches(true, 0, argument, 0, argument.length()))
.collect(Collectors.toList());
} else {
return ImmutableList.of();
}
}
@Override
public void execute(SimpleCommand.Invocation invocation) {
CommandSource source = invocation.source();
String[] args = invocation.arguments();
int argsAmount = args.length;
if (argsAmount > 0) {
try {
Subcommand subcommand = Subcommand.valueOf(args[0].toUpperCase(Locale.ROOT));
if (!subcommand.hasPermission(source)) {
this.showHelp(source);
return;
}
subcommand.executor.execute(this, source, args);
} catch (IllegalArgumentException e) {
this.showHelp(source);
}
} else {
this.showHelp(source);
}
}
private void showHelp(CommandSource source) {
HELP_MESSAGE.forEach(source::sendMessage);
List<Subcommand> availableSubcommands = Arrays.stream(Subcommand.values())
.filter(command -> command.hasPermission(source))
.collect(Collectors.toList());
if (availableSubcommands.size() > 0) {
source.sendMessage(AVAILABLE_SUBCOMMANDS_MESSAGE);
availableSubcommands.forEach(command -> source.sendMessage(command.getMessageLine()));
} else {
source.sendMessage(NO_AVAILABLE_SUBCOMMANDS_MESSAGE);
}
}
private enum Subcommand {
RELOAD("Reload config.", (LimboAuthCommand parent, CommandSource source, String[] args) -> {
parent.plugin.reload();
source.sendMessage(LimboAuth.getSerializer().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD));
});
private final String command;
private final String description;
private final SubcommandExecutor executor;
Subcommand(String description, SubcommandExecutor executor) {
this.command = this.name().toLowerCase(Locale.ROOT);
this.description = description;
this.executor = executor;
}
public boolean hasPermission(CommandSource source) {
return source.hasPermission("limboauth.admin." + this.command);
}
public Component getMessageLine() {
return Component.textOfChildren(
Component.text(" /limboauth " + this.command, NamedTextColor.GREEN),
Component.text(" - ", NamedTextColor.DARK_GRAY),
Component.text(this.description, NamedTextColor.YELLOW)
);
}
public String getCommand() {
return this.command;
}
}
private interface SubcommandExecutor {
void execute(LimboAuthCommand parent, CommandSource source, String[] args);
}
}
|