mirror of
https://github.com/ritonioz/minecraft-ai-companion-mod-EMVs12-Project.git
synced 2026-03-18 07:10:20 +01:00
latest version 2026-02-25
latest version on our other repository
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package AiCompanion.aicompanion2_0.client;
|
||||
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
|
||||
public class Aicompanion2_0Client implements ClientModInitializer {
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package AiCompanion.aicompanion2_0.client;
|
||||
|
||||
import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
|
||||
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
|
||||
|
||||
public class Aicompanion2_0DataGenerator implements DataGeneratorEntrypoint {
|
||||
|
||||
@Override
|
||||
public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
|
||||
FabricDataGenerator.Pack pack = fabricDataGenerator.createPack();
|
||||
}
|
||||
}
|
||||
14
src/client/resources/aicompanion2_0.client.mixins.json
Normal file
14
src/client/resources/aicompanion2_0.client.mixins.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "AiCompanion.aicompanion2_0.mixin.client",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"client": [
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
},
|
||||
"overwrites": {
|
||||
"requireAnnotations": true
|
||||
}
|
||||
}
|
||||
134
src/main/java/AiCompanion/aicompanion2_0/Aicompanion2_0.java
Normal file
134
src/main/java/AiCompanion/aicompanion2_0/Aicompanion2_0.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package AiCompanion.aicompanion2_0;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
|
||||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.text.Text;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
|
||||
public class Aicompanion2_0 implements ModInitializer {
|
||||
|
||||
public static final String MOD_ID = "aicompanion2_0";
|
||||
private static String API_BASE_URL = "http://localhost:11434";
|
||||
private static String API_PATH = "/api/generate";
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
System.out.println("[" + MOD_ID + "] MOD STARTET!");
|
||||
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
|
||||
System.out.println("[" + MOD_ID + "] Registriere /ai command");
|
||||
|
||||
dispatcher.register(
|
||||
CommandManager.literal("ai")
|
||||
.then(CommandManager.argument("frage", StringArgumentType.greedyString())
|
||||
.executes(ctx -> {
|
||||
var player = ctx.getSource().getPlayer();
|
||||
String frage = StringArgumentType.getString(ctx, "frage");
|
||||
|
||||
if (player != null) {
|
||||
player.sendMessage(
|
||||
Text.literal("§6[AI] §fDenke nach über: " + frage),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
// KI‑Request in neuem Thread, damit der Server nicht hängt
|
||||
new Thread(() -> {
|
||||
try {
|
||||
String antwort = callOllama(frage);
|
||||
if (antwort == null || antwort.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Antwort etwas kürzen, damit der Chat nicht explodiert
|
||||
if (antwort.length() > 2000) {
|
||||
antwort = antwort.substring(0, 2000) + "...";
|
||||
}
|
||||
|
||||
if (player != null) {
|
||||
player.sendMessage(
|
||||
Text.literal("§6[AI] §fAntwort: " + antwort),
|
||||
false
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
|
||||
return 1;
|
||||
})
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
System.out.println("[" + MOD_ID + "] MOD GELADEN!");
|
||||
}
|
||||
|
||||
private String callOllama(String prompt) throws Exception {
|
||||
// URL deines Ollama‑Servers, ggf. anpassen
|
||||
URL url = new URL(API_BASE_URL + API_PATH);
|
||||
|
||||
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||||
conn.setDoOutput(true);
|
||||
|
||||
String json = """
|
||||
{
|
||||
"model": "llama3",
|
||||
"prompt": "%s"
|
||||
}
|
||||
""".formatted(prompt.replace("\"", "\\\""));
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
byte[] input = json.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int status = conn.getResponseCode();
|
||||
if (status != 200) {
|
||||
System.out.println("[" + MOD_ID + "] Ollama HTTP Status: " + status);
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder resp = new StringBuilder();
|
||||
try (BufferedReader br = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream(), "utf-8"))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
resp.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
conn.disconnect();
|
||||
|
||||
// Sehr einfach: komplette JSON‑Antwort zurückgeben
|
||||
// Später können wir das noch sauber parsen
|
||||
return resp.toString();
|
||||
}
|
||||
}
|
||||
private void loadConfig() {
|
||||
try {
|
||||
Path configPath = Path.of("config", "aicompanion2_0.properties");
|
||||
Properties props = new Properties();
|
||||
try (FileInputStream in = new FileInputStream(configPath.toFile())) {
|
||||
props.load(in);
|
||||
}
|
||||
API_BASE_URL = props.getProperty("api.baseUrl", API_BASE_URL);
|
||||
API_PATH = props.getProperty("api.path", API_PATH);
|
||||
} catch (IOException e) {
|
||||
System.out.println("[" + MOD_ID + "] Keine Config gefunden, benutze Default-API.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
24
src/main/resources/fabric.mod.json
Normal file
24
src/main/resources/fabric.mod.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "aicompanion2_0",
|
||||
"version": "1.0.0",
|
||||
"name": "AI Companion 2.0",
|
||||
"description": "AI Chatbot powered by Ollama",
|
||||
"authors": ["Du"],
|
||||
"contact": {},
|
||||
"license": "MIT",
|
||||
"icon": "assets/aicompanion2_0/icon.png",
|
||||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"AiCompanion.aicompanion2_0.Aicompanion2_0"
|
||||
]
|
||||
},
|
||||
"mixins": [],
|
||||
"depends": {
|
||||
"fabricloader": ">=0.14.0",
|
||||
"fabric-api": "*",
|
||||
"minecraft": "~1.20.1",
|
||||
"java": ">=17"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user