Files
src
build.gradle
gradle.properties
LICENSE
settings.gradle
Code
6 341
src/main/java/com/kodari/safeanchor/SafeAnchorClient.java
package com.kodari.safeanchor;
import java.util.concurrent.ThreadLocalRandom;
import org.lwjgl.glfw.GLFW;
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.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
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;
public final class SafeAnchorClient implements ClientModInitializer {
private static final long MIN_DELAY_MILLIS = 15L;
private static final long MAX_DELAY_MILLIS = 30L;
private static KeyBinding activateKey;
private static Action action = Action.IDLE;
private static BlockPos farAnchor;
private static BlockPos nearAnchor;
private static int originalSlot;
private static long nextActionAt;
private static boolean activeLastTick;
@Override
public void onInitializeClient() {
activateKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.safe_anchor_motion.activate",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_GRAVE_ACCENT,
"category.safe_anchor_motion"));
ClientTickEvents.END_CLIENT_TICK.register(SafeAnchorClient::tick);
}
private static void tick(MinecraftClient client) {
boolean active = activateKey.isPressed();
if (!active) {
if (activeLastTick) {
cancel(client);
}
activeLastTick = false;
return;
}
if (!activeLastTick) {
start(client);
}
activeLastTick = true;
if (action == Action.IDLE || System.nanoTime() < nextActionAt) {
return;
}
execute(client);
}
private static void start(MinecraftClient client) {
ClientPlayerEntity player = client.player;
if (player == null || client.world == null || client.currentScreen != null || player.isSpectator()) {
action = Action.IDLE;
return;
}
PlayerInventory inventory = player.getInventory();
int anchorSlot = findSlot(inventory, Items.RESPAWN_ANCHOR);
if (anchorSlot < 0) {
action = Action.IDLE;
return;
}
originalSlot = inventory.selectedSlot;
Direction direction = player.getHorizontalFacing();
BlockPos origin = player.getBlockPos();
nearAnchor = origin.offset(direction, 2);
farAnchor = origin.offset(direction, 3);
if (!canPlace(client, nearAnchor) || !canPlace(client, farAnchor)) {
action = Action.IDLE;
return;
}
inventory.selectedSlot = anchorSlot;
action = Action.PLACE_FAR;
nextActionAt = System.nanoTime();
}
private static void execute(MinecraftClient client) {
ClientPlayerEntity player = client.player;
if (player == null || client.world == null || client.currentScreen != null) {
action = Action.IDLE;
return;
}
switch (action) {
case PLACE_FAR -> {
placeAnchor(client, farAnchor);
action = Action.PLACE_NEAR;
}
case PLACE_NEAR -> {
placeAnchor(client, nearAnchor);
action = findSlot(player.getInventory(), Items.GLOWSTONE) >= 0
? Action.CHARGE_FAR
: Action.RESTORE_ANCHOR;
}
case CHARGE_FAR -> {
int glowstoneSlot = findSlot(player.getInventory(), Items.GLOWSTONE);
if (glowstoneSlot >= 0) {
player.getInventory().selectedSlot = glowstoneSlot;
aimAt(player, farAnchor.toCenterPos());
client.interactionManager.interactBlock(player, Hand.MAIN_HAND, hitOn(farAnchor));
}
action = Action.RESTORE_ANCHOR;
}
case RESTORE_ANCHOR -> {
player.getInventory().selectedSlot = findSlot(player.getInventory(), Items.RESPAWN_ANCHOR);
action = Action.IDLE;
}
case IDLE -> {
}
}
if (action != Action.IDLE) {
nextActionAt = System.nanoTime() + randomDelayNanos();
}
}
private static void placeAnchor(MinecraftClient client, BlockPos target) {
ClientPlayerEntity player = client.player;
if (player == null || client.interactionManager == null) {
action = Action.IDLE;
return;
}
BlockPos support = target.down();
aimAt(player, target.toCenterPos());
client.interactionManager.interactBlock(player, Hand.MAIN_HAND, new BlockHitResult(
support.toCenterPos().add(0.0, 0.5, 0.0),
Direction.UP,
support,
false));
}
private static boolean canPlace(MinecraftClient client, BlockPos target) {
BlockState targetState = client.world.getBlockState(target);
BlockState supportState = client.world.getBlockState(target.down());
return targetState.isAir() && !supportState.isAir();
}
private static BlockHitResult hitOn(BlockPos position) {
return new BlockHitResult(
position.toCenterPos().add(0.0, 0.5, 0.0),
Direction.UP,
position,
false);
}
private static void aimAt(ClientPlayerEntity player, Vec3d target) {
Vec3d delta = target.subtract(player.getEyePos());
double horizontalDistance = Math.sqrt(delta.x * delta.x + delta.z * delta.z);
float yaw = (float) (Math.atan2(delta.z, delta.x) * 180.0 / Math.PI) - 90.0F;
float pitch = (float) -(Math.atan2(delta.y, horizontalDistance) * 180.0 / Math.PI);
player.setYaw(yaw);
player.setPitch(pitch);
}
private static int findSlot(PlayerInventory inventory, net.minecraft.item.Item item) {
for (int slot = 0; slot < inventory.main.size(); slot++) {
ItemStack stack = inventory.main.get(slot);
if (stack.isOf(item) && !stack.isEmpty()) {
return slot;
}
}
return -1;
}
private static long randomDelayNanos() {
long milliseconds = ThreadLocalRandom.current().nextLong(MIN_DELAY_MILLIS, MAX_DELAY_MILLIS + 1);
return milliseconds * 1_000_000L;
}
private static void cancel(MinecraftClient client) {
if (client.player != null && action != Action.IDLE) {
client.player.getInventory().selectedSlot = originalSlot;
}
action = Action.IDLE;
farAnchor = null;
nearAnchor = null;
}