саша

напиши мод который по нажатию бинда(настраивается через мод меню) меняет поножи с зачарованием на защиту на поножи с зачарование взрывоустойчевости которые находятся в хотбаре и наоборот при следующей активации. Сделай так что бы слот менялся с активного для смены понож и обратно после смены. Сделай настройку только через мод меню что бы ее не было в настройках управления

Kodari AI
KodariWed at 9:04 am 4
LeggingSwapper
Client-side Minecraft Fabric mod that swaps equipped leggings between Protection and Blast Protection enchanted variants stored in the hotbar. Pressing a configurable keybind toggles the leggings: it swaps the currently equipped pair with the matching enchanted pair found in the hotbar, then returns the player's selected hotbar slot back to its original position after the swap. The keybind is configured exclusively through ModMenu and is hidden from the vanilla Controls settings screen. The mod scans the hotbar for leggings with the required enchantment level, performs the swap via inventory manipulation, and restores the active slot, providing a seamless one-key toggle between defensive enchantments.
  • Добавлю переключение поножей по клавише с возвратом на прежний слот.

  • Настройку клавиши размещу только в Mod Menu; по умолчанию — R.

Мод создан. Наденьте поножи с защитой и положите поножи со взрывоустойчивостью в хотбар. Нажатие R меняет их местами; повторное нажатие возвращает прежние поножи. Выбранный слот после смены восстанавливается.

Клавишу можно изменить в Mod Menu → Leggings Swap → настройки — доступны клавиши A–Z. В обычных настройках управления она не отображается.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
8 217
src/client/java/com/kodari/leggingss​wap/client/LeggingsSwapClient.java
package com.kodari.leggingsswap.client;

import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.protocol.game.ServerboundSetCarriedItemPacket;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.inventory.ContainerInput;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.item.equipment.Equippable;

public class LeggingsSwapClient implements ClientModInitializer {
    private boolean wasDown;

    @Override
    public void onInitializeClient() {
        SwapConfig.HANDLER.load();
        ClientTickEvents.END_CLIENT_TICK.register(this::tick);
    }

    private void tick(Minecraft client) {
        boolean down = InputConstants.isKeyDown(SwapConfig.HANDLER.instance().key.scancode());
        if (down && !wasDown && client.gui.screen() == null && client.player != null
                && client.gameMode != null && client.getConnection() != null
                && client.player.containerMenu == client.player.inventoryMenu) {
            swap(client);
        }
        wasDown = down;
    }

    private void swap(Minecraft client) {
        var player = client.player;
        ItemStack equipped = player.getItemBySlot(EquipmentSlot.LEGS);
        boolean protection = hasEnchantment(equipped, Enchantments.PROTECTION);
        boolean blast = hasEnchantment(equipped, Enchantments.BLAST_PROTECTION);
        if (protection == blast) return;

        ResourceKey<Enchantment> wanted = protection ? Enchantments.BLAST_PROTECTION : Enchantments.PROTECTION;
        ResourceKey<Enchantment> unwanted = protection ? Enchantments.PROTECTION : Enchantments.BLAST_PROTECTION;
        var inventory = player.getInventory();
        int previous = inventory.getSelectedSlot();
        for (int slot = 0; slot < 9; slot++) {
            ItemStack candidate = inventory.getItem(slot);
            Equippable equippable = candidate.get(DataComponents.EQUIPPABLE);
            if (equippable == null || equippable.slot() != EquipmentSlot.LEGS
                    || !hasEnchantment(candidate, wanted) || hasEnchantment(candidate, unwanted)) continue;

            if (slot != previous) {
                inventory.setSelectedSlot(slot);
                client.getConnection().getConnection().send(new ServerboundSetCarriedItemPacket(slot));
            }
            client.gameMode.handleContainerInput(player.inventoryMenu.containerId,
                    InventoryMenu.ARMOR_SLOT_START + 2, slot, ContainerInput.SWAP, player);
            if (slot != previous) {
                inventory.setSelectedSlot(previous);
                client.getConnection().getConnection().send(new ServerboundSetCarriedItemPacket(previous));
            }
            return;
        }
    }

    private static boolean hasEnchantment(ItemStack stack, ResourceKey<Enchantment> key) {
        return stack.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY)
                .entrySet().stream().anyMatch(entry -> entry.getKey().is(key));
    }
}