Skip to content

Commit

Permalink
testing new CQ parser
Browse files Browse the repository at this point in the history
  • Loading branch information
glowingstone124 committed Nov 19, 2024
1 parent d87252d commit ecc0bb3
Showing 1 changed file with 57 additions and 10 deletions.
67 changes: 57 additions & 10 deletions src/main/java/vip/qoriginal/quantumplugin/ChatSync.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package vip.qoriginal.quantumplugin;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import kotlin.Pair;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.*;
import java.util.*;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -27,11 +24,9 @@

public class ChatSync implements Listener {
private final static int QO_CODE = 1;
private static Gson gson = new Gson();
private static WebMsgGetter webMsgGetter;
static ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
public void init() {
webMsgGetter = new WebMsgGetter();
WebMsgGetter webMsgGetter = new WebMsgGetter();
scheduler.scheduleAtFixedRate(webMsgGetter, 0, 500, TimeUnit.MILLISECONDS);
}
public static void exit(){
Expand Down Expand Up @@ -73,8 +68,7 @@ class WebMsgGetter implements Runnable {
public void run() {
try {
String response = Request.sendGetRequest("http://qoriginal.vip:8080/qo/msglist/download").get();
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(response);
JsonElement jsonElement = JsonParser.parseString(response);
if (jsonElement.isJsonObject()) {
JsonObject msgObj = jsonElement.getAsJsonObject();
if (msgObj.get("code").getAsInt() == 0) {
Expand Down Expand Up @@ -103,6 +97,59 @@ public String parseCQ(String content) {
content = content.replace("CQ:at,qq=", "@");
return content;
}

public String parseCQv2(String content) {
String[] nodes = content.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
if (nodes.length == 0) return "INVALID";

StringBuilder result = new StringBuilder();
switch (nodes[0]) {
case "CQ:face" -> {
result.append("FACE");
return result.toString();
}
case "CQ:image" -> {
result.append("IMAGE");
Optional<Pair<String, String>> filePair = parsePair(content).stream()
.filter(element -> "file".equals(element.getFirst()))
.findFirst();
filePair.ifPresent(pair -> result.append(" - File: ").append(pair.getSecond()));
return result.toString();
}
case "CQ:record" -> {
result.append("RECORD");
return result.toString();
}
case "CQ:share" -> {
result.append("SHARE");
return result.toString();
}
case "CQ:mface" -> {
result.append("MFACE");
return result.toString();
}
case "CQ:at" -> {
result.append("AT");
return result.toString();
}
default -> {
return "UNKNOWN";
}
}
}

public List<Pair<String, String>> parsePair(String content) {
List<Pair<String, String>> pairs = new ArrayList<>();
Arrays.stream(content.split(",")).forEach(it -> {
String[] nodes = it.split("=", 2); // 防止越界,最多分割两部分
if (nodes.length == 2) {
pairs.add(new Pair<>(nodes[0].trim(), nodes[1].trim()));
}
});
return pairs;
}


public static String generateCredential(String message) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("message", message);
Expand Down

0 comments on commit ecc0bb3

Please sign in to comment.