aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java
blob: 98859bee0edd0198e60e0a071be4b294d792d575 (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
package de.hysky.skyblocker.skyblock.item;

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

import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.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.text.Text;

public class ItemProtection {

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

	public static boolean isItemProtected(ItemStack stack) {
		if (stack == null) return false;
        String itemUuid = ItemUtils.getItemUuid(stack);
        return SkyblockerConfigManager.get().general.protectedItems.contains(itemUuid);
	}

	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();

		if (Utils.isOnSkyblock()) {
			String itemUuid = ItemUtils.getItemUuid(heldItem);

			if (!itemUuid.isEmpty()) {
				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;
	}
}