Skip to content

Commit

Permalink
+ 更新至2.1.5
Browse files Browse the repository at this point in the history
+ fix #73 #74
  • Loading branch information
cnlimiter committed Apr 19, 2023
1 parent 3105340 commit 5bf0aaf
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 73 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ minecraft_version=1.19.3
loader_version=0.14.13
# Mod Properties
version_prefix=1.19.3
mod_version=2.1.4
client_version=0.3.2-light
mod_version=2.1.5
client_version=0.3.3-light
jankson_version=1.2.1
parchement_version=2022.12.18
maven_group=cn.evolvefield.mods
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/cn/evolvefield/mods/botapi/BotApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.io.File;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -34,8 +33,8 @@ public class BotApi implements ModInitializer {
public static ConnectFactory service;
public static EventBus dispatchers;
public static Bot bot;
public static ExecutorService app = Executors.newFixedThreadPool(1);
;
public static Thread app;


public BotApi() {

Expand Down Expand Up @@ -71,10 +70,11 @@ private void onServerStarted(MinecraftServer server) {
blockingQueue = new LinkedBlockingQueue<>();//使用队列传输数据
if (ConfigHandler.cached().getCommon().isAutoOpen()) {
try {
app.submit(() -> {
app = new Thread(() -> {
service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), blockingQueue);//创建websocket连接
bot = service.ws.createBot();//创建机器人实例
});
}, "BotServer");
app.start();
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端未配置或未打开");
}
Expand All @@ -89,7 +89,7 @@ private void onServerStopping(MinecraftServer server) {
Const.LOGGER.info("▌ §c正在关闭群服互联 §a┈━═☆");
dispatchers.stop();//分发器关闭
service.stop();
app.shutdownNow();
app.interrupt();

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/evolvefield/mods/botapi/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class Const {
public static final String MODID = "botapi";
public static final Logger LOGGER = LogManager.getLogger(MODID);
public static final Logger LOGGER = LogManager.getLogger("BotConnect");
public static boolean isShutdown = false;


Expand Down
4 changes: 1 addition & 3 deletions src/main/java/cn/evolvefield/mods/botapi/api/cmd/CmdApi.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.evolvefield.mods.botapi.api.cmd;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.Const;
import cn.evolvefield.mods.botapi.init.handler.CustomCmdHandler;
import cn.evolvefield.mods.botapi.util.onebot.BotUtils;
import cn.evolvefield.onebot.client.core.Bot;
Expand Down Expand Up @@ -37,8 +36,7 @@ private static void GuildCmd(Bot bot, String guildId, String channelId, String c
public static void invokeCommandGroup(GroupMessageEvent event) {
String commandHead = event.getMessage().split(" ")[0].substring(1);
String command = event.getMessage().substring(1);//去除前缀
Const.LOGGER.info(event);
Const.LOGGER.info(BotUtils.groupAdminParse(event));

if (BotUtils.groupAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getCmdAlies().equals(commandHead))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evolvefield.mods.botapi.api.listener;

import cn.evolvefield.mods.botapi.api.cmd.CmdApi;
import cn.evolvefield.onebot.client.listener.DefaultHandlerListener;
import cn.evolvefield.onebot.client.handler.DefaultEventHandler;
import cn.evolvefield.onebot.sdk.event.message.GroupMessageEvent;


Expand All @@ -11,7 +11,7 @@
* Date: 2022/10/2 15:58
* Version: 1.0
*/
public class GroupCmdListener extends DefaultHandlerListener<GroupMessageEvent> {
public class GroupCmdEventListener extends DefaultEventHandler<GroupMessageEvent> {
@Override
public void onMessage(GroupMessageEvent event) {
//处理逻辑
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public static int cqhttpExecute(CommandContext<CommandSourceStack> context) thro
ConfigHandler.cached().getBotConfig().setMiraiHttp(false);

try {
BotApi.app.submit(() -> {
BotApi.app = new Thread(() -> {
BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接
BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例
});
}, "BotServer");
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand All @@ -56,10 +56,10 @@ public static int miraiExecute(CommandContext<CommandSourceStack> context) throw
context.getSource().sendSuccess(Component.literal("尝试链接框架" + ChatFormatting.LIGHT_PURPLE + "mirai"), true);
ConfigHandler.cached().getBotConfig().setMiraiHttp(true);
try {
BotApi.app.submit(() -> {
BotApi.app = new Thread(() -> {
BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接
BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例
});
}, "BotServer");
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand All @@ -79,10 +79,10 @@ public static int cqhttpCommonExecute(CommandContext<CommandSourceStack> context
context.getSource().sendSuccess(Component.literal("尝试链接框架" + ChatFormatting.LIGHT_PURPLE + "cqhttp"), true);
ConfigHandler.cached().getBotConfig().setMiraiHttp(false);
try {
BotApi.app.submit(() -> {
BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接
BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例
});
BotApi.app = new Thread(() -> {
BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接
BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例
}, "BotServer");
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand All @@ -99,10 +99,10 @@ public static int miraiCommonExecute(CommandContext<CommandSourceStack> context)
context.getSource().sendSuccess(Component.literal("尝试链接框架" + ChatFormatting.LIGHT_PURPLE + "mirai"), true);
ConfigHandler.cached().getBotConfig().setMiraiHttp(true);
try {
BotApi.app.submit(() -> {
BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接
BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例
});
BotApi.app = new Thread(() -> {
BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接
BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例
}, "BotServer");
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端配置不正确");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import cn.evolvefield.mods.botapi.api.cmd.CmdApi;
import cn.evolvefield.mods.botapi.util.onebot.CQUtils;
import cn.evolvefield.onebot.client.handler.EventBus;
import cn.evolvefield.onebot.client.listener.SimpleListener;
import cn.evolvefield.onebot.client.listener.SimpleEventListener;
import cn.evolvefield.onebot.sdk.event.message.GroupMessageEvent;
import cn.evolvefield.onebot.sdk.event.message.GuildMessageEvent;
import cn.evolvefield.onebot.sdk.event.meta.LifecycleMetaEvent;
import cn.evolvefield.onebot.sdk.event.notice.group.GroupDecreaseNoticeEvent;
import cn.evolvefield.onebot.sdk.event.notice.group.GroupIncreaseNoticeEvent;
import lombok.val;

/**
* Description:
Expand All @@ -21,21 +23,21 @@ public static void init(EventBus dispatchers) {

GroupChatHandler(dispatchers);
GroupCmdsHandler(dispatchers);
GroupMemberNotice(dispatchers);
GroupNoticeHandler(dispatchers);
GuildChatHandler(dispatchers);
GuildCmdsHandler(dispatchers);
dispatchers.start(4);//线程组处理任务
LifeCycleHandler(dispatchers);
}

private static void GroupChatHandler(EventBus dispatchers) {
dispatchers.addListener(new SimpleListener<GroupMessageEvent>() {
dispatchers.addListener(new SimpleEventListener<GroupMessageEvent>() {
@Override
public void onMessage(GroupMessageEvent event) {
if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId())//判断是否是配置中的群
&& !event.getMessage().startsWith(ConfigHandler.cached().getCmd().getCommandStart())//过滤命令前缀
&& ConfigHandler.cached().getStatus().isRECEIVE_ENABLED()//总接受开关
&& ConfigHandler.cached().getStatus().isR_CHAT_ENABLE()//接受聊天开关
&& event.getUserId() != ConfigHandler.cached().getCommon().getBotId()
&& event.getUserId() != ConfigHandler.cached().getCommon().getBotId()//过滤机器人
) {

String send = CQUtils.replace(event.getMessage());//暂时匹配仅符合字符串聊天内容与图片
Expand All @@ -48,15 +50,13 @@ public void onMessage(GroupMessageEvent event) {
}
String toSend = String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ConfigHandler.cached().getCmd().getQqPrefix(), event.getGroupId(), event.getSender().getNickname(), send);
TickEventHandler.getToSendQueue().add(toSend);


}
}
});
}

private static void GroupCmdsHandler(EventBus dispatchers) {
dispatchers.addListener(new SimpleListener<GroupMessageEvent>() {
dispatchers.addListener(new SimpleEventListener<GroupMessageEvent>() {
@Override
public void onMessage(GroupMessageEvent event) {
if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId())
Expand All @@ -70,8 +70,8 @@ public void onMessage(GroupMessageEvent event) {
});
}

private static void GroupMemberNotice(EventBus dispatchers) {
dispatchers.addListener(new SimpleListener<GroupDecreaseNoticeEvent>() {
private static void GroupNoticeHandler(EventBus dispatchers) {
dispatchers.addListener(new SimpleEventListener<GroupDecreaseNoticeEvent>() {
@Override
public void onMessage(GroupDecreaseNoticeEvent event) {
if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId())
Expand All @@ -82,7 +82,7 @@ public void onMessage(GroupDecreaseNoticeEvent event) {
}
});

dispatchers.addListener(new SimpleListener<GroupIncreaseNoticeEvent>() {
dispatchers.addListener(new SimpleEventListener<GroupIncreaseNoticeEvent>() {
@Override
public void onMessage(GroupIncreaseNoticeEvent event) {
if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId())
Expand All @@ -96,7 +96,7 @@ public void onMessage(GroupIncreaseNoticeEvent event) {
}

private static void GuildChatHandler(EventBus dispatchers) {
dispatchers.addListener(new SimpleListener<GuildMessageEvent>() {
dispatchers.addListener(new SimpleEventListener<GuildMessageEvent>() {
@Override
public void onMessage(GuildMessageEvent event) {
if (event.getGuildId().equals(ConfigHandler.cached().getCommon().getGuildId())
Expand All @@ -123,7 +123,7 @@ public void onMessage(GuildMessageEvent event) {
}

private static void GuildCmdsHandler(EventBus dispatchers) {
dispatchers.addListener(new SimpleListener<GuildMessageEvent>() {
dispatchers.addListener(new SimpleEventListener<GuildMessageEvent>() {
@Override
public void onMessage(GuildMessageEvent event) {
if (ConfigHandler.cached().getCommon().getChannelIdList().contains(event.getChannelId())
Expand All @@ -138,4 +138,26 @@ public void onMessage(GuildMessageEvent event) {
}


private static void LifeCycleHandler(EventBus dispatchers) {
dispatchers.addListener(new SimpleEventListener<LifecycleMetaEvent>() {
@Override
public void onMessage(LifecycleMetaEvent event) {
if (!event.getSubType().equals("connect")) return;
if (!ConfigHandler.cached().getCommon().getGroupIdList().isEmpty()
) {
for (val id : ConfigHandler.cached().getCommon().getGroupIdList()){
BotApi.bot.sendGroupMsg(id, "▌ 群服互联已连接 ┈━═☆", true);
}
}
if (!ConfigHandler.cached().getCommon().getChannelIdList().isEmpty()
) {
for (val id : ConfigHandler.cached().getCommon().getChannelIdList()){
BotApi.bot.sendGuildMsg(ConfigHandler.cached().getCommon().getGuildId() , id, "▌ 群服互联已连接 ┈━═☆");
}
}
}
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public abstract class Translation {
private static final Gson GSON = new Gson();
private static final Pattern UNSUPPORTED_FORMAT_PATTERN = Pattern.compile("%(\\d+\\$)?[\\d.]*[df]");
public static final String DEFAULT_LANGUAGE = "en_us";
public static final String DEFAULT_LANGUAGE = "zh_cn";
private static volatile Translation instance = loadDefault(ConfigHandler.cached().getCommon().getLanguageSelect());

public Translation() {
Expand All @@ -44,7 +44,7 @@ private static Translation loadDefault(String langId) {
InputStream inputStream = Translation.class.getResourceAsStream(resourceLocation);

if (inputStream == null) {
Const.LOGGER.info(String.format("No BotApi lang file for the language '%s' found. Make it to 'en_us' by default.", langId));
Const.LOGGER.info(String.format("'%s'的语言文件未找到,将使用默认zh_cn", langId));
inputStream = I18n.class.getResourceAsStream(String.format(resourceFString, DEFAULT_LANGUAGE));
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/cn/evolvefield/mods/botapi/util/onebot/BotUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import cn.evolvefield.onebot.sdk.event.message.GroupMessageEvent;
import cn.evolvefield.onebot.sdk.event.message.GuildMessageEvent;
import cn.evolvefield.onebot.sdk.event.message.MessageEvent;
import lombok.AllArgsConstructor;
import lombok.Data;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -91,4 +96,37 @@ private static int getSubStr(String str) {
}


public static MutableComponent msgBuild(UserMessage msg){
// 拼接要发送对消息
return Component.literal(ChatFormatting.DARK_GREEN + "[")
.append(ChatFormatting.AQUA + msg.getPlatform())
.append(ChatFormatting.WHITE + "(")
.append(ChatFormatting.DARK_PURPLE + msg.getId())
.append(ChatFormatting.WHITE + ")")
.append(ChatFormatting.DARK_GREEN + "]")
.append(ChatFormatting.GOLD + " <" + msg.getName() + "> ")
.append(ChatFormatting.WHITE + msg.getMessage() + " ")
;
}

@Data
@AllArgsConstructor
public static class UserMessage {
private String platform;
private String id;
private String name;
private String message;

@Override
public String toString() {
return "UserMessage{" +
"platform='" + platform + '\'' +
"id='" + id + '\'' +
"name='" + name + '\'' +
", message='" + message + '\'' +
'}';
}

}

}
Loading

0 comments on commit 5bf0aaf

Please sign in to comment.