diff --git a/.classpath b/.classpath index 7fc367e..eb0fa57 100644 --- a/.classpath +++ b/.classpath @@ -31,6 +31,11 @@ + + + + + @@ -96,6 +101,21 @@ + + + + + + + + + + + + + + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index cbd9ab0..98b3f38 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/build.gradle b/build.gradle index 349c1eb..c5fc3e6 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/ren/taske/nativebot/NativeBotPlugin.java b/src/main/java/ren/taske/nativebot/NativeBotPlugin.java index abae4a9..c5029fe 100644 --- a/src/main/java/ren/taske/nativebot/NativeBotPlugin.java +++ b/src/main/java/ren/taske/nativebot/NativeBotPlugin.java @@ -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; } } diff --git a/src/main/java/ren/taske/nativebot/bot/Bot.java b/src/main/java/ren/taske/nativebot/bot/Bot.java deleted file mode 100644 index 78cde52..0000000 --- a/src/main/java/ren/taske/nativebot/bot/Bot.java +++ /dev/null @@ -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 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.
- * 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 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); - } - -} diff --git a/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java b/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java index 29f32f6..e94ea62 100644 --- a/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java +++ b/src/main/java/ren/taske/nativebot/bot/chatting/Chatting.java @@ -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()); } diff --git a/src/main/java/ren/taske/nativebot/bot/command/CommandConsole.java b/src/main/java/ren/taske/nativebot/bot/command/CommandConsole.java index 9890651..c1c7a49 100644 --- a/src/main/java/ren/taske/nativebot/bot/command/CommandConsole.java +++ b/src/main/java/ren/taske/nativebot/bot/command/CommandConsole.java @@ -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() { + return Bukkit.getScheduler().callSyncMethod(NativeBotPlugin.getPlugin(), new Callable() { @Override public Boolean call() throws Exception { - NativeBotPlugin.logger().info("[$] "+command); + NativeBotPlugin.logger().log("[$] %s", command); return Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), command); } }).get(); diff --git a/src/main/java/ren/taske/nativebot/bot/command/CommandList.java b/src/main/java/ren/taske/nativebot/bot/command/CommandList.java new file mode 100644 index 0000000..9c3219a --- /dev/null +++ b/src/main/java/ren/taske/nativebot/bot/command/CommandList.java @@ -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 args) { + Collection 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(); + } + +} diff --git a/src/main/java/ren/taske/nativebot/bot/event/EventBotRegistration.java b/src/main/java/ren/taske/nativebot/bot/event/EventBotRegistration.java index 2f8a4c4..507f41a 100644 --- a/src/main/java/ren/taske/nativebot/bot/event/EventBotRegistration.java +++ b/src/main/java/ren/taske/nativebot/bot/event/EventBotRegistration.java @@ -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; diff --git a/src/main/java/ren/taske/nativebot/bot/listener/ServerLoadEventListener.java b/src/main/java/ren/taske/nativebot/bot/listener/ServerLoadEventListener.java index ecb9bb0..1bf1cba 100644 --- a/src/main/java/ren/taske/nativebot/bot/listener/ServerLoadEventListener.java +++ b/src/main/java/ren/taske/nativebot/bot/listener/ServerLoadEventListener.java @@ -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! } } diff --git a/src/main/java/ren/taske/nativebot/bot/permission/PermissionManager.java b/src/main/java/ren/taske/nativebot/bot/permission/PermissionManager.java index ed6299c..2b001c0 100644 --- a/src/main/java/ren/taske/nativebot/bot/permission/PermissionManager.java +++ b/src/main/java/ren/taske/nativebot/bot/permission/PermissionManager.java @@ -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); } - } diff --git a/src/main/java/ren/taske/nativebot/core/BotApi.java b/src/main/java/ren/taske/nativebot/core/BotApi.java new file mode 100644 index 0000000..0a68029 --- /dev/null +++ b/src/main/java/ren/taske/nativebot/core/BotApi.java @@ -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); + } + +} diff --git a/src/main/java/ren/taske/nativebot/core/NativeBot.java b/src/main/java/ren/taske/nativebot/core/NativeBot.java index e3bf90f..5eaec2e 100644 --- a/src/main/java/ren/taske/nativebot/core/NativeBot.java +++ b/src/main/java/ren/taske/nativebot/core/NativeBot.java @@ -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) { diff --git a/src/main/resources/i18n.txt b/src/main/resources/i18n.txt index ea5569a..2d0a05f 100644 --- a/src/main/resources/i18n.txt +++ b/src/main/resources/i18n.txt @@ -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 + diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e5699df..bf4e8af 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: NativeBot main: ren.taske.nativebot.NativeBotPlugin -version: 1.1.0 +version: 1.2.0 commands: qq: