diff --git a/plugin/src/main/java/de/epiceric/shopchest/language/LanguageLoader.java b/plugin/src/main/java/de/epiceric/shopchest/language/LanguageLoader.java index 435df00..e564823 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/language/LanguageLoader.java +++ b/plugin/src/main/java/de/epiceric/shopchest/language/LanguageLoader.java @@ -43,7 +43,8 @@ public class LanguageLoader { throw new RuntimeException(e); } final Map storedItems = languageConfigurationLoader.getTranslations(itemsFile, logger); - return new LanguageManager(messageRegistry); + final LocalizedItemManager localizedItemManager = new LocalizedItemManager(storedItems); + return new LanguageManager(messageRegistry, localizedItemManager); } @NotNull diff --git a/plugin/src/main/java/de/epiceric/shopchest/language/LanguageManager.java b/plugin/src/main/java/de/epiceric/shopchest/language/LanguageManager.java index 7b5048b..ab51f5d 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/language/LanguageManager.java +++ b/plugin/src/main/java/de/epiceric/shopchest/language/LanguageManager.java @@ -5,9 +5,11 @@ import org.jetbrains.annotations.NotNull; public class LanguageManager { private final MessageRegistry messageRegistry; + private final LocalizedItemManager localizedItemManager; - public LanguageManager(@NotNull MessageRegistry messageRegistry) { + public LanguageManager(@NotNull MessageRegistry messageRegistry, @NotNull LocalizedItemManager localizedItemManager) { this.messageRegistry = messageRegistry; + this.localizedItemManager = localizedItemManager; } @NotNull @@ -15,4 +17,9 @@ public class LanguageManager { return messageRegistry; } + @NotNull + public LocalizedItemManager getLocalizedItemManager() { + return localizedItemManager; + } + } diff --git a/plugin/src/main/java/de/epiceric/shopchest/language/LocalizedItemManager.java b/plugin/src/main/java/de/epiceric/shopchest/language/LocalizedItemManager.java new file mode 100644 index 0000000..8341923 --- /dev/null +++ b/plugin/src/main/java/de/epiceric/shopchest/language/LocalizedItemManager.java @@ -0,0 +1,71 @@ +package de.epiceric.shopchest.language; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.SkullMeta; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Map; +import java.util.Objects; + +public class LocalizedItemManager { + + private final Map itemTranslations; + + public LocalizedItemManager(@NotNull Map itemTranslations) { + this.itemTranslations = itemTranslations; + } + + public String getItemName(@Nullable ItemStack stack) { + if (stack == null) { + return null; + } + + final ItemMeta meta; + if (!stack.hasItemMeta() || (meta = stack.getItemMeta()) == null) { + return getDefaultName(stack); + } + + final String displayName; + if (meta.hasDisplayName() && !(displayName = meta.getDisplayName()).isEmpty()) { + return displayName; + } + + if (meta instanceof BookMeta) { + return ((BookMeta) meta).getTitle(); + } + + if (meta instanceof SkullMeta) { + final SkullMeta skullMeta = (SkullMeta) meta; + if (!skullMeta.hasOwner()) { + return getDefaultName(stack); + } + skullMeta.getOwningPlayer(); + final String defaultName = getDefaultName(stack); + final String ownerName = Objects.requireNonNull(skullMeta.getOwningPlayer()).getName(); + if (ownerName == null) { + return defaultName; + } + return String.format(defaultName, ownerName); + } + + return getDefaultName(stack); + } + + @NotNull + private String getDefaultName(@NotNull ItemStack stack) { + return getCached(stack.getTranslationKey()); + } + + @NotNull + private String getCached(@NotNull String key) { + final String cachedTranslation = itemTranslations.get(key); + if (cachedTranslation == null) { + throw new RuntimeException("Could not get the translation for '" + key + "'. Report it to github"); + } + return cachedTranslation; + } + +} diff --git a/plugin/src/main/java/de/epiceric/shopchest/listeners/CreativeModeListener.java b/plugin/src/main/java/de/epiceric/shopchest/listeners/CreativeModeListener.java index 7f15e57..8c50f37 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/listeners/CreativeModeListener.java +++ b/plugin/src/main/java/de/epiceric/shopchest/listeners/CreativeModeListener.java @@ -1,8 +1,8 @@ package de.epiceric.shopchest.listeners; import de.epiceric.shopchest.ShopChest; -import de.epiceric.shopchest.language.Message; -import de.epiceric.shopchest.language.MessageRegistry; +import de.epiceric.shopchest.config.Placeholder; +import de.epiceric.shopchest.language.*; import de.epiceric.shopchest.utils.ClickType; import de.epiceric.shopchest.utils.ClickType.SelectClickType; import org.bukkit.Material; @@ -48,10 +48,11 @@ public class CreativeModeListener implements Listener { ((SelectClickType) clickType).setItem(e.getCursor()); p.closeInventory(); - final MessageRegistry messageRegistry = plugin.getLanguageManager().getMessageRegistry(); - p.sendMessage(messageRegistry.getMessage(Message.ITEM_SELECTED - // TODO Link it - //, new Replacement(Placeholder.ITEM_NAME, LanguageUtils.getItemName(e.getCursor())) + final LanguageManager languageManager = plugin.getLanguageManager(); + final MessageRegistry messageRegistry = languageManager.getMessageRegistry(); + final LocalizedItemManager localizedItemManager = languageManager.getLocalizedItemManager(); + p.sendMessage(messageRegistry.getMessage(Message.ITEM_SELECTED, + new Replacement(Placeholder.ITEM_NAME, localizedItemManager.getItemName(e.getCursor())) )); plugin.getShopCommand().createShopAfterSelected(p, (SelectClickType) clickType); } diff --git a/plugin/src/main/java/de/epiceric/shopchest/shop/ShopProduct.java b/plugin/src/main/java/de/epiceric/shopchest/shop/ShopProduct.java index fa3755a..fa25b99 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/shop/ShopProduct.java +++ b/plugin/src/main/java/de/epiceric/shopchest/shop/ShopProduct.java @@ -1,5 +1,6 @@ package de.epiceric.shopchest.shop; +import de.epiceric.shopchest.ShopChest; import org.bukkit.inventory.ItemStack; public class ShopProduct { @@ -21,8 +22,8 @@ public class ShopProduct { * @return The localized name of the product's {@link ItemStack} in the selected language file. */ public String getLocalizedName() { - // TODO Link it to the current language implementation - return "";//LanguageUtils.getItemName(getItemStack()); + return ShopChest.getInstance().getLanguageManager().getLocalizedItemManager().getItemName(getItemStack()); + //LanguageUtils.getItemName(getItemStack()); } /** diff --git a/pom.xml b/pom.xml index 9ccc2e4..6dfa3aa 100644 --- a/pom.xml +++ b/pom.xml @@ -123,7 +123,7 @@ org.spigotmc spigot-api - 1.19-R0.1-SNAPSHOT + 1.20.1-R0.1-SNAPSHOT provided