Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 177
src/client/java/com/kodari/macelock/MaceLockClient.java
package com.kodari.macelock;
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.network.AbstractClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.item.Items;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
public class MaceLockClient implements ClientModInitializer {
private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of("macelock", "controls"));
private KeyBinding maceKey;
private KeyBinding totemKey;
private boolean autoMace;
private boolean autoTotem;
private AbstractClientPlayerEntity target;
@Override
public void onInitializeClient() {
maceKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.macelock.auto_mace", GLFW.GLFW_KEY_M, CATEGORY));
totemKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.macelock.auto_totem", GLFW.GLFW_KEY_N, CATEGORY));
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
private void tick(MinecraftClient client) {
while (maceKey.wasPressed()) {
autoMace = !autoMace;
target = null;
if (client.player != null) client.player.sendMessage(Text.of("Auto Mace: " + (autoMace ? "ON" : "OFF")), true);
}
while (totemKey.wasPressed()) {
autoTotem = !autoTotem;
if (client.player != null) client.player.sendMessage(Text.of("Auto Totem: " + (autoTotem ? "ON" : "OFF")), true);
}
if (client.player == null || client.world == null || client.interactionManager == null || !client.player.isAlive()) {
target = null;
return;
}
if (client.currentScreen != null) return;
if (autoTotem) refillTotem(client);
if (!autoMace || !client.player.getMainHandStack().isOf(Items.MACE) || client.player.isSpectator()) {
target = null;
return;
}
if (target == null || !client.world.getPlayers().contains(target) || !eligible(client, target)) {
target = null;
double nearest = Double.MAX_VALUE;
for (AbstractClientPlayerEntity candidate : client.world.getPlayers()) {
if (eligible(client, candidate)) {
double distance = client.player.squaredDistanceTo(candidate);
if (distance < nearest) {
nearest = distance;
target = candidate;
}
}
}
}
if (target != null && client.player.getAttackCooldownProgress(0.5F) >= 1.0F) {
client.interactionManager.attackEntity(client.player, target);
client.player.swingHand(Hand.MAIN_HAND);
}
}
private boolean eligible(MinecraftClient client, AbstractClientPlayerEntity candidate) {
return candidate != client.player && candidate.isAlive() && !candidate.isSpectator()
&& candidate.isAttackable() && client.player.squaredDistanceTo(candidate) <= 9.0
&& client.player.canSee(candidate);
}
private void refillTotem(MinecraftClient client) {
PlayerScreenHandler handler = client.player.playerScreenHandler;
if (client.player.currentScreenHandler != handler || !handler.getCursorStack().isEmpty()
|| !handler.getSlot(PlayerScreenHandler.OFFHAND_ID).getStack().isEmpty()) return;
for (int slot = PlayerScreenHandler.INVENTORY_START; slot < PlayerScreenHandler.HOTBAR_END; slot++) {
if (handler.getSlot(slot).getStack().isOf(Items.TOTEM_OF_UNDYING)) {
client.interactionManager.clickSlot(handler.syncId, slot, 0, SlotActionType.PICKUP, client.player);
client.interactionManager.clickSlot(handler.syncId, PlayerScreenHandler.OFFHAND_ID, 0, SlotActionType.PICKUP, client.player);
return;
}
}
}
}