PlayerCommands.java 10.4 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
package emu.grasscutter.commands;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
7
import java.util.Map;
Melledy's avatar
Melledy committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.entity.EntityAvatar;
import emu.grasscutter.game.entity.EntityItem;
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.game.entity.GenshinEntity;
import emu.grasscutter.game.inventory.GenshinItem;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
import emu.grasscutter.server.packet.send.PacketItemAddHintNotify;
import emu.grasscutter.utils.Position;

public class PlayerCommands {
27
28
29
	private static HashMap<String, PlayerCommand> commandList = new HashMap<String, PlayerCommand>();
	private static HashMap<String, PlayerCommand> commandAliasList = new HashMap<String, PlayerCommand>();

Melledy's avatar
Melledy committed
30
31
32
33
34
35
36
37
38
39
40
41
	
	static {
		try {
			// Look for classes
			for (Class<?> cls : PlayerCommands.class.getDeclaredClasses()) {
				// Get non abstract classes
			    if (!Modifier.isAbstract(cls.getModifiers())) {
			    	Command commandAnnotation = cls.getAnnotation(Command.class);
			    	PlayerCommand command = (PlayerCommand) cls.newInstance();
			    	
			    	if (commandAnnotation != null) {
			    		command.setLevel(commandAnnotation.gmLevel());
42
43
						command.setHelpText(commandAnnotation.helpText());
						for (String alias : commandAnnotation.aliases()) {
Melledy's avatar
Melledy committed
44
45
46
			    			if (alias.length() == 0) {
			    				continue;
			    			}
47
48
49

							String commandName = alias;
							commandAliasList.put(commandName, command);
Melledy's avatar
Melledy committed
50
51
			    		}
			    	}
52
53
54

			    	String commandName = cls.getSimpleName().toLowerCase();
			    	commandList.put(commandName, command);
Melledy's avatar
Melledy committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
			    }
		
			}
		} catch (Exception e) {
			
		}
	}

	public static void handle(GenshinPlayer player, String msg) {
		String[] split = msg.split(" ");
		
		// End if invalid
		if (split.length == 0) {
			return;
		}
70
71
72
73

		String first = split[0].toLowerCase().substring(1);
		PlayerCommand c = PlayerCommands.commandList.get(first);
		PlayerCommand a = PlayerCommands.commandAliasList.get(first);
Melledy's avatar
Melledy committed
74
		
75
76
		if (c != null || a != null) {
			PlayerCommand cmd = c != null ? c : a;
Melledy's avatar
Melledy committed
77
			// Level check
78
			if (player.getGmLevel() < cmd.getLevel()) {
Melledy's avatar
Melledy committed
79
80
81
				return;
			}
			// Execute
Melledy's avatar
Melledy committed
82
			int len = Math.min(split[0].length() + 1, msg.length());
83
			cmd.execute(player, msg.substring(len));
Melledy's avatar
Melledy committed
84
85
86
87
88
89
		}
	}
	
	public static abstract class PlayerCommand {
		// GM level required to use this command
		private int level;
90
91
		private String helpText;

Melledy's avatar
Melledy committed
92
93
		protected int getLevel() { return this.level; }
		protected void setLevel(int minLevel) { this.level = minLevel; }
94
95
96
97

		protected String getHelpText() { return this.helpText; }
		protected void setHelpText(String helpText) { this.helpText = helpText; }

Melledy's avatar
Melledy committed
98
99
100
101
102
		// Main
		public abstract void execute(GenshinPlayer player, String raw);
	}
	
	// ================ Commands ================
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

	@Command(aliases = {"h"}, helpText = "Shows this command")
	public static class Help extends PlayerCommand {

		@Override
		public void execute(GenshinPlayer player, String raw) {
			String helpMessage = "Grasscutter Commands: ";
			for (Map.Entry<String, PlayerCommand> cmd : commandList.entrySet()) {

				helpMessage += "\n" + cmd.getKey() + " - " + cmd.getValue().helpText;
			}

			player.dropMessage(helpMessage);
		}
	}
Melledy's avatar
Melledy committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	
	@Command(aliases = {"g", "item", "additem"}, helpText = "/give [item id] [count] - Gives {count} amount of {item id}")
	public static class Give extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			String[] split = raw.split(" ");
			int itemId = 0, count = 1;
			
			try {
				itemId = Integer.parseInt(split[0]);
			} catch (Exception e) {
				itemId = 0;
			}
			
			try {
				count = Math.max(Math.min(Integer.parseInt(split[1]), Integer.MAX_VALUE), 1);
			} catch (Exception e) {
				count = 1;
			}
			
			// Give
			ItemData itemData = GenshinData.getItemDataMap().get(itemId);
			GenshinItem item;
			
			if (itemData == null) {
				player.dropMessage("Error: Item data not found");
				return;
			}
			
			if (itemData.isEquip()) {
				List<GenshinItem> items = new LinkedList<>();
				for (int i = 0; i < count; i++) {
					item = new GenshinItem(itemData);
					items.add(item);
				}
				player.getInventory().addItems(items);
				player.sendPacket(new PacketItemAddHintNotify(items, ActionReason.SubfieldDrop));
			} else {
				item = new GenshinItem(itemData, count);
				player.getInventory().addItem(item);
				player.sendPacket(new PacketItemAddHintNotify(item, ActionReason.SubfieldDrop));
			}
		}
	}
	
	@Command(aliases = {"d"}, helpText = "/drop [item id] [count] - Drops {count} amount of {item id}")
	public static class Drop extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			String[] split = raw.split(" ");
			int itemId = 0, count = 1;
			
			try {
				itemId = Integer.parseInt(split[0]);
			} catch (Exception e) {
				itemId = 0;
			}
			
			try {
				count = Math.max(Math.min(Integer.parseInt(split[1]), Integer.MAX_VALUE), 1);
			} catch (Exception e) {
				count = 1;
			}
			
			// Give
			ItemData itemData = GenshinData.getItemDataMap().get(itemId);
			
			if (itemData == null) {
				player.dropMessage("Error: Item data not found");
				return;
			}
			
			if (itemData.isEquip()) {
				float range = (5f + (.1f * count));
				for (int i = 0; i < count; i++) {
					Position pos = player.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
194
195
					EntityItem entity = new EntityItem(player.getScene(), player, itemData, pos, 1);
					player.getScene().addEntity(entity);
Melledy's avatar
Melledy committed
196
197
				}
			} else {
198
199
				EntityItem entity = new EntityItem(player.getScene(), player, itemData, player.getPos().clone().addY(3f), count);
				player.getScene().addEntity(entity);
Melledy's avatar
Melledy committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
			}
		}
	}
	
	@Command(helpText = "/spawn [monster id] [count] - Creates {count} amount of {item id}")
	public static class Spawn extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			String[] split = raw.split(" ");
			int monsterId = 0, count = 1, level = 1;
			
			try {
				monsterId = Integer.parseInt(split[0]);
			} catch (Exception e) {
				monsterId = 0;
			}
			
			try {
				level = Math.max(Math.min(Integer.parseInt(split[1]), 200), 1);
			} catch (Exception e) {
				level = 1;
			}
			
			try {
				count = Math.max(Math.min(Integer.parseInt(split[2]), 1000), 1);
			} catch (Exception e) {
				count = 1;
			}
			
			// Give
			MonsterData monsterData = GenshinData.getMonsterDataMap().get(monsterId);
			
			if (monsterData == null) {
				player.dropMessage("Error: Monster data not found");
				return;
			}
			
			float range = (5f + (.1f * count));
			for (int i = 0; i < count; i++) {
				Position pos = player.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
240
241
				EntityMonster entity = new EntityMonster(player.getScene(), monsterData, pos, level);
				player.getScene().addEntity(entity);
Melledy's avatar
Melledy committed
242
243
244
245
246
247
248
249
250
			}
		}
	}
	
	@Command(helpText = "/killall")
	public static class KillAll extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			List<GenshinEntity> toRemove = new LinkedList<>();
251
			for (GenshinEntity entity : player.getScene().getEntities().values()) {
Melledy's avatar
Melledy committed
252
253
254
255
				if (entity instanceof EntityMonster) {
					toRemove.add(entity);
				}
			}
256
			toRemove.forEach(e -> player.getScene().killEntity(e, 0));
Melledy's avatar
Melledy committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
		}
	}
	
	@Command(helpText = "/resetconst - Resets all constellations for the currently active character")
	public static class ResetConst extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			EntityAvatar entity = player.getTeamManager().getCurrentAvatarEntity();
			
			if (entity == null) {
				return;
			}
			
			GenshinAvatar avatar = entity.getAvatar();
			
			avatar.getTalentIdList().clear();
			avatar.setCoreProudSkillLevel(0);
			avatar.recalcStats();
			avatar.save();
			
			player.dropMessage("Constellations for " + entity.getAvatar().getAvatarData().getName() + " have been reset. Please relogin to see changes.");
		}
	}
	
	@Command(helpText = "/godmode - Prevents you from taking damage")
	public static class Godmode extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			player.setGodmode(!player.hasGodmode());
			player.dropMessage("Godmode is now " + (player.hasGodmode() ? "ON" : "OFF"));
		}
	}
	
	@Command(helpText = "/sethp [hp]")
	public static class Sethp extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			String[] split = raw.split(" ");
			int hp = 0;
			
			try {
				hp = Math.max(Integer.parseInt(split[0]), 1);
			} catch (Exception e) {
				hp = 1;
			}
			
			EntityAvatar entity = player.getTeamManager().getCurrentAvatarEntity();
			
			if (entity == null) {
				return;
			}
			
			entity.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, hp);
310
			entity.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(entity, FightProperty.FIGHT_PROP_CUR_HP));
Melledy's avatar
Melledy committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
		}
	}
	
	@Command(aliases = {"clearart"}, helpText = "/clearartifacts")
	public static class ClearArtifacts extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			List<GenshinItem> toRemove = new LinkedList<>();
			for (GenshinItem item : player.getInventory().getItems().values()) {
				if (item.getItemType() == ItemType.ITEM_RELIQUARY && item.getLevel() == 1 && item.getExp() == 0 && !item.isLocked() && !item.isEquipped()) {
					toRemove.add(item);
				}
			}
			
			player.getInventory().removeItems(toRemove);
		}
	}
Melledy's avatar
Melledy committed
328
329
330
331
332
333
334
335
336
337
338
339
340
	
	@Command(aliases = {"scene"}, helpText = "/Changescene [Scene id]")
	public static class ChangeScene extends PlayerCommand {
		@Override
		public void execute(GenshinPlayer player, String raw) {
			int sceneId = 0;
		
			try {
				sceneId = Integer.parseInt(raw);
			} catch (Exception e) {
				return;
			}
			
Melledy's avatar
Melledy committed
341
342
343
344
345
			boolean result = player.getWorld().transferPlayerToScene(player, sceneId, player.getPos());
			
			if (!result) {
				player.dropMessage("Scene does not exist!");
			}
Melledy's avatar
Melledy committed
346
347
		}
	}
Melledy's avatar
Melledy committed
348
}