SetPropCommand.java 9.94 KB
Newer Older
github-actions's avatar
github-actions committed
1
2
3
4
5
6
7
8
package emu.grasscutter.command.commands;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
9
import emu.grasscutter.data.GameData;
github-actions's avatar
github-actions committed
10
11
12
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.PlayerProperty;
import emu.grasscutter.game.tower.TowerLevelRecord;
13
import emu.grasscutter.server.packet.send.PacketOpenStateChangeNotify;
14
15
import emu.grasscutter.server.packet.send.PacketSceneAreaUnlockNotify;
import emu.grasscutter.server.packet.send.PacketScenePointUnlockNotify;
github-actions's avatar
github-actions committed
16

17
@Command(label = "setProp", aliases = {"prop"}, usage = {"<prop> <value>"}, permission = "player.setprop", permissionTargeted = "player.setprop.others")
github-actions's avatar
github-actions committed
18
19
20
21
22
23
24
public final class SetPropCommand implements CommandHandler {
    static enum PseudoProp {
        NONE,
        WORLD_LEVEL,
        TOWER_LEVEL,
        BP_LEVEL,
        GOD_MODE,
25
        UNLIMITED_STAMINA,
26
27
        UNLIMITED_ENERGY,
        SET_OPENSTATE,
28
        UNSET_OPENSTATE,
29
        UNLOCK_MAP
github-actions's avatar
github-actions committed
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    }

    static class Prop {
        String name;
        PlayerProperty prop;
        PseudoProp pseudoProp;

        public Prop(PlayerProperty prop) {
            this(prop.toString(), prop, PseudoProp.NONE);
        }

        public Prop(String name) {
            this(name, PlayerProperty.PROP_NONE, PseudoProp.NONE);
        }

        public Prop(String name, PseudoProp pseudoProp) {
            this(name, PlayerProperty.PROP_NONE, pseudoProp);
        }

        public Prop(String name, PlayerProperty prop) {
            this(name, prop, PseudoProp.NONE);
        }

        public Prop(String name, PlayerProperty prop, PseudoProp pseudoProp) {
            this.name = name;
            this.prop = prop;
            this.pseudoProp = pseudoProp;
        }
    }

    Map<String, Prop> props;

    public SetPropCommand() {
        this.props = new HashMap<>();
        // Full PlayerProperty enum that won't be advertised but can be used by devs
        for (PlayerProperty prop : PlayerProperty.values()) {
            String name = prop.toString().substring(5);  // PROP_EXP -> EXP
            String key = name.toLowerCase();  // EXP -> exp
            this.props.put(key, new Prop(name, prop));
        }
        // Add special props
        Prop worldlevel = new Prop("World Level", PlayerProperty.PROP_PLAYER_WORLD_LEVEL, PseudoProp.WORLD_LEVEL);
        this.props.put("worldlevel", worldlevel);
        this.props.put("wl", worldlevel);

        Prop abyss = new Prop("Tower Level", PseudoProp.TOWER_LEVEL);
        this.props.put("abyss", abyss);
        this.props.put("abyssfloor", abyss);
        this.props.put("ut", abyss);
        this.props.put("tower", abyss);
        this.props.put("towerlevel", abyss);
        this.props.put("unlocktower", abyss);

        Prop bplevel = new Prop("BP Level", PseudoProp.BP_LEVEL);
        this.props.put("bplevel", bplevel);
        this.props.put("bp", bplevel);
        this.props.put("battlepass", bplevel);

88
        Prop godmode = new Prop("GodMode", PseudoProp.GOD_MODE);
github-actions's avatar
github-actions committed
89
90
91
        this.props.put("godmode", godmode);
        this.props.put("god", godmode);

92
93
94
        Prop nostamina = new Prop("UnlimitedStamina", PseudoProp.UNLIMITED_STAMINA);
        this.props.put("unlimitedstamina", nostamina);
        this.props.put("us", nostamina);
github-actions's avatar
github-actions committed
95
96
97
98
        this.props.put("nostamina", nostamina);
        this.props.put("nostam", nostamina);
        this.props.put("ns", nostamina);

99
        Prop unlimitedenergy = new Prop("UnlimitedEnergy", PseudoProp.UNLIMITED_ENERGY);
github-actions's avatar
github-actions committed
100
101
        this.props.put("unlimitedenergy", unlimitedenergy);
        this.props.put("ue", unlimitedenergy);
102

103
        Prop setopenstate = new Prop("SetOpenstate", PseudoProp.SET_OPENSTATE);
104
105
106
        this.props.put("setopenstate", setopenstate);
        this.props.put("so", setopenstate);

107
        Prop unsetopenstate = new Prop("UnsetOpenstate", PseudoProp.UNSET_OPENSTATE);
108
109
        this.props.put("unsetopenstate", unsetopenstate);
        this.props.put("uo", unsetopenstate);
110

111
        Prop unlockmap = new Prop("UnlockMap", PseudoProp.UNLOCK_MAP);
112
113
        this.props.put("unlockmap", unlockmap);
        this.props.put("um", unlockmap);
github-actions's avatar
github-actions committed
114
115
116
117
118
    }

    @Override
    public void execute(Player sender, Player targetPlayer, List<String> args) {
        if (args.size() != 2) {
119
            sendUsageMessage(sender);
github-actions's avatar
github-actions committed
120
121
122
123
124
125
126
            return;
        }
        String propStr = args.get(0).toLowerCase();
        String valueStr = args.get(1).toLowerCase();
        int value;

        if (!props.containsKey(propStr)) {
127
            sendUsageMessage(sender);
github-actions's avatar
github-actions committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
            return;
        }
        try {
            value = switch (valueStr.toLowerCase()) {
                case "on", "true" -> 1;
                case "off", "false" -> 0;
                case "toggle" -> -1;
                default -> Integer.parseInt(valueStr);
            };
        } catch (NumberFormatException ignored) {
            CommandHandler.sendTranslatedMessage(sender, "commands.execution.argument_error");
            return;
        }

        boolean success = false;
        Prop prop = props.get(propStr);

        success = switch (prop.pseudoProp) {
            case WORLD_LEVEL -> targetPlayer.setWorldLevel(value);
            case BP_LEVEL -> targetPlayer.getBattlePassManager().setLevel(value);
            case TOWER_LEVEL -> this.setTowerLevel(sender, targetPlayer, value);
149
            case GOD_MODE, UNLIMITED_STAMINA, UNLIMITED_ENERGY -> this.setBool(sender, targetPlayer, prop.pseudoProp, value);
150
151
            case SET_OPENSTATE -> this.setOpenState(targetPlayer, value, 1);
            case UNSET_OPENSTATE -> this.setOpenState(targetPlayer, value, 0);
152
            case UNLOCK_MAP -> unlockMap(targetPlayer);
github-actions's avatar
github-actions committed
153
154
155
156
157
158
159
160
161
162
163
164
            default -> targetPlayer.setProperty(prop.prop, value);
        };

        if (success) {
            if (targetPlayer == sender) {
                CommandHandler.sendTranslatedMessage(sender, "commands.generic.set_to", prop.name, valueStr);
            } else {
                String uidStr = targetPlayer.getAccount().getId();
                CommandHandler.sendTranslatedMessage(sender, "commands.generic.set_for_to", prop.name, uidStr, valueStr);
            }
        } else {
            if (prop.prop != PlayerProperty.PROP_NONE) {  // PseudoProps need to do their own error messages
AnimeGitB's avatar
AnimeGitB committed
165
166
                int min = targetPlayer.getPropertyMin(prop.prop);
                int max = targetPlayer.getPropertyMax(prop.prop);
github-actions's avatar
github-actions committed
167
168
169
170
171
172
173
174
                CommandHandler.sendTranslatedMessage(sender, "commands.generic.invalid.value_between", prop.name, min, max);
            }
        }
    }

    private boolean setTowerLevel(Player sender, Player targetPlayer, int topFloor) {
        List<Integer> floorIds = targetPlayer.getServer().getTowerSystem().getAllFloors();
        if (topFloor < 0 || topFloor > floorIds.size()) {
AnimeGitB's avatar
AnimeGitB committed
175
            CommandHandler.sendTranslatedMessage(sender, "commands.generic.invalid.value_between", "Tower Level", 0, floorIds.size());
github-actions's avatar
github-actions committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
            return false;
        }

        Map<Integer, TowerLevelRecord> recordMap = targetPlayer.getTowerManager().getRecordMap();
        // Add records for each unlocked floor
        for (int floor : floorIds.subList(0, topFloor)) {
            if (!recordMap.containsKey(floor)) {
                recordMap.put(floor, new TowerLevelRecord(floor));
            }
        }
        // Remove records for each floor past our target
        for (int floor : floorIds.subList(topFloor, floorIds.size())) {
            if (recordMap.containsKey(floor)) {
                recordMap.remove(floor);
            }
        }
        // Six stars required on Floor 8 to unlock Floor 9+
        if (topFloor > 8) {
            recordMap.get(floorIds.get(7)).setLevelStars(0, 6);  // levelIds seem to start at 1 for Floor 1 Chamber 1, so this doesn't get shown at all
        }
        return true;
    }

    private boolean setBool(Player sender, Player targetPlayer, PseudoProp pseudoProp, int value) {
        boolean enabled = switch (pseudoProp) {
            case GOD_MODE -> targetPlayer.inGodmode();
202
            case UNLIMITED_STAMINA -> targetPlayer.getUnlimitedStamina();
github-actions's avatar
github-actions committed
203
204
205
206
207
208
209
210
211
212
213
214
215
            case UNLIMITED_ENERGY -> !targetPlayer.getEnergyManager().getEnergyUsage();
            default -> false;
        };
        enabled = switch (value) {
            case -1 -> !enabled;
            case 0 -> false;
            default -> true;
        };

        switch (pseudoProp) {
            case GOD_MODE:
                targetPlayer.setGodmode(enabled);
                break;
216
            case UNLIMITED_STAMINA:
github-actions's avatar
github-actions committed
217
218
219
220
221
222
223
224
225
226
                targetPlayer.setUnlimitedStamina(enabled);
                break;
            case UNLIMITED_ENERGY:
                targetPlayer.getEnergyManager().setEnergyUsage(!enabled);
                break;
            default:
                return false;
        }
        return true;
    }
227
228
229
230
231

    private boolean setOpenState(Player targetPlayer, int state, int value) {
        targetPlayer.sendPacket(new PacketOpenStateChangeNotify(state, value));
        return true;
    }
232

233
234
    // List of map areas. Unfortunately, there is no readily available source for them in excels or bins.
    final static private List<Integer> sceneAreas = List.of(1,2,3,4,5,6,7,8,9,10,11,12,13,14,17,18,19,20,21,22,23,24,25,29,100,101,102,103,200,210,300,400,401,402,403);
235
236
    private boolean unlockMap(Player targetPlayer) {
        // Unlock.
237
        GameData.getScenePointsPerScene().forEach((sceneId, scenePoints) -> {
238
            // Unlock trans points.
239
            targetPlayer.getUnlockedScenePoints(sceneId).addAll(scenePoints);
240

241
242
243
            // Unlock map areas.
            targetPlayer.getUnlockedSceneAreas(sceneId).addAll(sceneAreas);
        });
244
245
246

        // Send notify.
        int playerScene = targetPlayer.getSceneId();
247
248
        targetPlayer.sendPacket(new PacketScenePointUnlockNotify(playerScene, targetPlayer.getUnlockedScenePoints(playerScene)));
        targetPlayer.sendPacket(new PacketSceneAreaUnlockNotify(playerScene, targetPlayer.getUnlockedSceneAreas(playerScene)));
249
250
        return true;
    }
github-actions's avatar
github-actions committed
251
}