Skip to content

Commit

Permalink
[初稿] 将MohistAI项目中的QQ SDK内置到 Mohist-1.20.1+中 #3313
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgazul committed Feb 4, 2025
1 parent b85cc0d commit ca79121
Show file tree
Hide file tree
Showing 16 changed files with 496 additions and 5 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ def sharedDeps = {
installer "net.kyori:examination-api:1.3.0"
installer "net.kyori:examination-string:1.3.0"
// Paper end

annotationProcessor 'org.projectlombok:lombok:1.18.30'
compileOnly 'org.projectlombok:lombok:1.18.30'
installer 'com.konghq:unirest-java-core:4.4.5'

}

def sharedFmlonlyForge = { Project prj ->
Expand Down
3 changes: 2 additions & 1 deletion mohistlauncher/src/main/resources/libraries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ net/kyori/examination-api/1.3.0/examination-api-1.3.0.jar|b1887361d811c89ccca4db
net/kyori/examination-string/1.3.0/examination-string-1.3.0.jar|9e4752ea3f53ae45e736c9d8f016f23d|13135|false
org/apache/httpcomponents/httpclient/4.5.13/httpclient-4.5.13.jar|40d6b9075fbd28fa10292a45a0db9457|780321|false
org/apache/httpcomponents/httpcore/4.4.14/httpcore-4.4.14.jar|2b3991eda121042765a5ee299556c200|328436|false
com/ibm/icu/icu4j/71.1/icu4j-71.1.jar|f59e87e2ed829e3d136a9184e147c50d|13963762|false
com/ibm/icu/icu4j/71.1/icu4j-71.1.jar|f59e87e2ed829e3d136a9184e147c50d|13963762|false
com/konghq/unirest-java-core/4.4.5/unirest-java-core-4.4.5.jar|831b5f6f99bf75c24e22cc7d754fb133|197342|false
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -2,19 +_,29 @@
@@ -2,19 +_,30 @@

import com.google.common.collect.Lists;
import com.google.common.net.InetAddresses;
+import com.mohistmc.ai.koukou.KouKou;
+import com.mohistmc.bukkit.inventory.MohistModsInventory;
+import com.mohistmc.paper.adventure.PaperAdventure;
+import com.mohistmc.paper.event.packet.PlayerChunkLoadEvent;
Expand Down Expand Up @@ -461,7 +462,6 @@
+ }
+
+ Component defaultMessage = this.m_21231_().m_19293_();
+ String deathmessage = defaultMessage.getString();
+
+ keepLevel = keepInventory; // SPIGOT-2222: pre-set keepLevel
+ PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), defaultMessage.getString(), keepInventory); // Paper - Adventure // Mohist - Mohist PaperAdventure
Expand Down Expand Up @@ -514,6 +514,7 @@
} else {
- this.f_8924_.m_6846_().m_240416_(component, false);
+ this.f_8924_.m_6846_().m_240416_(ichatbasecomponent, false);
+ KouKou.sendToGroup(ichatbasecomponent.getString());
}
} else {
this.f_8906_.m_9829_(new ClientboundPlayerCombatKillPacket(this.m_19879_(), CommonComponents.f_237098_));
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/mohistmc/ai/koukou/AIConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.mohistmc.ai.koukou;

import com.mohistmc.plugins.config.MohistPluginConfig;
import java.io.File;
import java.util.List;

public class AIConfig extends MohistPluginConfig {

public static AIConfig INSTANCE;

public AIConfig(File file) {
super(file);
}

public static void init() {
INSTANCE = new AIConfig(new File("mohist-config", "qq.yml"));
INSTANCE.yaml.addDefault("enable", false);
INSTANCE.yaml.addDefault("debug", false);
INSTANCE.yaml.addDefault("server_name", "群消息");
INSTANCE.yaml.addDefault("post_server", "http://0.0.0.0:3000");
INSTANCE.yaml.addDefault("http_server.hostname", "0.0.0.0");
INSTANCE.yaml.addDefault("http_server.port", 2025);
INSTANCE.yaml.addDefault("chat_post_group", List.of("123456789"));
INSTANCE.yaml.addDefault("command.enable", false);
INSTANCE.yaml.addDefault("command.owners", List.of("123456789"));
INSTANCE.yaml.addDefault("command.name", "执行");
INSTANCE.save();
}

public boolean enable() {
return yaml.getBoolean("enable", false);
}

public boolean debug() {
return yaml.getBoolean("debug", false);
}

public String post_server() {
return yaml.getString("post_server", "http://0.0.0.0:3000");
}

public String http_server_hostname() {
return yaml.getString("http_server.hostname", "0.0.0.0");
}

public int http_server_port() {
return yaml.getInt("http_server.port", 2025);
}

public List<String> chat_post_group() {
return yaml.getStringList("chat_post_group");
}

public boolean command_enable() {
return yaml.getBoolean("command.enable");
}

public List<String> command_owners() {
return yaml.getStringList("command.owners");
}

public String command_name() {
return yaml.getString("command.name", "执行");
}

public String server_name() {
return yaml.getString("server_name", "群消息");
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/mohistmc/ai/koukou/ApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mohistmc.ai.koukou;

import com.mohistmc.ai.koukou.network.MyHttpHandler;
import com.mohistmc.ai.koukou.network.event.ListenRegister;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ApiController {

public static ListenRegister eventBus = ListenRegister.getInstance();
public static Logger LOGGER = LogManager.getLogger("Mohist Http Server");

public static void init() {
if (!AIConfig.INSTANCE.enable()) return;
ApiController api = new ApiController();
api.start();
eventBus.registerListener(new KouKouPostListener());
}

@SneakyThrows
public void start() {
var host = AIConfig.INSTANCE.http_server_hostname();
var port = AIConfig.INSTANCE.http_server_port();
HttpServer server = HttpServer.create(new InetSocketAddress(host, port), 0);
server.createContext("/", new MyHttpHandler());
server.setExecutor(Executors.newFixedThreadPool(5));
server.start();
LOGGER.info("已部署AI服务 {}:{}", host, port);
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/mohistmc/ai/koukou/KouKou.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.mohistmc.ai.koukou;

import com.mohistmc.ai.koukou.network.HttpRequestUtils;
import com.mohistmc.mjson.Json;
import java.util.HashMap;
import java.util.Objects;
import lombok.SneakyThrows;

public class KouKou {

public static void sendToGroup(String message) {
if (!AIConfig.INSTANCE.enable()) return;
for (String groupId : AIConfig.INSTANCE.chat_post_group()) {
send_group_msg(String.valueOf(groupId), message);
}
}

public static void sendToGroup(String groupId, String message) {
if (!AIConfig.INSTANCE.enable()) return;
send_group_msg(String.valueOf(groupId), message);
}

@SneakyThrows
public static void send_group_msg(String group_id, String message) {
HashMap<String, String> param = new HashMap<>();
param.put("group_id", group_id);
param.put("message", message);
var string = HttpRequestUtils.post("/send_group_msg", param);
if (string == null) {
debug("string == null");
return;
}
debug(string);
var json = Json.read(string);
if (Objects.equals(json.asString("status"), "failed")) {
debug("发送失败");
return;
}
debug("返回数据: " + json);
}

public static void debug(String debug_message) {
if (AIConfig.INSTANCE.debug()) ApiController.LOGGER.info(debug_message);
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/mohistmc/ai/koukou/KouKouPostListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.mohistmc.ai.koukou;

import com.mohistmc.ai.koukou.entity.MessageRequest;
import com.mohistmc.ai.koukou.network.event.BaseListener;
import com.mohistmc.ai.koukou.network.event.HttpPostEvent;
import com.mohistmc.mjson.Json;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_20_R1.command.ColouredConsoleSender;

public class KouKouPostListener implements BaseListener {

@SneakyThrows
public void onEvent(HttpPostEvent event) {
Json json = event.getJson();
if (event.isQQ()) {
MessageRequest request = json.asBean(MessageRequest.class);
String t = request.getMessage_type();
var group = request.getGroup_id();
String msg = request.getRaw_message();
String sender_nickname = request.getSender().getNickname();
if (t != null && t.equals("group")) {
if (AIConfig.INSTANCE.chat_post_group().contains(String.valueOf(group))) {
if (AIConfig.INSTANCE.command_enable() && AIConfig.INSTANCE.command_owners().contains(String.valueOf(request.getUser_id()))) {
String label = AIConfig.INSTANCE.command_name();
if (msg.startsWith(label)) {
String cmd = msg.replace(label + " ", "");
Bukkit.dispatchCommand(new ColouredConsoleSender().group(group), cmd);
return;
}
}
msg = msg.replaceAll(\\S", "");
msg = msg.replace("__color__", "§");
String pattern = "\\[CQ:.*?]";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(msg);
if (!m.find()) {
Bukkit.broadcastMessage("[%s] <%s>: %s".formatted(AIConfig.INSTANCE.server_name(), sender_nickname, msg));
}

}
}
}
}
}
76 changes: 76 additions & 0 deletions src/main/java/com/mohistmc/ai/koukou/entity/MessageRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.mohistmc.ai.koukou.entity;

import com.mohistmc.mjson.ToJson;
import lombok.Data;

@Data
public class MessageRequest {
@ToJson
private long self_id;
@ToJson
private long user_id;
@ToJson
private long time;
@ToJson
private long message_id;
@ToJson
private long real_id;
@ToJson
private String message_type;
@ToJson
private Sender sender;
@ToJson
private String raw_message;
@ToJson
private long font;
@ToJson
private String sub_type;
@ToJson
private Message[] message;
@ToJson
private String message_format;
@ToJson
private String post_type;
@ToJson
private long group_id;
@ToJson
private String card_new;

@lombok.Data
public static class Message {
@ToJson
private Data data;
@ToJson
private String type;
}

@lombok.Data
public static class Data {
@ToJson
private String text;
@ToJson
private String id;
@ToJson
private String file;
@ToJson
private String file_id;
@ToJson
private String url;
@ToJson
private String file_size;
}

@lombok.Data
public static class Sender {
@ToJson
private long user_id;
@ToJson
private String nickname;
@ToJson
private String card;
@ToJson
private String role;
}
}


23 changes: 23 additions & 0 deletions src/main/java/com/mohistmc/ai/koukou/network/HttpRequestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mohistmc.ai.koukou.network;

import com.mohistmc.ai.koukou.AIConfig;
import com.mohistmc.mjson.Json;
import java.util.Map;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.Unirest;
import lombok.SneakyThrows;

public class HttpRequestUtils {

@SneakyThrows
public static String post(String path, Map<String, String> body) {
var json = Json.read(body);
String url = AIConfig.INSTANCE.post_server() + path;
HttpResponse<String> response = Unirest.post(url)
.header("User-Agent", "Mohist")
.header("Content-Type", "application/json")
.body(json.toString())
.asString();
return response.getBody();
}
}
Loading

0 comments on commit ca79121

Please sign in to comment.