Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 183
src/main/java/com/kodari/afkmacro/AfkMacroClient.java
package com.kodari.afkmacro;
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.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
public class AfkMacroClient implements ClientModInitializer {
private static final KeyBinding TOGGLE_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding(
"key.afkmacro.toggle",
InputUtil.Type.KEYSYM,
InputUtil.GLFW_KEY_RIGHT_ALT,
KeyBinding.MISC_CATEGORY
)
);
private static final Step[] SEQUENCE = {
new Step(Action.AFK, 100),
new Step(Action.AFK_SETTING, 30),
new Step(Action.CLICK_CENTER, 20),
new Step(Action.PRESS_E, 20),
new Step(Action.SPAWN, 100),
new Step(Action.AFK_SETTING, 30),
new Step(Action.AFK, 200),
new Step(Action.AFK_SETTING, 30),
new Step(Action.CLICK_CENTER, 20),
new Step(Action.PRESS_E, 20),
new Step(Action.SPAWN, 100)
};
private static boolean enabled;
private static int stepIndex;
private static int waitTicks;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(AfkMacroClient::onEndClientTick);
}
private static void onEndClientTick(MinecraftClient client) {
while (TOGGLE_KEY.wasPressed()) {
enabled = !enabled;
stepIndex = 0;
waitTicks = 0;
}
if (!enabled || client.player == null) {
return;
}
if (waitTicks > 0) {
waitTicks--;
if (waitTicks > 0) {
return;
}
}
Step step = SEQUENCE[stepIndex];
stepIndex = (stepIndex + 1) % SEQUENCE.length;
perform(client, step.action());
waitTicks = step.delayTicks();
}
private static void perform(MinecraftClient client, Action action) {
switch (action) {
case AFK -> client.player.networkHandler.sendChatCommand("afk");
case AFK_SETTING -> client.player.networkHandler.sendChatCommand("afksetting");
case SPAWN -> client.player.networkHandler.sendChatCommand("spawn 2");
case CLICK_CENTER -> {
Screen screen = client.currentScreen;
if (screen != null) {
double centerX = client.getWindow().getScaledWidth() / 2.0;
double centerY = client.getWindow().getScaledHeight() / 2.0;
screen.mouseClicked(centerX, centerY, 0);
screen.mouseReleased(centerX, centerY, 0);
}
}
case PRESS_E -> {
if (client.currentScreen != null) {
client.currentScreen.keyPressed(InputUtil.GLFW_KEY_E, 0, 0);
}
}
}
}
private enum Action {
AFK,
AFK_SETTING,
SPAWN,
CLICK_CENTER,
PRESS_E
}
private record Step(Action action, int delayTicks) {
}
}