Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 196
src/client/java/com/kodari/autototem/InventoryAutoTotemClient.java
package com.kodari.autototem;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Items;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;
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;
public class InventoryAutoTotemClient implements ClientModInitializer {
private static final int DEFAULT_DELAY_MS = 500;
private static final int MAX_DELAY_MS = 60000;
private final Path configPath = FabricLoader.getInstance().getConfigDir().resolve("inventory-autototem.properties");
private int delayMs = DEFAULT_DELAY_MS;
private long waitingSince = -1;
private long lastAttempt = -1;
@Override
public void onInitializeClient() {
loadConfig();
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(
ClientCommandManager.literal("autototem")
.executes(context -> {
context.getSource().sendFeedback(Text.literal("AutoTotem delay: " + delayMs + " ms. Set it with /autototem delay <milliseconds>."));
return 1;
})
.then(ClientCommandManager.literal("delay")
.then(ClientCommandManager.argument("milliseconds", IntegerArgumentType.integer(0, MAX_DELAY_MS))
.executes(context -> {
delayMs = IntegerArgumentType.getInteger(context, "milliseconds");
waitingSince = -1;
boolean saved = saveConfig();
context.getSource().sendFeedback(Text.literal("AutoTotem delay set to " + delayMs + " ms" + (saved ? "." : " (could not save settings).")));
return 1;
})))
));
}
private void tick(MinecraftClient client) {
if (client.player == null || client.interactionManager == null
|| !(client.currentScreen instanceof InventoryScreen)
|| client.player.currentScreenHandler != client.player.playerScreenHandler
|| client.player.getOffHandStack().isOf(Items.TOTEM_OF_UNDYING)) {
waitingSince = -1;
return;
}
long now = System.nanoTime();
if (waitingSince == -1) {
waitingSince = now;
}
if (now - waitingSince < delayMs * 1_000_000L
|| (lastAttempt != -1 && now - lastAttempt < 1_000_000_000L)) {
return;
}
PlayerInventory inventory = client.player.getInventory();
for (Slot slot : client.player.currentScreenHandler.slots) {
if (slot.inventory == inventory && slot.getIndex() >= 0 && slot.getIndex() < 36
&& inventory.getStack(slot.getIndex()).isOf(Items.TOTEM_OF_UNDYING)) {
client.interactionManager.clickSlot(client.player.currentScreenHandler.syncId, slot.id, 40,
SlotActionType.SWAP, client.player);
lastAttempt = now;
waitingSince = -1;
return;
}
}
}
private void loadConfig() {
if (!Files.isRegularFile(configPath)) {
return;
}
Properties properties = new Properties();
try (InputStream input = Files.newInputStream(configPath)) {
properties.load(input);
int value = Integer.parseInt(properties.getProperty("delay_ms", Integer.toString(DEFAULT_DELAY_MS)));
delayMs = Math.max(0, Math.min(MAX_DELAY_MS, value));
} catch (IOException | IllegalArgumentException ignored) {
delayMs = DEFAULT_DELAY_MS;
}
}
private boolean saveConfig() {
Properties properties = new Properties();
properties.setProperty("delay_ms", Integer.toString(delayMs));
try {
Files.createDirectories(configPath.getParent());
try (OutputStream output = Files.newOutputStream(configPath)) {
properties.store(output, "Inventory AutoTotem settings");
}
return true;
} catch (IOException exception) {
return false;
}
}
}