QQ群与Minecraft服务器互联 (IOTQQ插件)

介绍

  1. 通过本插件可以实现Minecraft服务器聊天窗口与QQ群的互联
  2. 远程执行Minecraft服务器的指令

编写教程

环境要求

  • Eclipse ( java IDE )
  • 可以连接互联网( 废话 )
  • IOTQQ环境

    第一步 依赖引用

    这里IOTQQ环境搭建就不介绍了,详细请看Github!

        <dependency>
            <groupId>io.socket</groupId>
            <artifactId>socket.io-client</artifactId>
            <version>0.8.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <!--SpigotAPI -->
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.12-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>

    第二步 开始编写插件

    @Override
    public void onEnable() {
        // new JavaSocketDemo();
        API.registerModule("mcoo", (Module)new WebServerModule());
        API.registerModule("player", (Module)new WebServerModule1());
        API.registerModule("Inv", (Module)new WebServerModule2());
        LanguageRegistry.INSTANCE.registerEntry(EnumLang.ZH_CN, "item.swordGold.name", "Excalibur");
        if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
            /*
             * We register the EventListeneres here, when PlaceholderAPI is installed. Since
             * all events are in the main class (this class), we simply use "this"
             */
            Bukkit.getPluginManager().registerEvents(new listen(), this);
            getLogger().info("MCOO插件成功启用!正在连接IOTQQ服务器...");
    
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    // TODO 自动生成的方法存根
                    String url = "http://i.mcenjoy.cn:8888";
                    String qq = "2629326992";
                    try {
                        IO.Options options = new IO.Options();
                        options.transports = new String[] { "websocket" };
                        // 失败重试次数
                        options.reconnectionAttempts = 10;
                        // 失败重连的时间间隔
                        options.reconnectionDelay = 1000;
                        // 连接超时时间(ms)
                        options.timeout = 500;
                        final Socket socket = IO.socket(url, options);
                        socket.emit("GetWebConn", qq, new Ack() {
                            @Override
                            public void call(Object... objects) {
                                System.out.println(qq + ":" + Arrays.toString(objects));
                            }
                        });
                        // 统一事件管理如好友进群事件 好友请求事件 退群等事件集合
                        socket.on("OnEvents", objects -> System.out.println(Arrays.toString(objects)));
                        // 收到好友消息的回调事件
                        socket.on("OnFriendMsgs", objects -> System.out.println(Arrays.toString(objects)));
                        // 收到群消息的回调事件
                        socket.on("OnGroupMsgs", objects -> {
                            String tmpString = String.valueOf(objects[0]);
                            // System.out.println(tmpString);
                            // tmpString = getSubString(tmpString, "[", "]");
                            // tmpString = tmpString.replace("\\", "");
                            // System.out.println(tmpString);
                            Map stringToMap = JSONObject.parseObject(tmpString);
                            Map stringToMap1 = JSONObject.parseObject(stringToMap.get("CurrentPacket").toString());
                            Map stringToMap2 = JSONObject.parseObject(stringToMap1.get("Data").toString());
    
                            // Map stMap = stringToMap["CurrentPacket"];
                            // getLogger().info();
                            String qqG = stringToMap2.get("FromGroupId").toString();
                            String FromQQ = stringToMap2.get("FromUserId").toString();
                            String Msg = stringToMap2.get("Content").toString();
                            // System.out.println(Msg);
                            // System.out.println(qqG);
                            // System.out.println(qqG == "237962582");
                            // System.out.println(qqG.equals(237962582));
                            // SmallFaceMsg
                            String[] cm = Msg.split("\\s+",2);
                            // 表情信息处理
    
                            if (qqG.equals("237962582")
                                    && !stringToMap2.get("FromUserId").toString().equals("2629326992")
                                    && stringToMap2.get("MsgType").equals("SmallFaceMsg")) {
                                Map emojiMap = JSONObject.parseObject(Msg);
                                Bukkit.broadcastMessage("§f[§aQQ群§f] <" + stringToMap2.get("FromNickName").toString()
                                        + "> " + emojiMap.get("Content").toString());
                                return;
                            }
    
                            // 文本信息处理
                            if (qqG.equals("237962582")
                                    && !stringToMap2.get("FromUserId").toString().equals("2629326992")
                                    && stringToMap2.get("MsgType").equals("TextMsg")) {
    
                                if (Msg.equals("刷新机器人连接")) {
                                    Map<String, String> parameters = new HashMap<String, String>();
                                    parameters.put("qq", "2629326992");
                                    String string1 = listen.sendGet("http://****.cn:88088/v1/RefreshKeys", parameters);
                                    try {
                                        listen.send(Long.parseLong(qqG), 2, "TextMsg", "执行刷新成功!返回"+string1,
                                                Long.parseLong(stringToMap2.get("FromUserId").toString()));
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                                if (cm[0].equals("聊天状态"))
                                {
                                    try {
                                        if (chat) {
                                            listen.send(Long.parseLong(qqG), 2, "TextMsg", "已连接服务器聊天窗口",
                                                    Long.parseLong(stringToMap2.get("FromUserId").toString()));
                                        }else{
                                            listen.send(Long.parseLong(qqG), 2, "TextMsg", "未连接服务器聊天窗口",
                                                    Long.parseLong(stringToMap2.get("FromUserId").toString()));
                                        }
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                        }
                                    }
                                if (FromQQ.equals("2435932516") && cm[0].equals("关闭聊天"))
                                {
                                    chat = false;
                                    try {
                                        listen.send(Long.parseLong(qqG), 2, "TextMsg", "关闭聊天成功!",
                                                Long.parseLong(stringToMap2.get("FromUserId").toString()));
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
    
                                }
                                if (FromQQ.equals("2435932516") && cm[0].equals("开启聊天"))
                                {
                                    chat = true;
                                    try {
                                        listen.send(Long.parseLong(qqG), 2, "TextMsg", "开启聊天成功!",
                                                Long.parseLong(stringToMap2.get("FromUserId").toString()));
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
    
                                }
                                if (cm.length>1)
                                {
    
                                    if (FromQQ.equals("2435932516") && cm[0].equals("执行"))
                                    {
                                        try {
                                            listen.send(Long.parseLong(qqG), 2, "TextMsg", "执行命令成功!",
                                                    Long.parseLong(stringToMap2.get("FromUserId").toString()));
                                        } catch (NumberFormatException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                        CommandSender se= Bukkit.getConsoleSender();
                                        Bukkit.dispatchCommand(se,cm[1]);
                                        return;
                                    }
    
                                }
                                if (FromQQ.equals("2435932516") && Msg.equals("重启服务器")) {
                                    Bukkit.broadcastMessage("[MCOO服务器官方] 服务器即将重启...");
                                    try {
                                        listen.send(Long.parseLong(qqG), 2, "TextMsg", "执行重启命令成功!",
                                                Long.parseLong(stringToMap2.get("FromUserId").toString()));
                                    } catch (NumberFormatException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"stop");
                                    return;
                                }
                                if (FromQQ.equals("2435932516") && Msg.equals("服务器人数")) {
                                    String msg = "服务器当前人数:";
                                    try {
                                        Collection<? extends Player> tmp = Bukkit.getOnlinePlayers();
                                        msg += String.valueOf(tmp.size()) + "\n";
                                        for (Player i : tmp) {
                                            msg += i.getName() + "\n";
                                        }
    
                                        System.out.println(listen.send(Long.parseLong(qqG), 2, "TextMsg", msg,
                                                Long.parseLong(stringToMap2.get("FromUserId").toString())));
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    return;
                                }
                                Bukkit.broadcastMessage("§f[§aQQ群§f] <" + stringToMap2.get("FromNickName").toString()
                                        + "> " + stringToMap2.get("Content").toString());
                            }
                            //图片消息处理
                            if (qqG.equals("237962582")
                                    && !stringToMap2.get("FromUserId").toString().equals("2629326992")
                                    && stringToMap2.get("MsgType").equals("PicMsg")) {
                                Bukkit.broadcastMessage("§f[§aQQ群§f] <" + stringToMap2.get("FromNickName").toString()
                                        + "> " + "[图片消息](请前往官方群查看!)");
                            }
                            //AT消息处理
                            if (qqG.equals("237962582")
                                    && !stringToMap2.get("FromUserId").toString().equals("2629326992")
                                    && stringToMap2.get("MsgType").equals("AtMsg")) {
                                Map stringToMap3 = JSONObject.parseObject(stringToMap2.get("Content").toString());
                                Bukkit.broadcastMessage("§f[§aQQ群§f] <" + stringToMap2.get("FromNickName").toString()
                                        + "> " + stringToMap3.get("Content").toString());
                            }
                            // System.out.println(stringToMap2.get("MsgType"));
                            // System.out.println(stringToMap2.get("FromNickName").toString()+" :
                            // "+stringToMap2.get("Content").toString());
                            // Bukkit.broadcastMessage("[QQ群] [" +
                            // stringToMap2.get("FromNickName").toString() + "] "+
                            // stringToMap2.get("Content").toString());
    
                        });
                        socket.on(Socket.EVENT_CONNECT, objects -> {
                            System.out.println("client: " + "连接成功");
    
                        });
                        socket.connect();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
    
                }
            }
    
            );
    
            t.start();
    
        } else {
            throw new RuntimeException("缺少PlaceholderAPI前置!");
        }
    
        // getServer().getPluginManager().registerEvents(this, this);
    }
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇