Files
src
build.gradle
gradle.properties
manifest.kod
Code
10 404
src/client/java/com/example/anchormacro/AnchorMacroClient.java
package com.example.anchormacro;
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.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.item.Items;
import net.minecraft.network.packet.c2s.play.UpdateSelectedSlotC2SPacket;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import org.lwjgl.glfw.GLFW;
public class AnchorMacroClient implements ClientModInitializer {
public static final String MOD_ID = "anchormacro";
private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(
Identifier.of(MOD_ID, "main")
);
private static final KeyBinding USE_ANCHOR_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.anchormacro.use_anchor", GLFW.GLFW_KEY_B, CATEGORY)
);
private static final KeyBinding OPEN_CONFIG_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.anchormacro.open_config", GLFW.GLFW_KEY_UNKNOWN, CATEGORY)
);
private enum Step {
IDLE,
SWAP_TO_ANCHOR,
PLACE_ANCHOR,
SWAP_TO_GLOWSTONE,
CHARGE_ANCHOR,
SWAP_BACK_TO_ANCHOR,
EXPLODE_ANCHOR,
RESTORE_SLOT
}
private Step step = Step.IDLE;
private int delay = 0;
private int originalSlot = 0;
private int anchorSlot = -1;
private int glowstoneSlot = -1;
private BlockPos anchorPos = null;
private BlockHitResult placeTarget = null;
@Override
public void onInitializeClient() {
AnchorMacroConfig.HANDLER.load();
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
private void tick(MinecraftClient client) {
while (OPEN_CONFIG_KEY.wasPressed()) {
if (client.currentScreen == null) {
client.setScreen(AnchorMacroConfig.createScreen(null));
}
}
while (USE_ANCHOR_KEY.wasPressed()) {
if (!AnchorMacroConfig.get().macroEnabled) continue;
// Re-pressing cancels a running sequence
if (step != Step.IDLE) {
if (client.player != null) restoreSlot(client);
reset();
continue;
}
if (client.player == null || client.world == null || client.interactionManager == null) continue;
if (!(client.crosshairTarget instanceof BlockHitResult hit)) continue;
int aSlot = findInHotbar(client, Items.RESPAWN_ANCHOR);
int gSlot = findInHotbar(client, Items.GLOWSTONE);
if (aSlot == -1) {
client.player.sendMessage(Text.translatable("anchormacro.missing.anchor"), true);
continue;
}
if (gSlot == -1) {
client.player.sendMessage(Text.translatable("anchormacro.missing.glowstone"), true);
continue;
}
anchorSlot = aSlot;
glowstoneSlot = gSlot;
originalSlot = ((com.example.anchormacro.mixin.PlayerInventoryAccessor) client.player.getInventory()).anchormacro$getSelectedSlot();
placeTarget = hit;
anchorPos = hit.getBlockPos().offset(hit.getSide());
step = Step.SWAP_TO_ANCHOR;
delay = 0;
}
if (step == Step.IDLE) return;
if (delay > 0) { delay--; return; }
if (client.player == null || client.world == null || client.interactionManager == null) { reset(); return; }
int sd = AnchorMacroConfig.get().stepDelay;
switch (step) {
case SWAP_TO_ANCHOR -> {
setSlot(client, anchorSlot);
step = Step.PLACE_ANCHOR;
delay = sd;
}
case PLACE_ANCHOR -> {
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, placeTarget);
swing(client);
step = Step.SWAP_TO_GLOWSTONE;
delay = sd;
}
case SWAP_TO_GLOWSTONE -> {
if (!client.world.getBlockState(anchorPos).isOf(Blocks.RESPAWN_ANCHOR)) {
client.player.sendMessage(Text.translatable("anchormacro.place_failed"), true);
restoreSlot(client);
reset();
return;
}
setSlot(client, glowstoneSlot);
step = Step.CHARGE_ANCHOR;
delay = sd;
}
case CHARGE_ANCHOR -> {
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hitAt(anchorPos));
swing(client);
step = Step.SWAP_BACK_TO_ANCHOR;
delay = sd;
}
case SWAP_BACK_TO_ANCHOR -> {
setSlot(client, anchorSlot);
step = Step.EXPLODE_ANCHOR;
delay = sd;
}
case EXPLODE_ANCHOR -> {
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hitAt(anchorPos));
swing(client);
step = Step.RESTORE_SLOT;
delay = sd;
}
case RESTORE_SLOT -> {
restoreSlot(client);
reset();
}
default -> reset();
}
}
private void swing(MinecraftClient client) {
if (AnchorMacroConfig.get().swingAnimation && client.player != null) {
client.player.swingHand(Hand.MAIN_HAND);
}
}
private void setSlot(MinecraftClient client, int slot) {
((com.example.anchormacro.mixin.PlayerInventoryAccessor) client.player.getInventory()).anchormacro$setSelectedSlot(slot);
client.player.networkHandler.sendPacket(new UpdateSelectedSlotC2SPacket(slot));
}
private void restoreSlot(MinecraftClient client) {
setSlot(client, originalSlot);
}
private int findInHotbar(MinecraftClient client, net.minecraft.item.Item item) {
for (int i = 0; i < 9; i++) {
if (client.player.getInventory().getStack(i).isOf(item)) return i;
}
return -1;
}
private BlockHitResult hitAt(BlockPos pos) {
return new BlockHitResult(Vec3d.ofCenter(pos), Direction.UP, pos, false);
}
private void reset() {
step = Step.IDLE;
delay = 0;
anchorSlot = -1;
glowstoneSlot = -1;
anchorPos = null;
placeTarget = null;
}
}