Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
9 766
src/client/java/com/kodari/litematicasmartswitch/LitematicaSmartSwitchClient.java
package com.kodari.litematicasmartswitch;
import java.lang.reflect.Method;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ContainerInput;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
public final class LitematicaSmartSwitchClient implements ClientModInitializer {
private static final String MOD_ID = "litematica_smartswitch";
private static final KeyMapping.Category KEY_CATEGORY = KeyMapping.Category.register(
Identifier.fromNamespaceAndPath(MOD_ID, "controls"));
private static final KeyMapping TOGGLE_KEY = registerKey("key.litematica_smartswitch.toggle", 71);
private static final KeyMapping INVENTORY_KEY = registerKey("key.litematica_smartswitch.inventory", 72);
private static final KeyMapping HUD_KEY = registerKey("key.litematica_smartswitch.hud", 74);
private static final KeyMapping DEBUG_KEY = registerKey("key.litematica_smartswitch.debug", 75);
private static final KeyMapping LOGIC_KEY = registerKey("key.litematica_smartswitch.logic", 76);
private static Probe probe;
private static BlockState neededState;
private static PendingSwap pendingSwap;
private static String availability = "Waiting";
private static int selectedSlotForHud = -1;
private static String status = "Waiting for Litematica";
private static BlockPos lastInventorySearchPosition;
private static Item lastInventorySearchItem;
private static long nextInventorySearchAtNanos;
@Override
public void onInitializeClient() {
SmartSwitchConfig.load(Minecraft.getInstance().gameDirectory.toPath());
ClientTickEvents.END_CLIENT_TICK.register(LitematicaSmartSwitchClient::tick);
HudElementRegistry.addLast(Identifier.fromNamespaceAndPath(MOD_ID, "status"),
(graphics, deltaTracker) -> renderHud(graphics));
}
private static KeyMapping registerKey(String translationKey, int keyCode) {
return KeyMappingHelper.registerKeyMapping(new KeyMapping(translationKey, keyCode, KEY_CATEGORY));
}
private static void tick(Minecraft client) {
if (TOGGLE_KEY.consumeClick()) {
SmartSwitchConfig.set("enabled", !SmartSwitchConfig.enabled);
status = SmartSwitchConfig.enabled ? "Enabled" : "Disabled";
}
if (INVENTORY_KEY.consumeClick()) {
SmartSwitchConfig.set("inventorySearch", !SmartSwitchConfig.inventorySearch);
}
if (HUD_KEY.consumeClick()) {
SmartSwitchConfig.set("hudEnabled", !SmartSwitchConfig.hudEnabled);
}
if (DEBUG_KEY.consumeClick()) {
SmartSwitchConfig.set("debugEnabled", !SmartSwitchConfig.debugEnabled);
}
if (LOGIC_KEY.consumeClick()) {
SmartSwitchConfig.set("firstSlotSelection", !SmartSwitchConfig.firstSlotSelection);
}
}
private static void updateSelection(Minecraft client) {
if (!SmartSwitchConfig.enabled) {
probe = null;
neededState = null;
availability = "Smart Switch disabled";
return;
}
if (client.player == null || client.level == null || client.gameMode == null) {
probe = null;
neededState = null;
pendingSwap = null;
availability = "Waiting for a world";
status = "Waiting for a world";
return;
}
selectedSlotForHud = client.player.getInventory().getSelectedSlot() + 1;
if (isSwapPending(client)) {
status = "Waiting for inventory swap";
availability = "Inventory swap pending";
return;
}
if (!(client.hitResult instanceof BlockHitResult hit) || hit.getType() != HitResult.Type.BLOCK) {
probe = null;
neededState = null;
availability = "No block targeted";
status = LitematicaAccess.status();
return;
}
BlockPos position = placementPosition(client, hit);
Inventory inventory = client.player.getInventory();
int selectedSlot = inventory.getSelectedSlot();
selectedSlotForHud = selectedSlot + 1;
BlockState actual = client.level.getBlockState(position);
if (!SmartSwitchConfig.refreshSchematicEveryTick && probe != null
&& probe.position().equals(position) && probe.actual().equals(actual) && neededState != null
&& matches(inventory.getItem(selectedSlot), neededState.getBlock().asItem(), neededState.getBlock())) {
return;
}
BlockState required = LitematicaAccess.getBlockState(position, SmartSwitchConfig.refreshSchematicEveryTick);
if (required == null) {
probe = null;
neededState = null;
availability = "Schematic data unavailable";
status = LitematicaAccess.status();
return;
}
probe = new Probe(position, required, actual);
if (required.isAir()) {
neededState = SmartSwitchConfig.ignoreAirBlocks ? null : required;
availability = "Air never selects an item";
status = SmartSwitchConfig.ignoreAirBlocks ? "Schematic air ignored" : "Schematic air target";
return;
}
boolean completed = SmartSwitchConfig.blockStateMatching
? required.equals(actual) : required.getBlock() == actual.getBlock();
if (SmartSwitchConfig.ignoreCompletedBlocks && completed) {
neededState = null;
availability = "Already complete";
status = "Already matches the schematic";
return;
}
Item requiredItem = required.getBlock().asItem();
if (requiredItem == Items.AIR) {
pendingSwap = null;
neededState = null;
availability = "No block item exists";
status = "Need " + blockName(required) + " — block has no matching item";
return;
}
neededState = required;
if (matches(inventory.getItem(selectedSlot), requiredItem, required.getBlock())) {
availability = "Available in selected hotbar slot";
status = "Matching item already selected";
return;
}
int hotbarSlot = findHotbarSlot(inventory, requiredItem, required.getBlock(), selectedSlot);
if (hotbarSlot >= 0) {
availability = "Available in hotbar";
if (SmartSwitchConfig.automaticHotbarSwitching && hotbarSlot != selectedSlot) {
inventory.setSelectedSlot(hotbarSlot);
selectedSlotForHud = hotbarSlot + 1;
}
status = SmartSwitchConfig.automaticHotbarSwitching
? "Matching hotbar slot selected" : "Matching item found in hotbar";
return;
}
availability = "Not in hotbar";
if (!SmartSwitchConfig.inventorySearch) {
status = "Item not in hotbar; inventory search is off";
return;
}
if (!SmartSwitchConfig.fullInventoryScan) {
status = "Full inventory scan is off";
return;
}
if (position.equals(lastInventorySearchPosition) && requiredItem == lastInventorySearchItem
&& System.nanoTime() < nextInventorySearchAtNanos) {
availability = "Not found in inventory";
status = "Required item not found";
return;
}
int inventorySlot = findInventorySlot(inventory, requiredItem, required.getBlock());
if (inventorySlot < 0) {
lastInventorySearchPosition = position.immutable();
lastInventorySearchItem = requiredItem;
nextInventorySearchAtNanos = System.nanoTime() + 100_000_000L;
availability = "Not found in inventory";
status = "Required item not found";
return;
}
availability = "Available in main inventory";
if (!SmartSwitchConfig.automaticInventorySwap) {
status = "Item found in inventory; automatic swap is off";
return;
}
if (client.player.containerMenu != client.player.inventoryMenu) {
status = "Close other container to swap inventory item";