Skip to content

Commit

Permalink
Add selection suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacrain committed May 24, 2022
1 parent fb1445f commit 220230d
Showing 1 changed file with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.MessageArgument;
import net.minecraft.commands.arguments.UuidArgument;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
Expand Down Expand Up @@ -64,18 +63,21 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.then(literal("id")
.then(argument("id", IntegerArgumentType.integer(1))
.requires(src -> Taterzens.getInstance().getPlatform().checkPermission(src, "taterzens.npc.select.id", config.perms.npcCommandPermissionLevel))
.suggests((context, builder) -> SharedSuggestionProvider.suggest(getAvailableTaterzenIndices(), builder))
.executes(NpcCommand::selectTaterzenById)
)
)
.then(literal("name") // TODO: Autocompletion suggestions
.then(literal("name")
.then(argument("name", StringArgumentType.string())
.requires(src -> Taterzens.getInstance().getPlatform().checkPermission(src, "taterzens.npc.select.name", config.perms.npcCommandPermissionLevel))
.suggests((context, builder) -> SharedSuggestionProvider.suggest(getAvailableTaterzenNames(), builder))
.executes(NpcCommand::selectTaterzenByName)
)
)
.then(literal("uuid") // TODO: Autocompletion suggestions
.then(literal("uuid")
.then(argument("uuid", StringArgumentType.string())
.requires(src -> Taterzens.getInstance().getPlatform().checkPermission(src, "taterzens.npc.select.uuid", config.perms.npcCommandPermissionLevel))
.suggests((context, builder) -> SharedSuggestionProvider.suggest(getAvailableTaterzenUUIDs(), builder))
.executes(NpcCommand::selectTaterzenByUUID)
)
)
Expand Down Expand Up @@ -187,6 +189,13 @@ private static int listTaterzens(CommandContext<CommandSourceStack> context) thr
source.sendSuccess(response, false);
return 1;
}
private static String[] getAvailableTaterzenIndices() {
String[] availableIDs = new String[TATERZEN_NPCS.size()];
for (int i = 0; i < TATERZEN_NPCS.size(); i++) {
availableIDs[i] = Integer.toString(i + 1);
}
return availableIDs;
}

private static int selectTaterzenById(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
int id = IntegerArgumentType.getInteger(context, "id");
Expand Down Expand Up @@ -215,6 +224,20 @@ private static int selectTaterzenById(CommandContext<CommandSourceStack> context
return 1;
}

private static String[] getAvailableTaterzenNames() {
String[] availableNames = new String[TATERZEN_NPCS.size()];
TaterzenNPC[] taterzenArray = TATERZEN_NPCS.toArray(new TaterzenNPC[0]);
for (int i = 0; i < TATERZEN_NPCS.size(); i++) {
availableNames[i] = taterzenArray[i].getName().getString();
availableNames[i] = "\"" + availableNames[i] + "\""; // Adds quotation marks to the suggested name, such that
// Names containing a whitespace character (ex. the
// name is 'Foo Bar') can be completed and correctly
// used without the user having to enclose the argument
// name with quotation marks themselves.
}
return availableNames;
}

private static int selectTaterzenByName(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSourceStack source = context.getSource();
String name = StringArgumentType.getString(context, "name");
Expand Down Expand Up @@ -270,6 +293,15 @@ else if (count == 1) {
}
}

private static String[] getAvailableTaterzenUUIDs() {
String[] availableUUIDs = new String[TATERZEN_NPCS.size()];
TaterzenNPC[] taterzenArray = TATERZEN_NPCS.toArray(new TaterzenNPC[0]);
for (int i = 0; i < TATERZEN_NPCS.size(); i++) {
availableUUIDs[i] = taterzenArray[i].getUUID().toString();
}
return availableUUIDs;
}

private static int selectTaterzenByUUID(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSourceStack source = context.getSource();

Expand Down

0 comments on commit 220230d

Please sign in to comment.