mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-19 19:25:48 +00:00
Build 400 - Players can now select their language
This commit is contained in:
parent
8735a8e1a3
commit
be0035f2f9
@ -25,6 +25,7 @@
|
||||
## Release Candidate 5 (TBD)
|
||||
|
||||
### Additions
|
||||
* Added user-configurable localization
|
||||
|
||||
### Changes
|
||||
* Removed Solar Array
|
||||
|
@ -19,6 +19,7 @@ import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
|
||||
import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
|
||||
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
|
||||
import io.github.thebusybiscuit.cscorelib2.skull.SkullItem;
|
||||
import io.github.thebusybiscuit.slimefun4.core.services.Language;
|
||||
import io.github.thebusybiscuit.slimefun4.core.services.github.Contributor;
|
||||
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
|
||||
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
|
||||
@ -239,6 +240,64 @@ public final class GuideSettings {
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
Language lang = SlimefunPlugin.getLocal().getLanguage(p);
|
||||
menu.addItem(i, new CustomItem(lang.getItem(), "&7Selected Language: &a" + SlimefunPlugin.getLocal().getMessage(p, "languages." + lang.getID()), "", "&b(experimental)", "", "&7You now have the option to change", "&7the language in which Slimefun", "&7will send you messages.", "&7Note that this only translates", "&7messages, not items.", "", "&7\u21E8 &eClick to change your language"),
|
||||
(pl, slot, item, action) -> {
|
||||
openLanguages(pl);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
private static void openLanguages(Player p) {
|
||||
ChestMenu menu = new ChestMenu("Slimefun4 Language Selector");
|
||||
|
||||
menu.setEmptySlotsClickable(false);
|
||||
menu.addMenuOpeningHandler(pl -> pl.playSound(pl.getLocation(), Sound.BLOCK_NOTE_BLOCK_HARP, 0.7F, 0.7F));
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (i != 1) {
|
||||
menu.addItem(i, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler());
|
||||
}
|
||||
else {
|
||||
menu.addItem(1, new CustomItem(ChestMenuUtils.getBackButton(), "&e\u21E6 Back", "", "&7Go back to the Settings Panel"));
|
||||
menu.addMenuClickHandler(1, (pl, slot, item, action) -> {
|
||||
openSettings(pl, p.getInventory().getItemInMainHand());
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Language defaultLanguage = SlimefunPlugin.getLocal().getDefaultLanguage();
|
||||
menu.addItem(9, new CustomItem(defaultLanguage.getItem(), ChatColor.GRAY + SlimefunPlugin.getLocal().getMessage(p, "languages.default"), "", "&7\u21E8 &eClick to select the default language of the Server"),
|
||||
(pl, i, item, action) -> {
|
||||
PersistentDataAPI.remove(pl, SlimefunPlugin.getLocal().getKey());
|
||||
|
||||
String name = SlimefunPlugin.getLocal().getMessage(p, "languages.default");
|
||||
SlimefunPlugin.getLocal().sendMessage(pl, "guide.languages.updated", msg -> msg.replace("%lang%", name));
|
||||
|
||||
openSettings(pl, p.getInventory().getItemInMainHand());
|
||||
return false;
|
||||
});
|
||||
|
||||
int slot = 10;
|
||||
|
||||
for (Language language : SlimefunPlugin.getLocal().getLanguages()) {
|
||||
menu.addItem(slot, new CustomItem(language.getItem(), ChatColor.GREEN + SlimefunPlugin.getLocal().getMessage(p, "languages." + language.getID()), "", "&7\u21E8 &eClick to select this language"),
|
||||
(pl, i, item, action) -> {
|
||||
PersistentDataAPI.setString(pl, SlimefunPlugin.getLocal().getKey(), language.getID());
|
||||
|
||||
String name = SlimefunPlugin.getLocal().getMessage(p, "languages." + language.getID());
|
||||
SlimefunPlugin.getLocal().sendMessage(pl, "guide.languages.updated", msg -> msg.replace("%lang%", name));
|
||||
|
||||
openSettings(pl, p.getInventory().getItemInMainHand());
|
||||
return false;
|
||||
});
|
||||
|
||||
slot++;
|
||||
}
|
||||
|
||||
menu.open(p);
|
||||
}
|
||||
|
||||
private static void openCredits(Player p, int page) {
|
||||
|
@ -0,0 +1,32 @@
|
||||
package io.github.thebusybiscuit.slimefun4.core.services;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import io.github.thebusybiscuit.cscorelib2.skull.SkullItem;
|
||||
|
||||
public final class Language {
|
||||
|
||||
private final String id;
|
||||
private final FileConfiguration config;
|
||||
private final ItemStack item;
|
||||
|
||||
public Language(String id, FileConfiguration config, String hash) {
|
||||
this.id = id;
|
||||
this.config = config;
|
||||
this.item = SkullItem.fromHash(hash);
|
||||
}
|
||||
|
||||
public String getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public FileConfiguration getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
}
|
@ -3,7 +3,8 @@ package io.github.thebusybiscuit.slimefun4.core.services;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@ -28,15 +29,19 @@ public class LocalizationService extends Localization implements Keyed {
|
||||
|
||||
private static final String LANGUAGE_PATH = "language";
|
||||
|
||||
private final Map<String, FileConfiguration> languages = new HashMap<>();
|
||||
// All supported languages are stored in this LinkedHashMap, it is Linked so we keep the order
|
||||
private final Map<String, Language> languages = new LinkedHashMap<>();
|
||||
private final SlimefunPlugin plugin;
|
||||
private final NamespacedKey languageKey;
|
||||
private final Language defaultLanguage;
|
||||
|
||||
public LocalizationService(SlimefunPlugin plugin) {
|
||||
super(plugin);
|
||||
|
||||
this.plugin = plugin;
|
||||
languageKey = new NamespacedKey(plugin, LANGUAGE_PATH);
|
||||
defaultLanguage = new Language("default", getConfig().getConfiguration(), "11b3188fd44902f72602bd7c2141f5a70673a411adb3d81862c69e536166b");
|
||||
loadLanguages();
|
||||
|
||||
String selectedLanguage = SlimefunPlugin.getSelectedLanguage();
|
||||
String language = getLanguage();
|
||||
@ -60,7 +65,7 @@ public class LocalizationService extends Localization implements Keyed {
|
||||
Optional<String> language = PersistentDataAPI.getOptionalString(p, languageKey);
|
||||
|
||||
if (language.isPresent()) {
|
||||
FileConfiguration cfg = languages.computeIfAbsent(language.get(), this::loadLanguage);
|
||||
FileConfiguration cfg = languages.get(language.get()).getConfig();
|
||||
return cfg.getString(key);
|
||||
}
|
||||
else {
|
||||
@ -72,7 +77,7 @@ public class LocalizationService extends Localization implements Keyed {
|
||||
Optional<String> language = PersistentDataAPI.getOptionalString(p, languageKey);
|
||||
|
||||
if (language.isPresent()) {
|
||||
FileConfiguration cfg = languages.computeIfAbsent(language.get(), this::loadLanguage);
|
||||
FileConfiguration cfg = languages.get(language.get()).getConfig();
|
||||
return cfg.getStringList(key);
|
||||
}
|
||||
else {
|
||||
@ -153,6 +158,10 @@ public class LocalizationService extends Localization implements Keyed {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Language> getLanguages() {
|
||||
return languages.values();
|
||||
}
|
||||
|
||||
private String getLanguage() {
|
||||
String language = getConfig().getString(LANGUAGE_PATH);
|
||||
return language == null ? "en": language;
|
||||
@ -179,6 +188,14 @@ public class LocalizationService extends Localization implements Keyed {
|
||||
save();
|
||||
}
|
||||
|
||||
private void loadLanguages() {
|
||||
addLanguage("en", "a1701f21835a898b20759fb30a583a38b994abf60d3912ab4ce9f2311e74f72");
|
||||
}
|
||||
|
||||
private void addLanguage(String id, String hash) {
|
||||
languages.put(id, new Language(id, loadLanguage(id), hash));
|
||||
}
|
||||
|
||||
private FileConfiguration loadLanguage(String id) {
|
||||
if (!hasLanguage(id)) {
|
||||
return getConfig().getConfiguration();
|
||||
@ -196,6 +213,17 @@ public class LocalizationService extends Localization implements Keyed {
|
||||
return plugin.getClass().getResource("/languages/messages_" + language + ".yml") != null;
|
||||
}
|
||||
|
||||
public Language getLanguage(Player p) {
|
||||
Optional<String> language = PersistentDataAPI.getOptionalString(p, languageKey);
|
||||
|
||||
if (language.isPresent()) return languages.get(language.get());
|
||||
else return getDefaultLanguage();
|
||||
}
|
||||
|
||||
public Language getDefaultLanguage() {
|
||||
return defaultLanguage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamespacedKey getKey() {
|
||||
return languageKey;
|
||||
|
@ -24,6 +24,8 @@ guide:
|
||||
- '&7Type your search term into chat'
|
||||
cheat:
|
||||
no-multiblocks: '&4You cannot cheat in Multiblocks, you have to build them!'
|
||||
languages:
|
||||
updated: '&aYour language was successfully set to: &b%lang%'
|
||||
|
||||
search:
|
||||
message: '&bWhat would you like to search for?'
|
||||
@ -165,6 +167,7 @@ android:
|
||||
already: '&4You have already left a Rating for this Script!'
|
||||
|
||||
languages:
|
||||
default: 'Server-Default'
|
||||
en: 'English'
|
||||
de: 'German'
|
||||
fr: 'French'
|
Loading…
Reference in New Issue
Block a user