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

tester233's avatar
tester233 committed
15
@Command(label = "givechar", usage = "givechar <player> <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
21
22
        int avatarId;
        int level = 1;

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

46
        AvatarData avatarData = GameData.getAvatarDataMap().get(avatarId);
KingRainbow44's avatar
KingRainbow44 committed
47
        if (avatarData == null) {
tester233's avatar
tester233 committed
48
            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.avatarId"));
KingRainbow44's avatar
KingRainbow44 committed
49
50
51
            return;
        }

52
53
        // Check level.
        if (level > 90) {
tester233's avatar
tester233 committed
54
            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.avatarLevel"));
55
56
57
            return;
        }

KingRainbow44's avatar
KingRainbow44 committed
58
        // Calculate ascension level.
AnimeGitB's avatar
AnimeGitB committed
59
        int ascension;
KingRainbow44's avatar
KingRainbow44 committed
60
        if (level <= 40) {
61
            ascension = (int) Math.ceil(level / 20f) - 1;
KingRainbow44's avatar
KingRainbow44 committed
62
63
        } else {
            ascension = (int) Math.ceil(level / 10f) - 3;
64
            ascension = Math.min(ascension, 6);
KingRainbow44's avatar
KingRainbow44 committed
65
66
        }

67
        Avatar avatar = new Avatar(avatarId);
KingRainbow44's avatar
KingRainbow44 committed
68
69
70
71
72
73
74
        avatar.setLevel(level);
        avatar.setPromoteLevel(ascension);

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

        targetPlayer.addAvatar(avatar);
Secretboy's avatar
Secretboy committed
75
        CommandHandler.sendMessage(sender, translate(sender, "commands.giveChar.given", Integer.toString(avatarId), Integer.toString(level), Integer.toString(targetPlayer.getUid())));
KingRainbow44's avatar
KingRainbow44 committed
76
77
    }
}