Skip to content

Commit

Permalink
feat(example-bukkit): add Either examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 19, 2024
1 parent fb9104e commit 653dcfd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,6 +54,7 @@ public final class AnnotationParserExample {
private static final List<AnnotationFeature> FEATURES = Arrays.asList(
new BuilderModifierExample(),
new CommandContainerExample(),
new EitherExample(),
new EnumExample(),
new FlagExample(),
new HelpExample(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommandSender> annotationParser) {
this.bukkitAudiences = examplePlugin.bukkitAudiences();
annotationParser.parse(this);
}

@Command("annotations either <uuid>")
public void eitherCommand(final @NonNull CommandSender sender, final @NonNull Either<UUID, Player> 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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +60,7 @@ public final class BuilderExample {
new AggregateCommandExample(),
new CommandBeanExample(),
new CompoundArgumentExample(),
new EitherExample(),
new EnumExample(),
new FlagExample(),
new HelpExample(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UUID, Player>> EITHER_KEY = CloudKey.of(
"uuid",
new TypeToken<Either<UUID, Player>>() {}
);

@Override
public void registerFeature(final @NonNull ExamplePlugin examplePlugin, final @NonNull BukkitCommandManager<CommandSender> manager) {
manager.command(
manager.commandBuilder("builder")
.literal("either")
.required(EITHER_KEY, ArgumentParser.firstOf(UUIDParser.uuidParser(), PlayerParser.playerParser()))
.handler(context -> {
final Either<UUID, Player> 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)));
})
);
}
}

0 comments on commit 653dcfd

Please sign in to comment.