Add ItemNameManager

This commit is contained in:
Flowsqy 2023-08-08 14:20:06 +02:00
parent 6efb778868
commit 8ad01dbe35
6 changed files with 33 additions and 15 deletions

View File

@ -3,6 +3,8 @@ package de.epiceric.shopchest.language;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.config.FileLoader; import de.epiceric.shopchest.config.FileLoader;
import de.epiceric.shopchest.config.LanguageConfigurationLoader; import de.epiceric.shopchest.config.LanguageConfigurationLoader;
import de.epiceric.shopchest.language.item.ItemNameManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
@ -45,7 +47,7 @@ public class LanguageLoader {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
final Map<String, String> storedItems = languageConfigurationLoader.getTranslations(itemsFile, logger); final Map<String, String> storedItems = languageConfigurationLoader.getTranslations(itemsFile, logger);
final LocalizedItemManager localizedItemManager = new LocalizedItemManager(storedItems); final ItemNameManager localizedItemManager = new ItemNameManager(storedItems);
return new LanguageManager(messageRegistry, localizedItemManager); return new LanguageManager(messageRegistry, localizedItemManager);
} }

View File

@ -2,14 +2,16 @@ package de.epiceric.shopchest.language;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import de.epiceric.shopchest.language.item.ItemNameManager;
public class LanguageManager { public class LanguageManager {
private final MessageRegistry messageRegistry; private final MessageRegistry messageRegistry;
private final LocalizedItemManager localizedItemManager; private final ItemNameManager itemNameManager;
public LanguageManager(@NotNull MessageRegistry messageRegistry, @NotNull LocalizedItemManager localizedItemManager) { public LanguageManager(@NotNull MessageRegistry messageRegistry, @NotNull ItemNameManager localizedItemManager) {
this.messageRegistry = messageRegistry; this.messageRegistry = messageRegistry;
this.localizedItemManager = localizedItemManager; this.itemNameManager = localizedItemManager;
} }
@NotNull @NotNull
@ -18,8 +20,8 @@ public class LanguageManager {
} }
@NotNull @NotNull
public LocalizedItemManager getLocalizedItemManager() { public ItemNameManager getItemNameManager() {
return localizedItemManager; return itemNameManager;
} }
} }

View File

@ -0,0 +1,11 @@
package de.epiceric.shopchest.language.item;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
public interface ItemNameManager {
@Nullable
String getItemName(@Nullable ItemStack itemStack);
}

View File

@ -1,4 +1,7 @@
package de.epiceric.shopchest.language; package de.epiceric.shopchest.language.item;
import java.util.Map;
import java.util.Objects;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.BookMeta;
@ -7,17 +10,16 @@ import org.bukkit.inventory.meta.SkullMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Map; public class LocalizedItemNameManager implements ItemNameManager {
import java.util.Objects;
public class LocalizedItemManager {
private final Map<String, String> itemTranslations; private final Map<String, String> itemTranslations;
public LocalizedItemManager(@NotNull Map<String, String> itemTranslations) { public LocalizedItemNameManager(@NotNull Map<String, String> itemTranslations) {
this.itemTranslations = itemTranslations; this.itemTranslations = itemTranslations;
} }
@Override
@Nullable
public String getItemName(@Nullable ItemStack stack) { public String getItemName(@Nullable ItemStack stack) {
if (stack == null) { if (stack == null) {
return null; return null;

View File

@ -3,6 +3,7 @@ package de.epiceric.shopchest.listeners;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.config.Placeholder; import de.epiceric.shopchest.config.Placeholder;
import de.epiceric.shopchest.language.*; import de.epiceric.shopchest.language.*;
import de.epiceric.shopchest.language.item.ItemNameManager;
import de.epiceric.shopchest.utils.ClickType; import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.ClickType.SelectClickType; import de.epiceric.shopchest.utils.ClickType.SelectClickType;
import org.bukkit.Material; import org.bukkit.Material;
@ -50,9 +51,9 @@ public class CreativeModeListener implements Listener {
final LanguageManager languageManager = plugin.getLanguageManager(); final LanguageManager languageManager = plugin.getLanguageManager();
final MessageRegistry messageRegistry = languageManager.getMessageRegistry(); final MessageRegistry messageRegistry = languageManager.getMessageRegistry();
final LocalizedItemManager localizedItemManager = languageManager.getLocalizedItemManager(); final ItemNameManager itemNameManager = languageManager.getItemNameManager();
p.sendMessage(messageRegistry.getMessage(Message.ITEM_SELECTED, p.sendMessage(messageRegistry.getMessage(Message.ITEM_SELECTED,
new Replacement(Placeholder.ITEM_NAME, localizedItemManager.getItemName(e.getCursor())) new Replacement(Placeholder.ITEM_NAME, itemNameManager.getItemName(e.getCursor()))
)); ));
plugin.getShopCommand().createShopAfterSelected(p, (SelectClickType) clickType); plugin.getShopCommand().createShopAfterSelected(p, (SelectClickType) clickType);
} }

View File

@ -22,7 +22,7 @@ public class ShopProduct {
* @return The localized name of the product's {@link ItemStack} in the selected language file. * @return The localized name of the product's {@link ItemStack} in the selected language file.
*/ */
public String getLocalizedName() { public String getLocalizedName() {
return ShopChest.getInstance().getLanguageManager().getLocalizedItemManager().getItemName(getItemStack()); return ShopChest.getInstance().getLanguageManager().getItemNameManager().getItemName(getItemStack());
//LanguageUtils.getItemName(getItemStack()); //LanguageUtils.getItemName(getItemStack());
} }