diff options
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java')
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java index 2d929c28..7e04652c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemProtection.java @@ -10,13 +10,25 @@ 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.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.option.KeyBinding; import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.entity.player.PlayerEntity; 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); } @@ -61,4 +73,46 @@ public class ItemProtection { return Command.SINGLE_SUCCESS; } + + public static void handleKeyPressed(ItemStack heldItem) { + PlayerEntity playerEntity = MinecraftClient.getInstance().player; + if (playerEntity == null){ + return; + } + if (!Utils.isOnSkyblock()) { + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.unableToProtect"))); + return; + } + + if (heldItem.isEmpty()) { + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.noItemUuid"))); + 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(); + + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.added", heldItem.getName()))); + } else { + protectedItems.remove(itemUuid); + SkyblockerConfigManager.save(); + + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.removed", heldItem.getName()))); + } + } else { + playerEntity.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.itemProtection.noItemUuid"))); + } + } + + public static void handleHotbarKeyPressed(ClientPlayerEntity player) { + while (itemProtection.wasPressed()) { + ItemStack heldItem = player.getMainHandStack(); + handleKeyPressed(heldItem); + } + } } |