TalentCommand.java 5.51 KB
Newer Older
alt3ri's avatar
alt3ri committed
1
2
package emu.grasscutter.command.commands;

方块君's avatar
方块君 committed
3
import emu.grasscutter.Grasscutter;
alt3ri's avatar
alt3ri committed
4
5
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
xtaodada's avatar
xtaodada committed
6
import emu.grasscutter.data.def.AvatarSkillDepotData;
7
import emu.grasscutter.game.avatar.Avatar;
alt3ri's avatar
alt3ri committed
8
import emu.grasscutter.game.entity.EntityAvatar;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.player.Player;
alt3ri's avatar
alt3ri committed
10
11
12
13
14
import emu.grasscutter.server.packet.send.PacketAvatarSkillChangeNotify;
import emu.grasscutter.server.packet.send.PacketAvatarSkillUpgradeRsp;

import java.util.List;

15
16
import static emu.grasscutter.utils.Language.translate;

方块君's avatar
方块君 committed
17
@Command(label = "talent", usage = "talent <talentID> <value>", permission = "player.settalent", description = "commands.talent.description")
KingRainbow44's avatar
KingRainbow44 committed
18
public final class TalentCommand implements CommandHandler {
AnimeGitB's avatar
AnimeGitB committed
19
20
21
    private void setTalentLevel(Player sender, Player player, Avatar avatar, int talentId, int talentLevel) {
        int oldLevel = avatar.getSkillLevelMap().get(talentId);
        if (talentLevel < 0 || talentLevel > 15) {
22
            CommandHandler.sendMessage(sender, translate("commands.talent.lower_16"));
AnimeGitB's avatar
AnimeGitB committed
23
24
25
26
27
28
29
30
31
32
33
            return;
        }

        // Upgrade skill
        avatar.getSkillLevelMap().put(talentLevel, talentLevel);
        avatar.save();

        // Packet
        player.sendPacket(new PacketAvatarSkillChangeNotify(avatar, talentId, oldLevel, talentLevel));
        player.sendPacket(new PacketAvatarSkillUpgradeRsp(avatar, talentId, oldLevel, talentLevel));

34
        String successMessage = "commands.talent.set_id";
AnimeGitB's avatar
AnimeGitB committed
35
36
        AvatarSkillDepotData depot = avatar.getData().getSkillDepot();
        if (talentId == depot.getSkills().get(0)) {
37
            successMessage = "commands.talent.set_atk";
AnimeGitB's avatar
AnimeGitB committed
38
        } else if (talentId == depot.getSkills().get(1)) {
39
            successMessage = "commands.talent.set_e";
AnimeGitB's avatar
AnimeGitB committed
40
        } else if (talentId == depot.getEnergySkill()) {
41
            successMessage = "commands.talent.set_q";
AnimeGitB's avatar
AnimeGitB committed
42
        }
43
        CommandHandler.sendMessage(sender, translate(successMessage, talentLevel));
AnimeGitB's avatar
AnimeGitB committed
44
    }
alt3ri's avatar
alt3ri committed
45
46

    @Override
AnimeGitB's avatar
AnimeGitB committed
47
    public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
48
        if (targetPlayer == null) {
49
            CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
alt3ri's avatar
alt3ri committed
50
51
52
            return;
        }

xtaodada's avatar
xtaodada committed
53
        if (args.size() < 1){
54
55
56
            CommandHandler.sendMessage(sender, translate("commands.talent.usage_1"));
            CommandHandler.sendMessage(sender, translate("commands.talent.usage_2"));
            CommandHandler.sendMessage(sender, translate("commands.talent.usage_3"));
alt3ri's avatar
alt3ri committed
57
58
59
            return;
        }

AnimeGitB's avatar
AnimeGitB committed
60
        EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity();
AnimeGitB's avatar
AnimeGitB committed
61
        Avatar avatar = entity.getAvatar(); 
alt3ri's avatar
alt3ri committed
62
63
        String cmdSwitch = args.get(0);
        switch (cmdSwitch) {
64
65
66
67
68
69
70
71
72
73
            default -> {
                CommandHandler.sendMessage(sender, translate("commands.talent.usage_1"));
                CommandHandler.sendMessage(sender, translate("commands.talent.usage_2"));
                CommandHandler.sendMessage(sender, translate("commands.talent.usage_3"));
                return;
            }
            case "set" -> {
                if (args.size() < 3) {
                    CommandHandler.sendMessage(sender, translate("commands.talent.usage_1"));
                    CommandHandler.sendMessage(sender, translate("commands.talent.usage_3"));
AnimeGitB's avatar
AnimeGitB committed
74
75
76
77
78
79
80
                    return;
                }
                try {
                    int skillId = Integer.parseInt(args.get(1));
                    int newLevel = Integer.parseInt(args.get(2));
                    setTalentLevel(sender, targetPlayer, avatar, skillId, newLevel);
                } catch (NumberFormatException ignored) {
81
                    CommandHandler.sendMessage(sender, translate("commands.talent.invalid_skill_id"));
AnimeGitB's avatar
AnimeGitB committed
82
83
                    return;
                }
84
85
86
87
            }
            case "n", "e", "q" -> {
                if (args.size() < 2) {
                    CommandHandler.sendMessage(sender, translate("commands.talent.usage_2"));
AnimeGitB's avatar
AnimeGitB committed
88
89
90
                    return;
                }
                AvatarSkillDepotData SkillDepot = avatar.getData().getSkillDepot();
91
92
93
94
95
                int skillId = switch (cmdSwitch) {
                    default -> SkillDepot.getSkills().get(0);
                    case "e" -> SkillDepot.getSkills().get(1);
                    case "q" -> SkillDepot.getEnergySkill();
                };
xtaodada's avatar
xtaodada committed
96
                try {
AnimeGitB's avatar
AnimeGitB committed
97
98
                    int newLevel = Integer.parseInt(args.get(1));
                    setTalentLevel(sender, targetPlayer, avatar, skillId, newLevel);
xtaodada's avatar
xtaodada committed
99
                } catch (NumberFormatException ignored) {
100
                    CommandHandler.sendMessage(sender, translate("commands.talent.invalid_level"));
xtaodada's avatar
xtaodada committed
101
102
                    return;
                }
103
104
            }
            case "getid" -> {
AnimeGitB's avatar
AnimeGitB committed
105
106
107
                int skillIdNorAtk = avatar.getData().getSkillDepot().getSkills().get(0);
                int skillIdE = avatar.getData().getSkillDepot().getSkills().get(1);
                int skillIdQ = avatar.getData().getSkillDepot().getEnergySkill();
108
109
110
111
                CommandHandler.sendMessage(sender, translate("commands.talent.normal_attack_id", Integer.toString(skillIdNorAtk)));
                CommandHandler.sendMessage(sender, translate("commands.talent.e_skill_id", Integer.toString(skillIdE)));
                CommandHandler.sendMessage(sender, translate("commands.talent.q_skill_id", Integer.toString(skillIdQ)));
            }
alt3ri's avatar
alt3ri committed
112
113
114
        }
    }
}