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

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

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import me.xmrvizzy.skyblocker.config.SkyblockerConfigManager;
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.command.CommandRegistryAccess;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;

public class ItemProtection {
	
	public static void init() {
		ClientCommandRegistrationCallback.EVENT.register(ItemProtection::registerCommand);
	}

	public static boolean isItemProtected(ItemStack stack) {
		if (stack == null || stack.isEmpty()) return false;
		
		NbtCompound nbt = stack.getNbt();
		
		if (nbt != null && nbt.contains("ExtraAttributes")) {
			NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes");
			String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : "";
			
			return SkyblockerConfigManager.get().general.protectedItems.contains(itemUuid);
		}
		
		return false;
	}
	
	private static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
		dispatcher.register(ClientCommandManager.literal("skyblocker")
				.then(ClientCommandManager.literal("protectItem")
						.executes(context -> protectMyItem(context.getSource()))));
	}
	
	private static int protectMyItem(FabricClientCommandSource source) {
		ItemStack heldItem = source.getPlayer().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) {
				ObjectOpenHashSet<String> protectedItems = SkyblockerConfigManager.get().general.protectedItems;
				
				if (!protectedItems.contains(itemUuid)) {
					protectedItems.add(itemUuid);
					SkyblockerConfigManager.save();
					
					source.sendFeedback(Text.translatable("skyblocker.itemProtection.added", heldItem.getName()));
				} else {
					protectedItems.remove(itemUuid);
					SkyblockerConfigManager.save();
					
					source.sendFeedback(Text.translatable("skyblocker.itemProtection.removed", heldItem.getName()));
				}
			} else {
				source.sendFeedback(Text.translatable("skyblocker.itemProtection.noItemUuid"));
			}
		} else {
			source.sendFeedback(Text.translatable("skyblocker.itemProtection.unableToProtect"));
		}
		
		return Command.SINGLE_SUCCESS;
	}
}