Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make translatables implement ComponentLike #6574

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion Towny/src/main/java/com/palmergames/bukkit/towny/Towny.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
Expand Down Expand Up @@ -153,7 +154,7 @@ public void onEnable() {
// NOTE: Runs regardless if Towny errors out!
// Important for safe mode.

adventure = BukkitAudiences.create(this);
adventure = BukkitAudiences.builder(this).componentRenderer(pointer -> pointer.getOrDefault(Identity.LOCALE, Translation.getDefaultLocale()), Translation.translationRenderer()).build();

// Check for plugins that we use or we develop,
// print helpful information to startup log.
Expand Down
331 changes: 199 additions & 132 deletions Towny/src/main/java/com/palmergames/bukkit/towny/TownyMessaging.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
Expand Down Expand Up @@ -2207,23 +2208,23 @@ private void adminToggleRegenerations(CommandSender sender, Optional<Boolean> ch
private void adminToggleDevMode(CommandSender sender, Optional<Boolean> choice) throws NoPermissionException {
checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_TOGGLE_DEVMODE.getNode());
TownySettings.setDevMode(choice.orElse(!TownySettings.isDevMode()));
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_devmode", (TownySettings.isDevMode() ? Translatable.literal(Colors.Green).append(Translatable.of("enabled")) : Translatable.literal(Colors.Red).append(Translatable.of("disabled"))))); }
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_devmode", (TownySettings.isDevMode() ? Component.translatable("enabled", NamedTextColor.DARK_GREEN) : Component.translatable("disabled", NamedTextColor.DARK_RED)))); }

private void adminToggleDebug(CommandSender sender, Optional<Boolean> choice) throws NoPermissionException {
checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_TOGGLE_DEBUG.getNode());
TownySettings.setDebug(choice.orElse(!TownySettings.getDebug()));
TownyLogger.getInstance().refreshDebugLogger();
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_debugmode", (TownySettings.getDebug() ? Translatable.literal(Colors.Green).append(Translatable.of("enabled")) : Translatable.literal(Colors.Red).append(Translatable.of("disabled"))))); }
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_debugmode", (TownySettings.getDebug() ? Component.translatable("enabled", NamedTextColor.DARK_GREEN) : Component.translatable("disabled", NamedTextColor.DARK_RED)))); }

private void adminToggleTownWithDraw(CommandSender sender, Optional<Boolean> choice) throws NoPermissionException {
checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_TOGGLE_TOWNWITHDRAW.getNode());
TownySettings.SetTownBankAllowWithdrawls(choice.orElse(!TownySettings.getTownBankAllowWithdrawls()));
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_townwithdraw", (TownySettings.getTownBankAllowWithdrawls() ? Translatable.literal(Colors.Green).append(Translatable.of("enabled")) : Translatable.literal(Colors.Red).append(Translatable.of("disabled"))))); }
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_townwithdraw", (TownySettings.getTownBankAllowWithdrawls() ? Component.translatable("enabled", NamedTextColor.DARK_GREEN) : Component.translatable("disabled", NamedTextColor.DARK_RED)))); }

private void adminToggleNationWithdraw(CommandSender sender, Optional<Boolean> choice) throws NoPermissionException {
checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_TOGGLE_NATIONWITHDRAW.getNode());
TownySettings.SetNationBankAllowWithdrawls(choice.orElse(!TownySettings.getNationBankAllowWithdrawls()));
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_nationwithdraw", (TownySettings.getNationBankAllowWithdrawls() ? Translatable.literal(Colors.Green).append(Translatable.of("enabled")) : Translatable.literal(Colors.Red).append(Translatable.of("disabled"))))); }
TownyMessaging.sendMsg(sender, Translatable.of("msg_admin_toggle_nationwithdraw", (TownySettings.getNationBankAllowWithdrawls() ? Component.translatable("enabled", NamedTextColor.DARK_GREEN) : Component.translatable("disabled", NamedTextColor.DARK_RED)))); }

private void parseAdminToggleNPC(CommandSender sender, String[] split) throws TownyException {
checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_TOGGLE_NPC.getNode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.palmergames.bukkit.towny.event.CancellableTownyEvent;
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.utils.TownyComponents;
import net.kyori.adventure.text.Component;

/**
* An object which stores information about confirmations. While this
Expand All @@ -14,13 +16,13 @@ public class Confirmation {

private final Runnable acceptHandler;
private final Runnable cancelHandler;
private final Translatable title;
private final Component title;
private final int duration;
private final ConfirmationTransaction transaction;
private final String confirmCommand;
private final String cancelCommand;
private final boolean isAsync;
private String pluginPrefix;
private final String pluginPrefix;
private final CancellableTownyEvent event;

/**
Expand Down Expand Up @@ -91,9 +93,21 @@ public Runnable getCancelHandler() {
* Gets the title of the confirmation message.
*
* @return The title of the confirmation message.
* @deprecated Deprecated as of todo insert version, Confirmation titles can now be any component
* @see #title()
*/
@Deprecated
public Translatable getTitle() {
return title;
return Translatable.literal(TownyComponents.plain(this.title));
}

/**
* Gets the title of the confirmation message.
*
* @return The title of the confirmation message.
*/
public Component title() {
return this.title;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.palmergames.bukkit.towny.confirmations;

import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.utils.TownyComponents;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import org.bukkit.command.CommandSender;

import com.palmergames.bukkit.towny.TownySettings;
Expand All @@ -12,7 +15,7 @@
public class ConfirmationBuilder {
Runnable acceptHandler;
Runnable cancelHandler;
Translatable title = Translatable.of("are_you_sure_you_want_to_continue");
Component title = Component.translatable("are_you_sure_you_want_to_continue");
String confirmCommand = TownySettings.getConfirmCommand();
String cancelCommand = TownySettings.getCancelCommand();
String pluginPrefix = "towny";
Expand All @@ -34,20 +37,29 @@ public ConfirmationBuilder runOnCancel(Runnable cancelHandler) {

/**
* Sets the title of the confirmation to be sent.
* The input string will be converted to a component using minimessage serialization.
*
* @param title The title of the confirmation.
* @return A builder reference of this object.
*/
public ConfirmationBuilder setTitle(String title) {
this.title = Translatable.literal(title);
this.title = TownyComponents.miniMessage(title);
return this;
}

public ConfirmationBuilder setTitle(Translatable title) {
this.title = title;
public ConfirmationBuilder setTitle(ComponentLike title) {
this.title = title.asComponent();
return this;
}

/**
* TODO insert version
*/
@SuppressWarnings("unused")
private ConfirmationBuilder setTitle$$bridge$$public(Translatable title) {
return setTitle(title);
}

/**
* Sets the duration the confirmation will run for.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public String getMessage() {
return (String) message;
}

// TODO can probably be exposed as a component to avoid instantly translating
public String getMessage(CommandSender sender) {
if (message instanceof Translatable)
return ((Translatable) message).translate(Translation.getLocale(sender));
if (message instanceof Translatable translatable)
return translatable.translate(Translation.getLocale(sender));
else
return (String) message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void sendSafeModeMessage(Player player) {
Translatable tipMsg = player.isOp() || player.hasPermission("towny.admin")
? Translatable.of("msg_safe_mode_admin")
: Translatable.of("msg_safe_mode_player");
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_safe_mode_base"), tipMsg);
TownyMessaging.sendErrorMsg(player, Translatable.of("msg_safe_mode_base").append(" ").append(tipMsg));
} catch (Exception e) {
// Safemode is affecting Towny's ability to use Translatables.
String msg = player.isOp() || player.hasPermission("towny.admin")
Expand Down
Loading