mirror of
https://github.com/Flowsqy/ShopChest.git
synced 2025-02-17 14:01:19 +01:00
Add LocalizedItemManager
This commit is contained in:
parent
4e1a336661
commit
345bc8a574
@ -43,7 +43,8 @@ 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);
|
||||||
return new LanguageManager(messageRegistry);
|
final LocalizedItemManager localizedItemManager = new LocalizedItemManager(storedItems);
|
||||||
|
return new LanguageManager(messageRegistry, localizedItemManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -5,9 +5,11 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
public class LanguageManager {
|
public class LanguageManager {
|
||||||
|
|
||||||
private final MessageRegistry messageRegistry;
|
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.messageRegistry = messageRegistry;
|
||||||
|
this.localizedItemManager = localizedItemManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -15,4 +17,9 @@ public class LanguageManager {
|
|||||||
return messageRegistry;
|
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;
|
package de.epiceric.shopchest.listeners;
|
||||||
|
|
||||||
import de.epiceric.shopchest.ShopChest;
|
import de.epiceric.shopchest.ShopChest;
|
||||||
import de.epiceric.shopchest.language.Message;
|
import de.epiceric.shopchest.config.Placeholder;
|
||||||
import de.epiceric.shopchest.language.MessageRegistry;
|
import de.epiceric.shopchest.language.*;
|
||||||
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;
|
||||||
@ -48,10 +48,11 @@ public class CreativeModeListener implements Listener {
|
|||||||
((SelectClickType) clickType).setItem(e.getCursor());
|
((SelectClickType) clickType).setItem(e.getCursor());
|
||||||
p.closeInventory();
|
p.closeInventory();
|
||||||
|
|
||||||
final MessageRegistry messageRegistry = plugin.getLanguageManager().getMessageRegistry();
|
final LanguageManager languageManager = plugin.getLanguageManager();
|
||||||
p.sendMessage(messageRegistry.getMessage(Message.ITEM_SELECTED
|
final MessageRegistry messageRegistry = languageManager.getMessageRegistry();
|
||||||
// TODO Link it
|
final LocalizedItemManager localizedItemManager = languageManager.getLocalizedItemManager();
|
||||||
//, new Replacement(Placeholder.ITEM_NAME, LanguageUtils.getItemName(e.getCursor()))
|
p.sendMessage(messageRegistry.getMessage(Message.ITEM_SELECTED,
|
||||||
|
new Replacement(Placeholder.ITEM_NAME, localizedItemManager.getItemName(e.getCursor()))
|
||||||
));
|
));
|
||||||
plugin.getShopCommand().createShopAfterSelected(p, (SelectClickType) clickType);
|
plugin.getShopCommand().createShopAfterSelected(p, (SelectClickType) clickType);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package de.epiceric.shopchest.shop;
|
package de.epiceric.shopchest.shop;
|
||||||
|
|
||||||
|
import de.epiceric.shopchest.ShopChest;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
public class ShopProduct {
|
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.
|
* @return The localized name of the product's {@link ItemStack} in the selected language file.
|
||||||
*/
|
*/
|
||||||
public String getLocalizedName() {
|
public String getLocalizedName() {
|
||||||
// TODO Link it to the current language implementation
|
return ShopChest.getInstance().getLanguageManager().getLocalizedItemManager().getItemName(getItemStack());
|
||||||
return "";//LanguageUtils.getItemName(getItemStack());
|
//LanguageUtils.getItemName(getItemStack());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
2
pom.xml
2
pom.xml
@ -123,7 +123,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
<artifactId>spigot-api</artifactId>
|
<artifactId>spigot-api</artifactId>
|
||||||
<version>1.19-R0.1-SNAPSHOT</version>
|
<version>1.20.1-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
Loading…
Reference in New Issue
Block a user