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;

AnimeGitB's avatar
AnimeGitB committed
19
@Command(label = "giveart", usage = "giveart <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", description = "Gives the player a specified artifact", aliases = {"gart"}, permission = "player.giveart")
20
21
public final class GiveArtifactCommand implements CommandHandler {
	@Override
AnimeGitB's avatar
AnimeGitB committed
22
	public void execute(Player sender, Player targetPlayer, List<String> args) {
AnimeGitB's avatar
AnimeGitB committed
23
		if (targetPlayer == null) {
24
			CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
AnimeGitB's avatar
AnimeGitB committed
25
26
			return;
		}
27

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

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

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

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

64
		List<Integer> appendPropIdList = new ArrayList<>();
AnimeGitB's avatar
AnimeGitB committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
		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) {
79
			CommandHandler.sendMessage(sender, translate("commands.execution.argument_error"));
80
81
82
83
84
85
			return;
		}

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

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