aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java
blob: 4ecff50819fddde4f49bace54ccb79ceb4a29b27 (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
package me.xmrvizzy.skyblocker.skyblock.item;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import me.xmrvizzy.skyblocker.config.SkyblockerConfig;
import me.xmrvizzy.skyblocker.utils.Utils;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.TextArgumentType;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;

public class CustomItemNames {

	public static void init() {
		ClientCommandRegistrationCallback.EVENT.register(CustomItemNames::registerCommands);
	}
	
	private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
		dispatcher.register(ClientCommandManager.literal("skyblocker")
				.then(ClientCommandManager.literal("renameItem")
						.executes(context -> renameItem(context.getSource(), null))
						.then(ClientCommandManager.argument("textComponent", TextArgumentType.text())
								.executes(context -> renameItem(context.getSource(), context.getArgument("textComponent", Text.class))))));
	}
	
	private static int renameItem(FabricClientCommandSource source, Text text) {
		MinecraftClient client = source.getClient();
		ItemStack heldItem = client.player.getMainHandStack();
		NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null;
		
		if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) {
			NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes");
			String itemUuid =  extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null;
			
			if (itemUuid != null) {
				Object2ObjectOpenHashMap<String, Text> customItemNames = SkyblockerConfig.get().general.customItemNames;
				
				if (text == null) {
					if (customItemNames.containsKey(itemUuid)) {
						//Remove custom item name when the text argument isn't passed
						customItemNames.remove(itemUuid);
						SkyblockerConfig.save();
						source.sendFeedback(Text.translatable("skyblocker.customItemNames.removed"));
					} else {
						source.sendFeedback(Text.translatable("skyblocker.customItemNames.neverHad"));
					}
				} else {
					//If the text is provided then set the item's custom name to it
					customItemNames.put(itemUuid, text);
					SkyblockerConfig.save();
					source.sendFeedback(Text.translatable("skyblocker.customItemNames.added"));
				}
			} else {
				source.sendError(Text.translatable("skyblocker.customItemNames.noItemUuid"));
			}
		} else {
			source.sendError(Text.translatable("skyblocker.customItemNames.unableToSetName"));
		}
		
		return Command.SINGLE_SUCCESS;
	}
}