Skip to content

Commit

Permalink
refactor(fabric): 新的onebot client
Browse files Browse the repository at this point in the history
但是出现了新bug,会发两遍
  • Loading branch information
cnlimiter committed Mar 10, 2024
1 parent 7b29191 commit 952078a
Show file tree
Hide file tree
Showing 56 changed files with 807 additions and 755 deletions.
2 changes: 2 additions & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id 'dev.architectury.loom' version '1.5-SNAPSHOT' apply false
id "com.replaymod.preprocess" version "SNAPSHOT"
id 'dev.vankka.dependencydownload.plugin' version '1.3.1' apply false

}

preprocess {
Expand Down
43 changes: 25 additions & 18 deletions fabric/common.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply(plugin: "dev.architectury.loom")
apply(plugin: "com.replaymod.preprocess")
apply(plugin: "dev.vankka.dependencydownload.plugin")

int mcVersion = 1

Expand All @@ -14,15 +15,19 @@ repositories {
maven { url = "https://api.modrinth.com/maven" }
mavenCentral()
}
jar.dependsOn generateRuntimeDownloadResourceForRuntimeDownload

dependencies {
minecraft ("com.mojang:minecraft:${project.minecraft_version}")
minecraft ("com.mojang:minecraft:${minecraft_version}")
mappings loom.officialMojangMappings()

modImplementation ("net.fabricmc:fabric-loader:${project.loader_version}")
modImplementation ("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
implementation include ("cn.evole.bot:OneBot-Client:${project.onebot_client_version}"){transitive = false}
implementation include ("cn.evole.config:AtomConfig-Toml:${project.toml_version}"){transitive = false}
modImplementation ("net.fabricmc:fabric-loader:${loader_version}")
modImplementation ("net.fabricmc.fabric-api:fabric-api:${fabric_version}")
modImplementation include("dev.vankka:dependencydownload-runtime:1.3.1") {exclude module: 'jar-relocator'}
modImplementation include("dev.vankka:dependencydownload-common:1.3.1")
implementation ("cn.evole.onebot:OneBot-Client:${onebot_client_version}"){transitive = false}
implementation ("cn.evole.config:AtomConfig-Toml:${toml_version}"){transitive = false}

if (mcVersion == 11605) include ("org.slf4j:slf4j-api:2.0.7")
modCompileOnly ("maven.modrinth:vanish:1.5.2-1.20.1")
compileOnly ("org.projectlombok:lombok:1.18.24")
Expand Down Expand Up @@ -53,18 +58,20 @@ remapJar {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

version = project.mod_version
group = project.maven_group
archivesBaseName = project.archives_base_name + "-" + project.minecraft_dependency

processResources {
filesMatching("fabric.mod.json") {
def valueMap = [
"mc_version" : project.minecraft_dependency.replace(".", "_"),
"version" : project.version,
"minecraft_dependency": project.minecraft_dependency
]
expand valueMap
version = mod_version
group = maven_group
archivesBaseName = archives_base_name + "-" + minecraft_dependency

tasks.withType(ProcessResources).configureEach {
def replaceProperties = [
"mc_version" : minecraft_dependency.replace(".", "_"),
"version" : mod_version,
"minecraft_dependency": minecraft_dependency
]
inputs.properties replaceProperties

filesMatching(["fabric.mod.json"]) {
expand replaceProperties + [project: project]
}
}

Expand All @@ -74,6 +81,6 @@ tasks.withType(JavaCompile).configureEach {

jar {
from("../../../LICENSE") {
rename { "${it}_${project.archives_base_name}" }
rename { "${it}_${archives_base_name}" }
}
}
7 changes: 5 additions & 2 deletions fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx8G

# Mod Properties
mod_version=2.1.9.2
mod_version=2.2.0.1
maven_group=cn.evole.mods
archives_base_name=McBot-fabric

Expand All @@ -11,5 +11,8 @@ archives_base_name=McBot-fabric
minecraft_version=1.20.1
loader_version=0.15.7
fabric_version=0.83.1+1.20.1
onebot_client_version=0.3.9.2
onebot_client_version=0.4.0
toml_version=0.1.5
http_core_version=4.4.16
apache_version=4.5.13
netty_version=4.1.97.Final
31 changes: 6 additions & 25 deletions fabric/src/main/java/cn/evole/mods/mcbot/Const.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cn.evole.mods.mcbot;

import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.mods.mcbot.config.ModConfig;
import net.fabricmc.loader.api.FabricLoader;
import cn.evole.onebot.sdk.util.BotUtils;
import java.nio.file.Path;
//#if MC >= 11700
//$$ import org.slf4j.Logger;
Expand All @@ -27,41 +26,23 @@ public class Const {
//#endif
public static boolean isShutdown = false;
public static Path configDir = FabricLoader.getInstance().getConfigDir();
public static Path gameDir = FabricLoader.getInstance().getGameDir();

public static boolean isLoad(String modId){
return FabricLoader.getInstance().isModLoaded(modId);
}

public static void sendGroupMsg(String message){
public static void sendAllGroupMsg(String message){
for (long id : ModConfig.INSTANCE.getCommon().getGroupIdList()){
groupMsg(id, message);
sendGroupMsg(id, message);
}
}

public static void groupMsg(long id, String message){
// 发送消息时实际上所调用的函数。
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
McBot.messageThread.submit(id, message, false);
}
else {
McBot.messageThread.submit(id, BotUtils.rawToJson(message), false);
}
public static void sendGroupMsg(long id, String message){
McBot.onebot.getBot().sendGroupMsg(id, message, false);
}

public static void sendGuildMsg(String message){
for (String id : ModConfig.INSTANCE.getCommon().getChannelIdList()){
guildMsg(ModConfig.INSTANCE.getCommon().getGuildId(), id, message);
}
}

public static void guildMsg(String guildId, String channelId, String message){
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
McBot.messageThread.submit(guildId, channelId, message);
}
else {
McBot.messageThread.submit(guildId, channelId, BotUtils.rawToJson(message));
}
}
}


Expand Down
49 changes: 17 additions & 32 deletions fabric/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package cn.evole.mods.mcbot;

import cn.evole.mods.mcbot.data.UserBindApi;
import cn.evole.mods.mcbot.data.ChatRecordApi;
import cn.evole.mods.mcbot.core.event.*;
import cn.evole.mods.mcbot.core.data.UserBindApi;
import cn.evole.mods.mcbot.core.data.ChatRecordApi;
import cn.evole.mods.mcbot.init.callbacks.IEvents;
import cn.evole.mods.mcbot.init.event.*;
import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.mods.mcbot.config.ModConfig;
import cn.evole.mods.mcbot.init.handler.CustomCmdHandler;
import cn.evole.mods.mcbot.util.MessageThread;
import cn.evole.mods.mcbot.util.lib.LibUtils;
import cn.evole.mods.mcbot.util.locale.I18n;
import cn.evole.onebot.client.connection.ConnectFactory;
import cn.evole.onebot.client.core.Bot;
import cn.evole.onebot.client.factory.ListenerFactory;
import cn.evole.onebot.client.OneBotClient;
import cn.evole.onebot.sdk.util.FileUtils;
import com.google.gson.JsonObject;
import cn.evole.onebot.sdk.util.java.Assert;
import lombok.Getter;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand All @@ -21,7 +19,6 @@
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
//#if MC >= 11900
//$$ import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
//#else
Expand All @@ -39,15 +36,11 @@ public class McBot implements ModInitializer {
public static MinecraftServer SERVER = null;
public static Path CONFIG_FOLDER;
public static Path CONFIG_FILE;

public static LinkedBlockingQueue<JsonObject> blockingQueue;
public static ConnectFactory service;
public static ListenerFactory listenerFactory;
public static Bot bot;
public static Path LIB_FOLDER;

public static McBot INSTANCE = new McBot();

public static MessageThread messageThread;
public static OneBotClient onebot;
public static ExecutorService CQUtilsExecutor;

@Override
Expand Down Expand Up @@ -77,9 +70,12 @@ public void onInitialize() {


public void init() {
CONFIG_FOLDER = Const.configDir.resolve("mcbot");
CONFIG_FOLDER = Const.gameDir.resolve("mcbot");
FileUtils.checkFolder(CONFIG_FOLDER);
LIB_FOLDER = CONFIG_FOLDER.resolve("libs");
FileUtils.checkFolder(LIB_FOLDER);
CONFIG_FILE = CONFIG_FOLDER.resolve("config.toml");
LibUtils.create(LIB_FOLDER, "libs.txt").download();
I18n.init();
UserBindApi.load(CONFIG_FOLDER);
ChatRecordApi.load(CONFIG_FOLDER);
Expand All @@ -90,35 +86,24 @@ public void onServerStarting(MinecraftServer server) {
}

public void onServerStarted(MinecraftServer server) {
blockingQueue = new LinkedBlockingQueue<>();//使用队列传输数据
if (ModConfig.INSTANCE.getCommon().isAutoOpen()) {
try {
service = new ConnectFactory(ModConfig.INSTANCE.getBotConfig().toBot(), blockingQueue);//创建websocket连接
bot = service.getBot();//创建机器人实例
} catch (Exception e) {
Const.LOGGER.error("▌ §c机器人服务端未配置或未打开");
}
onebot = OneBotClient.create(ModConfig.INSTANCE.getBotConfig().build()).open().registerEvents(new IBotEvent());
}
listenerFactory = new ListenerFactory(blockingQueue);//创建事件分发器
listenerFactory.start();
if (onebot != null) onebot.getEventsBus().register(new IBotEvent());
CustomCmdHandler.INSTANCE.load();//自定义命令加载
IBotEvent.init(listenerFactory);//事件监听
messageThread = new MessageThread(); // 创建消息处理线程池
CQUtilsExecutor = Executors.newSingleThreadExecutor(); // 创建CQ码处理线程池
}

public void onServerStopping(MinecraftServer server) {
Const.isShutdown = true;
Const.LOGGER.info("▌ §c正在关闭群服互联 §a┈━═☆");
Const.LOGGER.info("▌ §c正在关闭群服互联");
UserBindApi.save(CONFIG_FOLDER);
ChatRecordApi.save(CONFIG_FOLDER);
CustomCmdHandler.INSTANCE.clear();//自定义命令持久层清空
listenerFactory.stop();//分发器关闭
}

public void onServerStopped(MinecraftServer server) {
service.stop();
messageThread.stop();
if (onebot != null) onebot.close();
CQUtilsExecutor.shutdownNow();
}

Expand Down
26 changes: 4 additions & 22 deletions fabric/src/main/java/cn/evole/mods/mcbot/cmds/CmdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.init.compat.vanish.VanishAPI;
import cn.evole.mods.mcbot.init.handler.CustomCmdHandler;
import cn.evole.mods.mcbot.util.onebot.BotUtils;
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;
import cn.evole.onebot.sdk.event.message.GuildMessageEvent;

//兼容vanish
import cn.evole.mods.mcbot.init.compat.vanish.VanishAPI;

/**
* Description:
Expand All @@ -31,15 +28,11 @@ private static String CmdMain(String cmd, boolean isOp, boolean vanishSupport) {
}

private static void GroupCmd(long groupId, String cmd, boolean isOp, boolean OPEscape) {
Const.groupMsg(groupId, CmdMain(cmd, isOp, OPEscape));
}

private static void GuildCmd(String guildId, String channelId, String cmd, boolean isOp, boolean OPEscape) {
Const.guildMsg(guildId, channelId, CmdMain(cmd, isOp, OPEscape));
Const.sendGroupMsg(groupId, CmdMain(cmd, isOp, OPEscape));
}

public static void invokeCommandGroup(GroupMessageEvent event) {
String command = event.getMessage().substring(1);//去除前缀
public static void invokeCommandGroup(String msg, GroupMessageEvent event) {
String command = msg.substring(1);//去除前缀
String originCommand = command;
command = BotUtils.cmdParse(command);
String performedCommand = command;
Expand All @@ -62,15 +55,4 @@ public static void invokeCommandGroup(GroupMessageEvent event) {

}

public static void invokeCommandGuild(GuildMessageEvent event) {
String command = event.getMessage().substring(1);//去除前缀

if (BotUtils.guildAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), true, customCmd.isVanishSupport()));//admin
} else CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), false, customCmd.isVanishSupport()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.cmds;

import cn.evole.onebot.sdk.util.json.GsonUtils;
import cn.evole.onebot.sdk.util.GsonUtils;
import com.google.gson.JsonObject;
import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.data.UserBindApi;
import cn.evole.mods.mcbot.core.data.UserBindApi;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.mods.mcbot.config.ModConfig;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.val;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evole.mods.mcbot.command;

import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.mods.mcbot.config.ModConfig;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.val;
Expand Down
Loading

0 comments on commit 952078a

Please sign in to comment.