mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-28 03:19:15 +00:00
Translation work + More Placeholders + PAPI in menu item lore
This commit is contained in:
@@ -19,7 +19,7 @@ import com.hibiscusmc.hmccosmetics.listener.PlayerConnectionListener;
|
||||
import com.hibiscusmc.hmccosmetics.listener.PlayerGameListener;
|
||||
import com.hibiscusmc.hmccosmetics.nms.NMSHandlers;
|
||||
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
|
||||
import com.hibiscusmc.hmccosmetics.util.misc.Translation;
|
||||
import com.hibiscusmc.hmccosmetics.util.TranslationUtil;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -150,7 +150,17 @@ public final class HMCCosmeticsPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
// Translation setup
|
||||
Translation.setup();
|
||||
final File translationFile = Path.of(getInstance().getDataFolder().getPath(), "translations.yml").toFile();
|
||||
final YamlConfigurationLoader translationLoader = YamlConfigurationLoader.
|
||||
builder().
|
||||
path(translationFile.toPath())
|
||||
.nodeStyle(NodeStyle.BLOCK)
|
||||
.build();
|
||||
try {
|
||||
TranslationUtil.setup(translationLoader.load());
|
||||
} catch (ConfigurateException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// ItemHooks
|
||||
ItemHooks.setup();
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.hibiscusmc.hmccosmetics.util.misc.Placeholder;
|
||||
import dev.triumphteam.gui.builder.item.ItemBuilder;
|
||||
import dev.triumphteam.gui.guis.Gui;
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -128,8 +129,7 @@ public class Menu {
|
||||
|
||||
if (item.getItemMeta().hasLore()) {
|
||||
for (String loreLine : item.getItemMeta().getLore()) {
|
||||
processedLore.add(loreLine.replaceAll("%allowed%", "allowed?"));
|
||||
// TODO apply placeholders here
|
||||
processedLore.add(PlaceholderAPI.setPlaceholders(player, loreLine));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticArmorType;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticMainhandType;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||
import com.hibiscusmc.hmccosmetics.util.TranslationUtil;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -18,6 +19,12 @@ import java.util.List;
|
||||
|
||||
public class PAPIHook extends PlaceholderExpansion {
|
||||
|
||||
private static boolean papiEnabled = false;
|
||||
|
||||
public PAPIHook() {
|
||||
papiEnabled = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return "HMCCosmetics";
|
||||
@@ -80,6 +87,29 @@ public class PAPIHook extends PlaceholderExpansion {
|
||||
}
|
||||
}
|
||||
}
|
||||
case "unlocked":
|
||||
if (placeholderArgs == null) {
|
||||
return null;
|
||||
}
|
||||
if (placeholderArgs.get(1) != null) {
|
||||
Cosmetic cosmetic = Cosmetics.getCosmetic(placeholderArgs.get(1));
|
||||
if (cosmetic == null) return "INVALID_COSMETIC";
|
||||
return TranslationUtil.getTranslation("unlockedCosmetic", String.valueOf(user.canEquipCosmetic(cosmetic)));
|
||||
}
|
||||
case "equipped":
|
||||
if (placeholderArgs == null) {
|
||||
return null;
|
||||
}
|
||||
if (placeholderArgs.get(1) != null) {
|
||||
Cosmetic cosmetic = Cosmetics.getCosmetic(placeholderArgs.get(1));
|
||||
if (cosmetic == null) return "INVALID_COSMETIC";
|
||||
if (user.getCosmetic(cosmetic.getSlot()) == null) return "false";
|
||||
if (cosmetic.getId() == user.getCosmetic(cosmetic.getSlot()).getId()) {
|
||||
return "true";
|
||||
} else {
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
case "wardrobe-enabled":
|
||||
return String.valueOf(user.isInWardrobe());
|
||||
}
|
||||
@@ -129,18 +159,17 @@ public class PAPIHook extends PlaceholderExpansion {
|
||||
}
|
||||
|
||||
public String getItemLore(Cosmetic cosmetic) {
|
||||
if (cosmetic instanceof CosmeticArmorType) {
|
||||
ItemStack item = ((CosmeticArmorType) cosmetic).getItem();
|
||||
if (item.hasItemMeta()) {
|
||||
return String.valueOf(item.getItemMeta().getLore());
|
||||
}
|
||||
}
|
||||
if (cosmetic instanceof CosmeticMainhandType) {
|
||||
ItemStack item = ((CosmeticMainhandType) cosmetic).getItem();
|
||||
if (cosmetic instanceof CosmeticArmorType || cosmetic instanceof CosmeticMainhandType) {
|
||||
ItemStack item = cosmetic.getItem();
|
||||
if (item == null) return null;
|
||||
if (item.hasItemMeta()) {
|
||||
return String.valueOf(item.getItemMeta().getLore());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isPAPIEnabled() {
|
||||
return papiEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
/*
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ items:
|
||||
amount: 1
|
||||
lore:
|
||||
- "<WHITE>"
|
||||
- "<gray>Enabled: <#6D9DC5>%enabled%"
|
||||
- "<gray>Allowed: <#6D9DC5>%allowed%"
|
||||
- "<gray>Enabled: <#6D9DC5>%HMCCosmetics_equipped_niftyhat%"
|
||||
- "<gray>Allowed: <#6D9DC5>%HMCCosmetics_unlocked_niftyhat%"
|
||||
type: cosmetic
|
||||
cosmetic: niftyhat
|
||||
actions:
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
translations:
|
||||
true: "true"
|
||||
unlockedCosmetic:
|
||||
true: "True"
|
||||
false: "false"
|
||||
Reference in New Issue
Block a user