Skip to content

Commit

Permalink
⚒ | Fixed language autodetector
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo0111 committed Jul 16, 2021
1 parent ec4c214 commit d141ffe
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static void setLang(LocalizedPlayer player, String localeName, SubCommand
return;
}

if (!subcommand.getPlugin().getConfig().getStringList("languages").contains(localeName)) {
if (!subcommand.getPlugin().getConfigManager().getLocales().containsKey(localeName)) {
player.getPlayer().sendMessage(subcommand.format(MessagesManager.get("lang-not-found")));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import me.lorenzo0111.multilang.commands.SubCommand;
import me.lorenzo0111.multilang.handlers.MessagesManager;
import me.lorenzo0111.multilang.utils.GuiUtils;
import me.lorenzo0111.multilang.utils.Reflection;
import me.lorenzo0111.pluginslib.command.Command;
import me.lorenzo0111.pluginslib.command.annotations.NoArguments;
import me.lorenzo0111.pluginslib.command.annotations.Permission;
Expand Down Expand Up @@ -75,10 +76,17 @@ public void handleSubcommand(CommandSender sender, String[] args) {
gui.setItem(22,
ItemBuilder.from(Objects.requireNonNull(XMaterial.TORCH.parseItem()))
.name(Component.text(MessagesManager.get("gui.current") + player.getLocale().toString()))
.asGuiItem()
.lore(Component.text(MessagesManager.get("gui.current-lore")))
.asGuiItem(e -> {
gui.close(e.getWhoClicked());
String locale = Reflection.getLocale(player.getPlayer());
if (!getPlugin().getConfigManager().getLocales().containsValue(locale)) return;

player.setLocale(getPlugin().getConfigManager().byKey(locale));
})
);

for (String locale : this.getPlugin().getConfig().getStringList("languages")) {
for (String locale : this.getPlugin().getConfigManager().getLocales().keySet()) {
String base = this.getPlugin().getLoader().getGuiConfig().getString("base." + locale);

gui.addItem(ItemBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,28 @@

import me.lorenzo0111.multilang.MultiLangPlugin;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;

import java.io.IOException;
import java.util.Objects;

public class MessagesManager {
private static MultiLangPlugin plugin;

public static void setPlugin(MultiLangPlugin plugin) {
MessagesManager.plugin = plugin;
plugin.getLogger().info(String.format("Loaded messages.yml. (File version: %s)", plugin.getLoader().getMessagesConfig().getString("version", "1.0")));
}

public static void update(String path, Object value) {
FileConfiguration config = plugin.getLoader().getMessagesConfig();
config.set(path,value);
try {
config.save(plugin.getLoader().getMessagesFile());
plugin.getLoader().reloadMessages();
} catch (IOException e) {
e.printStackTrace();
}
}

public static String get(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void onPacketSending(PacketEvent event) {

// Get the component
WrappedChatComponent component = packet.getChatComponents().read(0);
if (component == null) return;

JsonObject json = new JsonParser().parse(component.getJson()).getAsJsonObject();

// If it has some text, update it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public boolean init() {
}

plugin.setConfigManager(new ConfigManager(plugin));
plugin.getConfigManager().parse();
plugin.getConfigManager().register();
plugin.getConfigManager().parse();
return true;
}

Expand Down Expand Up @@ -122,6 +122,8 @@ public void messages() {

this.reloadMessages();
MessagesManager.setPlugin(plugin);
MessagesManager.update("version", plugin.getDescription().getVersion());
MessagesManager.update("gui.current-lore", "&7Click here to autodetect.");
}

public void reloadGui() {
Expand Down Expand Up @@ -155,4 +157,8 @@ public FileConfiguration getGuiConfig() {
public FileConfiguration getMessagesConfig() {
return messagesConfig;
}

public File getMessagesFile() {
return messagesFile;
}
}
6 changes: 3 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ prefix: "&8[&9MultiLang&8] &r"

# Enable debug
debug:
enabled: false
default: false

# Storage types: FILE, MYSQL
storage: "FILE"
Expand All @@ -23,8 +23,8 @@ default-base: "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY

#
languages:
english: "en_US"
italiano: "it_IT"
english: "en_us"
italiano: "it_it"

strings:
1:
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ changed: "&7Language changed to &9%s&7."

gui:
title: "&7Languages"
current: "&7Current language: &9"
current: "&7Current language: &9"
current-lore: "&7Click here to autodetect."

0 comments on commit d141ffe

Please sign in to comment.