aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/PackDevCommand.java
blob: e1504472ecf0cc1ef46c284825800c6a9aaf8f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.commands.dev;

import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.commands.ClientCommandBase;
import io.github.moulberry.notenoughupdates.core.util.MiscUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.AbstractClientPlayer;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityArmorStand;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;

import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;

public class PackDevCommand extends ClientCommandBase {
	static Minecraft mc = Minecraft.getMinecraft();

	public PackDevCommand() {
		super("neupackdev");
	}

	private static final HashMap<String, Command<?, ?>> commands = new HashMap<String, Command<?, ?>>() {{
		put(
			"getnpc",
			new Command<>(
				"NPC",
				() -> mc.theWorld.playerEntities,
				true,
				AbstractClientPlayer.class
			)
		);
		put(
			"getnpcs",
			new Command<>(
				"NPC",
				() -> mc.theWorld.playerEntities,
				false,
				AbstractClientPlayer.class
			)
		);
		put(
			"getmob",
			new Command<>(
				"mob",
				() -> mc.theWorld.loadedEntityList,
				true,
				EntityLiving.class
			)
		);
		put(
			"getmobs",
			new Command<>(
				"mob",
				() -> mc.theWorld.loadedEntityList,
				false,
				EntityLiving.class
			)
		);
		put(
			"getarmorstand",
			new Command<>(
				"armor stand",
				() -> mc.theWorld.loadedEntityList,
				true,
				EntityArmorStand.class
			)
		);
		put(
			"getarmorstands",
			new Command<>(
				"armor stand",
				() -> mc.theWorld.loadedEntityList,
				false,
				EntityArmorStand.class
			)
		);
	}};

	@Override
	public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) {
		return args.length == 1 ? getListOfStringsMatchingLastWord(args, commands.keySet()) : null;
	}

	public static void togglePackDeveloperMode(ICommandSender sender) {
		NotEnoughUpdates.INSTANCE.packDevEnabled = !NotEnoughUpdates.INSTANCE.packDevEnabled;
		if (NotEnoughUpdates.INSTANCE.packDevEnabled) {
			sender.addChatMessage(new ChatComponentText(
				EnumChatFormatting.GREEN + "Enabled pack developer mode."));
		} else {
			sender.addChatMessage(new ChatComponentText(
				EnumChatFormatting.RED + "Disabled pack developer mode."));
		}
	}

	@Override
	public void processCommand(ICommandSender sender, String[] args) throws CommandException {
		if (args.length == 0) {
			togglePackDeveloperMode(sender);
			return;
		}

		double dist = 5.0;
		if (args.length >= 2) {
			try {
				dist = Double.parseDouble(args[1]);
			} catch (NumberFormatException e) {
				sender.addChatMessage(new ChatComponentText(
					EnumChatFormatting.RED + "Invalid distance! Must be a number, defaulting to a radius of 5."));
			}
		}

		StringBuilder output;
		String subCommand = args[0].toLowerCase();
		if (commands.containsKey(subCommand)) {
			Command<?, ?> command = commands.get(subCommand);
			output = command.getData(dist);
		} else if (subCommand.equals("getall")) {
			output = getAll(dist);
		} else if (subCommand.equals("getallclose")) {
			output = getAllClose(dist);
		} else {
			sender.addChatMessage(new ChatComponentText(
				EnumChatFormatting.RED + "Invalid sub-command."));
			return;
		}

		if (output.length() != 0) {
			MiscUtils.copyToClipboard(output.toString());
		}
	}

	private static StringBuilder getAllClose(Double dist) {
		StringBuilder sb = new StringBuilder();
		sb.append(commands.get("getmob").getData(dist));
		sb.append(commands.get("getarmorstand").getData(dist));
		sb.append(commands.get("getnpc").getData(dist));
		return sb;
	}

	private static StringBuilder getAll(Double dist) {
		StringBuilder sb = new StringBuilder();
		sb.append(commands.get("getmobs").getData(dist));
		sb.append(commands.get("getarmorstands").getData(dist));
		sb.append(commands.get("getnpcs").getData(dist));
		return sb;
	}

	public static <T extends EntityLivingBase> StringBuilder livingBaseDataBuilder(T entity, Class<T> clazz) {
		StringBuilder entityData = new StringBuilder();
		if (EntityPlayer.class.isAssignableFrom(entity.getClass())) {
			EntityPlayer entityPlayer = (EntityPlayer) entity;

			// NPC Information
			String skinResourcePath = ((AbstractClientPlayer) entityPlayer).getLocationSkin().getResourcePath();
			entityData
				.append("Player Id: ")
				.append(entityPlayer.getUniqueID() != null ? entityPlayer.getUniqueID().toString() : "null")
				.append(entityPlayer.getCustomNameTag() != null ? entityPlayer.getCustomNameTag() : "null")
				.append("\nEntity Texture Id: ")
				.append(skinResourcePath != null ? skinResourcePath.replace("skins/", "") : "null");
		}

		if (!clazz.isAssignableFrom(entity.getClass())) {
			return entityData;
		}

		//Entity Information
		entityData
			.append("Entity Id: ")
			.append(entity.getEntityId())
			.append("\nMob: ")
			.append(entity.getName() != null ? entity.getName() : "null")
			.append("\nCustom Name: ")
			.append(entity.getCustomNameTag() != null ? entity.getCustomNameTag() : "null");

		//Held Item
		if (entity.getHeldItem() != null) {
			entityData
				.append("\nItem: ")
				.append(entity.getHeldItem())
				.append("\nItem Display Name: ")
				.append(entity.getHeldItem().getDisplayName() != null
					? entity.getHeldItem().getDisplayName()
					: "null")
				.append("\nItem Tag Compound: ");
			NBTTagCompound heldItemTagCompound = entity.getHeldItem().getTagCompound();
			if (heldItemTagCompound != null) {
				String heldItemString = heldItemTagCompound.toString();
				NBTBase extraAttrTag = heldItemTagCompound.getTag("ExtraAttributes");
				entityData
					.append(heldItemString != null ? heldItemString : "null")
					.append("\nItem Tag Compound Extra Attributes: ")
					.append(extraAttrTag != null ? extraAttrTag : "null");
			} else {
				entityData.append("null");
			}

		} else {
			entityData.append("\nItem: null");
		}

		entityData.append(armorDataBuilder(entity)).append("\n\n");

		return entityData;
	}

	private static final String[] armorPieceTypes = {"Boots", "Leggings", "Chestplate", "Helmet"};

	public static <T extends EntityLivingBase> StringBuilder armorDataBuilder(T entity) {
		StringBuilder armorData = new StringBuilder();
		for (int i = 0; i < 4; i++) {
			ItemStack currentArmor = entity.getCurrentArmor(0);
			armorData.append(String.format("\n%s: ", armo