No chat to show

This project's files are all here to explore.

Files
src
build.gradle
gradle.properties
Code
13 470
src/main/java/com/autobowboost/AutoBowBoost.java
package com.autobowboost;

import com.autobowboost.network.BowBoostPayload;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Vec3d;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AutoBowBoost implements ModInitializer {

    public static final String MOD_ID = "autobowboost";
    public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

    // Tag key written onto the arrow so the mixin can identify it
    public static final String BOOST_TAG = "autobowboost_boost";

    // Gravity constant matching vanilla arrow physics (blocks/tick^2)
    private static final double GRAVITY = 0.05;
    // How many ticks ahead to predict the landing position
    private static final int PREDICT_TICKS = 14;
    // Extra horizontal boost multiplier applied on arrow hit
    public static final double BOOST_MULTIPLIER = 2.8;
    // Upward velocity added on hit
    public static final double BOOST_VERTICAL = 0.55;

    @Override
    public void onInitialize() {
        PayloadTypeRegistry.playC2S().register(BowBoostPayload.ID, BowBoostPayload.CODEC);

        ServerPlayNetworking.registerGlobalReceiver(BowBoostPayload.ID, (payload, context) -> {
            ServerPlayerEntity player = context.player();
            spawnBoostArrow(player);
        });
    }

    private void spawnBoostArrow(ServerPlayerEntity player) {
        if (!(player.getEntityWorld() instanceof net.minecraft.server.world.ServerWorld serverWorld)) return;

        Vec3d playerPos = player.getPos();
        Vec3d playerVel = player.getVelocity();

        // Predict where the player will be in PREDICT_TICKS ticks
        // Horizontal velocity decays by 0.91 per tick on ground, 0.98 in air
        double predX = playerPos.x;
        double predZ = playerPos.z;
        double vx = playerVel.x;
        double vz = playerVel.z;
        for (int i = 0; i < PREDICT_TICKS; i++) {
            vx *= 0.91;
            vz *= 0.91;
            predX += vx;
            predZ += vz;
        }

        // Arrow spawn point: directly above the predicted landing position
        double spawnX = predX;
        double spawnY = playerPos.y + 18.0;
        double spawnZ = predZ;

        ArrowEntity arrow = new ArrowEntity(EntityType.ARROW, serverWorld);
        arrow.setPosition(spawnX, spawnY, spawnZ);
        arrow.setOwner(player);

        // Aim the arrow straight down at the player's feet
        Vec3d target = new Vec3d(playerPos.x, playerPos.y + 1.0, playerPos.z);
        Vec3d direction = target.subtract(spawnX, spawnY, spawnZ).normalize();
        arrow.setVelocity(direction.x, direction.y, direction.z, 3.0f, 0.0f);

        // Tag the arrow so the mixin can apply the boost on hit
        NbtCompound tag = new NbtCompound();
        tag.putBoolean(BOOST_TAG, true);
        tag.putDouble("boostYaw", player.getYaw());
        arrow.writeCustomDataToNbt(tag);
        // Store boost data in the arrow's persistent data via custom NBT
        arrow.readCustomDataFromNbt(tag);

        // Mark arrow as critical (faster, more visual impact)
        arrow.setCritical(true);
        // Prevent the arrow from being picked up
        arrow.pickupType = ArrowEntity.PickupPermission.DISALLOWED;
        // Reduce damage — we want the knockback, not to kill the player
        arrow.setDamage(0.5);

        serverWorld.spawnEntity(arrow);
    }
}