Skip to content

Commit e2ac688

Browse files
committed
Switched to Brigadier command framework
1 parent 17a865b commit e2ac688

19 files changed

+233
-248
lines changed

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ repositories {
1818
dependencies {
1919
testImplementation(platform("org.junit:junit-bom:5.12.0"))
2020
testImplementation("org.junit.jupiter:junit-jupiter")
21-
implementation("net.kyori:adventure-text-minimessage:4.19.0")
2221
compileOnly("me.clip:placeholderapi:2.11.6")
2322
paperweight.paperDevBundle("1.21.4-R0.1-SNAPSHOT")
2423
}

src/main/java/de/pascalpex/pexnpc/PexNPC.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public void onEnable() {
3636
logger = getLogger();
3737
placedNPCs = new ArrayList<>();
3838
packetReader = new PacketReader();
39-
versionChecker = new VersionChecker(pluginVersion);
4039

4140
new Metrics(this, 14923);
4241

@@ -46,6 +45,7 @@ public void onEnable() {
4645
MessageHandler.prefix = MessageHandler.parse(Config.getPrefix());
4746
PlaceholderAPIAdapter.startup();
4847

48+
versionChecker = new VersionChecker(pluginVersion);
4949
versionChecker.clearUpdateNotified();
5050
if (Config.getUpdateChecker()) {
5151
versionChecker.fetchNewestVersion();
@@ -121,5 +121,11 @@ private static void loadAllNPCs() {
121121
NPCSender.sendNpcToPlayers(placeableNPC);
122122
placedNPCs.add(placeableNPC);
123123
}
124+
Bukkit.getConsoleSender().sendMessage(MessageHandler.prefixedMini("Loaded <gold>" + npcs.size() + " <aqua>NPCs"));
124125
}
126+
127+
public static String getPluginVersion() {
128+
return pluginVersion;
129+
}
130+
125131
}

src/main/java/de/pascalpex/pexnpc/PexNPCBootstrap.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
11
package de.pascalpex.pexnpc;
22

3-
import de.pascalpex.pexnpc.commands.PexNPCCommand;
3+
import com.mojang.brigadier.arguments.IntegerArgumentType;
4+
import com.mojang.brigadier.arguments.StringArgumentType;
5+
import com.mojang.brigadier.tree.LiteralCommandNode;
6+
import de.pascalpex.pexnpc.commands.IDSuggestionProvider;
7+
import de.pascalpex.pexnpc.commands.NPCSlotArgument;
8+
import io.papermc.paper.command.brigadier.CommandSourceStack;
9+
import io.papermc.paper.command.brigadier.Commands;
410
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
511
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
612
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
713
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
14+
import org.bukkit.Bukkit;
15+
import org.bukkit.entity.Player;
816
import org.bukkit.plugin.java.JavaPlugin;
917

1018
public class PexNPCBootstrap implements PluginBootstrap {
1119
@Override
1220
public void bootstrap(BootstrapContext context) {
1321
context.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, commands -> {
14-
commands.registrar().register("pexnpc", new PexNPCCommand());
22+
IDSuggestionProvider idSuggestionProvider = new IDSuggestionProvider();
23+
24+
LiteralCommandNode<CommandSourceStack> advancedCommandRoot = Commands.literal("pexnpc").requires(commandSourceStack -> commandSourceStack.getSender().hasPermission("pexnpc.command"))
25+
.then(Commands.literal("help"))
26+
.then(Commands.literal("reload"))
27+
.then(Commands.literal("create")
28+
.then(Commands.argument("name", StringArgumentType.greedyString())))
29+
.then(Commands.literal("list"))
30+
.then(Commands.literal("delete")
31+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
32+
.suggests(idSuggestionProvider::getSuggestions)))
33+
.then(Commands.literal("name")
34+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
35+
.suggests(idSuggestionProvider::getSuggestions)
36+
.then(Commands.argument("name", StringArgumentType.greedyString()))))
37+
.then(Commands.literal("movehere")
38+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
39+
.suggests(idSuggestionProvider::getSuggestions)))
40+
.then(Commands.literal("tp")
41+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
42+
.suggests(idSuggestionProvider::getSuggestions)))
43+
.then(Commands.literal("skin")
44+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
45+
.suggests(idSuggestionProvider::getSuggestions)
46+
.then(Commands.argument("skin", StringArgumentType.greedyString())
47+
.suggests((cmdContext, builder) -> {
48+
for (Player player : Bukkit.getOnlinePlayers()) {
49+
builder.suggest(player.getName());
50+
}
51+
return builder.buildFuture();
52+
}))))
53+
.then(Commands.literal("cmd")
54+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
55+
.suggests(idSuggestionProvider::getSuggestions)
56+
.then(Commands.argument("cmd", StringArgumentType.greedyString()))))
57+
.then(Commands.literal("msg")
58+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
59+
.suggests(idSuggestionProvider::getSuggestions)
60+
.then(Commands.argument("msg", StringArgumentType.greedyString()))))
61+
.then(Commands.literal("item")
62+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
63+
.suggests(idSuggestionProvider::getSuggestions)
64+
.then(Commands.argument("slot", new NPCSlotArgument()))))
65+
.then(Commands.literal("clear")
66+
.then(Commands.argument("id", IntegerArgumentType.integer(1))
67+
.suggests(idSuggestionProvider::getSuggestions)))
68+
.build();
69+
70+
commands.registrar().register(advancedCommandRoot);
1571
});
1672
}
1773

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package de.pascalpex.pexnpc.commands;
2+
3+
import com.mojang.brigadier.context.CommandContext;
4+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5+
import com.mojang.brigadier.suggestion.SuggestionProvider;
6+
import com.mojang.brigadier.suggestion.Suggestions;
7+
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
8+
import de.pascalpex.pexnpc.PexNPC;
9+
import de.pascalpex.pexnpc.npc.PlaceableNPC;
10+
import de.pascalpex.pexnpc.util.MessageHandler;
11+
import io.papermc.paper.command.brigadier.MessageComponentSerializer;
12+
13+
import java.util.concurrent.CompletableFuture;
14+
15+
public class IDSuggestionProvider implements SuggestionProvider {
16+
@Override
17+
public CompletableFuture<Suggestions> getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException {
18+
for(PlaceableNPC npc : PexNPC.getPlacedNpcs()) {
19+
String currentInput = "";
20+
try {
21+
currentInput = context.getArgument("id", Integer.class).toString();
22+
} catch (IllegalArgumentException ignored) {}
23+
if(String.valueOf(npc.getNpc().getId()).startsWith(currentInput)) {
24+
builder.suggest(String.valueOf(npc.getNpc().getId()), MessageComponentSerializer.message().serialize(MessageHandler.parseSection(npc.getNpc().getName())));
25+
}
26+
}
27+
return builder.buildFuture();
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package de.pascalpex.pexnpc.commands;
2+
3+
import com.mojang.brigadier.LiteralMessage;
4+
import com.mojang.brigadier.Message;
5+
import com.mojang.brigadier.StringReader;
6+
import com.mojang.brigadier.arguments.ArgumentType;
7+
import com.mojang.brigadier.arguments.StringArgumentType;
8+
import com.mojang.brigadier.context.CommandContext;
9+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
10+
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
11+
import com.mojang.brigadier.suggestion.Suggestions;
12+
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
13+
import de.pascalpex.pexnpc.npc.NPCItemSlot;
14+
import de.pascalpex.pexnpc.util.MessageHandler;
15+
import io.papermc.paper.command.brigadier.MessageComponentSerializer;
16+
import io.papermc.paper.command.brigadier.argument.CustomArgumentType;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
import java.util.concurrent.CompletableFuture;
20+
21+
public class NPCSlotArgument implements CustomArgumentType.Converted<NPCItemSlot, String> {
22+
@Override
23+
public NPCItemSlot convert(@NotNull String nativeType) throws CommandSyntaxException {
24+
for(NPCItemSlot slot : NPCItemSlot.values()) {
25+
if(slot.getName().equalsIgnoreCase(nativeType)) {
26+
return slot;
27+
}
28+
}
29+
final Message exceptionMessage = MessageComponentSerializer.message().serialize(MessageHandler.errorMessage("The NPC slot you entered is invalid"));
30+
throw new CommandSyntaxException(new SimpleCommandExceptionType(exceptionMessage), exceptionMessage);
31+
}
32+
33+
@Override
34+
public @NotNull ArgumentType getNativeType() {
35+
return StringArgumentType.word();
36+
}
37+
38+
@Override
39+
public <S> @NotNull CompletableFuture<Suggestions> listSuggestions(@NotNull CommandContext<S> context, @NotNull SuggestionsBuilder builder) {
40+
for(NPCItemSlot slot : NPCItemSlot.values()) {
41+
builder.suggest(slot.getName());
42+
}
43+
return builder.buildFuture();
44+
}
45+
}

src/main/java/de/pascalpex/pexnpc/commands/PexNPCCommand.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/main/java/de/pascalpex/pexnpc/commands/subcommands/HelpSubcommand.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/java/de/pascalpex/pexnpc/commands/subcommands/ListSubcommand.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/java/de/pascalpex/pexnpc/commands/subcommands/SubCommand.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/de/pascalpex/pexnpc/commands/subcommands/SubCommands.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)