-
Notifications
You must be signed in to change notification settings - Fork 11
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 (#16)
* feat(example-bukkit): add `Either` examples Depends on Incendo/cloud#647 * spotless :) * Update either example --------- Co-authored-by: Jason Penilla <[email protected]>
- Loading branch information
1 parent
1ca59e3
commit d47a759
Showing
4 changed files
with
134 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
62 changes: 62 additions & 0 deletions
62
...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,62 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
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 java.util.UUID; | ||
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 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.primaryOrMapFallback(Player::getUniqueId); | ||
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
68 changes: 68 additions & 0 deletions
68
...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,68 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
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 java.util.UUID; | ||
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 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.primaryOrMapFallback(Player::getUniqueId); | ||
examplePlugin.bukkitAudiences() | ||
.sender(context.sender()) | ||
.sendMessage(text("The UUID is: ", NamedTextColor.DARK_GREEN).append(text(uuid.toString(), NamedTextColor.GREEN))); | ||
}) | ||
); | ||
} | ||
} |