Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 228
src/client/java/com/kodari/maceswitch/MaceSwitchClient.java
package com.kodari.maceswitch;
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.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.EntityHitResult;
import java.util.concurrent.ThreadLocalRandom;
public class MaceSwitchClient implements ClientModInitializer {
private static final double RANGE_SQUARED = 2.8 * 2.8;
private KeyBinding toggleKey;
private KeyBinding attackToggleKey;
private KeyBinding surroundAttackToggleKey;
private KeyBinding rapidAttackToggleKey;
private static boolean enabled;
private static boolean attackEnabled;
private boolean surroundAttackEnabled;
private boolean rapidAttackEnabled;
private int attackDelay;
private int rapidTicksRemaining = 20;
private int rapidFourAttackTicksRemaining = 12;
public static float getIndicatorProgress() {
return attackEnabled ? 1.0F : enabled ? 0.5F : 0.0F;
}
@Override
public void onInitializeClient() {
toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.mace_switch.toggle", InputUtil.GLFW_KEY_U, KeyBinding.Category.MISC));
attackToggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.mace_switch.toggle_attack", InputUtil.GLFW_KEY_V, KeyBinding.Category.MISC));
surroundAttackToggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.mace_switch.toggle_surround_attack", InputUtil.GLFW_KEY_Z, KeyBinding.Category.MISC));
rapidAttackToggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.mace_switch.toggle_rapid_attack", InputUtil.GLFW_KEY_Y, KeyBinding.Category.MISC));
ClientTickEvents.END_CLIENT_TICK.register(this::onClientTick);
HudElementRegistry.addLast(Identifier.of("fullbright", "toggle_indicators"), (context, tickCounter) -> {
if (enabled) context.fill(0, 0, 1, 1, 0xFF00FF00);
if (attackEnabled) context.fill(2, 0, 3, 1, 0xFFFFFF00);
if (surroundAttackEnabled) context.fill(4, 0, 5, 1, 0xFFFFA500);
if (rapidAttackEnabled) context.fill(6, 0, 7, 1, 0xFFFF0000);
});
}
private void onClientTick(MinecraftClient client) {
if (attackDelay > 0) {
attackDelay--;
}
while (toggleKey.wasPressed()) {
enabled = !enabled;
}
while (attackToggleKey.wasPressed()) {
attackEnabled = !attackEnabled;
}
while (surroundAttackToggleKey.wasPressed()) {
surroundAttackEnabled = !surroundAttackEnabled;
}
while (rapidAttackToggleKey.wasPressed()) {
rapidAttackEnabled = !rapidAttackEnabled;
rapidTicksRemaining = 20;
rapidFourAttackTicksRemaining = 12;
}
if ((!enabled && !attackEnabled && !surroundAttackEnabled && !rapidAttackEnabled) || client.player == null || client.world == null) {
return;
}
var player = client.player;
boolean canAttack = !player.isOnGround()
&& client.world.isBlockSpaceEmpty(player, player.getBoundingBox().stretch(0.0, -1.3, 0.0));
PlayerEntity target = null;
boolean shouldAttack = false;
boolean rapidTarget = false;
if ((enabled || (canAttack && (attackEnabled || rapidAttackEnabled))) && client.crosshairTarget instanceof EntityHitResult hitResult
&& hitResult.getEntity() instanceof PlayerEntity otherPlayer
&& otherPlayer != player
&& player.getEyePos().squaredDistanceTo(hitResult.getPos()) <= RANGE_SQUARED) {
target = otherPlayer;
shouldAttack = canAttack && attackEnabled;
rapidTarget = canAttack && rapidAttackEnabled;
}
if (canAttack && surroundAttackEnabled && !rapidTarget) {
double nearestDistance = RANGE_SQUARED;
for (var otherPlayer : client.world.getPlayers()) {
if (otherPlayer == player || !otherPlayer.isAlive() || otherPlayer.isSpectator()) {
continue;
}
double distance = otherPlayer.getBoundingBox().squaredMagnitude(player.getEyePos());
if (distance <= nearestDistance) {
target = otherPlayer;
nearestDistance = distance;
shouldAttack = true;
}
}
}
if (target == null) {
return;
}
var inventory = player.getInventory();
for (int slot = 0; slot < 9; slot++) {
if (inventory.getStack(slot).isOf(Items.MACE)) {
inventory.setSelectedSlot(slot);
if (rapidTarget && client.interactionManager != null) {
boolean fourAttacks = ThreadLocalRandom.current().nextInt(rapidTicksRemaining) < rapidFourAttackTicksRemaining;
if (fourAttacks) {
rapidFourAttackTicksRemaining--;
}
if (--rapidTicksRemaining == 0) {
rapidTicksRemaining = 20;
rapidFourAttackTicksRemaining = 12;
}
for (int i = 0; i < (fourAttacks ? 4 : 3); i++) {
client.interactionManager.attackEntity(player, target);
player.swingHand(Hand.MAIN_HAND);
}
attackDelay = 5;
} else if (shouldAttack && client.interactionManager != null && attackDelay == 0) {
client.interactionManager.attackEntity(player, target);
player.swingHand(Hand.MAIN_HAND);
attackDelay = 5;
}
return;
}
}
}
}