No chat to show

This project's files are all here to explore.

Files
src
build.gradle
gradle.properties
IMPORTANT.txt
settings.gradle
Code
7 159
src/main/java/dev/kodari/pearlgrapple/PearlGrappleClient.java
package dev.kodari.pearlgrapple;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;
import org.lwjgl.glfw.GLFW;

import java.util.Comparator;

public class PearlGrappleClient implements ClientModInitializer {
    private static final KeyBinding GRAPPLE_KEY = KeyBindingHelper.registerKeyBinding(
            new KeyBinding("key.pearlgrapple.grapple", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_G,
                    KeyBinding.Category.MISC)
    );

    @Override
    public void onInitializeClient() {
        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            while (GRAPPLE_KEY.wasPressed()) {
                if (client.player == null || client.world == null || client.interactionManager == null) {
                    continue;
                }

                PlayerEntity player = client.player;
                PlayerEntity target = client.world.getPlayers().stream()
                        .filter(other -> other != player)
                        .min(Comparator.comparingDouble(player::squaredDistanceTo))
                        .orElse(null);
                if (target == null) {
                    player.sendMessage(net.minecraft.text.Text.literal("No other player nearby."), true);
                    continue;
                }

                int pearlSlot = -1;
                for (int slot = 0; slot < 9; slot++) {
                    if (player.getInventory().getStack(slot).isOf(Items.ENDER_PEARL)) {
                        pearlSlot = slot;
                        break;
                    }
                }
                if (pearlSlot == -1) {
                    player.sendMessage(net.minecraft.text.Text.literal("No ender pearl in your hotbar."), true);
                    continue;
                }

                int previousSlot = player.getInventory().getSelectedSlot();
                float previousYaw = player.getYaw();
                float previousPitch = player.getPitch();
                Vec3d direction = target.getPos().subtract(player.getEyePos());
                double horizontalDistance = Math.sqrt(direction.x * direction.x + direction.z * direction.z);
                float yaw = (float) (Math.toDegrees(Math.atan2(direction.z, direction.x)) - 90.0);
                float pitch = (float) -Math.toDegrees(Math.atan2(direction.y, horizontalDistance));

                try {
                    player.getInventory().setSelectedSlot(pearlSlot);
                    player.setYaw(yaw);
                    player.setPitch(pitch);
                    client.interactionManager.interactItem(player, Hand.MAIN_HAND);
                } finally {
                    player.setYaw(previousYaw);
                    player.setPitch(previousPitch);
                    player.getInventory().setSelectedSlot(previousSlot);
                }
            }
        });
    }
}