Added customization

This commit is contained in:
Cametendo
2026-03-26 20:13:18 +01:00
parent 90ba17c108
commit 14d8b0c431
10 changed files with 973 additions and 251 deletions

132
README.md
View File

@@ -4,52 +4,118 @@
--- ---
## About ## About
This Minecraft mod allows players, especially new ones, to get real-time assistance. If you are confused about a mechanic or a location, simply ask your "Companion" for advice. Minecraft AI Companion adds an AI-powered helper to Minecraft. You can ask questions directly in chat, or spawn a companion entity that follows you and opens its own chat window when you right-click it.
- Example: If a player is in the Deep Dark, they can ask: "What can I do here?" and the AI will provide useful tips and warnings about the Warden.
The mod is especially useful for newer players, but it also works as a general in-game assistant depending on the mode you choose.
Examples:
- "What can I do in the Deep Dark?"
- "How do I find Netherite?"
- "What does this structure do?"
## Requirements & Version ## Requirements & Version
* **Game Version:** 1.21.1 * **Minecraft Version:** 1.20.1
* **Java Version:** 17+
* **Loader:** [Fabric Loader](https://fabricmc.net/) * **Loader:** [Fabric Loader](https://fabricmc.net/)
* **Dependencies:** [Fabric API](https://www.curseforge.com/minecraft/mc-mods/fabric-api) * **Dependency:** [Fabric API](https://www.curseforge.com/minecraft/mc-mods/fabric-api)
* **Information**: This mod uses a self-hosted-AI. Active Internet connection and API key required (no subscription). * **Internet:** Required when using a remote AI provider
* **API Key**: This Mod requires an API key from [ai.cametendo.org](https://ai.cametendo.org). Steps on how to acquire an API will be listed further down in the README. * **AI Backend:** Supports local or self-hosted providers such as Ollama, Open-WebUI, or a custom OpenAI-compatible endpoint
## Usage ## Features
There are three commands included in this mod: - Ask questions directly from in-game chat with `/ai question <message>`
- `/ai question`: This will let you communicate with the AI directly from the In-Game Chat - Spawn one or more AI companion entities with `/ai spawn`
- `/ai spawn`: This will spawn an actual entity, your 'Companion' (multiple companions can be spawned). It will follow you around and can be interacted with by right-clicking. - Right-click a companion to open a persistent chat window for the current world session
- `/ai kill`: This will kill / remove all companions you spawned - Choose between different AI providers:
- `/ai delete-key`: This lets you delete your API-Key, incase you deleted your account on the WebUI or experience other problems, you can use this command to enter a new key. Ollama, Open-WebUI, or a custom endpoint
- Note: Depending on the question the AI will take 1 - 3 minutes to respond. Please be patient while your companion "thinks"! - Fetch available models directly from the configured provider
- Switch between two AI behavior modes:
`Casual` for a general assistant, or `Minecraft` to restrict answers to Minecraft-related topics
- Change the selected model later without redoing the full setup
- Delete only the stored API key, or reset the full AI configuration
- Stored API keys are saved locally in config files and are encrypted when possible
## Commands
The mod currently includes these commands:
- `/ai question <question>`
Sends a question to the configured AI provider and returns the answer in chat.
- `/ai spawn`
Spawns an AI companion entity at your position.
- `/ai kill`
Removes all spawned AI companions.
- `/ai delete-key`
Deletes the stored API key but keeps the rest of the provider configuration.
- `/ai delete-config`
Deletes the full saved AI configuration so setup starts from scratch next time.
- `/ai model`
Opens the model selection screen.
- `/ai change-mode`
Opens the mode selection screen.
Note: Response time depends on your provider, model, and hardware. Local models or busy servers may take longer to answer.
## AI Companion Entity ## AI Companion Entity
The AI Companion doesn't just stand around, he too has his own features: The AI Companion is more than just a decoration:
- `Chat-Window`: Right-clicking the Companion will open a chat window. In this, you will have a chat-interface that keeps your chat until you leave the world. - `Follow Owner`: The companion follows the player who spawned it.
- `Follows you`: Instead of just standing around, the companion will ffollow you around wherever you go and walk around the world. - `Right-Click Chat`: Interacting with the entity opens the companion chat screen.
- `API-Key-Verification`: The first time you play this mod, you may need to use the companion to enter your API key. - `Persistent Session per World Join`: Companion chat history stays available until you leave the world/server.
- `Invulnerable`: The entity cannot be damaged through normal gameplay.
- `Custom Appearance`: The companion uses a custom player-style model and can temporarily switch appearance through a built-in easter egg.
## First-Time Setup
The first time you use an AI feature, the setup screen opens automatically.
You can configure:
- `Provider Preset`
Choose `Ollama`, `Open-WebUI`, or `Custom`
- `Base URL`
Example: `http://localhost:11434` for Ollama
- `API Key`
Optional for local providers, required for protected remote providers
- `Model`
Fetch available models from the provider, or enter one manually
- `Advanced API Path`
Optional custom chat-completions path for OpenAI-compatible endpoints
- `Mode`
`Casual` or `Minecraft`
After saving, the mod stores the configuration in the `config/` folder and reuses it automatically.
## Provider Notes
- `Ollama`
Best for fully local use. Usually works with `http://localhost:11434` and supports model fetching from the setup screen.
- `Open-WebUI`
Works with Open-WebUI-compatible chat completion endpoints and model lists.
- `Custom`
Use this for any self-hosted or compatible provider with a custom base URL and optional custom API path.
If no custom API path is set, the client automatically tries standard chat-completions endpoints.
## Installation ## Installation
1. If not already done, install [Fabric 1.21.1](https://maven.fabricmc.net/net/fabricmc/fabric-installer/1.1.1/fabric-installer-1.1.1.jar). (Click to download the Fabric Installer instantly) 1. Install [Fabric Loader](https://fabricmc.net/use/installer/) for Minecraft 1.20.1 if you have not already done so.
2. Download the mod from the [Releases](https://github.com/Cametendo/minecraft-ai-companion-EMVs12-Project/releases) Tab. 2. Download the mod `.jar` from the [Releases](https://github.com/Cametendo/minecraft-ai-companion-EMVs12-Project/releases) page.
3. Place the `.jar` file in your `%appdata%/.minecraft/mods` folder. 3. Place the `.jar` file into your Minecraft `mods` folder.
4. Launch the game in your preferred launcher with the Fabric 1.21.1 Profile ([List of Minecraft Launchers](https://github.com/TayouVR/MinecraftLauncherComparison)) <br> 4. Make sure [Fabric API](https://www.curseforge.com/minecraft/mc-mods/fabric-api) is installed as well.
5. Start Minecraft with your Fabric 1.20.1 profile.
**IMPORTANT**: This Installation Process is strictly for the offical version of Minecraft (including the official Launcher). We are not not responsible for any issues, data loss, or crashes that may occur when using third-party launchers or unofficial versions of the game. Support is only guaranteed for the official Fabric environment. **Important:** Support is only guaranteed for the official Java Edition + Fabric environment. Third-party launchers or unofficial game versions may work, but are not officially supported.
## API-Key ## API Key
This mod requires an API key. To acquire an API key, follow these steps: You only need an API key if your chosen AI provider requires one.
1. Create an account on [ai.cametendo.org](https://ai.cametendo.org)
2. Press your user profile (bottom-left corner) and switch to the tab "Account". If you are using a hosted Open-WebUI instance such as ai.example.org, the general flow is:
3. Find the option "API Keys" and press show. A very long line of dots should appear. This is your API key (hidden by default). 1. Create an account on the provider website.
4. Copy it and start your game (or go back to it if it's already open) 2. Open your account settings and generate or reveal an API key.
5. Use /ai question and enter a question. A new window with "Enter your Open-WebUI API-Key" should appear. In the text box, enter your API Key and confirm it by pressing enter. 3. Copy the key.
6. Alternatively, if that didn't work, use /ai spawn to spawn your AI Companion. THen right-click it and you should see the "Enter your Open-WebUI API-Key" window. Enter it into the text box and confirm it by pressing enter. 4. In Minecraft, use any AI feature such as `/ai question <question>` or `/ai spawn`.
7. If everything worked, you can now send messages to your AI Companion. Have fun! 5. When the setup screen opens, paste the API key into the `API Key` field.
6. Choose the correct provider URL and model, then save the configuration.
If your provider does not require a key, you can leave that field empty.
## License & Credits ## License & Credits
* **Authors:** [Cametendo](https://www.github.com/Cametendo), [ritonioz](https://www.github.com/ritonioz), [Adam237A](https://www.github.com/Adam237A) * **Authors:** [Cametendo](https://www.github.com/Cametendo), [ritonioz](https://www.github.com/ritonioz), [Adam237A](https://www.github.com/Adam237A)
* **License:** CC0 1.0 (Public Domain). Feel free to include this in any modpack! (Credits are appreciated but not required). * **License:** CC0 1.0 (Public Domain). Feel free to include this mod in any modpack. Credits are appreciated, but not required.
NOT AN OFFICIAL MINECRAFT SERVICE. NOT APPROVED BY OR ASSOCIATED WITH MOJANG OR MICROSOFT NOT AN OFFICIAL MINECRAFT SERVICE. NOT APPROVED BY OR ASSOCIATED WITH MOJANG OR MICROSOFT.

View File

@@ -2,6 +2,7 @@ package AiCompanion.aicompanion2_0.client;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -22,6 +23,7 @@ public class AiChatSession {
this.apiBaseUrl = apiBaseUrl; this.apiBaseUrl = apiBaseUrl;
this.apiKey = apiKey; this.apiKey = apiKey;
this.model = model; this.model = model;
this.apiPath = (apiPath != null && !apiPath.isBlank()) ? apiPath : null;
displayLines.add("§7Start a conversation with your AI companion!"); displayLines.add("§7Start a conversation with your AI companion!");
} }
@@ -51,23 +53,17 @@ public class AiChatSession {
} }
private String callOpenWebUI() throws Exception { private String callOpenWebUI() throws Exception {
// Build messages array String json = buildChatRequestJson(false);
StringBuilder messages = new StringBuilder("[");
for (int i = 0; i < history.size(); i++) {
String[] msg = history.get(i);
messages.append("{\"role\":\"").append(msg[0])
.append("\",\"content\":\"").append(jsonEscape(msg[1]))
.append("\"}");
if (i < history.size() - 1) messages.append(",");
}
messages.append("]");
String json = "{\"model\":\"" + jsonEscape(model) + "\",\"messages\":" + messages + ",\"stream\":false}";
if (apiPath != null) { if (apiPath != null) {
// User configured a specific path — use it directly, no fallback // User configured a specific path — use it directly, no fallback
HttpResult result = postChatCompletion(apiPath, json); HttpResult result = postChatCompletion(apiPath, json);
if (result.status == 200) return extractAssistantContent(result.body); if (result.status == 200) return extractAssistantContent(result.body);
if (shouldRetryWithoutSystemPrompt(result)) {
HttpResult retry = postChatCompletion(apiPath, buildChatRequestJson(true));
if (retry.status == 200) return extractAssistantContent(retry.body);
return formatHttpError(retry.status, retry.body, apiPath);
}
return formatHttpError(result.status, result.body, apiPath); return formatHttpError(result.status, result.body, apiPath);
} }
@@ -77,19 +73,102 @@ public class AiChatSession {
return extractAssistantContent(primary.body); return extractAssistantContent(primary.body);
} }
if (shouldRetryWithoutSystemPrompt(primary)) {
HttpResult retry = postChatCompletion("/api/chat/completions", buildChatRequestJson(true));
if (retry.status == 200) {
return extractAssistantContent(retry.body);
}
if (retry.status < 500 && retry.status != 404) {
return formatHttpError(retry.status, retry.body, "/api/chat/completions");
}
}
if (primary.status >= 500 || primary.status == 404) { if (primary.status >= 500 || primary.status == 404) {
HttpResult fallback = postChatCompletion("/v1/chat/completions", json); HttpResult fallback = postChatCompletion("/v1/chat/completions", json);
if (fallback.status == 200) { if (fallback.status == 200) {
return extractAssistantContent(fallback.body); return extractAssistantContent(fallback.body);
} }
if (shouldRetryWithoutSystemPrompt(fallback)) {
HttpResult retry = postChatCompletion("/v1/chat/completions", buildChatRequestJson(true));
if (retry.status == 200) {
return extractAssistantContent(retry.body);
}
return formatHttpError(retry.status, retry.body, "/v1/chat/completions");
}
return formatHttpError(fallback.status, fallback.body, "/v1/chat/completions"); return formatHttpError(fallback.status, fallback.body, "/v1/chat/completions");
} }
return formatHttpError(primary.status, primary.body, "/api/chat/completions"); return formatHttpError(primary.status, primary.body, "/api/chat/completions");
} }
private String buildChatRequestJson(boolean inlineSystemPrompt) {
String messages = buildMessagesJson(inlineSystemPrompt);
return "{\"model\":\"" + jsonEscape(model) + "\",\"messages\":" + messages + ",\"stream\":false}";
}
private String buildMessagesJson(boolean inlineSystemPrompt) {
String systemPrompt = buildSystemPrompt();
StringBuilder messages = new StringBuilder("[");
if (inlineSystemPrompt) {
appendHistoryWithInlinePrompt(messages, systemPrompt);
} else {
messages.append("{\"role\":\"system\",\"content\":\"").append(jsonEscape(systemPrompt)).append("\"}");
for (String[] msg : history) {
messages.append(",{\"role\":\"").append(msg[0])
.append("\",\"content\":\"").append(jsonEscape(msg[1]))
.append("\"}");
}
}
messages.append("]");
return messages.toString();
}
private void appendHistoryWithInlinePrompt(StringBuilder messages, String systemPrompt) {
boolean first = true;
boolean injected = false;
for (String[] msg : history) {
String role = msg[0];
String content = msg[1];
if (!injected && "user".equals(role)) {
content = systemPrompt + "\n\nUser message: " + content;
injected = true;
}
if (!first) {
messages.append(",");
}
messages.append("{\"role\":\"").append(role)
.append("\",\"content\":\"").append(jsonEscape(content))
.append("\"}");
first = false;
}
if (!injected) {
messages.append("{\"role\":\"user\",\"content\":\"")
.append(jsonEscape(systemPrompt))
.append("\"}");
}
}
private boolean shouldRetryWithoutSystemPrompt(HttpResult result) {
if (result.status != 400 || result.body == null || result.body.isBlank()) {
return false;
}
String normalized = result.body.toLowerCase();
return normalized.contains("developer instruction")
|| normalized.contains("system instruction")
|| normalized.contains("system_message")
|| normalized.contains("system message");
}
private HttpResult postChatCompletion(String path, String json) throws Exception { private HttpResult postChatCompletion(String path, String json) throws Exception {
URL url = new URL(apiBaseUrl + path); URL url = URI.create(apiBaseUrl + path).toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
@@ -140,6 +219,16 @@ public class AiChatSession {
return "Error: HTTP " + status + " (" + path + ") - " + body; return "Error: HTTP " + status + " (" + path + ") - " + body;
} }
private String buildSystemPrompt() {
if ("minecraft".equals(ClientConfig.getMode())) {
return "You are a Minecraft-focused AI assistant. Always respond in the exact same language as the user's message. " +
"Only answer Minecraft-related questions. If the user asks about anything unrelated to Minecraft, " +
"respond in their language that you are only programmed to answer Minecraft-specific questions.";
}
return "You are a helpful AI companion. Always respond in the exact same language as the user's message. " +
"If the user writes in romanized text (e.g. Japanese romaji), also include a translation in square brackets in English.";
}
private String jsonEscape(String value) { private String jsonEscape(String value) {
if (value == null) return ""; if (value == null) return "";
return value.replace("\\", "\\\\") return value.replace("\\", "\\\\")

View File

@@ -2,10 +2,7 @@ package AiCompanion.aicompanion2_0.client;
import AiCompanion.aicompanion2_0.AIEntity; import AiCompanion.aicompanion2_0.AIEntity;
import AiCompanion.aicompanion2_0.Aicompanion2_0; import AiCompanion.aicompanion2_0.Aicompanion2_0;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer; 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.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry; import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
@@ -54,7 +51,7 @@ public class AiCompanionClient implements ClientModInitializer {
ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.QUESTION_PACKET_ID, (client, handler, buf, responseSender) -> { ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.QUESTION_PACKET_ID, (client, handler, buf, responseSender) -> {
String question = buf.readString(32767); String question = buf.readString(32767);
client.execute(() -> ensureApiKeyAndRun(client, () -> askQuestion(client, question))); client.execute(() -> ensureSetupAndRun(client, () -> askQuestion(client, question)));
}); });
ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.DELETE_KEY_PACKET_ID, (client, handler, buf, responseSender) -> { ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.DELETE_KEY_PACKET_ID, (client, handler, buf, responseSender) -> {
@@ -75,6 +72,30 @@ public class AiCompanionClient implements ClientModInitializer {
}); });
}); });
ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.DELETE_CONFIG_PACKET_ID, (client, handler, buf, responseSender) -> {
client.execute(() -> {
currentSession = null;
try {
ClientConfig.deleteConfig();
sendChatMessage(client, Text.literal("§6[AI] §fConfig deleted. Run any AI command to set it up again."));
} catch (Exception e) {
String error = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
sendChatMessage(client, Text.literal("§c[AI] Could not delete config: " + error));
}
});
});
ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.MODEL_SELECT_PACKET_ID, (client, handler, buf, responseSender) -> {
client.execute(() -> {
currentSession = null;
client.setScreen(new ModelSelectScreen());
});
});
ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.CHANGE_MODE_PACKET_ID, (client, handler, buf, responseSender) -> {
client.execute(() -> client.setScreen(new ModeSelectScreen()));
});
ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.ARCH_EASTER_EGG_RESPONSE_PACKET_ID, (client, handler, buf, responseSender) -> { ClientPlayNetworking.registerGlobalReceiver(Aicompanion2_0.ARCH_EASTER_EGG_RESPONSE_PACKET_ID, (client, handler, buf, responseSender) -> {
String response = buf.readString(32767); String response = buf.readString(32767);
client.execute(() -> finishArchEasterEgg(response)); client.execute(() -> finishArchEasterEgg(response));
@@ -85,23 +106,22 @@ public class AiCompanionClient implements ClientModInitializer {
if (world.isClient() && entity instanceof AIEntity) { if (world.isClient() && entity instanceof AIEntity) {
MinecraftClient client = MinecraftClient.getInstance(); MinecraftClient client = MinecraftClient.getInstance();
client.execute(() -> ensureApiKeyAndRun(client, () -> openChatScreen(client))); client.execute(() -> ensureSetupAndRun(client, () -> openChatScreen(client)));
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
return ActionResult.PASS; return ActionResult.PASS;
}); });
} }
private static void ensureApiKeyAndRun(MinecraftClient client, Runnable action) { private static void ensureSetupAndRun(MinecraftClient client, Runnable action) {
if (!ClientConfig.hasApiKey()) { if (!ClientConfig.isSetupDone()) {
currentSession = null; currentSession = null;
client.setScreen(new ApiKeyScreen(() -> { client.setScreen(new ProviderSetupScreen(() -> {
currentSession = null; currentSession = null;
action.run(); action.run();
})); }));
return; return;
} }
action.run(); action.run();
} }
@@ -132,9 +152,10 @@ public class AiCompanionClient implements ClientModInitializer {
sendChatMessage(client, Text.literal("§6[AI] §fThink about: " + question)); sendChatMessage(client, Text.literal("§6[AI] §fThink about: " + question));
AiChatSession session = new AiChatSession( AiChatSession session = new AiChatSession(
Aicompanion2_0.getApiBaseUrl(), ClientConfig.getBaseUrl(),
ClientConfig.getApiKey(), ClientConfig.getApiKey(),
Aicompanion2_0.getModel() ClientConfig.getModel(),
ClientConfig.getApiPath()
); );
session.sendMessage(question, response -> client.execute(() -> { session.sendMessage(question, response -> client.execute(() -> {

View File

@@ -13,7 +13,7 @@ public class ApiKeyScreen extends Screen {
private final Runnable onSuccess; private final Runnable onSuccess;
public ApiKeyScreen(Runnable onSuccess) { public ApiKeyScreen(Runnable onSuccess) {
super(Text.literal("Enter API Key (available on ai.cametendo.org)")); super(Text.literal("Enter API Key (available on ai.example.org)"));
this.onSuccess = onSuccess; this.onSuccess = onSuccess;
} }

View File

@@ -13,6 +13,7 @@ public class ClientConfig {
private static String baseUrl = null; private static String baseUrl = null;
private static String model = null; private static String model = null;
private static String apiPath = null; private static String apiPath = null;
private static String mode = null;
private static boolean loaded = false; private static boolean loaded = false;
public static String getApiKey() { public static String getApiKey() {
@@ -25,6 +26,34 @@ public class ClientConfig {
save(); save();
} }
/** Wipes all provider config (URL, model, key, path, mode) from memory and both config files. */
public static void deleteConfig() throws IOException {
apiKey = null;
baseUrl = null;
model = null;
apiPath = null;
mode = null;
loaded = false;
deleteConfigFrom(SHARED_CONFIG_PATH);
deleteConfigFrom(CONFIG_PATH);
}
private static void deleteConfigFrom(Path path) throws IOException {
if (!Files.exists(path)) return;
Properties props = new Properties();
try (FileInputStream in = new FileInputStream(path.toFile())) {
props.load(in);
}
props.remove("api.key");
props.remove("api.baseUrl");
props.remove("api.model");
props.remove("api.path");
props.remove("api.mode");
try (FileOutputStream out = new FileOutputStream(path.toFile())) {
props.store(out, "AI Companion Config");
}
}
public static boolean deleteApiKey() throws IOException { public static boolean deleteApiKey() throws IOException {
boolean deleted = deleteApiKeyFrom(SHARED_CONFIG_PATH, "AI Companion Shared Config"); boolean deleted = deleteApiKeyFrom(SHARED_CONFIG_PATH, "AI Companion Shared Config");
deleted = deleteApiKeyFrom(CONFIG_PATH, "AI Companion Client Config") || deleted; deleted = deleteApiKeyFrom(CONFIG_PATH, "AI Companion Client Config") || deleted;
@@ -52,6 +81,16 @@ public class ClientConfig {
return apiPath; return apiPath;
} }
public static String getMode() {
if (!loaded) load();
return mode != null && !mode.isBlank() ? mode : "casual";
}
public static void setMode(String m) {
mode = (m != null && !m.isBlank()) ? m : "casual";
save();
}
public static boolean isSetupDone() { public static boolean isSetupDone() {
if (!loaded) load(); if (!loaded) load();
return baseUrl != null && !baseUrl.isBlank() && model != null && !model.isBlank(); return baseUrl != null && !baseUrl.isBlank() && model != null && !model.isBlank();
@@ -66,12 +105,24 @@ public class ClientConfig {
save(); save();
} }
public static void setProviderConfig(String url, String mdl, String key, String path, String newMode) {
baseUrl = url;
model = mdl;
apiKey = key != null ? key : "";
apiPath = (path != null && !path.isBlank()) ? path : null;
mode = (newMode != null && !newMode.isBlank()) ? newMode : "casual";
loaded = true;
save();
}
private static void load() { private static void load() {
loaded = true; loaded = true;
// Remove the old plain-text key file if it still exists from a previous version
try { Files.deleteIfExists(Path.of("config", "aicompanion2_0.key")); } catch (Exception ignored) {}
try { try {
apiKey = loadProp(SHARED_CONFIG_PATH, "api.key"); apiKey = decryptKey(loadProp(SHARED_CONFIG_PATH, "api.key"));
if (apiKey == null || apiKey.isBlank()) { if (apiKey == null || apiKey.isBlank()) {
apiKey = loadProp(CONFIG_PATH, "api.key"); apiKey = decryptKey(loadProp(CONFIG_PATH, "api.key"));
} }
baseUrl = loadProp(SHARED_CONFIG_PATH, "api.baseUrl"); baseUrl = loadProp(SHARED_CONFIG_PATH, "api.baseUrl");
if (baseUrl == null || baseUrl.isBlank()) { if (baseUrl == null || baseUrl.isBlank()) {
@@ -85,22 +136,27 @@ public class ClientConfig {
if (apiPath == null || apiPath.isBlank()) { if (apiPath == null || apiPath.isBlank()) {
apiPath = loadProp(CONFIG_PATH, "api.path"); apiPath = loadProp(CONFIG_PATH, "api.path");
} }
mode = loadProp(SHARED_CONFIG_PATH, "api.mode");
if (mode == null || mode.isBlank()) {
mode = loadProp(CONFIG_PATH, "api.mode");
}
} catch (IOException e) { } catch (IOException e) {
apiKey = null; apiKey = null;
System.out.println("[aicompanion2_0] Could not load Client Config."); System.out.println("[aicompanion2_0] Could not load Client Config.");
} }
} }
private static void save() { /** Decrypts an ENC:-prefixed value; returns plaintext as-is for legacy configs. */
try { private static String decryptKey(String raw) {
saveApiKeyTo(CONFIG_PATH, "AI Companion Client Config"); if (raw == null || raw.isBlank()) return null;
saveApiKeyTo(SHARED_CONFIG_PATH, "AI Companion Shared Config"); if (raw.startsWith(CryptoUtil.PREFIX)) {
} catch (IOException e) { return CryptoUtil.decrypt(raw.substring(CryptoUtil.PREFIX.length()));
System.out.println("[aicompanion2_0] Could not save Client Config.");
} }
// Legacy plaintext — will be re-saved encrypted on next write
return raw;
} }
private static String loadApiKeyFrom(Path path) throws IOException { private static String loadProp(Path path, String key) throws IOException {
if (!Files.exists(path)) return null; if (!Files.exists(path)) return null;
Properties props = new Properties(); Properties props = new Properties();
try (FileInputStream in = new FileInputStream(path.toFile())) { try (FileInputStream in = new FileInputStream(path.toFile())) {
@@ -126,10 +182,19 @@ public class ClientConfig {
props.load(in); props.load(in);
} }
} }
props.setProperty("api.key", apiKey != null ? apiKey : ""); String keyToStore = "";
if (apiKey != null && !apiKey.isBlank()) {
try {
keyToStore = CryptoUtil.PREFIX + CryptoUtil.encrypt(apiKey);
} catch (Exception e) {
keyToStore = apiKey; // fallback: store plaintext if encryption fails
}
}
props.setProperty("api.key", keyToStore);
if (baseUrl != null) props.setProperty("api.baseUrl", baseUrl); if (baseUrl != null) props.setProperty("api.baseUrl", baseUrl);
if (model != null) props.setProperty("api.model", model); if (model != null) props.setProperty("api.model", model);
if (apiPath != null) props.setProperty("api.path", apiPath); if (apiPath != null) props.setProperty("api.path", apiPath);
if (mode != null) props.setProperty("api.mode", mode);
try (FileOutputStream out = new FileOutputStream(path.toFile())) { try (FileOutputStream out = new FileOutputStream(path.toFile())) {
props.store(out, comment); props.store(out, comment);
} }

View File

@@ -0,0 +1,110 @@
package AiCompanion.aicompanion2_0.client;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.*;
import java.security.spec.KeySpec;
import java.util.Base64;
/**
* AES-256-GCM encryption with a key derived from machine-specific data via PBKDF2.
* No key file is written to disk — the encryption key is re-derived on each session
* from identifiers tied to the current user and machine, making the stored ciphertext
* useless on any other machine or user account.
*/
public class CryptoUtil {
static final String PREFIX = "ENC:";
private static final int IV_LEN = 12;
private static final int TAG_BITS = 128;
private static final int PBKDF2_ITERATIONS = 65536;
// Fixed, non-secret salt — prevents pre-computation attacks.
private static final byte[] SALT = {
0x4d, 0x43, 0x41, 0x49, 0x5f, 0x73, 0x61, 0x6c,
0x74, 0x5f, 0x76, 0x31, 0x5f, 0x78, 0x79, 0x7a
};
private static SecretKey cachedKey = null;
// -------------------------------------------------------------------------
private static SecretKey deriveKey() throws Exception {
if (cachedKey != null) return cachedKey;
String machineId = buildMachineId();
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(machineId.toCharArray(), SALT, PBKDF2_ITERATIONS, 256);
byte[] keyBytes = factory.generateSecret(spec).getEncoded();
cachedKey = new SecretKeySpec(keyBytes, "AES");
return cachedKey;
}
/**
* Builds a stable identifier from OS username, home directory path, hostname,
* and MAC address. Changing any of these (e.g. renaming the user) will invalidate
* previously encrypted values — intentional machine-binding behaviour.
*/
private static String buildMachineId() {
StringBuilder id = new StringBuilder();
id.append(System.getProperty("user.name", "?"));
id.append('\0');
id.append(System.getProperty("user.home", "?"));
id.append('\0');
try {
id.append(InetAddress.getLocalHost().getHostName());
} catch (Exception e) {
id.append("localhost");
}
// MAC address for stronger hardware binding
try {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
id.append('\0');
id.append(Base64.getEncoder().encodeToString(mac));
}
}
} catch (Exception ignored) {}
return id.toString();
}
// -------------------------------------------------------------------------
/** Encrypts {@code plaintext} and returns a Base64 string (IV prepended to ciphertext). */
public static String encrypt(String plaintext) throws Exception {
SecretKey key = deriveKey();
byte[] iv = new byte[IV_LEN];
new SecureRandom().nextBytes(iv);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(TAG_BITS, iv));
byte[] ct = cipher.doFinal(plaintext.getBytes("UTF-8"));
byte[] out = new byte[IV_LEN + ct.length];
System.arraycopy(iv, 0, out, 0, IV_LEN);
System.arraycopy(ct, 0, out, IV_LEN, ct.length);
return Base64.getEncoder().encodeToString(out);
}
/**
* Decrypts a Base64 string produced by {@link #encrypt}.
* Returns {@code null} on failure (wrong machine, corrupt data, etc.).
*/
public static String decrypt(String encoded) {
try {
SecretKey key = deriveKey();
byte[] combined = Base64.getDecoder().decode(encoded);
byte[] iv = new byte[IV_LEN];
byte[] ct = new byte[combined.length - IV_LEN];
System.arraycopy(combined, 0, iv, 0, IV_LEN);
System.arraycopy(combined, IV_LEN, ct, 0, ct.length);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(TAG_BITS, iv));
return new String(cipher.doFinal(ct), "UTF-8");
} catch (Exception e) {
return null;
}
}
}

View File

@@ -0,0 +1,57 @@
package AiCompanion.aicompanion2_0.client;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
public class ModeSelectScreen extends Screen {
public ModeSelectScreen() {
super(Text.literal("Change AI Mode"));
}
@Override
protected void init() {
int cx = width / 2;
int cy = height / 2;
boolean casualSelected = !"minecraft".equals(ClientConfig.getMode());
boolean mcSelected = "minecraft".equals(ClientConfig.getMode());
addDrawableChild(ButtonWidget.builder(
Text.literal(casualSelected ? "§a▶ Casual" : " Casual"),
btn -> { ClientConfig.setMode("casual"); close(); }
).dimensions(cx - 75, cy - 20, 150, 20).build());
addDrawableChild(ButtonWidget.builder(
Text.literal(mcSelected ? "§a▶ Minecraft" : " Minecraft"),
btn -> { ClientConfig.setMode("minecraft"); close(); }
).dimensions(cx - 75, cy + 5, 150, 20).build());
addDrawableChild(ButtonWidget.builder(Text.literal("Cancel"), btn -> close())
.dimensions(cx - 55, cy + 32, 110, 20).build());
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);
int cx = width / 2;
int cy = height / 2;
context.drawCenteredTextWithShadow(textRenderer, "§6Change AI Mode", cx, cy - 48, 0xFFFFFF);
context.drawCenteredTextWithShadow(textRenderer,
"§8Current: §7" + ClientConfig.getMode(), cx, cy - 36, 0x888888);
context.drawCenteredTextWithShadow(textRenderer,
"§8Casual §7- general assistant, matches your language",
cx, cy - 24, 0x666666);
context.drawCenteredTextWithShadow(textRenderer,
"§8Minecraft §7- only answers Minecraft questions",
cx, cy - 14, 0x666666);
super.render(context, mouseX, mouseY, delta);
}
@Override
public boolean shouldPause() { return false; }
}

View File

@@ -0,0 +1,264 @@
package AiCompanion.aicompanion2_0.client;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class ModelSelectScreen extends Screen {
private enum FetchState { IDLE, LOADING, SUCCESS, ERROR }
private FetchState fetchState = FetchState.IDLE;
private List<String> fetchedModels = new ArrayList<>();
private String fetchError = null;
private int scrollOffset = 0;
private String savedModel;
private String savedMode;
private TextFieldWidget modelField;
private static final int MAX_VISIBLE = 5;
private static final int ITEM_H = 16;
public ModelSelectScreen() {
super(Text.literal("Change Model"));
this.savedModel = ClientConfig.getModel() != null ? ClientConfig.getModel() : "";
this.savedMode = ClientConfig.getMode();
}
@Override
protected void init() {
modelField = null;
int cx = width / 2;
int cy = height / 2;
// Fetch button
String fetchLabel = switch (fetchState) {
case LOADING -> "§7Fetching...";
case SUCCESS -> "↻ Refetch";
case ERROR -> "↻ Retry";
default -> "Fetch Models";
};
addDrawableChild(ButtonWidget.builder(Text.literal(fetchLabel), btn -> {
if (fetchState != FetchState.LOADING) startFetch();
}).dimensions(cx - 75, cy - 55, 150, 20).build());
// Model list or manual text field
boolean hasList = fetchState == FetchState.SUCCESS && !fetchedModels.isEmpty();
int listStartY = cy - 28;
if (hasList) {
int visible = Math.min(fetchedModels.size(), MAX_VISIBLE);
int end = Math.min(fetchedModels.size(), scrollOffset + visible);
for (int i = scrollOffset; i < end; i++) {
String m = fetchedModels.get(i);
boolean selected = m.equals(savedModel);
final String model = m;
addDrawableChild(ButtonWidget.builder(
Text.literal((selected ? "§a▶ " : " ") + m),
btn -> { savedModel = model; reinit(); }
).dimensions(cx - 150, listStartY + (i - scrollOffset) * ITEM_H, 290, ITEM_H - 2).build());
}
if (fetchedModels.size() > MAX_VISIBLE) {
addDrawableChild(ButtonWidget.builder(Text.literal(""),
btn -> { scrollOffset = Math.max(0, scrollOffset - 1); reinit(); }
).dimensions(cx + 143, listStartY, 7, ITEM_H - 2).build());
addDrawableChild(ButtonWidget.builder(Text.literal(""),
btn -> { scrollOffset = Math.min(fetchedModels.size() - MAX_VISIBLE, scrollOffset + 1); reinit(); }
).dimensions(cx + 143, listStartY + ITEM_H, 7, ITEM_H - 2).build());
}
} else {
modelField = new TextFieldWidget(textRenderer, cx - 150, listStartY, 300, 20, Text.literal("Model name..."));
modelField.setMaxLength(100);
modelField.setText(savedModel);
modelField.setFocused(true);
addDrawableChild(modelField);
}
int listH = hasList ? Math.min(fetchedModels.size(), MAX_VISIBLE) * ITEM_H : 22;
int modeY = listStartY + listH + 8;
boolean casualSelected = !"minecraft".equals(savedMode);
boolean mcSelected = "minecraft".equals(savedMode);
addDrawableChild(ButtonWidget.builder(
Text.literal(casualSelected ? "§a▶ Casual" : " Casual"),
btn -> { savedMode = "casual"; reinit(); }
).dimensions(cx - 110, modeY, 100, 12).build());
addDrawableChild(ButtonWidget.builder(
Text.literal(mcSelected ? "§a▶ Minecraft" : " Minecraft"),
btn -> { savedMode = "minecraft"; reinit(); }
).dimensions(cx - 5, modeY, 110, 12).build());
int saveY = modeY + 18;
addDrawableChild(ButtonWidget.builder(Text.literal("Save"), btn -> confirm())
.dimensions(cx - 55, saveY, 110, 20).build());
addDrawableChild(ButtonWidget.builder(Text.literal("Cancel"), btn -> close())
.dimensions(cx - 55, saveY + 24, 110, 20).build());
}
private void reinit() {
if (modelField != null) savedModel = modelField.getText();
MinecraftClient mc = MinecraftClient.getInstance();
this.init(mc, mc.getWindow().getScaledWidth(), mc.getWindow().getScaledHeight());
}
private void confirm() {
if (modelField != null) savedModel = modelField.getText();
String model = savedModel.trim();
if (model.isEmpty()) return;
ClientConfig.setProviderConfig(
ClientConfig.getBaseUrl(),
model,
ClientConfig.getApiKey(),
ClientConfig.getApiPath(),
savedMode
);
close();
}
// -------------------------------------------------------------------------
// Fetching
// -------------------------------------------------------------------------
private void startFetch() {
if (modelField != null) savedModel = modelField.getText();
fetchState = FetchState.LOADING;
fetchError = null;
reinit();
String url = ClientConfig.getBaseUrl();
String key = ClientConfig.getApiKey();
new Thread(() -> {
try {
List<String> models = fetchModels(url, key);
if (models.isEmpty()) {
fetchState = FetchState.ERROR;
fetchError = "No models found";
} else {
fetchedModels = models;
fetchState = FetchState.SUCCESS;
scrollOffset = 0;
if (savedModel.isEmpty()) savedModel = models.get(0);
}
} catch (Exception e) {
fetchState = FetchState.ERROR;
fetchError = e.getMessage() != null ? e.getMessage() : "Connection failed";
}
MinecraftClient.getInstance().execute(this::reinit);
}).start();
}
private List<String> fetchModels(String baseUrl, String apiKey) throws Exception {
try {
List<String> m = fetchFromPath(baseUrl, "/api/tags", apiKey, "name");
if (!m.isEmpty()) return m;
} catch (Exception ignored) {}
try {
List<String> m = fetchFromPath(baseUrl, "/v1/models", apiKey, "id");
if (!m.isEmpty()) return m;
} catch (Exception ignored) {}
return fetchFromPath(baseUrl, "/api/models", apiKey, "id");
}
private List<String> fetchFromPath(String baseUrl, String path, String apiKey, String field) throws Exception {
URL url = URI.create(baseUrl + path).toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
if (apiKey != null && !apiKey.isBlank()) {
conn.setRequestProperty("Authorization", "Bearer " + apiKey);
}
int status = conn.getResponseCode();
if (status != 200) throw new Exception("HTTP " + status + " from " + path);
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
String line;
while ((line = br.readLine()) != null) sb.append(line);
}
conn.disconnect();
List<String> result = new ArrayList<>();
String key = "\"" + field + "\"";
String body = sb.toString();
int idx = 0;
while ((idx = body.indexOf(key, idx)) >= 0) {
idx += key.length();
while (idx < body.length() && (body.charAt(idx) == ' ' || body.charAt(idx) == '\t' || body.charAt(idx) == ':')) idx++;
if (idx < body.length() && body.charAt(idx) == '"') {
int start = idx + 1;
int end = body.indexOf('"', start);
if (end > start) result.add(body.substring(start, end));
idx = end + 1;
}
}
return result;
}
// -------------------------------------------------------------------------
// Rendering
// -------------------------------------------------------------------------
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);
int cx = width / 2;
int cy = height / 2;
context.drawCenteredTextWithShadow(textRenderer, "§6Change Model", cx, cy - 75, 0xFFFFFF);
context.drawCenteredTextWithShadow(textRenderer,
"§8Current: §7" + (ClientConfig.getModel() != null ? ClientConfig.getModel() : "none"),
cx, cy - 63, 0x888888);
context.drawTextWithShadow(textRenderer, "§7Model:", cx - 150, cy - 30, 0xAAAAAA);
int listH = (fetchState == FetchState.SUCCESS && !fetchedModels.isEmpty())
? Math.min(fetchedModels.size(), MAX_VISIBLE) * ITEM_H : 22;
int modeY = (cy - 28) + listH + 8;
context.drawTextWithShadow(textRenderer, "§7Mode:", cx - 110, modeY + 2, 0xAAAAAA);
if (fetchState == FetchState.LOADING) {
context.drawTextWithShadow(textRenderer, "§7Fetching...", cx - 150, cy - 14, 0x888888);
} else if (fetchState == FetchState.ERROR && fetchError != null) {
context.drawTextWithShadow(textRenderer, "§c" + fetchError, cx - 150, cy - 14, 0xFF5555);
}
super.render(context, mouseX, mouseY, delta);
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == 257 || keyCode == 335) { // Enter
confirm();
return true;
}
return super.keyPressed(keyCode, scanCode, modifiers);
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
if (fetchState == FetchState.SUCCESS && fetchedModels.size() > MAX_VISIBLE) {
scrollOffset = Math.max(0, Math.min(
fetchedModels.size() - MAX_VISIBLE,
scrollOffset - (int) Math.signum(amount)
));
reinit();
return true;
}
return super.mouseScrolled(mouseX, mouseY, amount);
}
@Override
public boolean shouldPause() { return false; }
}

View File

@@ -5,6 +5,8 @@ import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import java.io.BufferedReader; import java.io.BufferedReader;
@@ -17,38 +19,37 @@ import java.util.List;
public class ProviderSetupScreen extends Screen { public class ProviderSetupScreen extends Screen {
private boolean configStep = false;
private boolean advancedExpanded = false; private boolean advancedExpanded = false;
private String provider = "ollama";
// Field values preserved across reinit() calls
private String savedUrl = "";
private String savedModel = "";
private String savedKey = "";
private String savedPath = "";
// Model fetch state
private enum FetchState { IDLE, LOADING, SUCCESS, ERROR } private enum FetchState { IDLE, LOADING, SUCCESS, ERROR }
private FetchState fetchState = FetchState.IDLE; private FetchState fetchState = FetchState.IDLE;
private List<String> fetchedModels = new ArrayList<>(); private List<String> fetchedModels = new ArrayList<>();
private String fetchError = null; private String fetchError = null;
private int modelScrollOffset = 0; private int modelScrollOffset = 0;
// Values preserved across reinit() calls
private String savedUrl = "http://localhost:11434";
private String savedKey = "";
private String savedModel = "";
private String savedPath = "";
private String savedMode = ClientConfig.getMode();
private TextFieldWidget urlField; private TextFieldWidget urlField;
private TextFieldWidget modelField;
private TextFieldWidget keyField; private TextFieldWidget keyField;
private TextFieldWidget modelField;
private TextFieldWidget pathField; private TextFieldWidget pathField;
private final Runnable onSuccess; private final Runnable onSuccess;
// Layout constants (relative to baseY) private static final int MAX_VISIBLE = 3;
private static final int URL_FIELD_Y = 10; private static final int ITEM_H = 14;
private static final int KEY_LABEL_Y = 37;
private static final int KEY_FIELD_Y = 47; // Layout Y offsets relative to baseY (= height/2 - 100)
private static final int MODEL_LABEL_Y = 74; private static final int Y_PRESETS = 20; // preset buttons
private static final int MODEL_CONTENT_Y = 86; private static final int Y_URL_FIELD = 50; // URL text field
private static final int ITEM_HEIGHT = 14; private static final int Y_KEY_FIELD = 85; // key text field
private static final int MAX_VISIBLE = 4; private static final int Y_MODEL_ROW = 110; // "Model:" label + fetch button
private static final int Y_MODEL_CONT = 123; // model list / text field
public ProviderSetupScreen(Runnable onSuccess) { public ProviderSetupScreen(Runnable onSuccess) {
super(Text.literal("AI Companion Setup")); super(Text.literal("AI Companion Setup"));
@@ -57,35 +58,50 @@ public class ProviderSetupScreen extends Screen {
@Override @Override
protected void init() { protected void init() {
urlField = null;
keyField = null;
modelField = null;
pathField = null;
int cx = width / 2; int cx = width / 2;
int baseY = height / 2 - 75; int baseY = height / 2 - 100;
if (!configStep) { // --- Preset buttons (pre-fill URL) ---
int cy = height / 2; addDrawableChild(ButtonWidget.builder(Text.literal("Ollama"), btn -> {
addDrawableChild(ButtonWidget.builder(Text.literal("Ollama (Local)"), btn -> goToConfig("ollama")) saveCurrentValues();
.dimensions(cx - 155, cy, 100, 20).build()); savedUrl = "http://localhost:11434";
addDrawableChild(ButtonWidget.builder(Text.literal("Open-WebUI"), btn -> goToConfig("openwebui")) reinit();
.dimensions(cx - 50, cy, 100, 20).build()); }).dimensions(cx - 155, baseY + Y_PRESETS, 95, 14).build());
addDrawableChild(ButtonWidget.builder(Text.literal("Custom"), btn -> goToConfig("custom"))
.dimensions(cx + 55, cy, 100, 20).build());
return;
}
// --- Config step --- addDrawableChild(ButtonWidget.builder(Text.literal("Open-WebUI"), btn -> {
saveCurrentValues();
savedUrl = "https://ai.example.org";
savedPath = "/api/chat/completions";
advancedExpanded = true;
reinit();
}).dimensions(cx - 50, baseY + Y_PRESETS, 95, 14).build());
// URL field addDrawableChild(ButtonWidget.builder(Text.literal("Custom"), btn -> {
urlField = new TextFieldWidget(textRenderer, cx - 150, baseY + URL_FIELD_Y, 300, 20, Text.literal("http://...")); saveCurrentValues();
savedUrl = "";
reinit();
}).dimensions(cx + 55, baseY + Y_PRESETS, 95, 14).build());
// --- URL field ---
urlField = new TextFieldWidget(textRenderer, cx - 150, baseY + Y_URL_FIELD, 300, 20, Text.literal("http://..."));
urlField.setMaxLength(200); urlField.setMaxLength(200);
urlField.setText(savedUrl.isEmpty() ? defaultUrl() : savedUrl); urlField.setText(savedUrl);
addDrawableChild(urlField); addDrawableChild(urlField);
// Key field // --- API key field (password-masked, optional) ---
keyField = new TextFieldWidget(textRenderer, cx - 150, baseY + KEY_FIELD_Y, 300, 20, Text.literal("API Key (optional)")); keyField = new TextFieldWidget(textRenderer, cx - 150, baseY + Y_KEY_FIELD, 300, 20, Text.literal("API Key (optional)"));
keyField.setMaxLength(200); keyField.setMaxLength(256);
keyField.setText(savedKey); keyField.setText(savedKey);
keyField.setRenderTextProvider((text, offset) ->
OrderedText.styledForwardsVisitedString("".repeat(text.length()), Style.EMPTY));
addDrawableChild(keyField); addDrawableChild(keyField);
// Fetch Models button (right side of model row header) // --- Fetch button (right side of model row) ---
String fetchLabel = switch (fetchState) { String fetchLabel = switch (fetchState) {
case LOADING -> "§7Fetching..."; case LOADING -> "§7Fetching...";
case SUCCESS -> "↻ Refetch"; case SUCCESS -> "↻ Refetch";
@@ -95,51 +111,43 @@ public class ProviderSetupScreen extends Screen {
addDrawableChild(ButtonWidget.builder(Text.literal(fetchLabel), btn -> { addDrawableChild(ButtonWidget.builder(Text.literal(fetchLabel), btn -> {
if (fetchState != FetchState.LOADING) { if (fetchState != FetchState.LOADING) {
saveCurrentValues(); saveCurrentValues();
fetchModels(); startFetch();
} }
}).dimensions(cx + 5, baseY + MODEL_LABEL_Y, 145, 12).build()); }).dimensions(cx + 5, baseY + Y_MODEL_ROW, 145, 12).build());
// Model content area: clickable list OR text field // --- Model area ---
boolean hasModelList = fetchState == FetchState.SUCCESS && !fetchedModels.isEmpty(); boolean hasList = fetchState == FetchState.SUCCESS && !fetchedModels.isEmpty();
int listHeight; if (hasList) {
int visible = Math.min(fetchedModels.size(), MAX_VISIBLE);
if (hasModelList) { int end = Math.min(fetchedModels.size(), modelScrollOffset + visible);
int visibleCount = Math.min(fetchedModels.size(), MAX_VISIBLE); for (int i = modelScrollOffset; i < end; i++) {
int endIdx = Math.min(fetchedModels.size(), modelScrollOffset + visibleCount);
listHeight = visibleCount * ITEM_HEIGHT;
for (int i = modelScrollOffset; i < endIdx; i++) {
String m = fetchedModels.get(i); String m = fetchedModels.get(i);
boolean selected = m.equals(savedModel); boolean selected = m.equals(savedModel);
final String model = m; final String model = m;
addDrawableChild(ButtonWidget.builder( addDrawableChild(ButtonWidget.builder(
Text.literal((selected ? "§a▶ " : " ") + m), Text.literal((selected ? "§a▶ " : " ") + m),
btn -> { savedModel = model; reinit(); } btn -> { savedModel = model; reinit(); }
).dimensions(cx - 150, baseY + MODEL_CONTENT_Y + (i - modelScrollOffset) * ITEM_HEIGHT, 290, ITEM_HEIGHT - 2).build()); ).dimensions(cx - 150, baseY + Y_MODEL_CONT + (i - modelScrollOffset) * ITEM_H, 290, ITEM_H - 2).build());
} }
// Up/down scroll when list is longer than visible
if (fetchedModels.size() > MAX_VISIBLE) { if (fetchedModels.size() > MAX_VISIBLE) {
addDrawableChild(ButtonWidget.builder(Text.literal(""), addDrawableChild(ButtonWidget.builder(Text.literal(""),
btn -> { modelScrollOffset = Math.max(0, modelScrollOffset - 1); reinit(); } btn -> { modelScrollOffset = Math.max(0, modelScrollOffset - 1); reinit(); }
).dimensions(cx + 143, baseY + MODEL_CONTENT_Y, 7, ITEM_HEIGHT - 2).build()); ).dimensions(cx + 143, baseY + Y_MODEL_CONT, 7, ITEM_H - 2).build());
addDrawableChild(ButtonWidget.builder(Text.literal(""), addDrawableChild(ButtonWidget.builder(Text.literal(""),
btn -> { modelScrollOffset = Math.min(fetchedModels.size() - MAX_VISIBLE, modelScrollOffset + 1); reinit(); } btn -> { modelScrollOffset = Math.min(fetchedModels.size() - MAX_VISIBLE, modelScrollOffset + 1); reinit(); }
).dimensions(cx + 143, baseY + MODEL_CONTENT_Y + ITEM_HEIGHT, 7, ITEM_HEIGHT - 2).build()); ).dimensions(cx + 143, baseY + Y_MODEL_CONT + ITEM_H, 7, ITEM_H - 2).build());
} }
} else { } else {
// Manual text input fallback modelField = new TextFieldWidget(textRenderer, cx - 150, baseY + Y_MODEL_CONT, 300, 20, Text.literal("Model name..."));
modelField = new TextFieldWidget(textRenderer, cx - 150, baseY + MODEL_CONTENT_Y, 300, 20, Text.literal("Model name..."));
modelField.setMaxLength(100); modelField.setMaxLength(100);
modelField.setText(savedModel.isEmpty() ? defaultModel() : savedModel); modelField.setText(savedModel);
addDrawableChild(modelField); addDrawableChild(modelField);
listHeight = 22;
} }
// Positions below model area int listH = hasList ? Math.min(fetchedModels.size(), MAX_VISIBLE) * ITEM_H : 22;
int afterModelY = baseY + MODEL_CONTENT_Y + listHeight + 5; int afterModelY = baseY + Y_MODEL_CONT + listH + 4;
// Advanced toggle // --- Advanced toggle ---
String advLabel = advancedExpanded ? "Advanced ▲" : "Advanced ▼"; String advLabel = advancedExpanded ? "Advanced ▲" : "Advanced ▼";
addDrawableChild(ButtonWidget.builder(Text.literal("§8" + advLabel), btn -> { addDrawableChild(ButtonWidget.builder(Text.literal("§8" + advLabel), btn -> {
saveCurrentValues(); saveCurrentValues();
@@ -147,88 +155,80 @@ public class ProviderSetupScreen extends Screen {
reinit(); reinit();
}).dimensions(cx + 55, afterModelY, 95, 12).build()); }).dimensions(cx + 55, afterModelY, 95, 12).build());
int buttonsY; // --- Advanced: API path ---
int saveY;
if (advancedExpanded) { if (advancedExpanded) {
pathField = new TextFieldWidget(textRenderer, cx - 150, afterModelY + 18, 300, 20, Text.literal("/api/chat/completions")); pathField = new TextFieldWidget(textRenderer, cx - 150, afterModelY + 18, 300, 20, Text.literal("/api/chat/completions"));
pathField.setMaxLength(200); pathField.setMaxLength(200);
pathField.setText(savedPath.isEmpty() ? defaultPath() : savedPath); pathField.setText(savedPath);
addDrawableChild(pathField); addDrawableChild(pathField);
buttonsY = afterModelY + 45; saveY = afterModelY + 45;
} else { } else {
buttonsY = afterModelY + 18; saveY = afterModelY + 18;
} }
addDrawableChild(ButtonWidget.builder(Text.literal("< Back"), btn -> { // --- Mode selector ---
configStep = false; boolean casualSelected = !"minecraft".equals(savedMode);
advancedExpanded = false; boolean mcSelected = "minecraft".equals(savedMode);
fetchState = FetchState.IDLE; addDrawableChild(ButtonWidget.builder(
fetchedModels = new ArrayList<>(); Text.literal(casualSelected ? "§a▶ Casual" : " Casual"),
savedUrl = savedModel = savedKey = savedPath = ""; btn -> { saveCurrentValues(); savedMode = "casual"; reinit(); }
reinit(); ).dimensions(cx - 155, saveY, 95, 12).build());
}).dimensions(cx - 155, buttonsY, 70, 20).build()); addDrawableChild(ButtonWidget.builder(
Text.literal(mcSelected ? "§a▶ Minecraft" : " Minecraft"),
btn -> { saveCurrentValues(); savedMode = "minecraft"; reinit(); }
).dimensions(cx - 50, saveY, 100, 12).build());
addDrawableChild(ButtonWidget.builder(Text.literal("Confirm"), btn -> confirm()) // --- Save button ---
.dimensions(cx - 75, buttonsY, 150, 20).build()); addDrawableChild(ButtonWidget.builder(Text.literal("Save & Connect"), btn -> confirm())
.dimensions(cx - 75, saveY + 18, 150, 20).build());
urlField.setFocused(true); urlField.setFocused(true);
} }
private void saveCurrentValues() { private void saveCurrentValues() {
if (urlField != null) savedUrl = urlField.getText(); if (urlField != null) savedUrl = urlField.getText();
if (modelField != null) savedModel = modelField.getText();
if (keyField != null) savedKey = keyField.getText(); if (keyField != null) savedKey = keyField.getText();
if (modelField != null) savedModel = modelField.getText();
if (pathField != null) savedPath = pathField.getText(); if (pathField != null) savedPath = pathField.getText();
} }
private void goToConfig(String p) {
this.provider = p;
this.configStep = true;
reinit();
}
private void reinit() { private void reinit() {
MinecraftClient client = MinecraftClient.getInstance(); MinecraftClient mc = MinecraftClient.getInstance();
this.init(client, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight()); this.init(mc, mc.getWindow().getScaledWidth(), mc.getWindow().getScaledHeight());
} }
private String defaultUrl() { private void confirm() {
return switch (provider) { saveCurrentValues();
case "ollama" -> "http://localhost:11434"; String url = savedUrl.trim();
case "openwebui" -> "https://ai.cametendo.org"; String model = savedModel.trim();
default -> ""; String key = savedKey.trim();
}; String path = savedPath.trim();
if (url.isEmpty() || model.isEmpty()) return;
ClientConfig.setProviderConfig(url, model, key.isEmpty() ? null : key, path.isEmpty() ? null : path, savedMode);
MinecraftClient.getInstance().execute(() -> {
close();
onSuccess.run();
});
} }
private String defaultModel() { // -------------------------------------------------------------------------
return switch (provider) { // Model fetching
case "openwebui" -> "minecraft-helper"; // -------------------------------------------------------------------------
default -> "";
};
}
private String defaultPath() { private void startFetch() {
return switch (provider) {
case "ollama" -> "/v1/chat/completions";
case "openwebui" -> "/api/chat/completions";
default -> "";
};
}
// --- Model fetching ---
private void fetchModels() {
fetchState = FetchState.LOADING; fetchState = FetchState.LOADING;
fetchError = null; fetchError = null;
String url = savedUrl.isEmpty() ? defaultUrl() : savedUrl; String url = savedUrl.trim().isEmpty() ? "http://localhost:11434" : savedUrl.trim();
String key = savedKey; String key = savedKey.trim();
reinit(); reinit();
new Thread(() -> { new Thread(() -> {
try { try {
List<String> models = doFetchModels(url, key); List<String> models = fetchModels(url, key);
if (models.isEmpty()) { if (models.isEmpty()) {
fetchState = FetchState.ERROR; fetchState = FetchState.ERROR;
fetchError = "No models found at " + url; fetchError = "No models found";
} else { } else {
fetchedModels = models; fetchedModels = models;
fetchState = FetchState.SUCCESS; fetchState = FetchState.SUCCESS;
@@ -243,18 +243,22 @@ public class ProviderSetupScreen extends Screen {
}).start(); }).start();
} }
private List<String> doFetchModels(String baseUrl, String apiKey) throws Exception { private List<String> fetchModels(String baseUrl, String apiKey) throws Exception {
// Try Ollama native: GET /api/tags → {"models": [{"name": "..."}]} // Ollama native: GET /api/tags → {"models":[{"name":"..."}]}
try { try {
List<String> models = fetchFromPath(baseUrl, "/api/tags", apiKey, "name"); List<String> m = fetchFromPath(baseUrl, "/api/tags", apiKey, "name");
if (!models.isEmpty()) return models; if (!m.isEmpty()) return m;
} catch (Exception ignored) {} } catch (Exception ignored) {}
// OpenAI-compat: GET /v1/models → {"data":[{"id":"..."}]}
// Try OpenAI-compat: GET /v1/models → {"data": [{"id": "..."}]} try {
return fetchFromPath(baseUrl, "/v1/models", apiKey, "id"); List<String> m = fetchFromPath(baseUrl, "/v1/models", apiKey, "id");
if (!m.isEmpty()) return m;
} catch (Exception ignored) {}
// Open-WebUI native: GET /api/models
return fetchFromPath(baseUrl, "/api/models", apiKey, "id");
} }
private List<String> fetchFromPath(String baseUrl, String path, String apiKey, String nameField) throws Exception { private List<String> fetchFromPath(String baseUrl, String path, String apiKey, String field) throws Exception {
URL url = URI.create(baseUrl + path).toURL(); URL url = URI.create(baseUrl + path).toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
@@ -263,10 +267,8 @@ public class ProviderSetupScreen extends Screen {
if (apiKey != null && !apiKey.isBlank()) { if (apiKey != null && !apiKey.isBlank()) {
conn.setRequestProperty("Authorization", "Bearer " + apiKey); conn.setRequestProperty("Authorization", "Bearer " + apiKey);
} }
int status = conn.getResponseCode(); int status = conn.getResponseCode();
if (status != 200) throw new Exception("HTTP " + status + " from " + path); if (status != 200) throw new Exception("HTTP " + status + " from " + path);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) { try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
String line; String line;
@@ -274,39 +276,65 @@ public class ProviderSetupScreen extends Screen {
} }
conn.disconnect(); conn.disconnect();
// Simple field extraction: find all occurrences of "nameField":"value" // Robust parser: handles both `"field":"value"` and `"field": "value"`
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
String search = "\"" + nameField + "\":\"";
String body = sb.toString(); String body = sb.toString();
String key = "\"" + field + "\"";
int idx = 0; int idx = 0;
while ((idx = body.indexOf(search, idx)) >= 0) { while ((idx = body.indexOf(key, idx)) >= 0) {
int start = idx + search.length(); idx += key.length();
int end = body.indexOf("\"", start); // skip whitespace and colon
if (end > start) result.add(body.substring(start, end)); while (idx < body.length() && (body.charAt(idx) == ' ' || body.charAt(idx) == '\t' || body.charAt(idx) == ':')) idx++;
idx = end + 1; if (idx < body.length() && body.charAt(idx) == '"') {
int start = idx + 1;
int end = body.indexOf('"', start);
if (end > start) result.add(body.substring(start, end));
idx = end + 1;
}
} }
return result; return result;
} }
// --- Confirm --- // -------------------------------------------------------------------------
// Rendering
// -------------------------------------------------------------------------
private void confirm() { @Override
saveCurrentValues(); public void render(DrawContext context, int mouseX, int mouseY, float delta) {
String url = savedUrl.trim(); renderBackground(context);
String model = savedModel.trim(); int cx = width / 2;
String key = savedKey.trim(); int baseY = height / 2 - 100;
String path = advancedExpanded ? savedPath.trim() : "";
if (url.isEmpty() || model.isEmpty()) return; context.drawCenteredTextWithShadow(textRenderer, "§6AI Companion Setup", cx, baseY - 5, 0xFFFFFF);
ClientConfig.setProviderConfig(url, model, key.isEmpty() ? null : key, path.isEmpty() ? null : path); context.drawCenteredTextWithShadow(textRenderer, "§8Quick presets:", cx, baseY + 8, 0x888888);
MinecraftClient.getInstance().execute(() -> {
close(); context.drawTextWithShadow(textRenderer, "§7Server URL:", cx - 150, baseY + 38, 0xAAAAAA);
onSuccess.run(); context.drawTextWithShadow(textRenderer, "§7API Key §8(optional):", cx - 150, baseY + 73, 0xAAAAAA);
}); context.drawTextWithShadow(textRenderer, "§7Model:", cx - 150, baseY + Y_MODEL_ROW, 0xAAAAAA);
if (fetchState == FetchState.LOADING) {
context.drawTextWithShadow(textRenderer, "§7Fetching...", cx - 150, baseY + Y_MODEL_CONT + 4, 0x888888);
} else if (fetchState == FetchState.ERROR && fetchError != null) {
context.drawTextWithShadow(textRenderer, "§c" + fetchError, cx - 150, baseY + Y_MODEL_CONT + 4, 0xFF5555);
}
{
boolean hasList = fetchState == FetchState.SUCCESS && !fetchedModels.isEmpty();
int listH = hasList ? Math.min(fetchedModels.size(), MAX_VISIBLE) * ITEM_H : 22;
int afterModelY = baseY + Y_MODEL_CONT + listH + 4;
if (advancedExpanded) {
context.drawTextWithShadow(textRenderer, "§7API Path:", cx - 150, afterModelY + 8, 0xAAAAAA);
}
int modeY = advancedExpanded ? afterModelY + 45 : afterModelY + 18;
context.drawTextWithShadow(textRenderer, "§7Mode:", cx - 155, modeY + 2, 0xAAAAAA);
}
super.render(context, mouseX, mouseY, delta);
} }
@Override @Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) { public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (configStep && (keyCode == 257 || keyCode == 335)) { // Enter if (keyCode == 257 || keyCode == 335) { // Enter
confirm(); confirm();
return true; return true;
} }
@@ -314,47 +342,18 @@ public class ProviderSetupScreen extends Screen {
} }
@Override @Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) { public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
renderBackground(context); if (fetchState == FetchState.SUCCESS && fetchedModels.size() > MAX_VISIBLE) {
int cx = width / 2; modelScrollOffset = Math.max(0, Math.min(
int baseY = height / 2 - 75; fetchedModels.size() - MAX_VISIBLE,
modelScrollOffset - (int) Math.signum(amount)
if (!configStep) { ));
int cy = height / 2; reinit();
context.drawCenteredTextWithShadow(textRenderer, "§6AI Companion Setup", cx, cy - 30, 0xFFFFFF); return true;
context.drawCenteredTextWithShadow(textRenderer, "§fChoose your AI provider:", cx, cy - 15, 0xAAAAAA);
} else {
String title = switch (provider) {
case "ollama" -> "§6Configure Ollama";
case "openwebui" -> "§6Configure Open-WebUI";
default -> "§6Custom Configuration";
};
context.drawCenteredTextWithShadow(textRenderer, title, cx, baseY - 5, 0xFFFFFF);
context.drawTextWithShadow(textRenderer, "§7Server URL:", cx - 150, baseY, 0xAAAAAA);
context.drawTextWithShadow(textRenderer, "§7API Key §8(optional):", cx - 150, baseY + KEY_LABEL_Y, 0xAAAAAA);
context.drawTextWithShadow(textRenderer, "§7Model:", cx - 150, baseY + MODEL_LABEL_Y, 0xAAAAAA);
// Fetch status / error below model label
if (fetchState == FetchState.LOADING) {
context.drawTextWithShadow(textRenderer, "§7Loading...", cx - 150, baseY + MODEL_CONTENT_Y + 4, 0xAAAAAA);
} else if (fetchState == FetchState.ERROR && fetchError != null) {
context.drawTextWithShadow(textRenderer, "§c" + fetchError, cx - 150, baseY + MODEL_CONTENT_Y + 4, 0xFF5555);
}
// Advanced path label
if (advancedExpanded) {
boolean hasModelList = fetchState == FetchState.SUCCESS && !fetchedModels.isEmpty();
int listHeight = hasModelList ? Math.min(fetchedModels.size(), MAX_VISIBLE) * ITEM_HEIGHT : 22;
int afterModelY = baseY + MODEL_CONTENT_Y + listHeight + 5;
context.drawTextWithShadow(textRenderer, "§7API Path:", cx - 150, afterModelY + 8, 0xAAAAAA);
}
} }
return super.mouseScrolled(mouseX, mouseY, amount);
super.render(context, mouseX, mouseY, delta);
} }
@Override @Override
public boolean shouldPause() { public boolean shouldPause() { return false; }
return false;
}
} }

View File

@@ -38,7 +38,7 @@ import net.minecraft.util.Identifier;
public class Aicompanion2_0 implements ModInitializer { public class Aicompanion2_0 implements ModInitializer {
public static final String MOD_ID = "aicompanion2_0"; public static final String MOD_ID = "aicompanion2_0";
private static final String DEFAULT_API_BASE_URL = "https://ai.cametendo.org"; private static final String DEFAULT_API_BASE_URL = "https://ai.example.org";
private static final String DEFAULT_MODEL = "minecraft-helper"; private static final String DEFAULT_MODEL = "minecraft-helper";
private static final String DEFAULT_API_PATH = "/api/chat/completions"; private static final String DEFAULT_API_PATH = "/api/chat/completions";
private static String API_BASE_URL = DEFAULT_API_BASE_URL; private static String API_BASE_URL = DEFAULT_API_BASE_URL;
@@ -53,6 +53,9 @@ public class Aicompanion2_0 implements ModInitializer {
private static final Map<UUID, Long> ARCH_EASTER_EGG_LAST_USED = new HashMap<>(); private static final Map<UUID, Long> ARCH_EASTER_EGG_LAST_USED = new HashMap<>();
public static final Identifier QUESTION_PACKET_ID = new Identifier(MOD_ID, "question"); public static final Identifier QUESTION_PACKET_ID = new Identifier(MOD_ID, "question");
public static final Identifier DELETE_KEY_PACKET_ID = new Identifier(MOD_ID, "delete_key"); public static final Identifier DELETE_KEY_PACKET_ID = new Identifier(MOD_ID, "delete_key");
public static final Identifier DELETE_CONFIG_PACKET_ID = new Identifier(MOD_ID, "delete_config");
public static final Identifier MODEL_SELECT_PACKET_ID = new Identifier(MOD_ID, "model_select");
public static final Identifier CHANGE_MODE_PACKET_ID = new Identifier(MOD_ID, "change_mode");
public static final Identifier ARCH_EASTER_EGG_PACKET_ID = new Identifier(MOD_ID, "arch_easter_egg"); public static final Identifier ARCH_EASTER_EGG_PACKET_ID = new Identifier(MOD_ID, "arch_easter_egg");
public static final Identifier ARCH_EASTER_EGG_RESPONSE_PACKET_ID = new Identifier(MOD_ID, "arch_easter_egg_response"); public static final Identifier ARCH_EASTER_EGG_RESPONSE_PACKET_ID = new Identifier(MOD_ID, "arch_easter_egg_response");
@@ -152,6 +155,54 @@ public class Aicompanion2_0 implements ModInitializer {
} }
}) })
) )
// /ai delete-config
.then(CommandManager.literal("delete-config")
.executes(ctx -> {
var player = ctx.getSource().getPlayer();
if (player != null) {
if (!ServerPlayNetworking.canSend(player, DELETE_CONFIG_PACKET_ID)) {
ctx.getSource().sendFeedback(
() -> Text.literal("§c[AI] delete-config requires the AI Companion client mod."), false);
return 0;
}
ServerPlayNetworking.send(player, DELETE_CONFIG_PACKET_ID, PacketByteBufs.create());
return 1;
}
return 0;
})
)
// /ai model
.then(CommandManager.literal("model")
.executes(ctx -> {
var player = ctx.getSource().getPlayer();
if (player != null) {
if (!ServerPlayNetworking.canSend(player, MODEL_SELECT_PACKET_ID)) {
ctx.getSource().sendFeedback(
() -> Text.literal("§c[AI] model command requires the AI Companion client mod."), false);
return 0;
}
ServerPlayNetworking.send(player, MODEL_SELECT_PACKET_ID, PacketByteBufs.create());
return 1;
}
return 0;
})
)
// /ai change-mode
.then(CommandManager.literal("change-mode")
.executes(ctx -> {
var player = ctx.getSource().getPlayer();
if (player != null) {
if (!ServerPlayNetworking.canSend(player, CHANGE_MODE_PACKET_ID)) {
ctx.getSource().sendFeedback(
() -> Text.literal("§c[AI] change-mode requires the AI Companion client mod."), false);
return 0;
}
ServerPlayNetworking.send(player, CHANGE_MODE_PACKET_ID, PacketByteBufs.create());
return 1;
}
return 0;
})
)
// /ai question <question> // /ai question <question>
.then(CommandManager.literal("question") .then(CommandManager.literal("question")
.then(CommandManager.argument("question", StringArgumentType.greedyString()) .then(CommandManager.argument("question", StringArgumentType.greedyString())