From b8cf8caa35b596a6f01b62a07842fff03aabb5b3 Mon Sep 17 00:00:00 2001 From: creatorfromhell Date: Sat, 25 Nov 2023 17:46:23 -0500 Subject: [PATCH] Add Placeholder support to translation messages. --- .../tnemc/bukkit/hook/misc/PAPIParser.java | 34 +++++++++++++++++++ .../bukkit/impl/BukkitPlayerProvider.java | 4 +++ .../bukkit/impl/BukkitServerProvider.java | 20 +++++++++++ .../net/tnemc/core/command/MoneyCommand.java | 14 ++++++++ .../core/compatibility/ServerConnector.java | 8 +++++ .../tnemc/core/io/message/MessageHandler.java | 3 +- .../io/storage/datables/yaml/YAMLAccount.java | 7 ++++ .../storage/datables/yaml/YAMLHoldings.java | 7 ++-- .../tnemc/folia/impl/FoliaServerProvider.java | 21 ++++++++++++ .../net/tnemc/paper/hook/misc/PAPIParser.java | 34 +++++++++++++++++++ .../tnemc/paper/impl/PaperPlayerProvider.java | 4 +++ .../tnemc/paper/impl/PaperServerProvider.java | 20 +++++++++++ .../sponge/impl/SpongeServerProvider.java | 13 +++++++ .../sponge/impl/SpongeServerProvider.java | 13 +++++++ 14 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 Bukkit/src/net/tnemc/bukkit/hook/misc/PAPIParser.java create mode 100644 PaperCore/src/net/tnemc/paper/hook/misc/PAPIParser.java diff --git a/Bukkit/src/net/tnemc/bukkit/hook/misc/PAPIParser.java b/Bukkit/src/net/tnemc/bukkit/hook/misc/PAPIParser.java new file mode 100644 index 00000000..b89fa021 --- /dev/null +++ b/Bukkit/src/net/tnemc/bukkit/hook/misc/PAPIParser.java @@ -0,0 +1,34 @@ +package net.tnemc.bukkit.hook.misc; +/* + * The New Economy + * Copyright (C) 2022 - 2023 Daniel "creatorfromhell" Vidmar + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import me.clip.placeholderapi.PlaceholderAPI; +import net.tnemc.bukkit.impl.BukkitPlayerProvider; + +/** + * PAPIParser + * + * @author creatorfromhell + * @since 0.1.2.0 + */ +public class PAPIParser { + + public static String parse(final BukkitPlayerProvider provider, final String message) { + return PlaceholderAPI.setPlaceholders(provider.getPlayer(), message); + } +} \ No newline at end of file diff --git a/Bukkit/src/net/tnemc/bukkit/impl/BukkitPlayerProvider.java b/Bukkit/src/net/tnemc/bukkit/impl/BukkitPlayerProvider.java index c3cbb11c..cfc7960b 100644 --- a/Bukkit/src/net/tnemc/bukkit/impl/BukkitPlayerProvider.java +++ b/Bukkit/src/net/tnemc/bukkit/impl/BukkitPlayerProvider.java @@ -57,6 +57,10 @@ public UUID identifier() { return player.getUniqueId(); } + public OfflinePlayer getPlayer() { + return player; + } + /** * Used to get the name of this player. * diff --git a/Bukkit/src/net/tnemc/bukkit/impl/BukkitServerProvider.java b/Bukkit/src/net/tnemc/bukkit/impl/BukkitServerProvider.java index 016b2f41..b860e067 100644 --- a/Bukkit/src/net/tnemc/bukkit/impl/BukkitServerProvider.java +++ b/Bukkit/src/net/tnemc/bukkit/impl/BukkitServerProvider.java @@ -19,7 +19,9 @@ */ import net.tnemc.bukkit.TNE; +import net.tnemc.bukkit.hook.misc.PAPIParser; import net.tnemc.bukkit.impl.scheduler.BukkitScheduler; +import net.tnemc.core.TNECore; import net.tnemc.core.compatibility.CmdSource; import net.tnemc.core.compatibility.PlayerProvider; import net.tnemc.core.compatibility.ProxyProvider; @@ -68,6 +70,24 @@ public String name() { return "bukkit"; } + /** + * Used to replace placeholders from a string. + * + * @param player The player to use for the placeholder replacement. + * @param message The message to replace placeholders in. + * + * @return The string after placeholders have been replaced. + */ + @Override + public String replacePlaceholder(UUID player, String message) { + final Optional playerOpt = TNECore.server().findPlayer(player); + if(Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI") && playerOpt.isPresent() + && playerOpt.get() instanceof BukkitPlayerProvider bukkitPlayer) { + return PAPIParser.parse(bukkitPlayer, message); + } + return message; + } + /** * The proxy provider to use for this implementation. * diff --git a/Core/src/net/tnemc/core/command/MoneyCommand.java b/Core/src/net/tnemc/core/command/MoneyCommand.java index 7dd85a9c..74adcf21 100644 --- a/Core/src/net/tnemc/core/command/MoneyCommand.java +++ b/Core/src/net/tnemc/core/command/MoneyCommand.java @@ -74,6 +74,7 @@ public static void onBalance(CmdSource sender, Currency currency, String regi final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "balance check"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -97,6 +98,7 @@ public static void onConvert(CmdSource sender, PercentBigDecimal amount, Curr final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "convert to"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } @@ -104,6 +106,7 @@ public static void onConvert(CmdSource sender, PercentBigDecimal amount, Curr final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "convert from"); data.addReplacement("$currency", fromCurrency.getDisplay()); + sender.message(data); return; } } @@ -169,6 +172,7 @@ public static void onDeposit(CmdSource sender, PercentBigDecimal amount, Curr final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "deposit"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -224,6 +228,7 @@ public static void onGive(CmdSource sender, Account account, PercentBigDecima final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "give funds"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -280,6 +285,7 @@ public static void onNote(CmdSource sender, PercentBigDecimal amount, Currenc final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "note"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } @@ -330,6 +336,7 @@ public static void onOther(CmdSource sender, Account account, String region, final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "balance check other"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -377,6 +384,7 @@ public static void onPay(CmdSource sender, Account account, PercentBigDecimal final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "pay"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -478,6 +486,7 @@ public static void onRequest(CmdSource sender, Account account, BigDecimal am final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "request funds"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -515,6 +524,7 @@ public static void onSet(CmdSource sender, Account account, BigDecimal amount final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "set funds"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -553,6 +563,7 @@ public static void onSetAll(CmdSource sender, BigDecimal amount, String regio final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "set all funds"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -600,6 +611,7 @@ public static void onTake(CmdSource sender, Account account, PercentBigDecima final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "take funds"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -651,6 +663,7 @@ public static void onTop(CmdSource sender, Integer page, Currency currency, B final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "balance top"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } @@ -695,6 +708,7 @@ public static void onWithdraw(CmdSource sender, PercentBigDecimal amount, Cur final MessageData data = new MessageData("Messages.Account.BlockedAction"); data.addReplacement("$action", "withdraw funds"); data.addReplacement("$currency", currency.getDisplay()); + sender.message(data); return; } } diff --git a/Core/src/net/tnemc/core/compatibility/ServerConnector.java b/Core/src/net/tnemc/core/compatibility/ServerConnector.java index 83019f75..dcce9447 100644 --- a/Core/src/net/tnemc/core/compatibility/ServerConnector.java +++ b/Core/src/net/tnemc/core/compatibility/ServerConnector.java @@ -48,6 +48,14 @@ public interface ServerConnector { */ String name(); + /** + * Used to replace placeholders from a string. + * @param player The player to use for the placeholder replacement. + * @param message The message to replace placeholders in. + * @return The string after placeholders have been replaced. + */ + String replacePlaceholder(final UUID player, final String message); + /** * The proxy provider to use for this implementation. * @return The proxy provider to use for this implementation. diff --git a/Core/src/net/tnemc/core/io/message/MessageHandler.java b/Core/src/net/tnemc/core/io/message/MessageHandler.java index ce110b04..8dd1f13e 100644 --- a/Core/src/net/tnemc/core/io/message/MessageHandler.java +++ b/Core/src/net/tnemc/core/io/message/MessageHandler.java @@ -23,6 +23,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import net.tnemc.core.TNECore; import java.util.UUID; @@ -75,7 +76,7 @@ public static Component grab(final MessageData messageData, UUID id) { * @param audience The audience that should receive the translated message. */ public static void translate(final MessageData messageData, UUID identifier, Audience audience) { - audience.sendMessage(instance.mini.deserialize(instance.translator.translateNode(messageData, "default"))); + audience.sendMessage(instance.mini.deserialize(TNECore.server().replacePlaceholder(identifier, instance.translator.translateNode(messageData, "default")))); } /** diff --git a/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLAccount.java b/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLAccount.java index 0ef3f46b..7fbc0b7b 100644 --- a/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLAccount.java +++ b/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLAccount.java @@ -213,6 +213,13 @@ public Optional load(StorageConnector connector, File accFile, final Account account = null; + //Validate account file + if(!yaml.contains("Info.Name") || !yaml.contains("Info.Type")) { + + TNECore.log().error("Invalid account file. Account: " + identifier, DebugLevel.OFF); + return Optional.empty(); + } + final String type = yaml.getString("Info.Type"); //create our account from the type diff --git a/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLHoldings.java b/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLHoldings.java index 0e2ab1be..8e8e9a7e 100644 --- a/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLHoldings.java +++ b/Core/src/net/tnemc/core/io/storage/datables/yaml/YAMLHoldings.java @@ -181,22 +181,23 @@ public Collection loadAll(StorageConnector connector, @Nullabl final ConfigurationSection main = yaml.getConfigurationSection("Holdings"); for(final String server : main.getKeys(false)) { - if(!main.contains(server) || ! main.isConfigurationSection(server)) { + if(!main.contains(server) || !main.isConfigurationSection(server)) { continue; } for(final String region : main.getConfigurationSection(server).getKeys(false)) { - if(!main.contains(server + "." + region)) { + if(!main.contains(server + "." + region) || !main.isConfigurationSection(server + "." + region)) { continue; } + for(final String currency : main.getConfigurationSection(server + "." + region).getKeys(false)) { if(TNECore.eco().currency().findCurrency(currency).isEmpty()) { EconomyManager.invalidCurrencies().add(currency); } - if(!main.contains(server + "." + region + "." + currency)) { + if(!main.contains(server + "." + region + "." + currency) || !main.isConfigurationSection(server + "." + region + "." + currency)) { continue; } for(final String handler : main.getConfigurationSection(server + "." + region + "." + currency).getKeys(false)) { diff --git a/Folia/src/net/tnemc/folia/impl/FoliaServerProvider.java b/Folia/src/net/tnemc/folia/impl/FoliaServerProvider.java index 1e4e0f96..a9038f8a 100644 --- a/Folia/src/net/tnemc/folia/impl/FoliaServerProvider.java +++ b/Folia/src/net/tnemc/folia/impl/FoliaServerProvider.java @@ -19,7 +19,10 @@ */ import com.destroystokyo.paper.profile.PlayerProfile; +import net.tnemc.bukkit.hook.misc.PAPIParser; import net.tnemc.bukkit.impl.BukkitItemCalculations; +import net.tnemc.bukkit.impl.BukkitPlayerProvider; +import net.tnemc.core.TNECore; import net.tnemc.core.compatibility.CmdSource; import net.tnemc.core.compatibility.PlayerProvider; import net.tnemc.core.compatibility.ProxyProvider; @@ -69,6 +72,24 @@ public String name() { return "folia"; } + /** + * Used to replace placeholders from a string. + * + * @param player The player to use for the placeholder replacement. + * @param message The message to replace placeholders in. + * + * @return The string after placeholders have been replaced. + */ + @Override + public String replacePlaceholder(UUID player, String message) { + final Optional playerOpt = TNECore.server().findPlayer(player); + if(Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI") && playerOpt.isPresent() + && playerOpt.get() instanceof BukkitPlayerProvider bukkitPlayer) { + return PAPIParser.parse(bukkitPlayer, message); + } + return message; + } + /** * The proxy provider to use for this implementation. * diff --git a/PaperCore/src/net/tnemc/paper/hook/misc/PAPIParser.java b/PaperCore/src/net/tnemc/paper/hook/misc/PAPIParser.java new file mode 100644 index 00000000..9de21d47 --- /dev/null +++ b/PaperCore/src/net/tnemc/paper/hook/misc/PAPIParser.java @@ -0,0 +1,34 @@ +package net.tnemc.paper.hook.misc; +/* + * The New Economy + * Copyright (C) 2022 - 2023 Daniel "creatorfromhell" Vidmar + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import me.clip.placeholderapi.PlaceholderAPI; +import net.tnemc.paper.impl.PaperPlayerProvider; + +/** + * PAPIParser + * + * @author creatorfromhell + * @since 0.1.2.0 + */ +public class PAPIParser { + + public static String parse(final PaperPlayerProvider provider, final String message) { + return PlaceholderAPI.setPlaceholders(provider.getPlayer(), message); + } +} \ No newline at end of file diff --git a/PaperCore/src/net/tnemc/paper/impl/PaperPlayerProvider.java b/PaperCore/src/net/tnemc/paper/impl/PaperPlayerProvider.java index 14800af3..357bdd98 100644 --- a/PaperCore/src/net/tnemc/paper/impl/PaperPlayerProvider.java +++ b/PaperCore/src/net/tnemc/paper/impl/PaperPlayerProvider.java @@ -56,6 +56,10 @@ public UUID identifier() { return player.getUniqueId(); } + public OfflinePlayer getPlayer() { + return player; + } + /** * Used to get the name of this player. * diff --git a/PaperCore/src/net/tnemc/paper/impl/PaperServerProvider.java b/PaperCore/src/net/tnemc/paper/impl/PaperServerProvider.java index 2f8651d1..0e200175 100644 --- a/PaperCore/src/net/tnemc/paper/impl/PaperServerProvider.java +++ b/PaperCore/src/net/tnemc/paper/impl/PaperServerProvider.java @@ -18,6 +18,7 @@ */ import com.destroystokyo.paper.profile.PlayerProfile; +import net.tnemc.core.TNECore; import net.tnemc.core.compatibility.CmdSource; import net.tnemc.core.compatibility.PlayerProvider; import net.tnemc.core.compatibility.ProxyProvider; @@ -30,6 +31,7 @@ import net.tnemc.item.bukkit.BukkitCalculationsProvider; import net.tnemc.item.bukkit.BukkitItemStack; import net.tnemc.paper.TNE; +import net.tnemc.paper.hook.misc.PAPIParser; import net.tnemc.paper.impl.scheduler.PaperScheduler; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -67,6 +69,24 @@ public String name() { return "paper"; } + /** + * Used to replace placeholders from a string. + * + * @param player The player to use for the placeholder replacement. + * @param message The message to replace placeholders in. + * + * @return The string after placeholders have been replaced. + */ + @Override + public String replacePlaceholder(UUID player, String message) { + final Optional playerOpt = TNECore.server().findPlayer(player); + if(Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI") && playerOpt.isPresent() + && playerOpt.get() instanceof PaperPlayerProvider paperPlayer) { + return PAPIParser.parse(paperPlayer, message); + } + return message; + } + /** * The proxy provider to use for this implementation. * diff --git a/Sponge/src/net/tnemc/sponge/impl/SpongeServerProvider.java b/Sponge/src/net/tnemc/sponge/impl/SpongeServerProvider.java index 76347f4a..e9f989f6 100644 --- a/Sponge/src/net/tnemc/sponge/impl/SpongeServerProvider.java +++ b/Sponge/src/net/tnemc/sponge/impl/SpongeServerProvider.java @@ -63,6 +63,19 @@ public String name() { return "sponge"; } + /** + * Used to replace placeholders from a string. + * + * @param player The player to use for the placeholder replacement. + * @param message The message to replace placeholders in. + * + * @return The string after placeholders have been replaced. + */ + @Override + public String replacePlaceholder(UUID player, String message) { + return message; + } + /** * The proxy provider to use for this implementation. * diff --git a/Sponge8/src/net/tnemc/sponge/impl/SpongeServerProvider.java b/Sponge8/src/net/tnemc/sponge/impl/SpongeServerProvider.java index f0c77236..40f0c10c 100644 --- a/Sponge8/src/net/tnemc/sponge/impl/SpongeServerProvider.java +++ b/Sponge8/src/net/tnemc/sponge/impl/SpongeServerProvider.java @@ -74,6 +74,19 @@ public String name() { return "sponge"; } + /** + * Used to replace placeholders from a string. + * + * @param player The player to use for the placeholder replacement. + * @param message The message to replace placeholders in. + * + * @return The string after placeholders have been replaced. + */ + @Override + public String replacePlaceholder(UUID player, String message) { + return message; + } + /** * The proxy provider to use for this implementation. *