Skip to content

Commit 8eb50c7

Browse files
Fix redirect help and also support comma separated lists
1 parent 0311508 commit 8eb50c7

File tree

4 files changed

+88
-41
lines changed

4 files changed

+88
-41
lines changed

src/main/java/net/pistonmaster/soulfire/brigadier/BrigadierHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package net.pistonmaster.soulfire.brigadier;
1919

2020
import com.mojang.brigadier.Command;
21+
import com.mojang.brigadier.RedirectModifier;
2122
import com.mojang.brigadier.arguments.ArgumentType;
2223
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
2324
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
@@ -38,6 +39,10 @@ public static Command<ConsoleSubject> help(String help, Command<ConsoleSubject>
3839
return new CommandHelpWrapper(command, help, false);
3940
}
4041

42+
public static RedirectModifier<ConsoleSubject> helpRedirect(String help, RedirectModifier<ConsoleSubject> redirect) {
43+
return new RedirectHelpWrapper(redirect, help, false);
44+
}
45+
4146
public static Command<ConsoleSubject> privateCommand(Command<ConsoleSubject> command) {
4247
return new CommandHelpWrapper(command, null, true);
4348
}

src/main/java/net/pistonmaster/soulfire/brigadier/ConsoleSubject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class ConsoleSubject {
2626
private static final Logger log = LoggerFactory.getLogger("Console");
27-
public Map<String, Object> extraData = new Object2ObjectOpenHashMap<>();
27+
public Map<String, String> extraData = new Object2ObjectOpenHashMap<>();
2828

2929
public void sendMessage(String message) {
3030
log.info(message);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SoulFire
3+
* Copyright (C) 2024 AlexProgrammerDE
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package net.pistonmaster.soulfire.brigadier;
19+
20+
import com.mojang.brigadier.RedirectModifier;
21+
import com.mojang.brigadier.context.CommandContext;
22+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
23+
import java.util.Collection;
24+
25+
public record RedirectHelpWrapper(
26+
RedirectModifier<ConsoleSubject> command, String help, boolean privateCommand)
27+
implements RedirectModifier<ConsoleSubject> {
28+
@Override
29+
public Collection<ConsoleSubject> apply(CommandContext<ConsoleSubject> context) throws CommandSyntaxException {
30+
return command.apply(context);
31+
}
32+
}

src/main/java/net/pistonmaster/soulfire/server/ServerCommandManager.java

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.mojang.brigadier.CommandDispatcher.ARGUMENT_SEPARATOR;
2121
import static net.pistonmaster.soulfire.brigadier.BrigadierHelper.argument;
2222
import static net.pistonmaster.soulfire.brigadier.BrigadierHelper.help;
23+
import static net.pistonmaster.soulfire.brigadier.BrigadierHelper.helpRedirect;
2324
import static net.pistonmaster.soulfire.brigadier.BrigadierHelper.literal;
2425
import static net.pistonmaster.soulfire.brigadier.BrigadierHelper.privateCommand;
2526

@@ -45,6 +46,7 @@
4546
import java.nio.file.StandardOpenOption;
4647
import java.time.Instant;
4748
import java.util.ArrayList;
49+
import java.util.Arrays;
4850
import java.util.Collections;
4951
import java.util.List;
5052
import java.util.Map;
@@ -58,6 +60,7 @@
5860
import lombok.extern.slf4j.Slf4j;
5961
import net.pistonmaster.soulfire.brigadier.CommandHelpWrapper;
6062
import net.pistonmaster.soulfire.brigadier.ConsoleSubject;
63+
import net.pistonmaster.soulfire.brigadier.RedirectHelpWrapper;
6164
import net.pistonmaster.soulfire.server.api.SoulFireAPI;
6265
import net.pistonmaster.soulfire.server.api.event.EventUtil;
6366
import net.pistonmaster.soulfire.server.api.event.bot.BotPreTickEvent;
@@ -102,7 +105,7 @@ public void postConstruct() {
102105
for (var command : getAllUsage(dispatcher.getRoot(), c.getSource(), false)) {
103106
c.getSource()
104107
.sendMessage(
105-
String.format("%s: %s", command.command(), command.help()));
108+
String.format("%s -> %s", command.command(), command.help()));
106109
}
107110

108111
return Command.SINGLE_SUCCESS;
@@ -537,7 +540,8 @@ public void postConstruct() {
537540
help(
538541
"Attempts to crash the server with a WorldEdit calculation",
539542
c -> {
540-
log.info("Attempting to crash the server with a WorldEdit calculation");
543+
log.info(
544+
"Attempting to crash the server with a WorldEdit calculation");
541545

542546
return forEveryBot(
543547
c,
@@ -678,7 +682,7 @@ public void postConstruct() {
678682
literal("chest")
679683
.executes(
680684
help(
681-
"Attempts to crash the server with a chest",
685+
"Attempts to crash the server with a chest.",
682686
c -> {
683687
log.info("Attempting to crash the server with a chest");
684688

@@ -694,28 +698,34 @@ public void postConstruct() {
694698
dispatcher.register(
695699
literal("bot")
696700
.then(
697-
argument("bot_name", StringArgumentType.string())
698-
.redirect(
701+
argument("bot_names", StringArgumentType.string())
702+
.forward(
699703
dispatcher.getRoot(),
700-
c -> {
701-
c.getSource()
702-
.extraData
703-
.put("bot_name", StringArgumentType.getString(c, "bot_name"));
704-
return c.getSource();
705-
})));
704+
helpRedirect(
705+
"Instead of running a command for all bots, run it for a specific list of bots. Use a comma to separate the names",
706+
c -> {
707+
c.getSource()
708+
.extraData
709+
.put("bot_names", StringArgumentType.getString(c, "bot_names"));
710+
return Collections.singleton(c.getSource());
711+
}),
712+
false)));
706713

707714
dispatcher.register(
708715
literal("attack")
709716
.then(
710-
argument("attack_id", IntegerArgumentType.integer(0))
711-
.redirect(
717+
argument("attack_ids", StringArgumentType.string())
718+
.forward(
712719
dispatcher.getRoot(),
713-
c -> {
714-
c.getSource()
715-
.extraData
716-
.put("attack_id", IntegerArgumentType.getInteger(c, "attack_id"));
717-
return c.getSource();
718-
})));
720+
helpRedirect(
721+
"Instead of running a command for all attacks, run it for a specific list of attacks. Use a comma to separate the ids",
722+
c -> {
723+
c.getSource()
724+
.extraData
725+
.put("attack_ids", StringArgumentType.getString(c, "attack_ids"));
726+
return Collections.singleton(c.getSource());
727+
}),
728+
false)));
719729
}
720730

721731
private int forEveryAttack(
@@ -727,8 +737,10 @@ private int forEveryAttack(
727737

728738
var resultCode = Command.SINGLE_SUCCESS;
729739
for (var attackManager : soulFireServer.attacks().values()) {
730-
if (context.getSource().extraData.containsKey("attack_id")
731-
&& context.getSource().extraData.get("attack_id").equals(attackManager.id())) {
740+
if (context.getSource().extraData.containsKey("attack_ids")
741+
&& Arrays.stream(context.getSource().extraData.get("attack_ids").split(","))
742+
.mapToInt(Integer::parseInt)
743+
.noneMatch(i -> i == attackManager.id())) {
732744
continue;
733745
}
734746

@@ -763,11 +775,9 @@ private int forEveryBot(
763775
attackManager -> {
764776
var resultCode = Command.SINGLE_SUCCESS;
765777
for (var bot : attackManager.botConnections()) {
766-
if (context.getSource().extraData.containsKey("bot_name")
767-
&& !bot.meta()
768-
.minecraftAccount()
769-
.username()
770-
.equals(context.getSource().extraData.get("bot_name"))) {
778+
if (context.getSource().extraData.containsKey("bot_names")
779+
&& Arrays.stream(context.getSource().extraData.get("bot_names").split(","))
780+
.noneMatch(s -> s.equals(bot.meta().minecraftAccount().username()))) {
771781
continue;
772782
}
773783

@@ -928,26 +938,26 @@ private void getAllUsage(
928938
}
929939

930940
if (node.getCommand() != null) {
931-
if (node.getCommand() instanceof CommandHelpWrapper helpWrapper) {
941+
var helpWrapper = (CommandHelpWrapper) node.getCommand();
932942
if (!helpWrapper.privateCommand()) {
933943
result.add(new HelpData(prefix, helpWrapper.help()));
934944
}
935-
} else {
936-
result.add(new HelpData(prefix, "N/A"));
937-
}
938945
}
939946

940947
if (node.getRedirect() != null) {
941-
final var redirect =
942-
node.getRedirect() == dispatcher.getRoot()
943-
? "..."
944-
: "-> " + node.getRedirect().getUsageText();
945-
result.add(
946-
new HelpData(
947-
prefix.isEmpty()
948-
? node.getUsageText() + ARGUMENT_SEPARATOR + redirect
949-
: prefix + ARGUMENT_SEPARATOR + redirect,
950-
"N/A"));
948+
var redirectHelpWrapper = (RedirectHelpWrapper) node.getRedirectModifier();
949+
if (!redirectHelpWrapper.privateCommand()) {
950+
final var redirect =
951+
node.getRedirect() == dispatcher.getRoot()
952+
? "..."
953+
: "-> " + node.getRedirect().getUsageText();
954+
result.add(
955+
new HelpData(
956+
prefix.isEmpty()
957+
? node.getUsageText() + ARGUMENT_SEPARATOR + redirect
958+
: prefix + ARGUMENT_SEPARATOR + redirect,
959+
redirectHelpWrapper.help()));
960+
}
951961
} else if (!node.getChildren().isEmpty()) {
952962
for (final var child : node.getChildren()) {
953963
getAllUsage(

0 commit comments

Comments
 (0)