GiveArtifactCommand.java 7.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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;
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 checks
AnimeGitB's avatar
AnimeGitB committed
117
		if (targetPlayer == null) {
Secretboy's avatar
Secretboy committed
118
			CommandHandler.sendMessage(sender, translate(sender, "commands.execution.need_target"));
AnimeGitB's avatar
AnimeGitB committed
119
120
121
			return;
		}
		if (args.size() < 2) {
Secretboy's avatar
Secretboy committed
122
			CommandHandler.sendMessage(sender, translate(sender, "commands.giveArtifact.usage"));
123
124
125
			return;
		}

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

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

141
142
143
144
		// 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
145
		int mainPropId;
146

AnimeGitB's avatar
AnimeGitB committed
147
		try {
148
			mainPropId = Integer.parseInt(mainPropIdString);
AnimeGitB's avatar
AnimeGitB committed
149
		} catch (NumberFormatException ignored) {
150
151
152
153
154
155
156
157
			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
158
			CommandHandler.sendMessage(sender, translate(sender, "commands.execution.argument_error"));
159
160
161
			return;
		}

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

173
174
		// Get substats.
		ArrayList<Integer> appendPropIdList = new ArrayList<>();
AnimeGitB's avatar
AnimeGitB committed
175
		try {
176
			// Every remaining argument is a substat.
AnimeGitB's avatar
AnimeGitB committed
177
			args.forEach(it -> {
178
179
				// 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
180
181
182
183
184
185
186
187
188
				String[] arr;
				int n = 1;
				if ((arr = it.split(",")).length == 2) {
					it = arr[0];
					n = Integer.parseInt(arr[1]);
					if (n > 200) {
						n = 200;
					}
				}
189
190
191
192
193
194

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

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

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

Secretboy's avatar
Secretboy committed
209
		CommandHandler.sendMessage(sender, translate(sender, "commands.giveArtifact.success", Integer.toString(itemId), Integer.toString(targetPlayer.getUid())));
210
211
212
	}
}