diff --git a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/AnnotationParserExample.java b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/AnnotationParserExample.java index 2153698b..cf7ee5e8 100644 --- a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/AnnotationParserExample.java +++ b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/AnnotationParserExample.java @@ -28,6 +28,7 @@ import cloud.commandframework.examples.bukkit.ExamplePlugin; import cloud.commandframework.examples.bukkit.annotations.feature.BuilderModifierExample; import cloud.commandframework.examples.bukkit.annotations.feature.CommandContainerExample; +import cloud.commandframework.examples.bukkit.annotations.feature.EitherExample; import cloud.commandframework.examples.bukkit.annotations.feature.EnumExample; import cloud.commandframework.examples.bukkit.annotations.feature.FlagExample; import cloud.commandframework.examples.bukkit.annotations.feature.HelpExample; @@ -53,6 +54,7 @@ public final class AnnotationParserExample { private static final List FEATURES = Arrays.asList( new BuilderModifierExample(), new CommandContainerExample(), + new EitherExample(), new EnumExample(), new FlagExample(), new HelpExample(), diff --git a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/feature/EitherExample.java b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/feature/EitherExample.java new file mode 100644 index 00000000..53fb2ce3 --- /dev/null +++ b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/feature/EitherExample.java @@ -0,0 +1,37 @@ +package cloud.commandframework.examples.bukkit.annotations.feature; + +import cloud.commandframework.annotations.AnnotationParser; +import cloud.commandframework.annotations.Command; +import cloud.commandframework.examples.bukkit.ExamplePlugin; +import cloud.commandframework.examples.bukkit.annotations.AnnotationFeature; +import cloud.commandframework.types.Either; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.UUID; + +import static net.kyori.adventure.text.Component.text; + +/** + * Example of a command accepting {@link Either}. + */ +public final class EitherExample implements AnnotationFeature { + + private BukkitAudiences bukkitAudiences; + + @Override + public void registerFeature(final @NonNull ExamplePlugin examplePlugin, final @NonNull AnnotationParser annotationParser) { + this.bukkitAudiences = examplePlugin.bukkitAudiences(); + annotationParser.parse(this); + } + + @Command("annotations either ") + public void eitherCommand(final @NonNull CommandSender sender, final @NonNull Either uuid) { + final UUID resolvedUuid = uuid.primary().orElseGet(() -> uuid.fallback().map(Player::getUniqueId).get()); + this.bukkitAudiences.sender(sender) + .sendMessage(text("The UUID is: ", NamedTextColor.DARK_GREEN).append(text(resolvedUuid.toString(), NamedTextColor.GREEN))); + } +} diff --git a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/builder/BuilderExample.java b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/builder/BuilderExample.java index 24028f84..3bca3476 100644 --- a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/builder/BuilderExample.java +++ b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/builder/BuilderExample.java @@ -31,6 +31,7 @@ import cloud.commandframework.examples.bukkit.builder.feature.AggregateCommandExample; import cloud.commandframework.examples.bukkit.builder.feature.CommandBeanExample; import cloud.commandframework.examples.bukkit.builder.feature.CompoundArgumentExample; +import cloud.commandframework.examples.bukkit.builder.feature.EitherExample; import cloud.commandframework.examples.bukkit.builder.feature.EnumExample; import cloud.commandframework.examples.bukkit.builder.feature.FlagExample; import cloud.commandframework.examples.bukkit.builder.feature.HelpExample; @@ -59,6 +60,7 @@ public final class BuilderExample { new AggregateCommandExample(), new CommandBeanExample(), new CompoundArgumentExample(), + new EitherExample(), new EnumExample(), new FlagExample(), new HelpExample(), diff --git a/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/builder/feature/EitherExample.java b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/builder/feature/EitherExample.java new file mode 100644 index 00000000..8dd43552 --- /dev/null +++ b/examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/builder/feature/EitherExample.java @@ -0,0 +1,46 @@ +package cloud.commandframework.examples.bukkit.builder.feature; + +import cloud.commandframework.arguments.parser.ArgumentParser; +import cloud.commandframework.arguments.standard.UUIDParser; +import cloud.commandframework.bukkit.BukkitCommandManager; +import cloud.commandframework.bukkit.parser.PlayerParser; +import cloud.commandframework.examples.bukkit.ExamplePlugin; +import cloud.commandframework.examples.bukkit.builder.BuilderFeature; +import cloud.commandframework.keys.CloudKey; +import cloud.commandframework.types.Either; +import io.leangen.geantyref.TypeToken; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.UUID; + +import static net.kyori.adventure.text.Component.text; + +/** + * Example of a command accepting {@link Either}. + */ +public final class EitherExample implements BuilderFeature { + + private static final CloudKey> EITHER_KEY = CloudKey.of( + "uuid", + new TypeToken>() {} + ); + + @Override + public void registerFeature(final @NonNull ExamplePlugin examplePlugin, final @NonNull BukkitCommandManager manager) { + manager.command( + manager.commandBuilder("builder") + .literal("either") + .required(EITHER_KEY, ArgumentParser.firstOf(UUIDParser.uuidParser(), PlayerParser.playerParser())) + .handler(context -> { + final Either either = context.get(EITHER_KEY); + final UUID uuid = either.primary().orElseGet(() -> either.fallback().map(Player::getUniqueId).get()); + examplePlugin.bukkitAudiences() + .sender(context.sender()) + .sendMessage(text("The UUID is: ", NamedTextColor.DARK_GREEN).append(text(uuid.toString(), NamedTextColor.GREEN))); + }) + ); + } +}