GiveArtifactCommand.java 3.07 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;

19
@Command(label = "giveart", usage = "giveart <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", aliases = {"gart"}, permission = "player.giveart")
20
public final class GiveArtifactCommand implements CommandHandler {
21
22
23
24
25
	@Override
	public String description() {
		return translate("commands.giveArtifact.description");
	}

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

AnimeGitB's avatar
AnimeGitB committed
33
		if (args.size() < 2) {
34
			CommandHandler.sendMessage(sender, translate("commands.giveArtifact.usage"));
35
36
37
			return;
		}

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

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

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

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

		GameItem item = new GameItem(itemData);
		item.setLevel(level);
		item.setMainPropId(mainPropId);
91
		item.getAppendPropIdList().clear();
92
93
94
		item.getAppendPropIdList().addAll(appendPropIdList);
		targetPlayer.getInventory().addItem(item, ActionReason.SubfieldDrop);

95
		CommandHandler.sendMessage(sender, translate("commands.giveArtifact.success", Integer.toString(itemId), Integer.toString(targetPlayer.getUid())));
96
97
98
	}
}