GiveCharCommand.java 3.52 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
13
14
15
16
17

import java.util.List;

@Command(label = "givechar", usage = "givechar <playerId> <avatarId> [level]",
        description = "Gives the player a specified character", aliases = {"givec"}, permission = "player.givechar")
public final class GiveCharCommand implements CommandHandler {

    @Override
18
    public void execute(Player sender, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        int target, avatarId, level = 1, ascension;

        if (sender == null && args.size() < 2) {
            CommandHandler.sendMessage(null, "Usage: givechar <player> <itemId|itemName> [amount]");
            return;
        }

        switch (args.size()) {
            default:
                CommandHandler.sendMessage(sender, "Usage: givechar <player> <avatarId> [level]");
                return;
            case 2:
                try {
                    target = Integer.parseInt(args.get(0));
                    if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
                        target = sender.getUid();
                        level = Integer.parseInt(args.get(1));
                        avatarId = Integer.parseInt(args.get(0));
                    } else {
                        avatarId = Integer.parseInt(args.get(1));
                    }
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from avatar name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid avatar or player ID.");
                    return;
                }
                break;
            case 3:
                try {
                    target = Integer.parseInt(args.get(0));
                    if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
                        CommandHandler.sendMessage(sender, "Invalid player ID.");
                        return;
                    }

                    avatarId = Integer.parseInt(args.get(1));
                    level = Integer.parseInt(args.get(2));
                } catch (NumberFormatException ignored) {
                    // TODO: Parse from avatar name using GM Handbook.
                    CommandHandler.sendMessage(sender, "Invalid avatar or player ID.");
                    return;
                }
                break;
        }

64
        Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
KingRainbow44's avatar
KingRainbow44 committed
65
66
67
68
69
        if (targetPlayer == null) {
            CommandHandler.sendMessage(sender, "Player not found.");
            return;
        }

70
        AvatarData avatarData = GameData.getAvatarDataMap().get(avatarId);
KingRainbow44's avatar
KingRainbow44 committed
71
72
73
74
75
76
77
78
79
80
81
82
        if (avatarData == null) {
            CommandHandler.sendMessage(sender, "Invalid avatar id.");
            return;
        }

        // Calculate ascension level.
        if (level <= 40) {
            ascension = (int) Math.ceil(level / 20f);
        } else {
            ascension = (int) Math.ceil(level / 10f) - 3;
        }

83
        Avatar avatar = new Avatar(avatarId);
KingRainbow44's avatar
KingRainbow44 committed
84
85
86
87
88
89
90
91
92
93
        avatar.setLevel(level);
        avatar.setPromoteLevel(ascension);

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

        targetPlayer.addAvatar(avatar);
        CommandHandler.sendMessage(sender, String.format("Given %s to %s.", avatarId, target));
    }
}