-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example-bukkit): add
Either
examples
Depends on Incendo/cloud#647
- Loading branch information
1 parent
fb9104e
commit 653dcfd
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...c/main/java/cloud/commandframework/examples/bukkit/annotations/feature/EitherExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...t/src/main/java/cloud/commandframework/examples/bukkit/builder/feature/EitherExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
}) | ||
); | ||
} | ||
} |