GiveArtifactCommand.java 3.01 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
@Command(label = "giveart", usage = "giveart [player] <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", description = "Gives the player a specified artifact", aliases = {"gart"}, permission = "player.giveart")
18
19
20
public final class GiveArtifactCommand implements CommandHandler {
	@Override
	public void execute(Player sender, List<String> args) {
21
		int size = args.size(), target, itemId, mainPropId, level = 1;
22
		ArrayList<Integer> appendPropIdList = new ArrayList<>();
23
		String msg = "Usage: giveart|gart [player] <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]";
24
25
26
27
28
29
30
31

		if (sender == null && size < 2) {
			CommandHandler.sendMessage(null, msg);
			return;
		}

		if (size >= 2) {
			try {
32
33
34
35
36
37
38
39
				try {
					int last = Integer.parseInt(args.get(size - 1));
					if (last >= 1 && last <= 21) {
						level = last;
						size--;
					}
				} catch (NumberFormatException ignored) {
				}
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
				target = Integer.parseInt(args.get(0));
				int fromIdx;
				if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
					target = sender.getUid();
					itemId = Integer.parseInt(args.get(0));
					mainPropId = Integer.parseInt(args.get(1));
					fromIdx = 2;
				} else {
					target = Integer.parseInt(args.get(0));
					itemId = Integer.parseInt(args.get(1));
					mainPropId = Integer.parseInt(args.get(2));
					fromIdx = 3;
				}
				args.subList(fromIdx, size).forEach(it -> {
					String[] arr;
					int n = 1;
					if ((arr = it.split(",")).length == 2) {
						it = arr[0];
						n = Integer.parseInt(arr[1]);
					}
					appendPropIdList.addAll(Collections.nCopies(n, Integer.parseInt(it)));
				});
			} catch (Exception ignored) {
				CommandHandler.sendMessage(sender, msg);
				return;
			}
		} else {
			CommandHandler.sendMessage(sender, msg);
			return;
		}

		Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
		if (targetPlayer == null) {
			CommandHandler.sendMessage(sender, "Player not found.");
			return;
		}

		ItemData itemData = GameData.getItemDataMap().get(itemId);

		if (itemData.getItemType() != ItemType.ITEM_RELIQUARY) {
			CommandHandler.sendMessage(sender, "Invalid artifact ID.");
			return;
		}

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

		CommandHandler.sendMessage(sender, String.format("Given %s to %s.", itemId, target));
	}
}