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

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.utils.Constants;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Utils;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
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.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;

public class ItemProtection {
	public static KeyBinding itemProtection;

	public static void init() {
		itemProtection = KeyBindingHelper.registerKeyBinding(new KeyBinding(
				"key.itemProtection",
				GLFW.GLFW_KEY_V,
				"key.categories.skyblocker"
		));
		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(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.added", heldItem.getName())));
				} else {
					protectedItems.remove(itemUuid);
					SkyblockerConfigManager.save();

					source.sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.removed", heldItem.getName())));
				}
			} else {
				source.sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.noItemUuid")));
			}
		} else {
			source.sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.unableToProtect")));
		}

		return Command.SINGLE_SUCCESS;
	}

	public static void handleKeyPressed(ItemStack heldItem) {
		if (!Utils.isOnSkyblock() || heldItem.isEmpty()) return;

		String itemUuid = ItemUtils.getItemUuid(heldItem);
		if (!itemUuid.isEmpty()) {
			ObjectOpenHashSet<String> protectedItems = SkyblockerConfigManager.get().general.protectedItems;

			if (!protectedItems.contains(itemUuid)) {
				protectedItems.add(itemUuid);
				SkyblockerConfigManager.save();
			} else {
				protectedItems.remove(itemUuid);
				SkyblockerConfigManager.save();
			}
		}
	}

	public static void handleHotbarKeyPressed(ClientPlayerEntity player) {
		while (itemProtection.wasPressed()) {
			ItemStack heldItem = player.getMainHandStack();
			handleKeyPressed(heldItem);
		}
	}
}