Catch LocalizeItemNameManager cache exception

This commit is contained in:
Flowsqy 2023-08-09 12:52:38 +02:00
parent 88747a8f76
commit 3509557243

View File

@ -2,6 +2,7 @@ package de.epiceric.shopchest.language.item;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
@ -10,8 +11,12 @@ import org.bukkit.inventory.meta.SkullMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import de.epiceric.shopchest.ShopChest;
public class LocalizedItemNameManager implements ItemNameManager {
private final static String ERROR_ITEM_NAME = "ERROR";
private final Map<String, String> itemTranslations;
public LocalizedItemNameManager(@NotNull Map<String, String> itemTranslations) {
@ -58,13 +63,19 @@ public class LocalizedItemNameManager implements ItemNameManager {
@NotNull
private String getDefaultName(@NotNull ItemStack stack) {
try {
return getCached(stack.getTranslationKey());
} catch (Exception e) {
ShopChest.getInstance().getLogger().log(Level.SEVERE, e.getMessage());
return ERROR_ITEM_NAME;
}
}
@NotNull
private String getCached(@NotNull String key) {
final String cachedTranslation = itemTranslations.get(key);
if (cachedTranslation == null) {
// Keep this behavior to ensure quick fixes
throw new RuntimeException("Could not get the translation for '" + key + "'. Report it to github");
}
return cachedTranslation;