feat: item translator
This commit is contained in:
parent
26daa3a038
commit
5d3a99f72d
4 changed files with 82 additions and 27 deletions
|
@ -1,6 +1,5 @@
|
||||||
package xyz.atnrch.nicko.gui.items.home;
|
package xyz.atnrch.nicko.gui.items.home;
|
||||||
|
|
||||||
import com.comphenix.protocol.utility.MinecraftVersion;
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
import xyz.xenondevs.invui.item.builder.ItemBuilder;
|
import xyz.xenondevs.invui.item.builder.ItemBuilder;
|
||||||
|
@ -9,13 +8,14 @@ import xyz.xenondevs.invui.item.impl.SimpleItem;
|
||||||
public class ExitItem extends SimpleItem {
|
public class ExitItem extends SimpleItem {
|
||||||
public ExitItem() {
|
public ExitItem() {
|
||||||
super(new ItemBuilder(Material.OAK_DOOR).setDisplayName("Exit"), click -> {
|
super(new ItemBuilder(Material.OAK_DOOR).setDisplayName("Exit"), click -> {
|
||||||
if(MinecraftVersion.BEE_UPDATE.atOrAbove()) {
|
|
||||||
|
|
||||||
}
|
|
||||||
final ClickType clickType = click.getClickType();
|
final ClickType clickType = click.getClickType();
|
||||||
if (clickType.isLeftClick() || clickType.isRightClick()) {
|
if (clickType.isLeftClick() || clickType.isRightClick()) {
|
||||||
click.getEvent().getView().close();
|
click.getEvent().getView().close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ItemBuilder getItemBuilder() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import java.util.List;
|
||||||
|
|
||||||
public class I18N {
|
public class I18N {
|
||||||
private final MessageFormat formatter = new MessageFormat("");
|
private final MessageFormat formatter = new MessageFormat("");
|
||||||
|
private final YamlConfig yamlConfig;
|
||||||
private final NickoBukkit instance = NickoBukkit.getInstance();
|
private final NickoBukkit instance = NickoBukkit.getInstance();
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final Locale playerLocale;
|
private final Locale playerLocale;
|
||||||
|
@ -20,11 +21,13 @@ public class I18N {
|
||||||
public I18N(Player player) {
|
public I18N(Player player) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.playerLocale = getPlayerLocale();
|
this.playerLocale = getPlayerLocale();
|
||||||
|
this.yamlConfig = getYamlConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public I18N(Locale locale) {
|
public I18N(Locale locale) {
|
||||||
this.player = null;
|
this.player = null;
|
||||||
this.playerLocale = locale;
|
this.playerLocale = locale;
|
||||||
|
this.yamlConfig = getYamlConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> translateItem(String key, Object... arguments) {
|
public List<String> translateItem(String key, Object... arguments) {
|
||||||
|
@ -64,35 +67,20 @@ public class I18N {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readString(String key) {
|
private String readString(String key) {
|
||||||
YamlConfig yamlFile;
|
return yamlConfig.getString(key);
|
||||||
if (playerLocale == Locale.CUSTOM) {
|
|
||||||
yamlFile = instance.getLocaleFileManager().getYamlFile();
|
|
||||||
} else {
|
|
||||||
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
|
||||||
yamlFile = YamlConfig.load(resource);
|
|
||||||
}
|
|
||||||
return yamlFile.getString(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<String> readList(String key) {
|
private ArrayList<String> readList(String key) {
|
||||||
final ArrayList<String> lines = new ArrayList<>();
|
return yamlConfig.getStringList(key);
|
||||||
YamlConfig yamlFile;
|
}
|
||||||
|
|
||||||
|
private YamlConfig getYamlConfig() {
|
||||||
if (playerLocale == Locale.CUSTOM) {
|
if (playerLocale == Locale.CUSTOM) {
|
||||||
yamlFile = instance.getLocaleFileManager().getYamlFile();
|
return instance.getLocaleFileManager().getYamlFile();
|
||||||
} else {
|
} else {
|
||||||
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
||||||
yamlFile = YamlConfig.load(resource);
|
return new YamlConfig(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9 is a magic number
|
|
||||||
for (int i = 0; i < yamlFile.getInt(key + ".length"); i++) {
|
|
||||||
final String line = yamlFile.getString(key + ".content[" + i + "]");
|
|
||||||
System.out.println("line = " + line);
|
|
||||||
if (line != null && !line.equals("{" + i + "}")) {
|
|
||||||
lines.add(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return lines;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Locale getPlayerLocale() {
|
private Locale getPlayerLocale() {
|
||||||
|
|
38
src/main/java/xyz/atnrch/nicko/i18n/ItemTranslator.java
Normal file
38
src/main/java/xyz/atnrch/nicko/i18n/ItemTranslator.java
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package xyz.atnrch.nicko.i18n;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class ItemTranslator {
|
||||||
|
private final String name;
|
||||||
|
private final ArrayList<String> lore;
|
||||||
|
|
||||||
|
public ItemTranslator(String name, ArrayList<String> lore) {
|
||||||
|
this.name = name;
|
||||||
|
this.lore = lore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] translate(String... args) {
|
||||||
|
// Add all elements to a list
|
||||||
|
final ArrayList<String> toTranslate = new ArrayList<>();
|
||||||
|
toTranslate.add(name);
|
||||||
|
toTranslate.addAll(lore);
|
||||||
|
|
||||||
|
// Set starting index to 0
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
// While iterator next value exists/isn't null
|
||||||
|
final Iterator<String> iterator = toTranslate.iterator();
|
||||||
|
while (!iterator.hasNext() || iterator.next() == null) {
|
||||||
|
// Get the current line
|
||||||
|
final String currentLine = toTranslate.get(index);
|
||||||
|
|
||||||
|
// Replace with the corresponding varargs index
|
||||||
|
toTranslate.set(index, currentLine.replace("{" + index + "}", args[index]));
|
||||||
|
|
||||||
|
// Increment the index
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return new Object[]{toTranslate.get(0), toTranslate.subList(1, toTranslate.size())};
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,4 +25,33 @@ event:
|
||||||
admin:
|
admin:
|
||||||
cache:
|
cache:
|
||||||
invalidate_cache: "§fComplete cache invalidated."
|
invalidate_cache: "§fComplete cache invalidated."
|
||||||
invalidate_entry: "§6{0} §fhas been invalidated."
|
invalidate_entry: "§6{0} §fhas been invalidated."
|
||||||
|
|
||||||
|
gui:
|
||||||
|
exit: "Exit"
|
||||||
|
home:
|
||||||
|
admin:
|
||||||
|
name: "Administration panel"
|
||||||
|
lore:
|
||||||
|
- "Configure and manage Nicko."
|
||||||
|
settings:
|
||||||
|
name: "Settings"
|
||||||
|
lore:
|
||||||
|
- "Configure your experience."
|
||||||
|
change_name:
|
||||||
|
name: "§6Nickname §fchange"
|
||||||
|
change_skin:
|
||||||
|
name: "§6Skin §fchange"
|
||||||
|
change_both:
|
||||||
|
name: "Change §6both"
|
||||||
|
reset:
|
||||||
|
name: "Reset appearance"
|
||||||
|
lore:
|
||||||
|
- "Completely remove your disguise."
|
||||||
|
settings:
|
||||||
|
language:
|
||||||
|
name: "Language"
|
||||||
|
lore:
|
||||||
|
- "{0}"
|
||||||
|
- "§7§oGet through the values"
|
||||||
|
- "§7§oby left or right clicking."
|
Loading…
Reference in a new issue