GiveCharCommand.java 3 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;

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

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

AnimeGitB's avatar
AnimeGitB committed
26
27
28
        int avatarId;
        int level = 1;

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

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

58
59
        // Check level.
        if (level > 90) {
60
            CommandHandler.sendMessage(sender, translate("commands.execution.invalid.avatarLevel"));
61
62
63
            return;
        }

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

72
        Avatar avatar = new Avatar(avatarId);
KingRainbow44's avatar
KingRainbow44 committed
73
74
75
76
77
78
79
        avatar.setLevel(level);
        avatar.setPromoteLevel(ascension);

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

        targetPlayer.addAvatar(avatar);
80
        CommandHandler.sendMessage(sender, translate("commands.execution.giveChar.given", Integer.toString(avatarId), Integer.toString(level), Integer.toString(targetPlayer.getUid())));
KingRainbow44's avatar
KingRainbow44 committed
81
82
    }
}