Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 273
src/client/java/com/kodari/anvilrepairer/AnvilRepairClient.java
package com.kodari.anvilrepairer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.AnvilScreen;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.screen.slot.SlotActionType;
public class AnvilRepairClient implements ClientModInitializer {
private enum Step {
IDLE,
PICK_UP_PICKAXE,
PLACE_PICKAXE,
PICK_UP_DIAMONDS,
PLACE_ONE_DIAMOND,
RETURN_REMAINING_DIAMONDS,
WAIT_FOR_RESULT,
RETURN_REPAIRED_PICKAXE,
ROLLBACK_PICKAXE,
RETURN_ROLLED_BACK_PICKAXE,
ROLLBACK_DIAMONDS,
RETURN_ROLLED_BACK_DIAMONDS
}
private Step step = Step.IDLE;
private int pickaxeInventoryIndex = -1;
private int diamondInventoryIndex = -1;
private int outputWaitTicks;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(this::onClientTick);
}
private void onClientTick(MinecraftClient client) {
if (!(client.currentScreen instanceof AnvilScreen anvilScreen)
|| client.player == null
|| client.interactionManager == null) {
reset();
return;
}
ClientPlayerEntity player = client.player;
AnvilScreenHandler handler = anvilScreen.getScreenHandler();
if (player.currentScreenHandler != handler) {
reset();
return;
}
if (step == Step.IDLE) {
beginIfReady(player, handler);
return;
}
switch (step) {
case PICK_UP_PICKAXE -> {
click(client, handler, inventorySlotId(pickaxeInventoryIndex), 0);
step = Step.PLACE_PICKAXE;
}
case PLACE_PICKAXE -> {
if (handler.getCursorStack().isOf(Items.DIAMOND_PICKAXE)) {
click(client, handler, AnvilScreenHandler.INPUT_1_ID, 0);
step = Step.PICK_UP_DIAMONDS;
}
}
case PICK_UP_DIAMONDS -> {
if (handler.getCursorStack().isEmpty()) {
click(client, handler, inventorySlotId(diamondInventoryIndex), 0);
step = Step.PLACE_ONE_DIAMOND;
}
}
case PLACE_ONE_DIAMOND -> {
if (handler.getCursorStack().isOf(Items.DIAMOND)) {
click(client, handler, AnvilScreenHandler.INPUT_2_ID, 1);
step = Step.RETURN_REMAINING_DIAMONDS;
}
}
case RETURN_REMAINING_DIAMONDS -> {
ItemStack cursorStack = handler.getCursorStack();
if (cursorStack.isEmpty()) {
step = Step.WAIT_FOR_RESULT;
} else if (cursorStack.isOf(Items.DIAMOND)) {
click(client, handler, inventorySlotId(diamondInventoryIndex), 0);
step = Step.WAIT_FOR_RESULT;
}
}
case WAIT_FOR_RESULT -> waitForResult(client, player, handler);
case RETURN_REPAIRED_PICKAXE -> {
if (handler.getCursorStack().isOf(Items.DIAMOND_PICKAXE)) {
click(client, handler, inventorySlotId(pickaxeInventoryIndex), 0);
reset();
}
}
case ROLLBACK_PICKAXE -> {
if (handler.getCursorStack().isEmpty() && handler.getSlot(AnvilScreenHandler.INPUT_1_ID).hasStack()) {
click(client, handler, AnvilScreenHandler.INPUT_1_ID, 0);
step = Step.RETURN_ROLLED_BACK_PICKAXE;
}
}
case RETURN_ROLLED_BACK_PICKAXE -> {
if (handler.getCursorStack().isOf(Items.DIAMOND_PICKAXE)) {
click(client, handler, inventorySlotId(pickaxeInventoryIndex), 0);
step = Step.ROLLBACK_DIAMONDS;
}
}
case ROLLBACK_DIAMONDS -> {
if (handler.getCursorStack().isEmpty() && handler.getSlot(AnvilScreenHandler.INPUT_2_ID).hasStack()) {
click(client, handler, AnvilScreenHandler.INPUT_2_ID, 0);
step = Step.RETURN_ROLLED_BACK_DIAMONDS;
}
}
case RETURN_ROLLED_BACK_DIAMONDS -> {
if (handler.getCursorStack().isOf(Items.DIAMOND)) {
click(client, handler, inventorySlotId(diamondInventoryIndex), 0);
reset();
}
}
case IDLE -> {
}
}
}
private void beginIfReady(ClientPlayerEntity player, AnvilScreenHandler handler) {
if (!handler.getCursorStack().isEmpty()
|| handler.getSlot(AnvilScreenHandler.INPUT_1_ID).hasStack()
|| handler.getSlot(AnvilScreenHandler.INPUT_2_ID).hasStack()) {
return;
}
PlayerInventory inventory = player.getInventory();
for (int i = 0; i < PlayerInventory.MAIN_SIZE; i++) {
ItemStack stack = inventory.getStack(i);
if (stack.isOf(Items.DIAMOND_PICKAXE) && stack.getMaxDamage() - stack.getDamage() < 30) {
pickaxeInventoryIndex = i;
break;
}
}
if (pickaxeInventoryIndex < 0) {
return;
}
for (int i = 0; i < PlayerInventory.MAIN_SIZE; i++) {
if (inventory.getStack(i).isOf(Items.DIAMOND)) {
diamondInventoryIndex = i;
break;
}
}
if (diamondInventoryIndex < 0) {
pickaxeInventoryIndex = -1;
return;
}
step = Step.PICK_UP_PICKAXE;
}
private void waitForResult(MinecraftClient client, ClientPlayerEntity player, AnvilScreenHandler handler) {
if (handler.getSlot(AnvilScreenHandler.OUTPUT_ID).hasStack()) {
int levelCost = handler.getLevelCost();
if (levelCost > 0 && (player.isInCreativeMode() || player.experienceLevel >= levelCost)) {
click(client, handler, AnvilScreenHandler.OUTPUT_ID, 0);
step = Step.RETURN_REPAIRED_PICKAXE;
return;
}
}
if (++outputWaitTicks >= 20) {
step = Step.ROLLBACK_PICKAXE;
}
}
private void click(MinecraftClient client, AnvilScreenHandler handler, int slotId, int button) {
client.interactionManager.clickSlot(handler.syncId, slotId, button, SlotActionType.PICKUP, client.player);
}
private static int inventorySlotId(int inventoryIndex) {
return inventoryIndex < PlayerInventory.HOTBAR_SIZE ? inventoryIndex + 30 : inventoryIndex - 6;
}
private void reset() {
step = Step.IDLE;
pickaxeInventoryIndex = -1;
diamondInventoryIndex = -1;
outputWaitTicks = 0;
}
}