GiveCharCommand.java 3.04 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
4
5
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
6
import emu.grasscutter.data.GameData;
KingRainbow44's avatar
KingRainbow44 committed
7
import emu.grasscutter.data.def.AvatarData;
8
import emu.grasscutter.game.avatar.Avatar;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
10
11
12

import java.util.List;

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

15
@Command(label = "givechar", usage = "givechar <avatarId> [level]", aliases = {"givec"}, permission = "player.givechar")
KingRainbow44's avatar
KingRainbow44 committed
16
17
public final class GiveCharCommand implements CommandHandler {

18
19
20
21
22
    @Override
    public String description() {
        return translate("commands.giveChar.description");
    }

KingRainbow44's avatar
KingRainbow44 committed
23
    @Override
AnimeGitB's avatar
AnimeGitB committed
24
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
25
        if (targetPlayer == null) {
26
            CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
KingRainbow44's avatar
KingRainbow44 committed
27
28
29
            return;
        }

AnimeGitB's avatar
AnimeGitB committed
30
31
32
        int avatarId;
        int level = 1;

KingRainbow44's avatar
KingRainbow44 committed
33
34
35
        switch (args.size()) {
            case 2:
                try {
AnimeGitB's avatar
AnimeGitB committed
36
                    level = Integer.parseInt(args.get(1));
KingRainbow44's avatar
KingRainbow44 committed
37
38
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from avatar name using GM Handbook.
39
                    CommandHandler.sendMessage(sender, translate("commands.execution.invalid.avatarLevel"));
KingRainbow44's avatar
KingRainbow44 committed
40
                    return;
AnimeGitB's avatar
AnimeGitB committed
41
42
                }  // Cheeky fall-through to parse first argument too
            case 1:
KingRainbow44's avatar
KingRainbow44 committed
43
                try {
AnimeGitB's avatar
AnimeGitB committed
44
                    avatarId = Integer.parseInt(args.get(0));
KingRainbow44's avatar
KingRainbow44 committed
45
46
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from avatar name using GM Handbook.
47
                    CommandHandler.sendMessage(sender, translate("commands.execution.invalid.avatarId"));
KingRainbow44's avatar
KingRainbow44 committed
48
49
50
                    return;
                }
                break;
AnimeGitB's avatar
AnimeGitB committed
51
            default:
52
                CommandHandler.sendMessage(sender, translate("commands.giveChar.usage"));
AnimeGitB's avatar
AnimeGitB committed
53
                return;
KingRainbow44's avatar
KingRainbow44 committed
54
55
        }

56
        AvatarData avatarData = GameData.getAvatarDataMap().get(avatarId);
KingRainbow44's avatar
KingRainbow44 committed
57
        if (avatarData == null) {
58
            CommandHandler.sendMessage(sender, translate("commands.execution.invalid.avatarId"));
KingRainbow44's avatar
KingRainbow44 committed
59
60
61
            return;
        }

62
63
        // Check level.
        if (level > 90) {
64
            CommandHandler.sendMessage(sender, translate("commands.execution.invalid.avatarLevel"));
65
66
67
            return;
        }

KingRainbow44's avatar
KingRainbow44 committed
68
        // Calculate ascension level.
AnimeGitB's avatar
AnimeGitB committed
69
        int ascension;
KingRainbow44's avatar
KingRainbow44 committed
70
71
72
73
74
75
        if (level <= 40) {
            ascension = (int) Math.ceil(level / 20f);
        } else {
            ascension = (int) Math.ceil(level / 10f) - 3;
        }

76
        Avatar avatar = new Avatar(avatarId);
KingRainbow44's avatar
KingRainbow44 committed
77
78
79
80
81
82
83
        avatar.setLevel(level);
        avatar.setPromoteLevel(ascension);

        // This will handle stats and talents
        avatar.recalcStats();

        targetPlayer.addAvatar(avatar);
zhaodice's avatar
zhaodice committed
84
        CommandHandler.sendMessage(sender, translate("commands.giveChar.given", Integer.toString(avatarId), Integer.toString(level), Integer.toString(targetPlayer.getUid())));
KingRainbow44's avatar
KingRainbow44 committed
85
86
    }
}