Files
src
build.gradle
gradle.properties
Code
5 185
src/main/java/com/pearlcatcher/PearlCatcher.java
package com.pearlcatcher;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.EnderPearlEntity;
import net.minecraft.entity.projectile.WindChargeEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.List;
public class PearlCatcher implements ModInitializer {
public static final String MOD_ID = "pearlcatcher";
// Ticks after the pearl is thrown before the wind charge launches.
private static final int LAUNCH_DELAY_TICKS = 5;
// How many ticks ahead to predict the pearl position for the intercept point.
private static final int INTERCEPT_LEAD_TICKS = 18;
// Ender pearl physics constants (per-tick).
private static final double PEARL_DRAG = 0.99;
private static final double PEARL_GRAVITY = 0.03;
// Wind charge has no gravity but does have drag (same coefficient as pearl).
private static final double WIND_CHARGE_DRAG = 0.99;
private record PearlTracker(EnderPearlEntity pearl, PlayerEntity thrower, int ticksRemaining) {}
private final List<PearlTracker> trackers = new ArrayList<>();
@Override
public void onInitialize() {
ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if (entity instanceof EnderPearlEntity pearl
&& pearl.getOwner() instanceof PlayerEntity player) {
trackers.add(new PearlTracker(pearl, player, LAUNCH_DELAY_TICKS));
}
});
ServerTickEvents.START_SERVER_TICK.register(server -> {
List<PearlTracker> toRemove = new ArrayList<>();
List<PearlTracker> toAdd = new ArrayList<>();
for (PearlTracker tracker : trackers) {
toRemove.add(tracker);
if (tracker.pearl().isRemoved()) {
continue;
}
if (tracker.ticksRemaining() <= 0) {
launchWindCharge(tracker.pearl(), tracker.thrower());
} else {
toAdd.add(new PearlTracker(tracker.pearl(), tracker.thrower(), tracker.ticksRemaining() - 1));
}
}
trackers.removeAll(toRemove);
trackers.addAll(toAdd);
});
}
private void launchWindCharge(EnderPearlEntity pearl, PlayerEntity thrower) {
World world = pearl.getEntityWorld();
if (!(world instanceof ServerWorld serverWorld)) return;
if (pearl.isRemoved()) return;
// Simulate the pearl's trajectory INTERCEPT_LEAD_TICKS steps forward,
// accounting for per-tick drag and gravity, to find a good intercept point.
Vec3d pearlPos = pearl.getPos();
Vec3d pearlVel = pearl.getVelocity();
Vec3d simPos = pearlPos;
Vec3d simVel = pearlVel;
for (int i = 0; i < INTERCEPT_LEAD_TICKS; i++) {
simVel = new Vec3d(
simVel.x * PEARL_DRAG,
simVel.y * PEARL_DRAG - PEARL_GRAVITY,
simVel.z * PEARL_DRAG
);
simPos = simPos.add(simVel);
}
Vec3d targetPos = simPos;
// Spawn the wind charge just in front of the player's eyes.
Vec3d spawnPos = thrower.getEyePos().add(thrower.getRotationVector().multiply(0.5));
Vec3d displacement = targetPos.subtract(spawnPos);
// Solve the initial velocity needed to cover `displacement` in exactly
// INTERCEPT_LEAD_TICKS ticks under geometric drag:
// total displacement = v0 * sum_{n=0}^{N-1} drag^n = v0 * (1 - drag^N) / (1 - drag)
// => v0 = displacement * (1 - drag) / (1 - drag^N)
double dragPowN = Math.pow(WIND_CHARGE_DRAG, INTERCEPT_LEAD_TICKS);
double scale = (1.0 - WIND_CHARGE_DRAG) / (1.0 - dragPowN);
Vec3d windChargeVelocity = displacement.multiply(scale);
WindChargeEntity windCharge = new WindChargeEntity(EntityType.WIND_CHARGE, serverWorld);
windCharge.setOwner(thrower);
windCharge.setPosition(spawnPos);
windCharge.setVelocity(windChargeVelocity);
serverWorld.spawnEntity(windCharge);
}
}