SetStatsCommand.java 7.17 KB
Newer Older
Jaida Wu's avatar
Jaida Wu committed
1
2
package emu.grasscutter.command.commands;

3
4
5
6
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Jaida Wu's avatar
Jaida Wu committed
7
8
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
9
import emu.grasscutter.game.avatar.Avatar;
Jaida Wu's avatar
Jaida Wu committed
10
import emu.grasscutter.game.entity.EntityAvatar;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.player.Player;
Jaida Wu's avatar
Jaida Wu committed
12
13
14
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;

15
16
17
18
19
20
21
22
23
@Command(
    label = "setStats",
    aliases = {"stats", "stat"},
    usage = {
        "[set] <stat> <value>",
        "(lock|freeze) <stat> [<value>]",  // Can lock to current value
        "(unlock|unfreeze) <stat>"},
    permission = "player.setstats",
    permissionTargeted = "player.setstats.others")
KingRainbow44's avatar
KingRainbow44 committed
24
public final class SetStatsCommand implements CommandHandler {
25
    private static class Stat {
26
27
        String name;
        FightProperty prop;
Jaida Wu's avatar
Jaida Wu committed
28

29
30
31
32
33
34
        public Stat(FightProperty prop) {
            this.name = prop.toString();
            this.prop = prop;
        }

        public Stat(String name, FightProperty prop) {
35
36
            this.name = name;
            this.prop = prop;
Jaida Wu's avatar
Jaida Wu committed
37
        }
38
    }
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

    private static enum Action {
        ACTION_SET("commands.generic.set_to", "commands.generic.set_for_to"),
        ACTION_LOCK("commands.setStats.locked_to", "commands.setStats.locked_for_to"),
        ACTION_UNLOCK("commands.setStats.unlocked", "commands.setStats.unlocked_for");
        public final String messageKeySelf;
        public final String messageKeyOther;
        private Action(String messageKeySelf, String messageKeyOther) {
            this.messageKeySelf = messageKeySelf;
            this.messageKeyOther = messageKeyOther;
        }
    }

    private Map<String, Stat> stats;

54
    public SetStatsCommand() {
55
56
57
58
        this.stats = new HashMap<>();
        for (String key : FightProperty.getShortNames()) {
            this.stats.put(key, new Stat(FightProperty.getPropByShortName(key)));
        }
59
60
        // Full FightProperty enum that won't be advertised but can be used by devs
        // They have a prefix to avoid the "hp" clash
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        for (FightProperty prop : FightProperty.values()) {
            String name = prop.toString().substring(10);  // FIGHT_PROP_BASE_HP -> _BASE_HP
            String key = name.toLowerCase();  // _BASE_HP -> _base_hp
            name = name.substring(1);  // _BASE_HP -> BASE_HP
            this.stats.put(key, new Stat(name, prop));
        }

        // Compatibility aliases
        this.stats.put("mhp", this.stats.get("maxhp"));
        this.stats.put("hp", new Stat(FightProperty.FIGHT_PROP_CUR_HP));  // Overrides FIGHT_PROP_HP
        this.stats.put("atk", new Stat(FightProperty.FIGHT_PROP_CUR_ATTACK));  // Overrides FIGHT_PROP_ATTACK
        this.stats.put("atkb", new Stat(FightProperty.FIGHT_PROP_BASE_ATTACK));  // This doesn't seem to get used to recalculate ATK, so it's only useful for stuff like Bennett's buff.
        this.stats.put("eanemo", this.stats.get("anemo%"));
        this.stats.put("ecryo", this.stats.get("cryo%"));
        this.stats.put("edendro", this.stats.get("dendro%"));
        this.stats.put("edend", this.stats.get("dendro%"));
        this.stats.put("eelectro", this.stats.get("electro%"));
        this.stats.put("eelec", this.stats.get("electro%"));
        this.stats.put("ethunder", this.stats.get("electro%"));
        this.stats.put("egeo", this.stats.get("geo%"));
        this.stats.put("ehydro", this.stats.get("hydro%"));
        this.stats.put("epyro", this.stats.get("pyro%"));
        this.stats.put("ephys", this.stats.get("phys%"));
84
    }
fengyuecanzhu's avatar
fengyuecanzhu committed
85

86
87
88
89
90
91
92
93
    public static float parsePercent(String input) throws NumberFormatException {
        if (input.endsWith("%")) {
            return Float.parseFloat(input.substring(0, input.length()-1))/100f;
        } else {
            return Float.parseFloat(input);
        }
    }

94
    @Override
AnimeGitB's avatar
AnimeGitB committed
95
    public void execute(Player sender, Player targetPlayer, List<String> args) {
96
        String statStr = null;
97
        String valueStr;
98
        float value = 0f;
99

100
        if (args.size() < 2) {
101
            sendUsageMessage(sender);
102
103
            return;
        }
104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        // Get the action and stat
        String arg0 = args.remove(0).toLowerCase();
        Action action = switch (arg0) {
            default -> {statStr = arg0; yield Action.ACTION_SET;}  // Implicit set command
            case "set" -> Action.ACTION_SET;  // Explicit set command
            case "lock", "freeze" -> Action.ACTION_LOCK;
            case "unlock", "unfreeze" -> Action.ACTION_UNLOCK;
        };
        if (statStr == null) {
            statStr = args.remove(0).toLowerCase();
        }
        if (!stats.containsKey(statStr)) {
            sendUsageMessage(sender);  // Invalid stat or action
            return;
        }
        Stat stat = stats.get(statStr);
121
        EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity();
122
        Avatar avatar = entity.getAvatar();
123

124
        // Get the value if the action requires it
125
        try {
126
127
128
129
130
131
132
133
134
135
136
            switch (action) {
                case ACTION_LOCK:
                    if (args.isEmpty()) {  // Lock to current value
                        value = avatar.getFightProperty(stat.prop);
                        break;
                    }  // Else fall-through and lock to supplied value
                case ACTION_SET:
                    value = parsePercent(args.remove(0));
                    break;
                case ACTION_UNLOCK:
                    break;
137
138
            }
        } catch (NumberFormatException ignored) {
139
            CommandHandler.sendTranslatedMessage(sender, "commands.generic.invalid.statValue");
140
            return;
141
142
143
        } catch (IndexOutOfBoundsException ignored) {
            sendUsageMessage(sender);
            return;
144
145
        }

146
        if (!args.isEmpty()) {  // Leftover arguments!
147
            sendUsageMessage(sender);
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
            return;
        }

        switch (action) {
            case ACTION_SET:
                entity.setFightProperty(stat.prop, value);
                entity.getWorld().broadcastPacket(new PacketEntityFightPropUpdateNotify(entity, stat.prop));
                break;
            case ACTION_LOCK:
                avatar.getFightPropOverrides().put(stat.prop.getId(), value);
                avatar.recalcStats();
                break;
            case ACTION_UNLOCK:
                avatar.getFightPropOverrides().remove(stat.prop.getId());
                avatar.recalcStats();
                break;
        }

        // Report action
        if (FightProperty.isPercentage(stat.prop)) {
            valueStr = String.format("%.1f%%", value * 100f);
        } else {
            valueStr = String.format("%.0f", value);
        }
        if (targetPlayer == sender) {
            CommandHandler.sendTranslatedMessage(sender, action.messageKeySelf, stat.name, valueStr);
        } else {
            String uidStr = targetPlayer.getAccount().getId();
            CommandHandler.sendTranslatedMessage(sender, action.messageKeyOther, stat.name, uidStr, valueStr);
Jaida Wu's avatar
Jaida Wu committed
177
        }
178
        return;
Jaida Wu's avatar
Jaida Wu committed
179
180
    }
}