Skip to content

Commit

Permalink
feat: configure cloud exception handling & translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Feb 1, 2024
1 parent b8d0603 commit 275c4e7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ coroutines = "1.7.3"

# Cloud
cloud = "2.0.0-SNAPSHOT"
cloudTranslations = "1.0.0-SNAPSHOT"

# External
# - Common
Expand All @@ -48,9 +49,13 @@ cloud-build-logic = { module = "org.incendo:cloud-build-logic", version.ref = "c
gradleKotlinJvm = { group = "org.jetbrains.kotlin.jvm", name = "org.jetbrains.kotlin.jvm.gradle.plugin", version.ref = "kotlin" }

# Cloud
# - Common
cloud-core = { group = "org.incendo", name = "cloud-core", version.ref = "cloud" }
cloud-annotations = { group = "org.incendo", name = "cloud-annotations", version.ref = "cloud" }
cloud-translations-core = { group = "org.incendo", name = "cloud-translations-core", version.ref = "cloudTranslations" }
# - Minecraft
cloud-paper = { group = "org.incendo", name = "cloud-paper", version.ref = "cloud" }
cloud-translations-bukkit = { group = "org.incendo", name = "cloud-translations-bukkit", version.ref = "cloudTranslations" }
cloud-minecraft-extras = { group = "org.incendo", name = "cloud-minecraft-extras", version.ref = "cloud" }

# External
Expand Down
1 change: 1 addition & 0 deletions kitchensink-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
api(libs.cloud.core)
api(libs.guice)
api(libs.jakarta.inject)
api(libs.cloud.translations.core)

compileOnlyApi(libs.slf4j)
compileOnlyApi(libs.adventure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@
//
package org.incendo.kitchensink.command;

import java.util.Locale;
import net.kyori.adventure.audience.Audience;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Sender of commands.
*/
public interface KitchenSinkCommandSender extends Audience {

/**
* Returns the command sender locale.
*
* @return the locale
*/
@NonNull Locale locale();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.Command;
import org.incendo.cloud.bean.CommandProperties;
import org.incendo.cloud.component.DefaultValue;
import org.incendo.cloud.context.CommandContext;
import org.incendo.kitchensink.command.KitchenSinkCommandBean;
import org.incendo.kitchensink.command.KitchenSinkCommandSender;
Expand All @@ -51,7 +50,7 @@ public final class PingCommand extends KitchenSinkCommandBean {
protected Command.@NonNull Builder<? extends KitchenSinkCommandSender> configureKitchenSinkCommand(
final Command.@NonNull Builder<KitchenSinkCommandSender> builder
) {
return builder.optional("text", greedyStringParser(), DefaultValue.constant(""));
return builder.required("text", greedyStringParser());
}

@Override
Expand Down
1 change: 1 addition & 0 deletions kitchensink-paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
implementation(projects.kitchensinkCommon)
implementation(libs.cloud.paper)
implementation(libs.cloud.minecraft.extras)
implementation(libs.cloud.translations.bukkit)

compileOnlyApi(libs.paper)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
package org.incendo.kitchensink.paper.command;

import java.util.List;
import java.util.Locale;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import org.bukkit.Bukkit;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.kitchensink.command.KitchenSinkCommandSender;
import org.jetbrains.annotations.NotNull;

Expand All @@ -39,4 +41,9 @@ final class PaperConsole implements KitchenSinkCommandSender, ForwardingAudience
public @NotNull Iterable<? extends Audience> audiences() {
return List.of(Bukkit.getConsoleSender());
}

@Override
public @NonNull Locale locale() {
return Locale.ENGLISH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.auto.factory.AutoFactory;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import net.kyori.adventure.audience.Audience;
Expand Down Expand Up @@ -72,6 +73,11 @@ public PaperPlayer(final @NonNull Player player) {
return List.of(this.player);
}

@Override
public @NonNull Locale locale() {
return this.player.locale();
}

/**
* Returns the Paper player that backs this instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import java.util.Objects;
import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.minecraft.extras.AudienceProvider;
import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler;
import org.incendo.cloud.paper.PaperCommandManager;
import org.incendo.cloud.translations.LocaleExtractor;
import org.incendo.cloud.translations.TranslationBundle;
import org.incendo.cloud.translations.bukkit.BukkitTranslationBundle;
import org.incendo.kitchensink.command.KitchenSinkCommandSender;
import org.incendo.kitchensink.paper.PaperKitchenSink;
import org.incendo.kitchensink.paper.command.KitchenSinkSenderMapper;

import static net.kyori.adventure.text.Component.text;

/**
* Module for Cloud bindings.
*/
Expand Down Expand Up @@ -73,6 +81,18 @@ public CommandManager<KitchenSinkCommandSender> commandManager(
commandManager.registerAsynchronousCompletions();
}

final LocaleExtractor<KitchenSinkCommandSender> localeExtractor = KitchenSinkCommandSender::locale;
commandManager.captionRegistry().registerProvider(TranslationBundle.core(localeExtractor));
commandManager.captionRegistry().registerProvider(BukkitTranslationBundle.bukkit(localeExtractor));

MinecraftExceptionHandler.<KitchenSinkCommandSender>create(AudienceProvider.nativeAudience())
.defaultHandlers()
// TOD(City): Style this :)
.decorator(component -> text("[KitchenSink]", NamedTextColor.DARK_AQUA)
.append(text(" ", NamedTextColor.WHITE))
.append(component)
).registerTo(commandManager);

return commandManager;
}
}

0 comments on commit 275c4e7

Please sign in to comment.