Abdullah Ahmed Ahmed

i want you to make me a cobweb optimizer so when a player is in 3 block radius of me and i hit him with a sword and when he is in midair the mod should make me switch to seventh slot where i will have cobwebs and it should have 30 percent chance to place the cobweb right where the other player will land like so it player should land on the cobweb after that it should switch back to the first slot .if the player tried to place the water on the web then mod should insta swittch to seventh slot and place the cobweb where the other player placed the cobweb. also it should not send packets, and there should be a option menu for it where i can turn off this mod and turn on it should let me change the keybind and there should be a specific friend list area where i can put my friends names and the mod should ignore them

Kodari AI
KodariOct 21 at 5:12 am 3
CobwebComboOptimizer
A client-side Minecraft Fabric mod that automatically optimizes cobweb combat. When the player strikes an airborne enemy within a 3-block radius, the mod has a 30% chance to instantly switch to the seventh inventory slot, place a cobweb at the enemy's predicted landing point, then return to the first slot. It also detects when an enemy places water on a web and immediately replaces it with a new cobweb. The mod operates entirely client-side, sending no packets. Features a configurable settings menu to toggle the mod on/off, customize the keybind, and maintain a friends list of players to ignore during combat.
  1. Set up the client-only Fabric 1.21.11 mod and metadata.

  2. Add a YACL options page for the toggle, keybind, and friend names.

  3. Implement nearby sword-hit tracking, probabilistic landing-web placement, and water-response attempts using vanilla interactions.

  4. Document the multiplayer limitation: changing slots or placing blocks necessarily uses vanilla server interactions; no custom packets will be constructed.

Created the client-only Fabric 1.21.11 Cobweb Optimizer with a YACL settings screen available through ModMenu.

  • On a sword hit within 3 blocks, it rolls a 30% chance and, if the target is airborne, predicts a landing spot and attempts to place a cobweb from hotbar slot 7. It then switches to slot 1.

  • The settings screen lets you enable or disable the feature, change its toggle key using a key translation ID (for example, key.keyboard.g), and enter comma-separated friend names to ignore.

  • It watches its last placed web for water and attempts to place another cobweb at that location when water appears. This is best-effort: water may already occupy the spot or the server may reject the placement.

A truly packet-free version cannot switch a server-visible hotbar slot or place blocks in multiplayer. This mod doesn’t construct custom packets, but vanilla interactions send the normal packets needed for those actions.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
10 409
src/client/java/com/kodari/cobweboptimizer/CobwebOptimizerClient.java
package com.kodari.cobweboptimizer;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.fluid.FluidState;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.tag.FluidTags;
import net.minecraft.registry.tag.ItemTags;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.concurrent.ThreadLocalRandom;

public final class CobwebOptimizerClient implements ClientModInitializer {
    private static PendingAttack pendingAttack;
    private static BlockPos monitoredWebPosition;
    private static int monitorTicks;
    private static boolean monitoredWebSeen;
    private static boolean waterResponseAttempted;
    private static boolean toggleKeyWasDown;

    @Override
    public void onInitializeClient() {
        CobwebConfig.load();

        AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
            MinecraftClient client = MinecraftClient.getInstance();
            if (!(player instanceof ClientPlayerEntity localPlayer)
                    || localPlayer != client.player
                    || hand != Hand.MAIN_HAND
                    || !CobwebConfig.get().enabled
                    || !(entity instanceof PlayerEntity target)
                    || !localPlayer.getMainHandStack().isIn(ItemTags.SWORDS)
                    || localPlayer.getEntityPos().squaredDistanceTo(target.getEntityPos()) > 9.0
                    || CobwebConfig.isFriend(target.getStringifiedName())
                    || ThreadLocalRandom.current().nextInt(100) >= 30) {
                return ActionResult.PASS;
            }

            pendingAttack = new PendingAttack(target, 20);
            return ActionResult.PASS;
        });

        ClientTickEvents.END_CLIENT_TICK.register(CobwebOptimizerClient::tick);
    }

    private static void tick(MinecraftClient client) {
        if (client.player == null || client.world == null) {
            pendingAttack = null;
            monitoredWebPosition = null;
            toggleKeyWasDown = false;
            return;
        }

        boolean toggleKeyDown = isToggleKeyDown(client);
        if (client.currentScreen == null && toggleKeyDown && !toggleKeyWasDown) {
            CobwebConfig.get().enabled = !CobwebConfig.get().enabled;
            CobwebConfig.save();
            if (!CobwebConfig.get().enabled) {
                pendingAttack = null;
            }
        }
        toggleKeyWasDown = toggleKeyDown;

        if (!CobwebConfig.get().enabled) {
            return;
        }

        monitorWaterAtLastWeb(client);
        handlePendingAttack(client);
    }

    private static boolean isToggleKeyDown(MinecraftClient client) {
        String translationKey = CobwebConfig.get().toggleKey;
        if (translationKey == null || translationKey.isBlank()) {
            return false;
        }

        try {
            int keyCode = InputUtil.fromTranslationKey(translationKey).getCode();
            return InputUtil.isKeyPressed(client.getWindow(), keyCode);
        } catch (IllegalArgumentException exception) {
            return false;
        }
    }

    private static void handlePendingAttack(MinecraftClient client) {
        if (pendingAttack == null) {
            return;
        }

        PlayerEntity target = pendingAttack.target;
        if (pendingAttack.ticksRemaining-- <= 0
                || target.getEntityWorld() != client.world
                || CobwebConfig.isFriend(target.getStringifiedName())) {
            pendingAttack = null;
            return;
        }

        if (!target.isOnGround()) {
            BlockPos landingPosition = findLandingPosition(client.world, target);
            if (landingPosition != null) {
                placeCobweb(client, client.player, landingPosition, landingPosition.down(), true);
                pendingAttack = null;
            }
        }
    }

    private static BlockPos findLandingPosition(World world, PlayerEntity target) {
        Vec3d position = target.getEntityPos();
        Vec3d velocity = target.getVelocity();

        for (int ticks = 1; ticks <= 60; ticks++) {
            double x = position.x + velocity.x * ticks;
            double y = position.y + velocity.y * ticks - 0.04 * ticks * ticks;
            double z = position.z + velocity.z * ticks;
            BlockPos feetPosition = BlockPos.ofFloored(x, y, z);
            BlockPos supportPosition = feetPosition.down();

            if (world.getBlockState(supportPosition).isSolid()
                    && world.getBlockState(feetPosition).isReplaceable()
                    && world.getBlockState(feetPosition.up()).isReplaceable()) {
                return feetPosition;
            }
        }

        return null;
    }

    private static void monitorWaterAtLastWeb(MinecraftClient client) {
        if (monitoredWebPosition == null) {
            return;
        }

        BlockState state = client.world.getBlockState(monitoredWebPosition);
        if (state.isOf(Blocks.COBWEB)) {
            monitoredWebSeen = true;
            monitorTicks = 40;
            return;
        }

        FluidState fluidState = state.getFluidState();
        if (monitoredWebSeen && !waterResponseAttempted && fluidState.isIn(FluidTags.WATER)) {
            waterResponseAttempted = true;
            placeCobweb(client, client.player, monitoredWebPosition, monitoredWebPosition, false);
            monitoredWebPosition = null;
            return;
        }

        if (monitoredWebSeen || monitorTicks-- <= 0) {
            monitoredWebPosition = null;
        }
    }

    private static boolean placeCobweb(MinecraftClient client, ClientPlayerEntity player, BlockPos targetPosition,
                                      BlockPos clickedPosition, boolean monitorForWater) {
        if (client.interactionManager == null) {
            return false;
        }

        PlayerInventory inventory = player.getInventory();
        ItemStack cobwebStack = inventory.getStack(6);
        if (!cobwebStack.isOf(Items.COBWEB)) {
            return false;
        }

        Vec3d hitPosition = new Vec3d(clickedPosition.getX() + 0.5, clickedPosition.getY() + 1.0,
                clickedPosition.getZ() + 0.5);
        BlockHitResult hitResult = new BlockHitResult(hitPosition, Direction.UP, clickedPosition, false);

        ActionResult result;
        inventory.setSelectedSlot(6);
        try {
            result = client.interactionManager.interactBlock(player, Hand.MAIN_HAND, hitResult);
        } finally {
            inventory.setSelectedSlot(0);
        }

        if (result.isAccepted() && monitorForWater) {
            monitoredWebPosition = targetPosition;
            monitorTicks = 40;
            monitoredWebSeen = false;
            waterResponseAttempted = false;
        }