diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java index 2f2285fa..3e2e0b0f 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/HMCCosmeticsPlugin.java @@ -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(); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/gui/Menu.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/gui/Menu.java index 9e2b184e..64790b05 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/gui/Menu.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/gui/Menu.java @@ -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)); } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/PAPIHook.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/PAPIHook.java index 2eac2a4b..051659c5 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/PAPIHook.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/PAPIHook.java @@ -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; + } } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java index 73b63cae..d7e2dc08 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java @@ -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); } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/TranslationUtil.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/TranslationUtil.java new file mode 100644 index 00000000..8faa7e06 --- /dev/null +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/TranslationUtil.java @@ -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 keys = new HashMap<>(); + + public static void setup(ConfigurationNode config) { + // TODO: Finish this + /* + for (ConfigurationNode node : config.childrenMap().values()) { + HashMap 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 config = keys.get(key); + if (config.getFirst() == message) { + return config.getSecond().toString(); + } + return message; + */ + } +} diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java index 9877d93b..20bcb307 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java @@ -80,23 +80,6 @@ public class ItemBuilder { return this; } - /** - * Sets placeholders to the item's name - * - * @param placeholders placeholders - */ - - public ItemBuilder namePlaceholders(final Map 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 placeholders) { - if (this.itemMeta == null) { - return this; - } - final List lore = new ArrayList<>(); - - final List 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); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Placeholder.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Placeholder.java index 7f2afafd..18d443f8 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Placeholder.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Placeholder.java @@ -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 placeholders) { - for (final Map.Entry 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) { /* diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Translation.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Translation.java deleted file mode 100644 index 4e7e0712..00000000 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Translation.java +++ /dev/null @@ -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 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)); - } - } -} diff --git a/common/src/main/resources/menus/examplemenu.yml b/common/src/main/resources/menus/examplemenu.yml index c2a9590a..bb4c3606 100644 --- a/common/src/main/resources/menus/examplemenu.yml +++ b/common/src/main/resources/menus/examplemenu.yml @@ -9,8 +9,8 @@ items: amount: 1 lore: - "" - - "Enabled: <#6D9DC5>%enabled%" - - "Allowed: <#6D9DC5>%allowed%" + - "Enabled: <#6D9DC5>%HMCCosmetics_equipped_niftyhat%" + - "Allowed: <#6D9DC5>%HMCCosmetics_unlocked_niftyhat%" type: cosmetic cosmetic: niftyhat actions: diff --git a/common/src/main/resources/translations.yml b/common/src/main/resources/translations.yml index 3e6acef5..0ee32b3a 100644 --- a/common/src/main/resources/translations.yml +++ b/common/src/main/resources/translations.yml @@ -1,3 +1,3 @@ -translations: - true: "true" +unlockedCosmetic: + true: "True" false: "false" \ No newline at end of file