mirror of
https://github.com/Flowsqy/ShopChest.git
synced 2024-11-26 01:05:12 +01:00
Add LocalizedItemManager
This commit is contained in:
parent
4e1a336661
commit
345bc8a574
@ -43,7 +43,8 @@ public class LanguageLoader {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
final Map<String, String> storedItems = languageConfigurationLoader.getTranslations(itemsFile, logger);
|
||||
return new LanguageManager(messageRegistry);
|
||||
final LocalizedItemManager localizedItemManager = new LocalizedItemManager(storedItems);
|
||||
return new LanguageManager(messageRegistry, localizedItemManager);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<String, String> itemTranslations;
|
||||
|
||||
public LocalizedItemManager(@NotNull Map<String, String> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user