Add LocalizedItemManager

This commit is contained in:
Flowsqy 2023-07-20 01:24:03 +02:00
parent 4e1a336661
commit 345bc8a574
6 changed files with 92 additions and 11 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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());
}
/**

View File

@ -123,7 +123,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>