Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 350
src/client/java/com/kodari/crystalcombat/CrystalCombatClient.java
package com.kodari.crystalcombat;
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.block.RespawnAnchorBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.decoration.EndCrystalEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
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.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.lwjgl.glfw.GLFW;
import java.util.List;
public class CrystalCombatClient implements ClientModInitializer {
private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of("crystalcombat", "combat"));
private static KeyBinding autoCrystalKey;
private static KeyBinding autoAnchorKey;
private static KeyBinding criticalAuraKey;
private static boolean autoCrystalEnabled;
private static boolean autoAnchorEnabled;
private static boolean criticalAuraEnabled;
@Override
public void onInitializeClient() {
autoCrystalKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.crystalcombat.auto_crystal", GLFW.GLFW_KEY_K, CATEGORY)
);
autoAnchorKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.crystalcombat.auto_anchor", GLFW.GLFW_KEY_L, CATEGORY)
);
criticalAuraKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.crystalcombat.critical_aura", GLFW.GLFW_KEY_J, CATEGORY)
);
ClientTickEvents.END_CLIENT_TICK.register(CrystalCombatClient::tick);
}
private static void tick(MinecraftClient client) {
if (client.player == null || client.world == null || client.interactionManager == null) {
return;
}
if (autoCrystalKey.wasPressed()) {
autoCrystalEnabled = !autoCrystalEnabled;
announce(client, "Auto Crystal", autoCrystalEnabled);
}
if (autoAnchorKey.wasPressed()) {
autoAnchorEnabled = !autoAnchorEnabled;
announce(client, "Auto Anchor", autoAnchorEnabled);
}
if (criticalAuraKey.wasPressed()) {
criticalAuraEnabled = !criticalAuraEnabled;
announce(client, "Critical-Only Aura", criticalAuraEnabled);
}
if (criticalAuraEnabled) {
attackCriticalTarget(client);
}
if (autoCrystalEnabled && useAutoCrystal(client)) {
return;
}
if (autoAnchorEnabled) {
useAutoAnchor(client);
}
}
private static void announce(MinecraftClient client, String feature, boolean enabled) {
client.player.sendMessage(Text.literal(feature + (enabled ? " enabled" : " disabled")), true);
}
private static void attackCriticalTarget(MinecraftClient client) {
PlayerEntity player = client.player;
if (player.fallDistance <= 0.0
|| player.isOnGround()
|| player.isClimbing()
|| player.isTouchingWater()
|| player.hasBlindnessEffect()
|| player.hasVehicle()
|| player.isSprinting()
|| player.getAttackCooldownProgress(0.5f) <= 0.9f) {
return;
}
PlayerEntity closest = null;
double closestDistance = 3.0 * 3.0;
for (PlayerEntity target : client.world.getPlayers()) {
if (target == player || !target.isAlive() || target.isSpectator()) {
continue;
}
double distance = player.squaredDistanceTo(target);
if (distance < closestDistance) {
closest = target;
closestDistance = distance;
}
}
if (closest != null) {
client.interactionManager.attackEntity(player, closest);
player.swingHand(Hand.MAIN_HAND);
}
}
private static boolean useAutoCrystal(MinecraftClient client) {
PlayerEntity player = client.player;
Box searchArea = new Box(player.getBlockPos()).expand(4.0);
List<EndCrystalEntity> crystals = client.world.getEntitiesByClass(
EndCrystalEntity.class,
searchArea,
crystal -> crystal.isAlive() && crystal.squaredDistanceTo(player) <= 16.0
);
if (!crystals.isEmpty()) {
EndCrystalEntity closest = crystals.get(0);
for (EndCrystalEntity crystal : crystals) {
if (crystal.squaredDistanceTo(player) < closest.squaredDistanceTo(player)) {
closest = crystal;
}
}
client.interactionManager.attackEntity(player, closest);
player.swingHand(Hand.MAIN_HAND);
return true;
}
int crystalSlot = findHotbarSlot(player, Items.END_CRYSTAL);
if (crystalSlot < 0) {
return false;
}
BlockPos base = findCrystalBase(client);
if (base == null) {
return false;
}
player.getInventory().setSelectedSlot(crystalSlot);
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hitTop(base));
return true;
}
private static BlockPos findCrystalBase(MinecraftClient client) {
BlockPos origin = client.player.getBlockPos();
for (int y = -2; y <= 2; y++) {
for (int x = -4; x <= 4; x++) {
for (int z = -4; z <= 4; z++) {
BlockPos base = origin.add(x, y, z);
if ((client.world.getBlockState(base).isOf(Blocks.OBSIDIAN)
|| client.world.getBlockState(base).isOf(Blocks.BEDROCK))
&& client.world.getBlockState(base.add(0, 1, 0)).isAir()
&& client.world.getBlockState(base.add(0, 2, 0)).isAir()) {
return base;
}
}
}
}
return null;
}
private static void useAutoAnchor(MinecraftClient client) {
PlayerEntity player = client.player;
BlockPos anchor = findNearbyAnchor(client);
if (anchor == null) {
int anchorSlot = findHotbarSlot(player, Items.RESPAWN_ANCHOR);
BlockPos support = findAnchorSupport(client);
if (anchorSlot >= 0 && support != null) {
player.getInventory().setSelectedSlot(anchorSlot);
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hitTop(support));
}
return;
}
int charges = client.world.getBlockState(anchor).get(RespawnAnchorBlock.CHARGES);
if (charges < 4) {
int glowstoneSlot = findHotbarSlot(player, Items.GLOWSTONE);
if (glowstoneSlot >= 0) {
player.getInventory().setSelectedSlot(glowstoneSlot);
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hitTop(anchor));
}
return;
}
if (!client.world.getRegistryKey().equals(World.NETHER)) {
int detonationSlot = findDetonationSlot(player);
if (detonationSlot >= 0) {
player.getInventory().setSelectedSlot(detonationSlot);
client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hitTop(anchor));
}
}
}
private static BlockPos findNearbyAnchor(MinecraftClient client) {