Skip to content

Commit

Permalink
feat(example-bukkit): add Either examples (#16)
Browse files Browse the repository at this point in the history
* 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
Citymonstret and jpenilla authored Jan 22, 2024
1 parent 1ca59e3 commit d47a759
Show file tree
Hide file tree
Showing 4 changed files with 134 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,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)));
}
}
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,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)));
})
);
}
}

0 comments on commit d47a759

Please sign in to comment.