Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
7 934
src/client/java/com/kodari/autohelmetcombiner/AutoHelmetCombinerClient.java
package com.kodari.autohelmetcombiner;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.function.Predicate;
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.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.enums.ChestType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.network.ClientPlayerInteractionManager;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Identifier;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.inventory.Inventory;
import org.lwjgl.glfw.GLFW;
public final class AutoHelmetCombinerClient implements ClientModInitializer {
private static final String MOD_ID = "auto_helmet_combiner";
private static final String[] HELMET_NAMES = {
"leather", "chainmail", "iron", "golden", "diamond", "netherite", "turtle", "copper"
};
private static final Item[] HELMETS = {
Items.LEATHER_HELMET,
Items.CHAINMAIL_HELMET,
Items.IRON_HELMET,
Items.GOLDEN_HELMET,
Items.DIAMOND_HELMET,
Items.NETHERITE_HELMET,
Items.TURTLE_HELMET,
Items.COPPER_HELMET
};
private static final double[] RANGE_OPTIONS = {2.5, 3.0, 3.5, 4.0, 4.5};
private static final Settings SETTINGS = new Settings();
private static KeyBinding toggleBinding;
private static KeyBinding helmetBinding;
private static KeyBinding levelBinding;
private static KeyBinding storageBinding;
private static KeyBinding rangeBinding;
private static boolean enabled;
private static String status = "Off";
private static Stage stage = Stage.IDLE;
private static long tick;
private static int waitTicks;
private static int completionTicks;
private static Item runHelmet;
private static int runProtectionLevel;
private static boolean runAutoStore;
private static int firstInventoryIndex = -1;
private static int secondInventoryIndex = -1;
private static ItemStack firstInput = ItemStack.EMPTY;
private static ItemStack secondInput = ItemStack.EMPTY;
private static ItemStack resultTemplate = ItemStack.EMPTY;
private static int playerResultCountBefore;
private static int chestResultCountBefore;
private static int playerChestCountBefore;
private static BlockPos anvilPos;
private static BlockPos chestPos;
@Override
public void onInitializeClient() {
MinecraftClient client = MinecraftClient.getInstance();
SETTINGS.load(client.runDirectory.toPath());
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of(MOD_ID, "general"));
toggleBinding = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.auto_helmet_combiner.toggle", GLFW.GLFW_KEY_H, category));
helmetBinding = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.auto_helmet_combiner.helmet", GLFW.GLFW_KEY_K, category));
levelBinding = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.auto_helmet_combiner.level", GLFW.GLFW_KEY_L, category));
storageBinding = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.auto_helmet_combiner.storage", GLFW.GLFW_KEY_J, category));
rangeBinding = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.auto_helmet_combiner.range", GLFW.GLFW_KEY_R, category));
ClientTickEvents.END_CLIENT_TICK.register(AutoHelmetCombinerClient::tick);
HudElementRegistry.addLast(Identifier.of(MOD_ID, "status"), AutoHelmetCombinerClient::renderHud);
}
private static void tick(MinecraftClient client) {
tick++;
handleKeybinds();
if (client.player == null || client.world == null || client.interactionManager == null) {
if (enabled) {
status = "Waiting for a world";
}
return;
}
if (!enabled) {
status = "Paused";
return;
}
switch (stage) {
case IDLE -> {
if (tick % 20 == 0) {
startIfPossible(client);
}
}
case OPENING_ANVIL -> waitForAnvil(client);
case PICKUP_FIRST -> pickupFirst(client);
case WAIT_FIRST_CURSOR -> placeFirst(client);
case WAIT_FIRST_PLACED -> waitForFirst(client);
case PICKUP_SECOND -> pickupSecond(client);
case WAIT_SECOND_CURSOR -> placeSecond(client);
case WAIT_SECOND_PLACED -> waitForSecond(client);
case WAIT_RESULT -> waitForResult(client);
case WAIT_OUTPUT_TRANSFER -> waitForOutputTransfer(client);
case WAIT_CHEST -> findChest(client);
case OPENING_CHEST -> waitForChest(client);
case STORE_RESULT -> storeResult(client);
case WAIT_CHEST_TRANSFER -> waitForChestTransfer(client);
case COMPLETE -> {
if (++completionTicks >= 40) {
resetOperation();
status = "Ready";
}
}
}
}
private static void handleKeybinds() {
while (toggleBinding.wasPressed()) {
enabled = !enabled;
status = enabled ? "Enabled" : "Paused";
}
while (helmetBinding.wasPressed()) {
SETTINGS.helmetIndex = (SETTINGS.helmetIndex + 1) % HELMETS.length;
SETTINGS.save();
status = "Helmet: " + displayName(SETTINGS.helmetIndex);
}
while (levelBinding.wasPressed()) {
SETTINGS.protectionLevel = SETTINGS.protectionLevel % 4 + 1;
SETTINGS.save();
status = "Input enchantment: Protection " + SETTINGS.protectionLevel;
}
while (storageBinding.wasPressed()) {
SETTINGS.autoStore = !SETTINGS.autoStore;
SETTINGS.save();
status = "Chest storage: " + (SETTINGS.autoStore ? "On" : "Off");
}
while (rangeBinding.wasPressed()) {
SETTINGS.rangeIndex = (SETTINGS.rangeIndex + 1) % RANGE_OPTIONS.length;
SETTINGS.save();
status = "Search range: " + range() + " blocks";
}
}
private static void startIfPossible(MinecraftClient client) {
if (client.currentScreen != null) {
status = "Close the open screen to start";
return;
}
PlayerInventory inventory = client.player.getInventory();
int[] pair = findHelmetPair(client, inventory);
if (pair == null) {
status = "Waiting for matching Protection " + SETTINGS.protectionLevel + " helmets";
return;
}
BlockPos foundAnvil = findNearest(client, state -> state.isOf(Blocks.ANVIL)
|| state.isOf(Blocks.CHIPPED_ANVIL)
|| state.isOf(Blocks.DAMAGED_ANVIL));
if (foundAnvil == null) {