SetStatsCommand.java 4.81 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
9
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.entity.EntityAvatar;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.player.Player;
Jaida Wu's avatar
Jaida Wu committed
11
12
13
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;

14
@Command(label = "setstats", usage = "setstats|stats <stat> <value>", aliases = {"stats"}, permission = "player.setstats", permissionTargeted = "player.setstats.others", description = "commands.setStats.description")
KingRainbow44's avatar
KingRainbow44 committed
15
public final class SetStatsCommand implements CommandHandler {
16
    static class Stat {
17
18
        String name;
        FightProperty prop;
Jaida Wu's avatar
Jaida Wu committed
19

20
21
22
23
24
25
        public Stat(FightProperty prop) {
            this.name = prop.toString();
            this.prop = prop;
        }

        public Stat(String name, FightProperty prop) {
26
27
            this.name = name;
            this.prop = prop;
Jaida Wu's avatar
Jaida Wu committed
28
        }
29
    }
30
    
31
    Map<String, Stat> stats;
32
33
    
    public SetStatsCommand() {
34
35
36
37
        this.stats = new HashMap<>();
        for (String key : FightProperty.getShortNames()) {
            this.stats.put(key, new Stat(FightProperty.getPropByShortName(key)));
        }
38
39
        // Full FightProperty enum that won't be advertised but can be used by devs
        // They have a prefix to avoid the "hp" clash
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        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%"));
63
    }
fengyuecanzhu's avatar
fengyuecanzhu committed
64

65
    @Override
AnimeGitB's avatar
AnimeGitB committed
66
    public void execute(Player sender, Player targetPlayer, List<String> args) {
67
68
69
        String statStr;
        String valueStr;

70
71
72
73
        if (args.size() == 2) {
            statStr = args.get(0).toLowerCase();
            valueStr = args.get(1);
        } else {
74
            CommandHandler.sendTranslatedMessage(sender, "commands.setStats.usage");
75
76
            return;
        }
77
78
79
80
81
82
83
84
85
86
87

        EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity();

        float value;
        try {
            if (valueStr.endsWith("%")) {
                value = Float.parseFloat(valueStr.substring(0, valueStr.length()-1))/100f;
            } else {
                value = Float.parseFloat(valueStr);
            }
        } catch (NumberFormatException ignored) {
88
            CommandHandler.sendTranslatedMessage(sender, "commands.generic.invalid.statValue");
89
90
91
92
93
94
95
            return;
        }

        if (stats.containsKey(statStr)) {
            Stat stat = stats.get(statStr);
            entity.setFightProperty(stat.prop, value);
            entity.getWorld().broadcastPacket(new PacketEntityFightPropUpdateNotify(entity, stat.prop));
96
97
            if (FightProperty.isPercentage(stat.prop)) {
                valueStr = String.format("%.1f%%", value * 100f);
98
99
100
101
            } else {
                valueStr = String.format("%.0f", value);
            }
            if (targetPlayer == sender) {
102
                CommandHandler.sendTranslatedMessage(sender, "commands.generic.set_to", stat.name, valueStr);
103
            } else {
AnimeGitB's avatar
AnimeGitB committed
104
                String uidStr = targetPlayer.getAccount().getId();
105
                CommandHandler.sendTranslatedMessage(sender, "commands.generic.set_for_to", stat.name, uidStr, valueStr);
106
107
            }
        } else {
108
            CommandHandler.sendTranslatedMessage(sender, "commands.setStats.usage");
Jaida Wu's avatar
Jaida Wu committed
109
        }
110
        return;
Jaida Wu's avatar
Jaida Wu committed
111
112
    }
}