GiveArtifactCommand.java 7.63 KB
Newer Older
1
2
3
4
5
6
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.data.excels.ItemData;
8
9
10
11
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActionReason;
12
import emu.grasscutter.game.inventory.EquipType;
13
14
15
16

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
17
18
import java.util.Map;
import static java.util.Map.entry;
19

20
21
import static emu.grasscutter.utils.Language.translate;

22
@Command(label = "giveart", usage = "giveart <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", aliases = {"gart"}, permission = "player.giveart", permissionTargeted = "player.giveart.others", description = "commands.giveArtifact.description")
23
public final class GiveArtifactCommand implements CommandHandler {
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
	private static final Map<String, Map<EquipType, Integer>> mainPropMap = Map.ofEntries(
		entry("hp", Map.ofEntries(entry(EquipType.EQUIP_BRACER, 14001))),
		entry("hp%", Map.ofEntries(entry(EquipType.EQUIP_SHOES, 10980), entry(EquipType.EQUIP_RING, 50980), entry(EquipType.EQUIP_DRESS, 30980))),
		entry("atk", Map.ofEntries(entry(EquipType.EQUIP_NECKLACE, 12001))),
		entry("atk%", Map.ofEntries(entry(EquipType.EQUIP_SHOES, 10990), entry(EquipType.EQUIP_RING, 50990), entry(EquipType.EQUIP_DRESS, 30990))),
		entry("def%", Map.ofEntries(entry(EquipType.EQUIP_SHOES, 10970), entry(EquipType.EQUIP_RING, 50970), entry(EquipType.EQUIP_DRESS, 30970))),
		entry("er", Map.ofEntries(entry(EquipType.EQUIP_SHOES, 10960))),
		entry("em", Map.ofEntries(entry(EquipType.EQUIP_SHOES, 10950), entry(EquipType.EQUIP_RING, 50880), entry(EquipType.EQUIP_DRESS, 30930))),
		entry("hb", Map.ofEntries(entry(EquipType.EQUIP_DRESS, 30940))),
		entry("cdmg", Map.ofEntries(entry(EquipType.EQUIP_DRESS, 30950))),
		entry("cr", Map.ofEntries(entry(EquipType.EQUIP_DRESS, 30960))),
		entry("phys%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50890))),
		entry("dendro%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50900))),
		entry("geo%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50910))),
		entry("anemo%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50920))),
		entry("hydro%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50930))),
		entry("cryo%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50940))),
		entry("electro%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50950))),
		entry("pyro%", Map.ofEntries(entry(EquipType.EQUIP_RING, 50960)))
	);
	private static final Map<String, String> appendPropMap = Map.ofEntries(
		entry("hp", "0102"),
		entry("hp%", "0103"),
		entry("atk", "0105"),
		entry("atk%", "0106"),
		entry("def", "0108"),
		entry("def%", "0109"),
		entry("er", "0123"),
		entry("em", "0124"),
		entry("cr", "0120"),
		entry("cdmg", "0122")
	);

	private int getAppendPropId(String substatText, ItemData itemData) {
		int res;

		// If the given substat text is an integer, we just use that
		// as the append prop ID.
		try {
			res = Integer.parseInt(substatText);
			return res;
		}
		catch (NumberFormatException ignores) {
			// No need to handle this here. We just continue with the
			// possibility of the argument being a substat string.
		}

		// If the argument was not an integer, we try to determine
		// the append prop ID from the given text + artifact information.
ImmuState's avatar
ImmuState committed
73
74
		// A substat string has the format `substat_tier`, with the
		// `_tier` part being optional.
75
		String[] substatArgs = substatText.split("_");
ImmuState's avatar
ImmuState committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
		String substatType;
		int substatTier;

		if (substatArgs.length == 1) {
			substatType = substatArgs[0];
			substatTier = 
				itemData.getRankLevel() == 1 ? 2 
				: itemData.getRankLevel() == 2 ? 3 
				: 4;
		}
		else if (substatArgs.length == 2) {
			substatType = substatArgs[0];
			substatTier = Integer.parseInt(substatArgs[1]);
		}
		else {
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
			throw new IllegalArgumentException();
		}

		// Check if the specified tier is legal for the artifact rarity.
		if (substatTier < 1 || substatTier > 4) {
			throw new IllegalArgumentException();
		}
		if (itemData.getRankLevel() == 1 && substatTier > 2) {
			throw new IllegalArgumentException();
		}
		if (itemData.getRankLevel() == 2 && substatTier > 3) {
			throw new IllegalArgumentException();
		} 

		// Check if the given substat type string is a legal stat.
		if (!appendPropMap.containsKey(substatType)) {
			throw new IllegalArgumentException();
		}

		// Build the append prop ID.
		return Integer.parseInt(Integer.toString(itemData.getRankLevel()) + appendPropMap.get(substatType) + Integer.toString(substatTier));
	}
113

114
	@Override
AnimeGitB's avatar
AnimeGitB committed
115
	public void execute(Player sender, Player targetPlayer, List<String> args) {
116
		// Sanity check
AnimeGitB's avatar
AnimeGitB committed
117
		if (args.size() < 2) {
Secretboy's avatar
Secretboy committed
118
			CommandHandler.sendMessage(sender, translate(sender, "commands.giveArtifact.usage"));
119
120
121
			return;
		}

122
		// Get the artifact piece ID from the arguments.
AnimeGitB's avatar
AnimeGitB committed
123
124
125
126
		int itemId;
		try {
			itemId = Integer.parseInt(args.remove(0));
		} catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
127
			CommandHandler.sendMessage(sender, translate(sender, "commands.giveArtifact.id_error"));
AnimeGitB's avatar
AnimeGitB committed
128
129
			return;
		}
130

AnimeGitB's avatar
AnimeGitB committed
131
132
		ItemData itemData = GameData.getItemDataMap().get(itemId);
		if (itemData.getItemType() != ItemType.ITEM_RELIQUARY) {
Secretboy's avatar
Secretboy committed
133
			CommandHandler.sendMessage(sender, translate(sender, "commands.giveArtifact.id_error"));
134
135
136
			return;
		}

137
138
139
140
		// Get the main stat from the arguments.
		// If the given argument is an integer, we use that.
		// If not, we check if the argument string is in the main prop map.
		String mainPropIdString = args.remove(0);
AnimeGitB's avatar
AnimeGitB committed
141
		int mainPropId;
142

AnimeGitB's avatar
AnimeGitB committed
143
		try {
144
			mainPropId = Integer.parseInt(mainPropIdString);
AnimeGitB's avatar
AnimeGitB committed
145
		} catch (NumberFormatException ignored) {
146
147
148
149
150
151
152
153
			mainPropId = -1;
		}

		if (mainPropMap.containsKey(mainPropIdString) && mainPropMap.get(mainPropIdString).containsKey(itemData.getEquipType())) {
			mainPropId = mainPropMap.get(mainPropIdString).get(itemData.getEquipType());
		}

		if (mainPropId == -1) {
Secretboy's avatar
Secretboy committed
154
			CommandHandler.sendMessage(sender, translate(sender, "commands.execution.argument_error"));
155
156
157
			return;
		}

158
		// Get the level from the arguments.
AnimeGitB's avatar
AnimeGitB committed
159
160
161
		int level = 1;
		try {
			int last = Integer.parseInt(args.get(args.size()-1));
AnimeGitB's avatar
AnimeGitB committed
162
			if (last > 0 && last < 22) {  // Luckily appendPropIds aren't in the range of [1,21] 
AnimeGitB's avatar
AnimeGitB committed
163
164
165
166
167
				level = last;
				args.remove(args.size()-1);
			}
		} catch (NumberFormatException ignored) {  // Could be a stat,times string so no need to panic
		}
168

169
170
		// Get substats.
		ArrayList<Integer> appendPropIdList = new ArrayList<>();
AnimeGitB's avatar
AnimeGitB committed
171
		try {
172
			// Every remaining argument is a substat.
AnimeGitB's avatar
AnimeGitB committed
173
			args.forEach(it -> {
174
175
				// The substat syntax permits specifying a number of rolls for the given
				// substat. Split the string into stat and number if that is the case here.
AnimeGitB's avatar
AnimeGitB committed
176
177
178
179
180
181
182
183
184
				String[] arr;
				int n = 1;
				if ((arr = it.split(",")).length == 2) {
					it = arr[0];
					n = Integer.parseInt(arr[1]);
					if (n > 200) {
						n = 200;
					}
				}
185
186
187
188
189
190

				// Determine the substat ID.
				int appendPropId = getAppendPropId(it, itemData);

				// Add the current substat.
				appendPropIdList.addAll(Collections.nCopies(n, appendPropId));
AnimeGitB's avatar
AnimeGitB committed
191
192
			});
		} catch (Exception ignored) {
Secretboy's avatar
Secretboy committed
193
			CommandHandler.sendMessage(sender, translate(sender, "commands.execution.argument_error"));
194
195
196
			return;
		}

197
		// Create item for the artifact.
198
199
200
		GameItem item = new GameItem(itemData);
		item.setLevel(level);
		item.setMainPropId(mainPropId);
201
		item.getAppendPropIdList().clear();
202
203
204
		item.getAppendPropIdList().addAll(appendPropIdList);
		targetPlayer.getInventory().addItem(item, ActionReason.SubfieldDrop);

Secretboy's avatar
Secretboy committed
205
		CommandHandler.sendMessage(sender, translate(sender, "commands.giveArtifact.success", Integer.toString(itemId), Integer.toString(targetPlayer.getUid())));
206
207
208
	}
}