Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
8 357
src/client/java/com/kodari/shieldbreaker/ShieldBreakerClient.java
package com.kodari.shieldbreaker;
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.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.AxeItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import org.lwjgl.glfw.GLFW;
public final class ShieldBreakerClient implements ClientModInitializer {
private static final int MIN_DELAY_TICKS = 0;
private static final int MAX_DELAY_TICKS = 40;
private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of("shieldbreaker", "main"));
private static final KeyBinding TOGGLE_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.shieldbreaker.toggle", GLFW.GLFW_KEY_K, CATEGORY));
private static final KeyBinding OPEN_GUI_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.shieldbreaker.open_gui", GLFW.GLFW_KEY_G, CATEGORY));
private static final KeyBinding INCREASE_DELAY_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.shieldbreaker.delay_increase", GLFW.GLFW_KEY_RIGHT_BRACKET, CATEGORY));
private static final KeyBinding DECREASE_DELAY_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.shieldbreaker.delay_decrease", GLFW.GLFW_KEY_LEFT_BRACKET, CATEGORY));
private static boolean enabled = true;
private static int delayTicks = 4;
private static PlayerEntity pendingTarget;
private static int originalSlot = -1;
private static int axeSlot = -1;
private static int attackCountdown;
private static int restoreCountdown;
private static boolean attackSent;
private static boolean performingDeferredAttack;
@Override
public void onInitializeClient() {
AttackEntityCallback.EVENT.register((player, world, hand, target, hitResult) -> {
MinecraftClient client = MinecraftClient.getInstance();
if (!world.isClient() || player != client.player || !enabled) {
return ActionResult.PASS;
}
if (performingDeferredAttack) {
return ActionResult.PASS;
}
if (pendingTarget != null) {
return ActionResult.FAIL;
}
if (!(target instanceof PlayerEntity blockingPlayer) || !blockingPlayer.isBlocking()) {
return ActionResult.PASS;
}
ItemStack blockingItem = blockingPlayer.getBlockingItem();
if (blockingItem == null || !blockingItem.isOf(Items.SHIELD)) {
return ActionResult.PASS;
}
PlayerInventory inventory = client.player.getInventory();
if (inventory.getSelectedStack().getItem() instanceof AxeItem) {
return ActionResult.PASS;
}
int foundAxeSlot = -1;
for (int slot = 0; slot < 9; slot++) {
if (inventory.getStack(slot).getItem() instanceof AxeItem) {
foundAxeSlot = slot;
break;
}
}
if (foundAxeSlot < 0) {
return ActionResult.PASS;
}
originalSlot = inventory.getSelectedSlot();
axeSlot = foundAxeSlot;
pendingTarget = blockingPlayer;
attackCountdown = delayTicks;
restoreCountdown = 0;
attackSent = false;
inventory.setSelectedSlot(axeSlot);
return ActionResult.FAIL;
});
ClientTickEvents.END_CLIENT_TICK.register(ShieldBreakerClient::tick);
}
private static void tick(MinecraftClient client) {
if (OPEN_GUI_KEY.wasPressed() && client.currentScreen == null) {
client.setScreen(new ShieldBreakerScreen());
}
if (client.currentScreen == null) {
if (TOGGLE_KEY.wasPressed()) {
enabled = !enabled;
sendStatus(client, "Auto shield breaker " + (enabled ? "enabled" : "disabled"));
}
if (INCREASE_DELAY_KEY.wasPressed()) {
delayTicks = Math.min(MAX_DELAY_TICKS, delayTicks + 1);
sendStatus(client, "Shield-break delay: " + delayTicks + " ticks");
}
if (DECREASE_DELAY_KEY.wasPressed()) {
delayTicks = Math.max(MIN_DELAY_TICKS, delayTicks - 1);
sendStatus(client, "Shield-break delay: " + delayTicks + " ticks");
}
}
if (client.player == null || client.interactionManager == null) {
clearPending(client);
return;
}
if (client.currentScreen != null) {
clearPending(client);
return;
}
if (pendingTarget == null) {
return;
}
if (!attackSent) {
if (attackCountdown > 0) {
attackCountdown--;
if (attackCountdown > 0) {
return;
}
}
PlayerInventory inventory = client.player.getInventory();
if (inventory.getSelectedSlot() != axeSlot) {
inventory.setSelectedSlot(axeSlot);
}
attackSent = true;
performingDeferredAttack = true;
try {
client.interactionManager.attackEntity(client.player, pendingTarget);
} finally {
performingDeferredAttack = false;
restoreCountdown = 1;
}
return;
}
if (restoreCountdown > 0) {
restoreCountdown--;
}
if (restoreCountdown == 0) {
clearPending(client);
}
}
private static void sendStatus(MinecraftClient client, String message) {
if (client.player != null) {
client.player.sendMessage(Text.literal(message), true);
}
}
private static void clearPending(MinecraftClient client) {
if (client.player != null && originalSlot >= 0 && axeSlot >= 0) {
PlayerInventory inventory = client.player.getInventory();
if (inventory.getSelectedSlot() == axeSlot) {
inventory.setSelectedSlot(originalSlot);
}
}
pendingTarget = null;
originalSlot = -1;
axeSlot = -1;
attackCountdown = 0;
restoreCountdown = 0;
attackSent = false;
performingDeferredAttack = false;
}
static boolean isEnabled() {
return enabled;
}
static void setEnabled(boolean value) {
enabled = value;
}
static int getDelayTicks() {
return delayTicks;
}
static void setDelayTicks(int value) {
delayTicks = Math.max(MIN_DELAY_TICKS, Math.min(MAX_DELAY_TICKS, value));
}
}