Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 166
src/client/java/com/kodari/anchormacro/AnchorMacroClient.java
package com.kodari.anchormacro;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
public final class AnchorMacroClient implements ClientModInitializer {
private static final int IDLE = 0;
private static final int USE_GLOWSTONE = 1;
private static final int USE_TOTEM = 2;
private int stage = IDLE;
private int glowstoneSlot;
private int totemSlot;
private boolean wasUsePressed;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(this::onEndClientTick);
}
private void onEndClientTick(MinecraftClient client) {
if (client.player == null || client.interactionManager == null) {
stage = IDLE;
wasUsePressed = false;
return;
}
boolean usePressed = client.options.useKey.isPressed();
boolean justPressed = usePressed && !wasUsePressed;
wasUsePressed = usePressed;
if (stage != IDLE) {
if (!(client.crosshairTarget instanceof BlockHitResult hit)) {
return;
}
PlayerInventory inventory = client.player.getInventory();
if (stage == USE_GLOWSTONE) {
inventory.setSelectedSlot(glowstoneSlot);
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hit);
stage = USE_TOTEM;
} else {
inventory.setSelectedSlot(totemSlot);
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hit);
stage = IDLE;
}
return;
}
if (!justPressed
|| !(client.crosshairTarget instanceof BlockHitResult)
|| !client.player.getInventory().getMainStacks()
.get(client.player.getInventory().getSelectedSlot())
.isOf(Items.RESPAWN_ANCHOR)) {
return;
}
PlayerInventory inventory = client.player.getInventory();
int foundGlowstoneSlot = findHotbarSlot(inventory, Items.GLOWSTONE);
int foundTotemSlot = findHotbarSlot(inventory, Items.TOTEM_OF_UNDYING);
if (foundGlowstoneSlot < 0 || foundTotemSlot < 0) {
return;
}
glowstoneSlot = foundGlowstoneSlot;
totemSlot = foundTotemSlot;
stage = USE_GLOWSTONE;
}
private int findHotbarSlot(PlayerInventory inventory, Item item) {
for (int slot = 0; slot < 9; slot++) {
if (inventory.getMainStacks().get(slot).isOf(item)) {
return slot;
}
}
return -1;
}
}