LanguageCommand.java 2.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.utils.Utils;

import java.util.List;
import java.util.Locale;

import static emu.grasscutter.utils.Language.translate;

16
@Command(label = "language", usage = "language [language code]", description = "commands.language.description", aliases = {"lang"}, targetRequirement = Command.TargetRequirement.NONE)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public final class LanguageCommand implements CommandHandler {

    @Override
    public void execute(Player sender, Player targetPlayer, List<String> args) {
        if (args.isEmpty()) {
            String curLangCode = null;
            if (sender != null) {
                curLangCode = Utils.getLanguageCode(sender.getAccount().getLocale());
            }
            else {
                curLangCode = Grasscutter.getLanguage().getLanguageCode();
            }
            CommandHandler.sendMessage(sender, translate(sender, "commands.language.current_language", curLangCode));
            return;
        }

        String langCode = args.get(0);
34
35
36
37

        var languageInst = Grasscutter.getLanguage(langCode);
        var actualLangCode = languageInst.getLanguageCode();
        var locale = Locale.forLanguageTag(actualLangCode);
38
        if (sender != null) {
39
40
41
            var account = sender.getAccount();
            account.setLocale(locale);
            account.save();
42
43
44
        }
        else {
            Grasscutter.setLanguage(languageInst);
45
46
47
            var config = Grasscutter.getConfig();
            config.language.language = locale;
            Grasscutter.saveConfig(config);
48
        }
49
50

        if (!langCode.equals(actualLangCode)) {
51
            CommandHandler.sendMessage(sender, translate(sender, "commands.language.language_not_found", langCode));
52
53
        }

54
55
56
57
        CommandHandler.sendMessage(sender, translate(sender, "commands.language.language_changed", actualLangCode));

    }
}