From b411cc9c574175f5039bcca9c23bc7612435478d Mon Sep 17 00:00:00 2001 From: William Date: Mon, 17 Jun 2024 12:45:30 +0100 Subject: [PATCH] feat: add argument example to testbase --- example-plugin/build.gradle | 2 +- .../william278/uniform/ExampleCommand.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/example-plugin/build.gradle b/example-plugin/build.gradle index 3f32d2e..624e554 100644 --- a/example-plugin/build.gradle +++ b/example-plugin/build.gradle @@ -4,7 +4,7 @@ plugins { } dependencies { - implementation(project(":paper")) + implementation(project(":paper")) compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT' } diff --git a/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java b/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java index 81e447d..3278479 100644 --- a/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java +++ b/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java @@ -21,10 +21,14 @@ package net.william278.uniform; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.kyori.adventure.text.Component; +import net.william278.uniform.element.ArgumentElement; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; import java.util.List; +import java.util.Locale; import static net.william278.uniform.BaseCommand.greedyString; @@ -46,6 +50,38 @@ public class ExampleCommand extends Command { final CommandUser user = sub.getUser(ctx.getSource()); user.getAudience().sendMessage(Component.text(ctx.getArgument("message", String.class))); }, greedyString("message"))); + command.addSubCommand("flavor", (sub) -> sub.addSyntax((ctx) -> { + final CommandUser user = sub.getUser(ctx.getSource()); + final IceCreamFlavor flavor = ctx.getArgument("flavor", IceCreamFlavor.class); + switch (flavor) { + case VANILLA -> user.getAudience().sendMessage(Component.text("Vanilla ice cream is fine!")); + case CHOCOLATE -> user.getAudience().sendMessage(Component.text("Chocolate ice cream is kino!")); + case STRAWBERRY -> user.getAudience().sendMessage(Component.text("Strawberry ice cream is ok...")); + } + }, exampleCustomArg())); + } + + private static ArgumentElement exampleCustomArg() { + return new ArgumentElement<>("flavor", reader -> { + final String flavor = reader.readString(); + try { + return IceCreamFlavor.valueOf(flavor.toUpperCase(Locale.ENGLISH)); + } catch (IllegalArgumentException e) { + throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().create(); + } + }, (context, builder) -> { + Arrays.stream(IceCreamFlavor.values()).forEach( + flavor -> builder.suggest(flavor.name().toLowerCase(Locale.ENGLISH)) + ); + return builder.buildFuture(); + }); + + } + + enum IceCreamFlavor { + VANILLA, + CHOCOLATE, + STRAWBERRY } }