9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-30 20:39:13 +00:00

Translation work + More Placeholders + PAPI in menu item lore

This commit is contained in:
LoJoSho
2022-12-31 10:50:27 -06:00
parent a3b3cf8e9f
commit edb5d2f16d
10 changed files with 126 additions and 127 deletions

View File

@@ -2,8 +2,10 @@ package com.hibiscusmc.hmccosmetics.util;
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
import com.hibiscusmc.hmccosmetics.config.Settings;
import com.hibiscusmc.hmccosmetics.hooks.PAPIHook;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.util.misc.Adventure;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
@@ -34,47 +36,63 @@ public class MessagesUtil {
}
public static void sendMessage(Player player, String key) {
Component finalMessage = processString(key);
Component finalMessage = processString(player, key);
Audience target = BukkitAudiences.create(HMCCosmeticsPlugin.getInstance()).player(player);
target.sendMessage(finalMessage);
}
public static void sendMessage(CommandSender sender, String key) {
Component finalMessage = processString(key);
Component finalMessage = processString(null, key);
Audience target = BukkitAudiences.create(HMCCosmeticsPlugin.getInstance()).sender(sender);
target.sendMessage(finalMessage);
}
public static void sendMessage(Player player, String key, TagResolver placeholder) {
if (!messages.containsKey(key)) return;
if (messages.get(key) == null) return;
String message = messages.get(key);
message = message.replaceAll("%prefix%", prefix);
Component finalMessage = Adventure.MINI_MESSAGE.deserialize(message, placeholder);
Component finalMessage = processString(player, key, placeholder);
Audience target = BukkitAudiences.create(HMCCosmeticsPlugin.getInstance()).player(player);
target.sendMessage(finalMessage);
}
public static void sendActionBar(Player player, String key) {
Component finalMessage = processString(key);
Component finalMessage = processString(player, key);
Audience target = BukkitAudiences.create(HMCCosmeticsPlugin.getInstance()).player(player);
target.sendActionBar(finalMessage);
}
public static Component processString(String key) {
public static Component processString(Player player, String key) {
return processString(player, key, null);
}
public static Component processString(Player player, String key, TagResolver placeholders) {
if (!messages.containsKey(key)) return null;
if (messages.get(key) == null) return null;
String message = messages.get(key);
if (PAPIHook.isPAPIEnabled() && player != null) message = PlaceholderAPI.setPlaceholders(player, message);
message = message.replaceAll("%prefix%", prefix);
if (placeholders != null ) {
return Adventure.MINI_MESSAGE.deserialize(message, placeholders);
}
return Adventure.MINI_MESSAGE.deserialize(message);
}
public static Component processStringNoKey(String message) {
return processStringNoKey(null, message, null);
}
public static Component processStringNoKey(Player player, String message) {
return processStringNoKey(player, message, null);
}
public static Component processStringNoKey(Player player, String message, TagResolver placeholders) {
message = message.replaceAll("%prefix%", prefix);
if (PAPIHook.isPAPIEnabled() && player != null) message = PlaceholderAPI.setPlaceholders(player, message);
if (placeholders != null ) {
return Adventure.MINI_MESSAGE.deserialize(message, placeholders);
}
return Adventure.MINI_MESSAGE.deserialize(message);
}

View File

@@ -0,0 +1,43 @@
package com.hibiscusmc.hmccosmetics.util;
import com.google.common.collect.HashBiMap;
import kotlin.Pair;
import org.spongepowered.configurate.ConfigurationNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class TranslationUtil {
private static HashMap<String, String> keys = new HashMap<>();
public static void setup(ConfigurationNode config) {
// TODO: Finish this
/*
for (ConfigurationNode node : config.childrenMap().values()) {
HashMap<Pair> translableMessages = new HashMap<>();
for (ConfigurationNode translatableMessage : node.childrenMap().values()) {
translableMessages.put( new Pair<>(translatableMessage.key().toString(), translatableMessage.getString()))
MessagesUtil.sendDebugMessages("setupTranslation key:" + node.key().toString() + " | " + node);
}
keys.put(node.key().toString().toLowerCase(), HashMap);
}
*/
}
public static String getTranslation(String key, String message) {
// TODO: Finish this
return message;
/*
key = key.toLowerCase();
MessagesUtil.sendDebugMessages("getTranslation key:" + key + " | " + message);
if (!keys.containsKey(key)) return message;
List<Pair> config = keys.get(key);
if (config.getFirst() == message) {
return config.getSecond().toString();
}
return message;
*/
}
}

View File

@@ -80,23 +80,6 @@ public class ItemBuilder {
return this;
}
/**
* Sets placeholders to the item's name
*
* @param placeholders placeholders
*/
public ItemBuilder namePlaceholders(final Map<String, String> placeholders) {
if (this.itemMeta == null) {
return this;
}
final String name = Placeholder.
applyPlaceholders(this.itemMeta.getDisplayName(), placeholders);
this.itemMeta.setDisplayName(name);
return this;
}
/**
* @param lore ItemStack lore
* @return this
@@ -110,35 +93,6 @@ public class ItemBuilder {
return this;
}
/**
* Sets placeholders to the item's lore
*
* @param placeholders placeholders
*/
public ItemBuilder lorePlaceholders(final Map<String, String> placeholders) {
if (this.itemMeta == null) {
return this;
}
final List<String> lore = new ArrayList<>();
final List<String> previousLore = this.itemMeta.getLore();
if (previousLore == null) {
return this;
}
for (final String line : previousLore) {
lore.add(Placeholder.applyPlaceholders(
line, placeholders
));
}
this.itemMeta.setLore(lore);
return this;
}
public ItemBuilder papiPlaceholders(final Player player) {
this.lorePapiPlaceholders(player);
this.namePapiPlaceholders(player);

View File

@@ -1,5 +1,6 @@
package com.hibiscusmc.hmccosmetics.util.misc;
import com.hibiscusmc.hmccosmetics.util.TranslationUtil;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
@@ -18,18 +19,9 @@ public class Placeholder {
public static final String ID = "%id%";
/**
* @param message message being translated
* @param placeholders placeholders applied
* @return message with placeholders applied
*/
public static String applyPlaceholders(String message, final Map<String, String> placeholders) {
for (final Map.Entry<String, String> entry : placeholders.entrySet()) {
message = message.replace(entry.getKey(), Translation.translate(entry.getValue()));
}
return message;
}
public static String applyPapiPlaceholders(@Nullable final Player player,
final String message) {
/*

View File

@@ -1,47 +0,0 @@
package com.hibiscusmc.hmccosmetics.util.misc;
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class Translation {
public static final String TRUE = "true";
public static final String FALSE = "false";
public static final String NONE = "none";
private static final String FILE_NAME = "translations.yml";
private static final String TRANSLATION_PATH = "translations";
private static Map<String, String> translations;
public static String translate(final String key) {
return translations.getOrDefault(key, null);
}
public static void setup() {
final File file = new File(HMCCosmeticsPlugin.getInstance().getDataFolder(), FILE_NAME);
if (!file.exists()) {
HMCCosmeticsPlugin.getInstance().saveResource(FILE_NAME, false);
}
if (translations == null) {
translations = new HashMap<>();
}
final FileConfiguration config = YamlConfiguration.loadConfiguration(file);
final ConfigurationSection section = config.getConfigurationSection(TRANSLATION_PATH);
if (section == null) {
return;
}
for (final String key : section.getKeys(false)) {
translations.put(key, section.getString(key));
}
}
}