From 5d460e9f3a187665011413bc81fdcdfcc7db0b53 Mon Sep 17 00:00:00 2001 From: jmp Date: Thu, 18 Mar 2021 20:08:35 -0700 Subject: [PATCH] minecraft-extras: Add AudienceProvider.nativeAudience and MinecraftHelp.createNative convenience methods for sender types which are Audiences. --- .../minecraft/extras/AudienceProvider.java | 12 ++++++ .../minecraft/extras/MinecraftHelp.java | 27 ++++++++++-- .../extras/NativeAudienceProvider.java | 41 +++++++++++++++++++ .../velocity/ExampleVelocityPlugin.java | 12 ++++++ 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/NativeAudienceProvider.java diff --git a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/AudienceProvider.java b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/AudienceProvider.java index 8d43562c..249d1865 100644 --- a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/AudienceProvider.java +++ b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/AudienceProvider.java @@ -45,4 +45,16 @@ public interface AudienceProvider extends Function<@NonNull C, @NonNull Audie @Override @NonNull Audience apply(@NonNull C sender); + /** + * Get an audience provider for sender types which are already an {@link Audience}. + * + * @param sender type + * @return native audience provider + * @since 1.5.0 + */ + @SuppressWarnings("unchecked") + static AudienceProvider nativeAudience() { + return (AudienceProvider) NativeAudienceProvider.INSTANCE; + } + } diff --git a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftHelp.java b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftHelp.java index 5bdef435..08a7c660 100644 --- a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftHelp.java +++ b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftHelp.java @@ -103,8 +103,29 @@ public final class MinecraftHelp { private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH; private int maxResultsPerPage = DEFAULT_MAX_RESULTS_PER_PAGE; + /** - * Construct a new Minecraft help instance + * Construct a new Minecraft help instance for a sender type which is an {@link Audience}. + * + * @param commandPrefix Command that was used to trigger the help menu. Used to help insertion generation + * @param commandManager command manager + * @param sender type extending {@link Audience} + * @return new MinecraftHelp instance + * @since 1.5.0 + */ + public static MinecraftHelp createNative( + final @NonNull String commandPrefix, + final @NonNull CommandManager commandManager + ) { + return new MinecraftHelp<>( + commandPrefix, + AudienceProvider.nativeAudience(), + commandManager + ); + } + + /** + * Construct a new Minecraft help instance. * * @param commandPrefix Command that was used to trigger the help menu. Used to help insertion generation * @param audienceProvider Provider that maps the command sender type to {@link Audience} @@ -680,8 +701,8 @@ public final class MinecraftHelp { * Creates a component from a command sender, key, and arguments * * @param sender command sender - * @param key message key (constants in {@link MinecraftHelp} - * @param args args + * @param key message key (constants in {@link MinecraftHelp} + * @param args args * @return component */ @NonNull Component provide(@NonNull C sender, @NonNull String key, @NonNull String... args); diff --git a/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/NativeAudienceProvider.java b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/NativeAudienceProvider.java new file mode 100644 index 00000000..a19bf5bf --- /dev/null +++ b/cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/NativeAudienceProvider.java @@ -0,0 +1,41 @@ +// +// MIT License +// +// Copyright (c) 2021 Alexander Söderberg & Contributors +// +// 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.minecraft.extras; + +import net.kyori.adventure.audience.Audience; +import org.checkerframework.checker.nullness.qual.NonNull; + +final class NativeAudienceProvider implements AudienceProvider { + + static final NativeAudienceProvider INSTANCE = new NativeAudienceProvider<>(); + + private NativeAudienceProvider() { + } + + @Override + public @NonNull Audience apply(@NonNull final C sender) { + return sender; + } + +} diff --git a/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java b/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java index 1619faf8..3e29140e 100644 --- a/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java +++ b/examples/example-velocity/src/main/java/cloud/commandframework/examples/velocity/ExampleVelocityPlugin.java @@ -24,6 +24,8 @@ package cloud.commandframework.examples.velocity; import cloud.commandframework.execution.CommandExecutionCoordinator; +import cloud.commandframework.minecraft.extras.AudienceProvider; +import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler; import cloud.commandframework.velocity.CloudInjectionModule; import cloud.commandframework.velocity.VelocityCommandManager; import cloud.commandframework.velocity.arguments.PlayerArgument; @@ -41,6 +43,7 @@ import com.velocitypowered.api.proxy.server.RegisteredServer; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; import org.checkerframework.checker.nullness.qual.NonNull; import java.util.function.Function; @@ -74,6 +77,15 @@ public final class ExampleVelocityPlugin { Key.get(new TypeLiteral>() { }) ); + new MinecraftExceptionHandler() + .withDefaultHandlers() + .withDecorator(component -> Component.text() + .color(NamedTextColor.WHITE) + .append(Component.text('[')) + .append(Component.text("cloud-velocity-example", TextColor.color(0x1CBAE0))) + .append(Component.text(']')) + .build()) + .apply(commandManager, AudienceProvider.nativeAudience()); commandManager.command( commandManager.commandBuilder("example") .argument(PlayerArgument.of("player"))