GiveArtifactCommand.java 3.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActionReason;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

17
18
import static emu.grasscutter.utils.Language.translate;

方块君's avatar
方块君 committed
19
@Command(label = "giveart", usage = "giveart <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", aliases = {"gart"}, permission = "player.giveart", description = "commands.giveArtifact.description")
20
public final class GiveArtifactCommand implements CommandHandler {
21

22
	@Override
AnimeGitB's avatar
AnimeGitB committed
23
	public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
24
		if (targetPlayer == null) {
25
			CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
AnimeGitB's avatar
AnimeGitB committed
26
27
			return;
		}
28

AnimeGitB's avatar
AnimeGitB committed
29
		if (args.size() < 2) {
30
			CommandHandler.sendMessage(sender, translate("commands.giveArtifact.usage"));
31
32
33
			return;
		}

AnimeGitB's avatar
AnimeGitB committed
34
35
36
37
		int itemId;
		try {
			itemId = Integer.parseInt(args.remove(0));
		} catch (NumberFormatException ignored) {
38
			CommandHandler.sendMessage(sender, translate("commands.giveArtifact.id_error"));
AnimeGitB's avatar
AnimeGitB committed
39
40
41
42
			return;
		}
		ItemData itemData = GameData.getItemDataMap().get(itemId);
		if (itemData.getItemType() != ItemType.ITEM_RELIQUARY) {
43
			CommandHandler.sendMessage(sender, translate("commands.giveArtifact.id_error"));
44
45
46
			return;
		}

AnimeGitB's avatar
AnimeGitB committed
47
48
49
50
		int mainPropId;
		try {
			mainPropId = Integer.parseInt(args.remove(0));
		} catch (NumberFormatException ignored) {
51
			CommandHandler.sendMessage(sender, translate("commands.generic.execution.argument_error"));
52
53
54
			return;
		}

AnimeGitB's avatar
AnimeGitB committed
55
56
57
		int level = 1;
		try {
			int last = Integer.parseInt(args.get(args.size()-1));
AnimeGitB's avatar
AnimeGitB committed
58
			if (last > 0 && last < 22) {  // Luckily appendPropIds aren't in the range of [1,21] 
AnimeGitB's avatar
AnimeGitB committed
59
60
61
62
63
				level = last;
				args.remove(args.size()-1);
			}
		} catch (NumberFormatException ignored) {  // Could be a stat,times string so no need to panic
		}
64

65
		List<Integer> appendPropIdList = new ArrayList<>();
AnimeGitB's avatar
AnimeGitB committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		try {
			args.forEach(it -> {
				String[] arr;
				int n = 1;
				if ((arr = it.split(",")).length == 2) {
					it = arr[0];
					n = Integer.parseInt(arr[1]);
					if (n > 200) {
						n = 200;
					}
				}
				appendPropIdList.addAll(Collections.nCopies(n, Integer.parseInt(it)));
			});
		} catch (Exception ignored) {
80
			CommandHandler.sendMessage(sender, translate("commands.execution.argument_error"));
81
82
83
84
85
86
			return;
		}

		GameItem item = new GameItem(itemData);
		item.setLevel(level);
		item.setMainPropId(mainPropId);
87
		item.getAppendPropIdList().clear();
88
89
90
		item.getAppendPropIdList().addAll(appendPropIdList);
		targetPlayer.getInventory().addItem(item, ActionReason.SubfieldDrop);

91
		CommandHandler.sendMessage(sender, translate("commands.giveArtifact.success", Integer.toString(itemId), Integer.toString(targetPlayer.getUid())));
92
93
94
	}
}