Skip to content

Commit bf761c3

Browse files
committed
Implemented custom argument for NPC ids
1 parent 7a6c636 commit bf761c3

File tree

4 files changed

+62
-61
lines changed

4 files changed

+62
-61
lines changed

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package de.pascalpex.pexnpc;
22

3-
import com.mojang.brigadier.arguments.IntegerArgumentType;
43
import com.mojang.brigadier.arguments.LongArgumentType;
54
import com.mojang.brigadier.arguments.StringArgumentType;
65
import com.mojang.brigadier.tree.LiteralCommandNode;
7-
import de.pascalpex.pexnpc.commands.IDSuggestionProvider;
6+
import de.pascalpex.pexnpc.commands.IDArgument;
87
import de.pascalpex.pexnpc.commands.NPCSlotArgument;
98
import de.pascalpex.pexnpc.commands.subcommands.*;
109
import io.papermc.paper.command.brigadier.CommandSourceStack;
@@ -21,7 +20,7 @@ public class PexNPCBootstrap implements PluginBootstrap {
2120
@Override
2221
public void bootstrap(BootstrapContext context) {
2322
context.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, commands -> {
24-
IDSuggestionProvider idSuggestionProvider = new IDSuggestionProvider();
23+
IDArgument idArgument = new IDArgument();
2524
HelpSubcommand helpSubcommand = new HelpSubcommand();
2625

2726
LiteralCommandNode<CommandSourceStack> advancedCommandRoot = Commands.literal("pexnpc")
@@ -36,22 +35,17 @@ public void bootstrap(BootstrapContext context) {
3635
.then(Commands.literal("list")
3736
.executes(new ListSubcommand()))
3837
.then(Commands.literal("delete")
39-
.then(Commands.argument("id", LongArgumentType.longArg(1))
40-
.suggests(idSuggestionProvider::getSuggestions)))
38+
.then(Commands.argument("npc", idArgument)))
4139
.then(Commands.literal("name")
42-
.then(Commands.argument("id", LongArgumentType.longArg(1))
43-
.suggests(idSuggestionProvider::getSuggestions)
40+
.then(Commands.argument("npc", idArgument)
4441
.then(Commands.argument("name", StringArgumentType.greedyString()))))
4542
.then(Commands.literal("movehere")
46-
.then(Commands.argument("id", LongArgumentType.longArg(1))
47-
.suggests(idSuggestionProvider::getSuggestions)))
43+
.then(Commands.argument("npc", idArgument)))
4844
.then(Commands.literal("tp")
49-
.then(Commands.argument("id", LongArgumentType.longArg(1))
50-
.suggests(idSuggestionProvider::getSuggestions)
45+
.then(Commands.argument("npc", idArgument)
5146
.executes(new TpSubcommand())))
5247
.then(Commands.literal("skin")
53-
.then(Commands.argument("id", LongArgumentType.longArg(1))
54-
.suggests(idSuggestionProvider::getSuggestions)
48+
.then(Commands.argument("npc", idArgument)
5549
.then(Commands.argument("skin", StringArgumentType.greedyString())
5650
.suggests((cmdContext, builder) -> {
5751
for (Player player : Bukkit.getOnlinePlayers()) {
@@ -60,20 +54,16 @@ public void bootstrap(BootstrapContext context) {
6054
return builder.buildFuture();
6155
}))))
6256
.then(Commands.literal("cmd")
63-
.then(Commands.argument("id", LongArgumentType.longArg(1))
64-
.suggests(idSuggestionProvider::getSuggestions)
57+
.then(Commands.argument("npc", idArgument)
6558
.then(Commands.argument("cmd", StringArgumentType.greedyString()))))
6659
.then(Commands.literal("msg")
67-
.then(Commands.argument("id", LongArgumentType.longArg(1))
68-
.suggests(idSuggestionProvider::getSuggestions)
60+
.then(Commands.argument("npc", idArgument)
6961
.then(Commands.argument("msg", StringArgumentType.greedyString()))))
7062
.then(Commands.literal("item")
71-
.then(Commands.argument("id", LongArgumentType.longArg(1))
72-
.suggests(idSuggestionProvider::getSuggestions)
63+
.then(Commands.argument("npc", idArgument)
7364
.then(Commands.argument("slot", new NPCSlotArgument()))))
7465
.then(Commands.literal("clear")
75-
.then(Commands.argument("id", LongArgumentType.longArg(1))
76-
.suggests(idSuggestionProvider::getSuggestions)))
66+
.then(Commands.argument("npc", idArgument)))
7767
.executes(helpSubcommand)
7868
.build();
7969

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package de.pascalpex.pexnpc.commands;
2+
3+
import com.mojang.brigadier.Message;
4+
import com.mojang.brigadier.arguments.ArgumentType;
5+
import com.mojang.brigadier.arguments.LongArgumentType;
6+
import com.mojang.brigadier.context.CommandContext;
7+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8+
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
9+
import com.mojang.brigadier.suggestion.Suggestions;
10+
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
11+
import de.pascalpex.pexnpc.PexNPC;
12+
import de.pascalpex.pexnpc.npc.PlaceableNPC;
13+
import de.pascalpex.pexnpc.util.MessageHandler;
14+
import io.papermc.paper.command.brigadier.MessageComponentSerializer;
15+
import io.papermc.paper.command.brigadier.argument.CustomArgumentType;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
import java.util.concurrent.CompletableFuture;
19+
20+
public class IDArgument implements CustomArgumentType.Converted<PlaceableNPC, Long> {
21+
@Override
22+
public PlaceableNPC convert(@NotNull Long nativeType) throws CommandSyntaxException {
23+
PlaceableNPC placeableNPC = PexNPC.findNPCbyID(nativeType);
24+
if(placeableNPC != null) {
25+
return placeableNPC;
26+
}
27+
28+
final Message exceptionMessage = MessageComponentSerializer.message().serialize(MessageHandler.errorMessage("An NPC with this ID does not exist"));
29+
throw new CommandSyntaxException(new SimpleCommandExceptionType(exceptionMessage), exceptionMessage);
30+
}
31+
32+
@Override
33+
public @NotNull ArgumentType getNativeType() {
34+
return LongArgumentType.longArg(1);
35+
}
36+
37+
@Override
38+
public <S> @NotNull CompletableFuture<Suggestions> listSuggestions(@NotNull CommandContext<S> context, @NotNull SuggestionsBuilder builder) {
39+
for(PlaceableNPC placeableNPC : PexNPC.getPlacedNpcs()) {
40+
String currentInput = "";
41+
try {
42+
currentInput = context.getArgument("npc", Integer.class).toString();
43+
} catch (IllegalArgumentException ignored) {}
44+
if(String.valueOf(placeableNPC.getNpc().getId()).startsWith(currentInput)) {
45+
builder.suggest(String.valueOf(placeableNPC.getNpc().getId()), MessageComponentSerializer.message().serialize(MessageHandler.parseSection(placeableNPC.getNpc().getName())));
46+
}
47+
}
48+
return builder.buildFuture();
49+
}
50+
}

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

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

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package de.pascalpex.pexnpc.commands.subcommands;
22

33
import com.mojang.brigadier.Command;
4-
import com.mojang.brigadier.arguments.LongArgumentType;
54
import com.mojang.brigadier.context.CommandContext;
6-
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7-
import de.pascalpex.pexnpc.PexNPC;
85
import de.pascalpex.pexnpc.files.Config;
96
import de.pascalpex.pexnpc.npc.NPC;
107
import de.pascalpex.pexnpc.npc.PlaceableNPC;
@@ -21,14 +18,7 @@ public class TpSubcommand implements Command<CommandSourceStack> {
2118
public int run(CommandContext<CommandSourceStack> context) {
2219
CommandSender sender = context.getSource().getSender();
2320
Entity executor = context.getSource().getExecutor();
24-
long id = LongArgumentType.getLong(context, "id");
25-
26-
PlaceableNPC placeableNPC = PexNPC.findNPCbyID(id);
27-
if(placeableNPC == null) {
28-
sender.sendMessage(MessageHandler.errorMessage("The provided ID is invalid"));
29-
return SINGLE_SUCCESS;
30-
}
31-
NPC npc = placeableNPC.getNpc();
21+
NPC npc = context.getArgument("npc", PlaceableNPC.class).getNpc();
3222

3323
if(Config.getSpectatorModeOnTeleport() && executor instanceof Player player) {
3424
player.setGameMode(GameMode.SPECTATOR);

0 commit comments

Comments
 (0)