Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 186
src/main/java/com/ninjatotem/NinjaTotemMod.java
package com.ninjatotem;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class NinjaTotemMod implements ClientModInitializer {
public static final String MOD_ID = "ninjatotem";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final int HOTBAR_SCREEN_START = 36;
private static final int HOTBAR_SCREEN_END = 44;
private static final long SWAP_DELAY_MILLIS = 4L;
private static final ScheduledExecutorService SWAP_SCHEDULER = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable, "NinjaTotem-SwapDelay");
thread.setDaemon(true);
return thread;
});
private static int previousTotemCount = -1;
private static boolean swapScheduled;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(NinjaTotemMod::onClientTick);
LOGGER.info("Ninja Totem Optimizer loaded.");
}
private static void onClientTick(MinecraftClient client) {
ClientPlayerEntity player = client.player;
if (player == null) {
previousTotemCount = -1;
return;
}
int currentTotemCount = countTotems(player);
if (previousTotemCount == -1) {
previousTotemCount = currentTotemCount;
return;
}
if (currentTotemCount < previousTotemCount && currentTotemCount > 0 && !swapScheduled) {
scheduleHotbarTotemSelection(client);
}
previousTotemCount = currentTotemCount;
}
private static void scheduleHotbarTotemSelection(MinecraftClient client) {
swapScheduled = true;
SWAP_SCHEDULER.schedule(() -> client.execute(() -> {
swapScheduled = false;
selectNextHotbarTotem(client);
}), SWAP_DELAY_MILLIS, TimeUnit.MILLISECONDS);
}
private static void selectNextHotbarTotem(MinecraftClient client) {
ClientPlayerEntity player = client.player;
if (player == null || client.interactionManager == null) {
return;
}
for (int slot = HOTBAR_SCREEN_START; slot <= HOTBAR_SCREEN_END; slot++) {
if (player.playerScreenHandler.getSlot(slot).getStack().isOf(Items.TOTEM_OF_UNDYING)) {
player.getInventory().selectedSlot = slot - HOTBAR_SCREEN_START;
client.interactionManager.syncSelectedSlot();
return;
}
}
}
private static int countTotems(ClientPlayerEntity player) {
int total = 0;
for (int slot = 5; slot <= 45; slot++) {
ItemStack stack = player.playerScreenHandler.getSlot(slot).getStack();
if (stack.isOf(Items.TOTEM_OF_UNDYING)) {
total += stack.getCount();
}
}
return total;
}
}