Skip to content

Commit

Permalink
-修复了重启后配置还原的bug
Browse files Browse the repository at this point in the history
-删除了后台debug提示
-添加了控制欢迎新入群消息的开关
  • Loading branch information
cnlimiter committed Jan 18, 2022
1 parent 309a259 commit 2024381
Show file tree
Hide file tree
Showing 23 changed files with 80 additions and 3,248 deletions.
15 changes: 7 additions & 8 deletions src/main/java/cn/evolvefield/mods/botapi/BotApi.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.evolvefield.mods.botapi;

import cn.evolvefield.mods.botapi.config.BotConfig;
import cn.evolvefield.mods.botapi.config.ConfigManger;
import cn.evolvefield.mods.botapi.service.ClientThreadService;
import cn.evolvefield.mods.botapi.common.config.BotConfig;
import cn.evolvefield.mods.botapi.common.config.ConfigManger;
import cn.evolvefield.mods.botapi.core.service.ClientThreadService;
import com.google.gson.Gson;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.common.MinecraftForge;
Expand All @@ -16,7 +16,7 @@
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.fmllegacy.server.ServerLifecycleHooks;
import net.minecraftforge.fmlserverevents.FMLServerAboutToStartEvent;
import net.minecraftforge.fmlserverevents.FMLServerStartingEvent;
import net.minecraftforge.fmlserverevents.FMLServerStartedEvent;
import net.minecraftforge.fmlserverevents.FMLServerStoppingEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -39,14 +39,13 @@ public BotApi() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
MinecraftForge.EVENT_BUS.register(this);
MinecraftForge.EVENT_BUS.addListener(this::onServerStarting);
MinecraftForge.EVENT_BUS.addListener(this::onServerStarted);
MinecraftForge.EVENT_BUS.addListener(this::onServerStopping);

}

private void setup(final FMLCommonSetupEvent event) {
CONFIG_FOLDER = FMLPaths.CONFIGDIR.get();
config = new BotConfig();
}

private void doClientStuff(final FMLClientSetupEvent event) {
Expand All @@ -59,9 +58,9 @@ private void processIMC(final InterModProcessEvent event) {

}

private void onServerStarting(FMLServerStartingEvent event){
private void onServerStarted(FMLServerStartedEvent event){
//加载配置
ConfigManger.initBotConfig();
config = ConfigManger.initBotConfig();
if (BotApi.config.getCommon().isEnable()) {
ClientThreadService.runWebSocketClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import java.util.List;

import static cn.evolvefield.mods.botapi.network.WebSocket.WebSocketChannelSupervise.sendToAll;
import static cn.evolvefield.mods.botapi.core.network.WebSocket.WebSocketChannelSupervise.sendToAll;

public class SendMessage {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;


import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.config.ConfigManger;
import cn.evolvefield.mods.botapi.service.ClientThreadService;
import cn.evolvefield.mods.botapi.common.config.ConfigManger;
import cn.evolvefield.mods.botapi.core.service.ClientThreadService;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.config.ConfigManger;
import cn.evolvefield.mods.botapi.common.config.ConfigManger;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;


import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.config.ConfigManger;
import cn.evolvefield.mods.botapi.service.ClientThreadService;
import cn.evolvefield.mods.botapi.common.config.ConfigManger;
import cn.evolvefield.mods.botapi.core.service.ClientThreadService;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.commands.CommandRuntimeException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.config.ConfigManger;
import cn.evolvefield.mods.botapi.common.config.ConfigManger;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;


import cn.evolvefield.mods.botapi.BotApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.config.ConfigManger;
import cn.evolvefield.mods.botapi.common.config.ConfigManger;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.config.ConfigManger;
import cn.evolvefield.mods.botapi.common.config.ConfigManger;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
Expand Down Expand Up @@ -34,9 +34,29 @@ public class SendCommand {
.then(literal("achievements")
.then(Commands.argument("enabled", BoolArgumentType.bool())
.executes(SendCommand::achievementsExecute)))
.then(literal("welcome")
.then(Commands.argument("enabled", BoolArgumentType.bool())
.executes(SendCommand::welcomeExecute)))
;
}

public static int welcomeExecute(CommandContext<CommandSourceStack> context) throws CommandRuntimeException {
boolean isEnabled = context.getArgument("enabled", Boolean.class);
BotApi.config.getCommon().setS_WELCOME_ENABLE(isEnabled);
ConfigManger.saveBotConfig(BotApi.config);
if (isEnabled)
{
context.getSource().sendSuccess(
new TextComponent("发送新人加入QQ群的消息开关已被设置为打开"), true);
}
else
{
context.getSource().sendSuccess(
new TextComponent("发送新人加入QQ群的消息开关已被设置为关闭"), true);
}
return 0;
}

public static int allExecute(CommandContext<CommandSourceStack> context) throws CommandRuntimeException {
boolean isEnabled = context.getArgument("enabled", Boolean.class);
BotApi.config.getCommon().setSEND_ENABLED(isEnabled);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.evolvefield.mods.botapi.command;
package cn.evolvefield.mods.botapi.common.command;


import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.service.ClientThreadService;
import cn.evolvefield.mods.botapi.core.service.ClientThreadService;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.commands.CommandRuntimeException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evolvefield.mods.botapi.config;
package cn.evolvefield.mods.botapi.common.config;

import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -48,6 +48,8 @@ public static class Common {
private boolean R_CHAT_ENABLE = true;
@SerializedName("SEND_ENABLED")
private boolean SEND_ENABLED = true;
@SerializedName("WELCOME_ENABLE")
private boolean S_WELCOME_ENABLE = true;
@SerializedName("JOIN_ENABLE")
private boolean S_JOIN_ENABLE = true;
@SerializedName("LEAVE_ENABLE")
Expand All @@ -59,6 +61,14 @@ public static class Common {
@SerializedName("S_ADVANCE_ENABLE")
private boolean S_ADVANCE_ENABLE = true;

public void setS_WELCOME_ENABLE(boolean s_WELCOME_ENABLE) {
S_WELCOME_ENABLE = s_WELCOME_ENABLE;
}

public boolean isS_WELCOME_ENABLE() {
return S_WELCOME_ENABLE;
}


public String getWelcomeNotice() {
return welcomeNotice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evolvefield.mods.botapi.config;
package cn.evolvefield.mods.botapi.common.config;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.util.json.JSONFormat;
Expand All @@ -11,11 +11,10 @@
import java.nio.file.Path;



public class ConfigManger {
private static final Gson GSON = new Gson();

public static void initBotConfig() {
public static BotConfig initBotConfig() {
BotConfig config = new BotConfig();

if (!BotApi.CONFIG_FOLDER.toFile().isDirectory()) {
Expand All @@ -42,6 +41,7 @@ public static void initBotConfig() {
}
}

return config;
}

public static void saveBotConfig(BotConfig config) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evolvefield.mods.botapi.config;
package cn.evolvefield.mods.botapi.common.config;

public interface IConfig {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evolvefield.mods.botapi.network.WebSocket;
package cn.evolvefield.mods.botapi.core.network.WebSocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evolvefield.mods.botapi.network.WebSocket;
package cn.evolvefield.mods.botapi.core.network.WebSocket;


import io.netty.channel.Channel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.evolvefield.mods.botapi.network.WebSocket;
package cn.evolvefield.mods.botapi.core.network.WebSocket;


import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.service.ClientThreadService;
import cn.evolvefield.mods.botapi.core.service.ClientThreadService;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
Expand Down Expand Up @@ -39,6 +39,7 @@ public WebSocketClient(String host, int port, String key) {
this.key = key;
setUncaughtExceptionHandler((thread, throwable) -> {
logger.warn("连接出错,将结束客户端线程:", throwable);
logger.warn("请检查Go-cqhttp是否开启");
ClientThreadService.stopWebSocketClient();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.evolvefield.mods.botapi.network.WebSocket;
package cn.evolvefield.mods.botapi.core.network.WebSocket;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.service.ClientThreadService;
import cn.evolvefield.mods.botapi.service.MessageHandlerService;
import cn.evolvefield.mods.botapi.core.service.ClientThreadService;
import cn.evolvefield.mods.botapi.core.service.MessageHandlerService;
import io.netty.channel.*;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.websocketx.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evolvefield.mods.botapi.service;
package cn.evolvefield.mods.botapi.core.service;

import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.network.WebSocket.WebSocketClient;
import cn.evolvefield.mods.botapi.core.network.WebSocket.WebSocketClient;

public class ClientThreadService {
public static WebSocketClient client;
Expand All @@ -11,9 +11,9 @@ public static void runWebSocketClient() {
client.interrupt();
}
client = new WebSocketClient(
BotApi.config.getCommon().getWsHOST(),
BotApi.config.getCommon().getWsPORT(),
BotApi.config.getCommon().getKEY());
BotApi.config.getCommon().getWsHost(),
BotApi.config.getCommon().getWsPort(),
BotApi.config.getCommon().getWsKey());
client.start();
}
public static void runWebSocketClient(String host, int port, String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cn.evolvefield.mods.botapi.service;
package cn.evolvefield.mods.botapi.core.service;


import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.command.Invoke;
import cn.evolvefield.mods.botapi.event.TickEventHandler;
import cn.evolvefield.mods.botapi.api.MessageJson;
import cn.evolvefield.mods.botapi.api.SendMessage;
import cn.evolvefield.mods.botapi.common.command.Invoke;
import cn.evolvefield.mods.botapi.init.events.TickEventHandler;
import net.minecraftforge.event.ServerChatEvent;

public class MessageHandlerService {
Expand All @@ -16,7 +16,6 @@ public class MessageHandlerService {
public static void sendMessage(ServerChatEvent event) {

SendMessage.Group(BotApi.config.getCommon().getGroupId(),String.format("[MC]<%s> %s", event.getPlayer().getDisplayName().getString(), event.getMessage()));
//sendToAll(new TextWebSocketFrame("/send_group_msg?group_id=" + ModConfig.GROUP_ID.get() + "&message=" + event.getMessage()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.evolvefield.mods.botapi.event;
package cn.evolvefield.mods.botapi.init.events;
import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.service.MessageHandlerService;
import cn.evolvefield.mods.botapi.core.service.MessageHandlerService;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evolvefield.mods.botapi.event;
package cn.evolvefield.mods.botapi.init.events;


import cn.evolvefield.mods.botapi.command.*;
import cn.evolvefield.mods.botapi.common.command.*;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand Down
Loading

0 comments on commit 2024381

Please sign in to comment.