GiveCharCommand.java 3.02 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", permissionTargeted = "player.givechar.others", description = "commands.giveChar.description")
KingRainbow44's avatar
KingRainbow44 committed
16
17
18
public final class GiveCharCommand implements CommandHandler {

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

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

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

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

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

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

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

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

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