Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
29 1,409
src/client/java/ru/kodari/aichat/AiChatClient.java
package ru.kodari.aichat;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.prefs.Preferences;
import com.mojang.blaze3d.platform.InputConstants;
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.message.v1.ClientSendMessageEvents;
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.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ServerboundSetCarriedItemPacket;
import net.minecraft.resources.Identifier;
import net.minecraft.stats.StatType;
import net.minecraft.stats.StatsCounter;
import net.minecraft.stats.Stats;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ContainerInput;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
public final class AiChatClient implements ClientModInitializer {
private static final int MAX_PROMPT_LENGTH = 1200;
private static final int MAX_ANSWER_LENGTH = 3000;
private static final String ACTION_PREFIX = "[[ACTION:";
private static final String COMMAND_PREFIX = "[[COMMAND:";
private static final String MINE_PREFIX = "[[MINE|";
private static final Identifier HUD_ID = Identifier.fromNamespaceAndPath("aichat", "bitbot_head");
private static KeyMapping playIdleAnimationKey;
private static KeyMapping positionBitbotKey;
private static KeyMapping openChatTabletKey;
private static MiningTask miningTask;
private static final Identifier[] BITBOT_FRAMES = {
Identifier.fromNamespaceAndPath("aichat", "textures/gui/bitbot_frame_1.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/bitbot_frame_2.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/bitbot_frame_3.png")
};
private static final Identifier[] IDLE_FRAMES = {
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_1.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_2.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_3.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_4.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_5.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_6.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_7.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_8.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_9.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_10.png"),
Identifier.fromNamespaceAndPath("aichat", "textures/gui/idle/bitbot_idle_frame_11.png")
};
private static final long FRAME_DURATION_NANOS = 150_000_000L;
private static final long IDLE_FRAME_DURATION_NANOS = 250_000_000L;
private static final long MIN_IDLE_DELAY_NANOS = Duration.ofMinutes(1).toNanos();
private static final long MAX_IDLE_DELAY_NANOS = Duration.ofMinutes(5).toNanos();
private static volatile boolean aiSpeaking;
private static volatile long animationStartedAt;
private static volatile boolean idleAnimationActive;
private static volatile long idleAnimationStartedAt;
private static volatile long nextIdleAnimationAt;
private static final Preferences LOCAL_SETTINGS = Preferences.userNodeForPackage(AiChatClient.class);
private static final List<ChatMessage> CHAT_HISTORY = new ArrayList<>();
private static BitBotChatScreen chatScreen;
private static int bitbotXPercent = Math.clamp(LOCAL_SETTINGS.getInt("bitbot_x_percent", 100), 0, 100);
private static int bitbotYPercent = Math.clamp(LOCAL_SETTINGS.getInt("bitbot_y_percent", 0), 0, 100);
private static boolean positionEditorOpen;
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(15))
.build();
private static final ExecutorService REQUEST_EXECUTOR = Executors.newSingleThreadExecutor(task -> {
Thread thread = new Thread(task, "ai-chat-requests");
thread.setDaemon(true);
return thread;
});
@Override
public void onInitializeClient() {
resetIdleAnimationTimer();
playIdleAnimationKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.aichat.play_idle_animation", InputConstants.Type.KEYBOARD, InputConstants.KEY_R, KeyMapping.Category.MISC));
positionBitbotKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.aichat.position_bitbot", InputConstants.Type.KEYBOARD, InputConstants.KEY_P, KeyMapping.Category.MISC));
openChatTabletKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.aichat.open_chat_tablet", InputConstants.Type.KEYBOARD, InputConstants.KEY_B, KeyMapping.Category.MISC));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.player != null && !LOCAL_SETTINGS.getBoolean("bitbot_name_prompted", false)
&& client.gui.screen() == null) {
LOCAL_SETTINGS.putBoolean("bitbot_name_prompted", true);
client.gui.setScreen(new BitBotNameScreen());
}
while (openChatTabletKey.consumeClick()) {
if (client.gui.screen() == null) {
client.gui.setScreen(new BitBotChatScreen());
}
}
while (positionBitbotKey.consumeClick()) {
if (client.gui.screen() == null) {
positionEditorOpen = true;
client.gui.setScreen(new BitBotPositionScreen());
}
}
while (playIdleAnimationKey.consumeClick()) {
if (!aiSpeaking) {
idleAnimationStartedAt = System.nanoTime();
idleAnimationActive = true;
}
}
tickMining(client);
});
HudElementRegistry.addLast(HUD_ID, (graphics, deltaTracker) -> {
if (positionEditorOpen) {
return;
}
Identifier texture = getCurrentFrame(System.nanoTime());
int size = 40;
int maxX = Math.max(0, graphics.guiWidth() - 16 - size);
int maxY = Math.max(0, graphics.guiHeight() - 16 - size - BitBotPositionScreen.DRAG_STRIP);
int x = 8 + Math.round(maxX * bitbotXPercent / 100.0F);
int y = 8 + Math.round(maxY * bitbotYPercent / 100.0F) + BitBotPositionScreen.DRAG_STRIP;
graphics.blit(RenderPipelines.GUI_TEXTURED, texture, x, y, 0.0F, 0.0F,
size, size, size, size, size, size, -1);
});
ClientSendMessageEvents.ALLOW_CHAT.register(AiChatClient::handleChatMessage);
}
private static Identifier getCurrentFrame(long now) {
if (aiSpeaking) {
int frame = (int) ((now - animationStartedAt) / FRAME_DURATION_NANOS % BITBOT_FRAMES.length);
return BITBOT_FRAMES[frame];
}
if (!idleAnimationActive) {
if (now < nextIdleAnimationAt) {
return BITBOT_FRAMES[0];
}
idleAnimationStartedAt = now;
idleAnimationActive = true;
}
long elapsed = now - idleAnimationStartedAt;
if (elapsed >= IDLE_FRAME_DURATION_NANOS * IDLE_FRAMES.length) {
idleAnimationActive = false;
nextIdleAnimationAt = now + nextIdleDelayNanos();
return BITBOT_FRAMES[0];
}
return IDLE_FRAMES[(int) (elapsed / IDLE_FRAME_DURATION_NANOS)];
}
static Identifier previewFrame() {
return BITBOT_FRAMES[0];
}
static List<ChatMessage> chatHistory() {
return List.copyOf(CHAT_HISTORY);
}
static void registerChatScreen(BitBotChatScreen screen) {
chatScreen = screen;
}
static void unregisterChatScreen(BitBotChatScreen screen) {
if (chatScreen == screen) {
chatScreen = null;
}
}
private static void appendChatMessage(boolean fromPlayer, String text) {
ChatMessage message = new ChatMessage(fromPlayer, text);
CHAT_HISTORY.add(message);
if (CHAT_HISTORY.size() > 100) {
CHAT_HISTORY.remove(0);
}
if (chatScreen != null) {
chatScreen.addConversationMessage(message);
}






