[+] 应用ExtraBot

1. 机器人部分重写,使用MixinBot
2. 添加新指令 /list
3. 修复 /reload 导致机器人不运作的错误
4. 统一使用 HyLogger 而不是 Java 的 Logger
This commit is contained in:
Taskeren 2019-08-03 20:48:04 +08:00
parent d7c29c8917
commit 3f0c779b69
15 changed files with 154 additions and 148 deletions

View File

@ -31,6 +31,11 @@
<attribute name="gradle_used_by_scope" value=""/>
</attributes>
</classpathentry>
<classpathentry sourcepath="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.github.nitu2003/ExtraBot/v1.0p3/c2ed8b32762955844a355d1e2a33d4367210d6d7/ExtraBot-v1.0p3-sources.jar" kind="lib" path="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.github.nitu2003/ExtraBot/v1.0p3/14b8414a844182af9b3e3ebda4f2adcd08f46650/ExtraBot-v1.0p3.jar">
<attributes>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry sourcepath="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.github.nitu2003/TConfig/1.0/5b87c9cea241fac894987711cc4f7ee93d4605cf/TConfig-1.0-sources.jar" kind="lib" path="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.github.nitu2003/TConfig/1.0/7c8975f127c7606cb35ab0a8a3660963439bc139/TConfig-1.0.jar">
<attributes>
<attribute name="gradle_used_by_scope" value="main,test"/>
@ -96,6 +101,21 @@
<attribute name="gradle_used_by_scope" value=""/>
</attributes>
</classpathentry>
<classpathentry sourcepath="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.github.nitu2003/TUtils/588913ce05/d3bcc0878f0c56e0a863ca98e901e2893a84fa37/TUtils-588913ce05-sources.jar" kind="lib" path="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.github.nitu2003/TUtils/588913ce05/3f2bab14bebdbdf4136315c7f6da91b34dc13f8e/TUtils-588913ce05.jar">
<attributes>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry sourcepath="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-math3/3.6.1/8fab23986ea8886af34818daf32a718e81dc98ba/commons-math3-3.6.1-sources.jar" kind="lib" path="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-math3/3.6.1/e4ba98f1d4b3c80ec46392f25e094a6a2e58fcbf/commons-math3-3.6.1.jar">
<attributes>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry sourcepath="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.google.zxing/core/3.3.3/536b6ab3b3b4607ce86e2ff7f6643db11c473aa7/core-3.3.3-sources.jar" kind="lib" path="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/com.google.zxing/core/3.3.3/b640badcc97f18867c4dfd249ef8d20ec0204c07/core-3.3.3.jar">
<attributes>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry sourcepath="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/commons-io/commons-io/2.4/f2d8698c46d1167ff24b06a840a87d91a02db891/commons-io-2.4-sources.jar" kind="lib" path="C:/Users/Taskeren/.gradle/caches/modules-2/files-2.1/commons-io/commons-io/2.4/b1b6ea3b7e4aa4f492509a4952029cd8e48019ad/commons-io-2.4.jar">
<attributes>
<attribute name="gradle_used_by_scope" value="main,test"/>

View File

@ -1,5 +1,5 @@
#
#Wed Jul 10 15:19:10 CST 2019
#Sat Aug 03 12:19:56 CST 2019
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve

View File

@ -30,6 +30,7 @@ repositories {
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
dependencies {
implementation 'com.github.nitu2003:ExtraBot:v1.0p3'
implementation 'com.github.nitu2003:TConfig:1.0'
implementation 'com.github.nitu2003:SimpleDataStorage:1.2'
implementation 'com.github.nitu2003:T18n:1.3'

View File

@ -1,16 +1,17 @@
package ren.taske.nativebot;
import java.util.logging.Logger;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;
import cc.moecraft.icq.event.events.message.EventMessage;
import cc.moecraft.logger.HyLogger;
import cc.moecraft.logger.LoggerInstanceManager;
import cn.glycol.t18n.I18n;
import ren.taske.nativebot.bot.chatting.Chatting;
import ren.taske.nativebot.bot.chatting.MinecraftMessage;
import ren.taske.nativebot.bot.chatting.TencentMessage;
import ren.taske.nativebot.bot.listener.ServerLoadEventListener;
import ren.taske.nativebot.bot.permission.PermissionManager;
import ren.taske.nativebot.core.NativeBot;
import ren.taske.nativebot.i18n.I18nInit;
import ren.taske.nativebot.minecraft.command.CommandQQ;
@ -29,22 +30,28 @@ public class NativeBotPlugin extends JavaPlugin {
protected final CommandQQ cmdqq = new CommandQQ(this);
protected final CommandQQAdmin cmdqqadmin = new CommandQQAdmin();
protected final HyLogger logger = new LoggerInstanceManager().getLoggerInstance("NaB", false);
@Override
public void onEnable() {
nativebotJavaPlugin = this;
I18nInit.init();
PermissionManager.init();
getLogger().info(I18n.format("message.common.welcome"));
nativebot.onEnable();
// 绑定QQ号指令
getCommand("qq").setExecutor(cmdqq);
getCommand("qq-admin").setExecutor(cmdqqadmin);
nativebot.getBot().register(tencentevent);
// 注册消息事件监听器
nativebot.getBot().addEventListenr(tencentevent);
getServer().getPluginManager().registerEvents(mcevent, this);
// 注册服务器载入事件监听器
getServer().getPluginManager().registerEvents(botstartevent, this);
super.onEnable();
@ -53,7 +60,6 @@ public class NativeBotPlugin extends JavaPlugin {
@Override
public void onDisable() {
nativebot.onDisable();
super.onDisable();
}
public NativeBot getBot() {
@ -68,10 +74,14 @@ public class NativeBotPlugin extends JavaPlugin {
chatting.onMinecraftMessage(evt);
}
public static NativeBotPlugin nativebotJavaPlugin;
private static NativeBotPlugin nativebotJavaPlugin;
public static Logger logger() {
return nativebotJavaPlugin.getLogger();
public static NativeBotPlugin getPlugin() {
return nativebotJavaPlugin;
}
public static HyLogger logger() {
return nativebotJavaPlugin.logger;
}
}

View File

@ -1,94 +0,0 @@
package ren.taske.nativebot.bot;
import java.util.Collection;
import java.util.List;
import cc.moecraft.icq.PicqBotX;
import cc.moecraft.icq.PicqConfig;
import cc.moecraft.icq.command.CommandManager;
import cc.moecraft.icq.command.interfaces.IcqCommand;
import cc.moecraft.icq.event.IcqListener;
import cc.moecraft.icq.sender.IcqHttpApi;
import cc.moecraft.logger.environments.ColorSupportLevel;
import ren.taske.nativebot.bot.permission.Permission;
import ren.taske.nativebot.bot.permission.PermissionManager;
import ren.taske.nativebot.commons.Config;
public class Bot extends Thread {
protected final PicqBotX bot;
public Bot() {
this(Config.port_in, Config.port_out, Config.url_out, Config.prefixes);
}
/**
* new bot!
* @param portIn the port to receive messages from HttpApi
* @param portOut the port to send messages to HttpApi
* @param urlOut the url to send messages to HttpApi
* @param prefixes the prefixes of commands in Tencent
*/
public Bot(int portIn, int portOut, String urlOut, String...prefixes) {
PicqConfig botconfig = new PicqConfig(portIn).setColorSupportLevel(ColorSupportLevel.DISABLED);
bot = new PicqBotX(botconfig);
bot.addAccount("NightBot", urlOut, portOut);
bot.enableCommandManager(prefixes);
}
/**
* Add commands for bot
* @param cmds the commands
*/
public void register(IcqCommand...cmds) {
bot.getCommandManager().registerCommands(cmds);
}
/**
* Add commands for bot
* @param cmds the commands
*/
public void register(Collection<IcqCommand> cmds) {
register(cmds.toArray(new IcqCommand[0]));
}
/**
* Add event listener for bot
* @param listeners listeners
*/
public void register(IcqListener...listeners) {
bot.getEventManager().registerListeners(listeners);
}
/**
* To start the bot. <br>
* But wait! Did you {@code register} the commands?
*/
public void run() {
PermissionManager.init();
Permission.lock();
bot.startBot();
}
public CommandManager getCommandManager() {
return bot.getCommandManager();
}
public List<String> getCommands(){
return getCommandManager().getCommandNameList();
}
public IcqHttpApi getApi() {
return bot.getAccountManager().getNonAccountSpecifiedApi();
}
public void sendGroupMessage(long gid, String msg) {
getApi().sendGroupMsg(gid, msg);
}
public void sendPrivateMessage(long uid, String msg) {
getApi().sendPrivateMsg(uid, msg);
}
}

View File

@ -2,7 +2,6 @@ package ren.taske.nativebot.bot.chatting;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import cc.moecraft.icq.event.events.message.EventGroupMessage;
import cc.moecraft.icq.event.events.message.EventGroupOrDiscussMessage;
import cc.moecraft.icq.event.events.message.EventMessage;
import cc.moecraft.icq.user.User;
@ -10,6 +9,7 @@ import cc.moecraft.icq.utils.CQUtils;
import cn.glycol.t18n.I18n;
import ren.taske.nativebot.NativeBotPlugin;
import ren.taske.nativebot.commons.Config;
import ren.taske.nativebot.core.BotApi;
import ren.taske.nativebot.core.NativeBot;
import ren.taske.nativebot.core.profile.UserMinecraft;
import ren.taske.nativebot.core.profile.UserTencent;
@ -105,7 +105,7 @@ public class Chatting {
sb.append("\uff3b").append(username).append("\uff3d ");
sb.append(message);
plugin.getBot().getBot().sendGroupMessage(Config.group_id, sb.toString());
BotApi.sendGroupMessage(Config.group_id, sb.toString());
}

View File

@ -45,10 +45,10 @@ public class CommandConsole extends CommandBase {
public static Boolean callSyncDispatch(CommandSender sender, String command) {
try {
return Bukkit.getScheduler().callSyncMethod(NativeBotPlugin.nativebotJavaPlugin, new Callable<Boolean>() {
return Bukkit.getScheduler().callSyncMethod(NativeBotPlugin.getPlugin(), new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
NativeBotPlugin.logger().info("[$] "+command);
NativeBotPlugin.logger().log("[$] %s", command);
return Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), command);
}
}).get();

View File

@ -0,0 +1,31 @@
package ren.taske.nativebot.bot.command;
import java.util.ArrayList;
import java.util.Collection;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import cc.moecraft.icq.event.events.message.EventMessage;
import cc.moecraft.icq.sender.message.MessageBuilder;
import cc.moecraft.icq.user.User;
import cn.glycol.t18n.I18n;
public class CommandList extends CommandBase {
public CommandList() {
super("list", null);
}
@Override
public String execute(EventMessage evt, User user, long userid, String command, ArrayList<String> args) {
Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
MessageBuilder mb = new MessageBuilder();
mb.add(I18n.format("command.list.title")).newLine();
for(Player player : players) {
mb.add(I18n.format("command.list.prefix", player.getName())).newLine();
}
return mb.toString().trim();
}
}

View File

@ -6,7 +6,7 @@ import org.bukkit.event.HandlerList;
import cc.moecraft.icq.command.interfaces.IcqCommand;
import cc.moecraft.icq.event.IcqListener;
import ren.taske.nativebot.bot.Bot;
import cn.glycol.extrabot.bot.MixinBot;
import ren.taske.nativebot.bot.permission.PermissionManager;
/**
@ -23,25 +23,25 @@ public class EventBotRegistration extends Event {
return handlers;
}
protected final Bot bot;
protected final MixinBot bot;
public EventBotRegistration(Bot bot) {
public EventBotRegistration(MixinBot bot) {
this.bot = bot;
}
/** 获取机器人 */
public Bot getBot() {
public MixinBot getBot() {
return bot;
}
/** 注册指令 */
public void registerCommands(IcqCommand...cmds) {
bot.register(cmds);
public void registerCommands(IcqCommand command) {
bot.addCommand(command);
}
/** 注册事件监听器 */
public void registerListeners(IcqListener...listeners) {
bot.register(listeners);
public void registerListeners(IcqListener listener) {
bot.addEventListenr(listener);
}
/** 注册权限 */
@ -59,7 +59,7 @@ public class EventBotRegistration extends Event {
* 千万不要瞎jb调用它
* @return 返回结果
*/
public static EventBotRegistration newEventAndCall(Bot bot) {
public static EventBotRegistration newEventAndCall(MixinBot bot) {
EventBotRegistration evt = new EventBotRegistration(bot);
Bukkit.getServer().getPluginManager().callEvent(evt);
return evt;

View File

@ -1,6 +1,5 @@
package ren.taske.nativebot.bot.listener;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerLoadEvent;
@ -16,14 +15,11 @@ public class ServerLoadEventListener implements Listener {
LoadType type = evt.getType();
if(type == LoadType.STARTUP) {
NativeBotPlugin.nativebotJavaPlugin.getBot().start();
NativeBotPlugin.getPlugin().getBot().start();
}
if(type == LoadType.RELOAD) {
Bukkit.getServer().shutdown();
System.err.println("Don't try to reload NativeBot");
System.err.println("NativeBot CANNOT support reloading.");
System.err.println("不要尝试重载NativeBot这个插件不能重载");
// server has already started. look up!
}
}

View File

@ -29,7 +29,7 @@ public class PermissionManager {
return !(Permission.of(node) == null);
}
// Add defaults
// 注册权限节点
public static void init() {
add(Reference.NODE_OP);
@ -38,5 +38,4 @@ public class PermissionManager {
add(Chatting.NODE_CHATTING_MINECRAFT, true);
}
}

View File

@ -0,0 +1,26 @@
package ren.taske.nativebot.core;
import cc.moecraft.icq.sender.IcqHttpApi;
public class BotApi {
private static IcqHttpApi api;
/* 初始化机器人时设定API */
public static void setApi(IcqHttpApi api) {
BotApi.api = api;
}
public static void sendPrivateMessage(long dest, String message) {
api.sendPrivateMsg(dest, message);
}
public static void sendGroupMessage(long dest, String message) {
api.sendGroupMsg(dest, message);
}
public static void sendDiscussMessage(long dest, String message) {
api.sendDiscussMsg(dest, message);
}
}

View File

@ -4,44 +4,66 @@ import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin;
import ren.taske.nativebot.bot.Bot;
import cc.moecraft.icq.command.interfaces.IcqCommand;
import cn.glycol.extrabot.ExtraBot;
import cn.glycol.extrabot.bot.MixinBot;
import cn.glycol.extrabot.bot.MixinBot.MixinBotConfiguration;
import cn.glycol.extrabot.registration.AutoRegister;
import cn.glycol.extrabot.registration.AutoRegister.Type;
import ren.taske.nativebot.bot.command.CommandAbout;
import ren.taske.nativebot.bot.command.CommandList;
import ren.taske.nativebot.bot.command.CommandOperator;
import ren.taske.nativebot.bot.command.CommandPermission;
import ren.taske.nativebot.bot.event.EventBotRegistration;
import ren.taske.nativebot.util.ClassUtils;
import ren.taske.nativebot.commons.Config;
public class NativeBot {
protected static Logger logger;
private static final String[] COMMANDS = new String[] {
"CommandAbout",
"CommandOperator",
"CommandPermission",
"CommandConsole"
};
@AutoRegister(Type.COMMAND)
public static final IcqCommand CMD_ABOUT = new CommandAbout();
@AutoRegister(Type.COMMAND)
public static final IcqCommand CMD_OPERATOR = new CommandOperator();
@AutoRegister(Type.COMMAND)
public static final IcqCommand CMD_PERMISSION = new CommandPermission();
@AutoRegister(Type.COMMAND)
public static final IcqCommand CMD_LIST = new CommandList();
protected final JavaPlugin plugin;
protected final Bot bot;
protected final MixinBot bot;
public NativeBot(JavaPlugin plugin) {
this.plugin = plugin;
this.bot = new Bot();
bot = new MixinBot(new MixinBotConfiguration(Config.port_in));
bot.addAccount("Tbot", Config.url_out, Config.port_out);
bot.enableCommandManager(Config.prefixes);
BotApi.setApi(bot.getAccountManager().getNonAccountSpecifiedApi());
logger = plugin.getLogger();
}
public static Logger logger() {
return logger;
}
public Bot getBot() {
public MixinBot getBot() {
return this.bot;
}
public void onEnable() {
bot.register(ClassUtils.instantiate(COMMANDS));
ExtraBot.register(NativeBot.class, bot);
handleRegistrationEvent();
for(String cmd : bot.getCommands()) logger.info("[C] "+cmd);
for(IcqCommand cmd : bot.getCommandManager().getCommandList()) logger.info("[C] "+cmd.properties().getName());
}
public void onDisable() {
// bot.stopBot();
}
/**
@ -53,16 +75,7 @@ public class NativeBot {
/** 启动机器人 */
public void start() {
bot.start();
}
@SuppressWarnings("deprecation")
public void onDisable() {
try {
bot.stop();
} catch(Exception e) {
logger.warning(e.getMessage());
}
bot.startBot();
}
public static void bigWarning(String str, Object...format) {

View File

@ -23,6 +23,7 @@ command.about.message.3=/op - query if you're operator$/op* [uid] - set user as
command.about.message.4=/perm* [uid] [node] - get the value of the node
command.about.message.5=/perm* [uid] [node] [true/false] - set the value of the node
command.about.message.6=/console* [cmd] - to execute command as Console
command.about.message.7=/list - to list all the online players
command.operator.yeap=您是管理员!
command.operator.nope=您不是管理员!
@ -33,3 +34,6 @@ command.permission.query=[%s]的%s权限状态为%s。
command.permission.set=成功设置[%s]的%s权限状态为%s。
command.permission.unregistered=莫得这种权限你在抓梦jio
command.list.title=[List]
command.list.prefix=* %s

View File

@ -1,6 +1,6 @@
name: NativeBot
main: ren.taske.nativebot.NativeBotPlugin
version: 1.1.0
version: 1.2.0
commands:
qq: