minecraft-extras: Add AudienceProvider.nativeAudience and MinecraftHelp.createNative convenience methods for sender types which are Audiences.

This commit is contained in:
jmp 2021-03-18 20:08:35 -07:00 committed by Jason
parent c78c4aba08
commit 5d460e9f3a
4 changed files with 89 additions and 3 deletions

View file

@ -45,4 +45,16 @@ public interface AudienceProvider<C> extends Function<@NonNull C, @NonNull Audie
@Override @Override
@NonNull Audience apply(@NonNull C sender); @NonNull Audience apply(@NonNull C sender);
/**
* Get an audience provider for sender types which are already an {@link Audience}.
*
* @param <C> sender type
* @return native audience provider
* @since 1.5.0
*/
@SuppressWarnings("unchecked")
static <C extends Audience> AudienceProvider<C> nativeAudience() {
return (AudienceProvider<C>) NativeAudienceProvider.INSTANCE;
}
} }

View file

@ -103,8 +103,29 @@ public final class MinecraftHelp<C> {
private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH; private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH;
private int maxResultsPerPage = DEFAULT_MAX_RESULTS_PER_PAGE; 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 <C> sender type extending {@link Audience}
* @return new MinecraftHelp instance
* @since 1.5.0
*/
public static <C extends Audience> MinecraftHelp<C> createNative(
final @NonNull String commandPrefix,
final @NonNull CommandManager<C> 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 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} * @param audienceProvider Provider that maps the command sender type to {@link Audience}
@ -680,8 +701,8 @@ public final class MinecraftHelp<C> {
* Creates a component from a command sender, key, and arguments * Creates a component from a command sender, key, and arguments
* *
* @param sender command sender * @param sender command sender
* @param key message key (constants in {@link MinecraftHelp} * @param key message key (constants in {@link MinecraftHelp}
* @param args args * @param args args
* @return component * @return component
*/ */
@NonNull Component provide(@NonNull C sender, @NonNull String key, @NonNull String... args); @NonNull Component provide(@NonNull C sender, @NonNull String key, @NonNull String... args);

View file

@ -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<C extends Audience> implements AudienceProvider<C> {
static final NativeAudienceProvider<?> INSTANCE = new NativeAudienceProvider<>();
private NativeAudienceProvider() {
}
@Override
public @NonNull Audience apply(@NonNull final C sender) {
return sender;
}
}

View file

@ -24,6 +24,8 @@
package cloud.commandframework.examples.velocity; package cloud.commandframework.examples.velocity;
import cloud.commandframework.execution.CommandExecutionCoordinator; 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.CloudInjectionModule;
import cloud.commandframework.velocity.VelocityCommandManager; import cloud.commandframework.velocity.VelocityCommandManager;
import cloud.commandframework.velocity.arguments.PlayerArgument; 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.identity.Identity;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.function.Function; import java.util.function.Function;
@ -74,6 +77,15 @@ public final class ExampleVelocityPlugin {
Key.get(new TypeLiteral<VelocityCommandManager<CommandSource>>() { Key.get(new TypeLiteral<VelocityCommandManager<CommandSource>>() {
}) })
); );
new MinecraftExceptionHandler<CommandSource>()
.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.command(
commandManager.commandBuilder("example") commandManager.commandBuilder("example")
.argument(PlayerArgument.of("player")) .argument(PlayerArgument.of("player"))