Skip to content

Commit

Permalink
Add Placeholder support to translation messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed Nov 25, 2023
1 parent f883f7d commit b8cf8ca
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 4 deletions.
34 changes: 34 additions & 0 deletions Bukkit/src/net/tnemc/bukkit/hook/misc/PAPIParser.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
}
}
4 changes: 4 additions & 0 deletions Bukkit/src/net/tnemc/bukkit/impl/BukkitPlayerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public UUID identifier() {
return player.getUniqueId();
}

public OfflinePlayer getPlayer() {
return player;
}

/**
* Used to get the name of this player.
*
Expand Down
20 changes: 20 additions & 0 deletions Bukkit/src/net/tnemc/bukkit/impl/BukkitServerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PlayerProvider> 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.
*
Expand Down
14 changes: 14 additions & 0 deletions Core/src/net/tnemc/core/command/MoneyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -97,13 +98,15 @@ 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;
}

if(!player.get().hasPermission("tne.money.convert.from." + fromCurrency.getIdentifier())) {
final MessageData data = new MessageData("Messages.Account.BlockedAction");
data.addReplacement("$action", "convert from");
data.addReplacement("$currency", fromCurrency.getDisplay());
sender.message(data);
return;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
8 changes: 8 additions & 0 deletions Core/src/net/tnemc/core/compatibility/ServerConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion Core/src/net/tnemc/core/io/message/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"))));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ public Optional<Account> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,23 @@ public Collection<HoldingsEntry> 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)) {
Expand Down
21 changes: 21 additions & 0 deletions Folia/src/net/tnemc/folia/impl/FoliaServerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PlayerProvider> 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.
*
Expand Down
34 changes: 34 additions & 0 deletions PaperCore/src/net/tnemc/paper/hook/misc/PAPIParser.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
}
}
4 changes: 4 additions & 0 deletions PaperCore/src/net/tnemc/paper/impl/PaperPlayerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public UUID identifier() {
return player.getUniqueId();
}

public OfflinePlayer getPlayer() {
return player;
}

/**
* Used to get the name of this player.
*
Expand Down
20 changes: 20 additions & 0 deletions PaperCore/src/net/tnemc/paper/impl/PaperServerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<PlayerProvider> 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.
*
Expand Down
Loading

0 comments on commit b8cf8ca

Please sign in to comment.