예제 #1
0
        /// <summary>
        /// 发送一个简易表单
        /// </summary>
        /// <param name="tout">超时时间</param>
        /// <param name="func">主选择处理函数</param>
        /// <param name="tofunc">超时处理函数</param>
        /// <returns></returns>
        public bool send(int tout, ONSELECT func, ONTIMEOUT tofunc)
        {
            timeout    = tout;
            onselected = func;
            ontimeout  = tofunc;
            fmcb       = (x) => {
                var e = BaseEvent.getFrom(x) as FormSelectEvent;
                if (e.formid == id)                     // 确定是当前表单
                {
                    mapi.removeBeforeActListener(EventKey.onFormSelect, fmcb);
                    cancelTimeout();
                    onselected(e.selected);
                }
                return(true);
            };
            mapi.addBeforeActListener(EventKey.onFormSelect, fmcb);
            string bts = "[]";

            if (buttons != null && buttons.Count > 0)
            {
                var ser = new JavaScriptSerializer();
                bts = ser.Serialize(buttons);
            }
            id = mapi.sendSimpleForm(uuid, title, content, bts);
            bool ret = (id != 0);

            if (timeout > 0)
            {
                startTimeout();
            }
            return(ret);
        }
예제 #2
0
        public static void init(MCCSAPI api)
        {
            mcapi = api;
            Console.OutputEncoding = Encoding.UTF8;
            // 后台指令监听
            api.addBeforeActListener(EventKey.onServerCmd, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var se = BaseEvent.getFrom(x) as ServerCmdEvent;
                if (se != null)
                {
                    Console.WriteLine("后台指令={0}", se.cmd);
                }
                return(true);
            });
            // 后台指令输出监听
            api.addBeforeActListener(EventKey.onServerCmdOutput, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var se = BaseEvent.getFrom(x) as ServerCmdOutputEvent;
                if (se != null)
                {
                    Console.WriteLine("后台指令输出={0}", se.output);
                }
                return(true);
            });
            // 使用物品监听
            api.addAfterActListener(EventKey.onUseItem, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as UseItemEvent;
                if (ue != null && ue.RESULT)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处使用了 {5} 物品。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.itemname);
                }
                return(true);
            });
            // 放置方块监听
            api.addAfterActListener(EventKey.onPlacedBlock, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as PlacedBlockEvent;
                if (ue != null && ue.RESULT)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处放置了 {5} 方块。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 破坏方块监听
            api.addBeforeActListener(EventKey.onDestroyBlock, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as DestroyBlockEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处破坏 {5} 方块。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 开箱监听
            api.addBeforeActListener(EventKey.onStartOpenChest, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StartOpenChestEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处打开 {5} 箱子。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 开桶监听
            api.addBeforeActListener(EventKey.onStartOpenBarrel, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StartOpenBarrelEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处打开 {5} 木桶。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 关箱监听
            api.addAfterActListener(EventKey.onStopOpenChest, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StopOpenChestEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处关闭 {5} 箱子。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 关桶监听
            api.addAfterActListener(EventKey.onStopOpenBarrel, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StopOpenBarrelEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处关闭 {5} 木桶。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 放入取出监听
            api.addAfterActListener(EventKey.onSetSlot, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as SetSlotEvent;
                if (e != null)
                {
                    if (e.itemcount > 0)
                    {
                        Console.WriteLine("玩家 {0} 在 {1} 槽放入了 {2} 个 {3} 物品。",
                                          e.playername, e.slot, e.itemcount, e.itemname);
                    }
                    else
                    {
                        Console.WriteLine("玩家 {0} 在 {1} 槽取出了物品。",
                                          e.playername, e.slot);
                    }
                }
                return(true);
            });
            // 切换维度监听
            api.addAfterActListener(EventKey.onChangeDimension, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ChangeDimensionEvent;
                if (e != null && e.RESULT)
                {
                    Console.WriteLine("玩家 {0} {1} 切换维度至 {2} 的 ({3},{4},{5}) 处。",
                                      e.playername, e.isstand?"":"悬空地", e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z);
                }
                return(true);
            });
            // 生物死亡监听
            api.addAfterActListener(EventKey.onMobDie, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as MobDieEvent;
                if (e != null && !string.IsNullOrEmpty(e.mobname))
                {
                    Console.WriteLine(" {0} 在 {1} ({2:F2},{3:F2},{4:F2}) 处被 {5} 杀死了。",
                                      e.mobname, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z, e.srcname);
                }
                return(true);
            });
            // 生物伤害监听
            api.addBeforeActListener(EventKey.onMobHurt, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as MobHurtEvent;
                if (e != null && !string.IsNullOrEmpty(e.mobname))
                {
                    Console.WriteLine(" {0} 在 {1} ({2:F2},{3:F2},{4:F2}) 即将受到来自 {5} 的 {6} 点伤害,类型 {7}",
                                      e.mobname, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z, e.srcname, e.dmcount, e.dmtype);
                }
                return(true);
            });
            // 玩家重生监听
            api.addAfterActListener(EventKey.onRespawn, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as RespawnEvent;
                if (e != null && e.RESULT)
                {
                    Console.WriteLine("玩家 {0} 已于 {1} 的 ({2:F2},{3:F2},{4:F2}) 处重生。",
                                      e.playername, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z);
                }
                return(true);
            });
            // 聊天监听
            api.addAfterActListener(EventKey.onChat, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ChatEvent;
                if (e != null)
                {
                    Console.WriteLine(" {0} {1} 说:{2}", e.playername,
                                      !string.IsNullOrEmpty(e.target) ? "悄悄地对 " + e.target : "", e.msg);
                }
                return(true);
            });
            // 输入文本监听
            api.addBeforeActListener(EventKey.onInputText, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as InputTextEvent;
                if (e != null)
                {
                    Console.WriteLine(" <{0}> {1}", e.playername, e.msg);
                }
                return(true);
            });
            // 更新命令方块监听
            api.addBeforeActListener(EventKey.onCommandBlockUpdate, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as CommandBlockUpdateEvent;
                if (e != null)
                {
                    Console.WriteLine(" {0} 试图修改位于 {1} ({2},{3},{4}) 的 {5} 的命令为 {6}",
                                      e.playername, e.dimension, e.position.x, e.position.y, e.position.z,
                                      e.isblock ? "命令块" : "命令矿车", e.cmd);
                }
                return(true);
            });
            // 输入指令监听
            api.addBeforeActListener(EventKey.onInputCommand, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as InputCommandEvent;
                if (e != null)
                {
                    Console.WriteLine(" <{0}> {1}", e.playername, e.cmd);
                }
                return(true);
            });
            // 命令块执行指令监听,拦截
            api.addBeforeActListener(EventKey.onBlockCmd, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as BlockCmdEvent;
                if (e != null)
                {
                    Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图执行指令 {5}",
                                      e.dimension, e.position.x, e.position.y, e.position.z, e.name, e.cmd);
                }
                return(false);
            });
            // NPC执行指令监听,拦截
            api.addBeforeActListener(EventKey.onNpcCmd, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as NpcCmdEvent;
                if (e != null)
                {
                    Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图执行第 {5} 条指令,指令集\n{6}",
                                      e.dimension, e.position.x, e.position.y, e.position.z, e.npcname, e.actionid, e.actions);
                }
                return(false);
            });
            // 世界范围爆炸监听,拦截
            api.addBeforeActListener(EventKey.onLevelExplode, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as LevelExplodeEvent;
                if (e != null)
                {
                    Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图发生强度 {5} 的爆炸。",
                                      e.dimension, e.position.x, e.position.y, e.position.z,
                                      string.IsNullOrEmpty(e.entity) ? e.blockname : e.entity, e.explodepower);
                }
                return(false);
            });

            /*
             * // 玩家移动监听
             * api.addAfterActListener(EventKey.onMove, x => {
             *      var e = BaseEvent.getFrom(x) as MoveEvent;
             *      if (e != null) {
             *              Console.WriteLine("玩家 {0} {1} 移动至 {2} ({3},{4},{5}) 处。",
             *                      e.playername, (e.isstand) ? "":"悬空地", e.dimension,
             *                      e.XYZ.x, e.XYZ.y, e.XYZ.z);
             *      }
             *      return false;
             * });
             */
            // 玩家加入游戏监听
            api.addAfterActListener(EventKey.onLoadName, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as LoadNameEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 加入了游戏,xuid={1}", ue.playername, ue.xuid);
                }
                return(true);
            });
            // 玩家离开游戏监听
            api.addAfterActListener(EventKey.onPlayerLeft, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as PlayerLeftEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 离开了游戏,xuid={1}", ue.playername, ue.xuid);
                }
                return(true);
            });

            // 攻击监听
            // API 方式注册监听器
            api.addAfterActListener(EventKey.onAttack, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                AttackEvent ae = BaseEvent.getFrom(x) as AttackEvent;
                if (ae != null)
                {
                    string str = "玩家 " + ae.playername + " 在 (" + ae.XYZ.x.ToString("F2") + "," +
                                 ae.XYZ.y.ToString("F2") + "," + ae.XYZ.z.ToString("F2") + ") 处攻击了 " + ae.actortype + " 。";
                    Console.WriteLine(str);
                    //Console.WriteLine("list={0}", api.getOnLinePlayers());
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    ArrayList al             = ser.Deserialize <ArrayList>(api.getOnLinePlayers());
                    object uuid = null;
                    foreach (Dictionary <string, object> p in al)
                    {
                        object name;
                        if (p.TryGetValue("playername", out name))
                        {
                            if ((string)name == ae.playername)
                            {
                                // 找到
                                p.TryGetValue("uuid", out uuid);
                                break;
                            }
                        }
                    }
                    if (uuid != null)
                    {
                        api.sendSimpleForm((string)uuid,
                                           "致命选项",
                                           "test choose:",
                                           "[\"生存\",\"死亡\",\"求助\"]");
                        //api.transferserver((string)uuid, "www.xiafox.com", 19132);
                    }
                }
                else
                {
                    Console.WriteLine("Event convent fail.");
                }
                return(true);
            });

            // Json 解析部分 使用JavaScriptSerializer序列化Dictionary或array即可

            //JavaScriptSerializer ser = new JavaScriptSerializer();
            //var data = ser.Deserialize<Dictionary<string, object>>("{\"x\":9}");
            //var ary = ser.Deserialize<ArrayList>("[\"x\",\"y\"]");
            //Console.WriteLine(data["x"]);
            //foreach(string v in ary) {
            //	Console.WriteLine(v);
            //}
            //data["y"] = 8;
            //string dstr = ser.Serialize(data);
            //Console.WriteLine(dstr);

            // 高级玩法,硬编码方式注册hook
            THook.init(api);
        }
예제 #3
0
        public static void Dracoup(MCCSAPI api)
        {
            Dictionary <string, int>    level    = new Dictionary <string, int>();    //等级
            Dictionary <string, double> addition = new Dictionary <string, double>(); //加成值
            Dictionary <string, string> uuid     = new Dictionary <string, string>(); //uuid
            Dictionary <string, string> xuid     = new Dictionary <string, string>(); //xuid
            Dictionary <string, string> job      = new Dictionary <string, string>(); //职业
            Dictionary <string, string> ifban    = new Dictionary <string, string>(); //是否为黑名单
            string MoneyPath   = "./plugins/Draco/RewardMoney.yaml";
            string playersPath = "./plugins/Draco/playersjob.yaml";
            string banPath     = "./plugins/Draco/banplayers.yaml";

            #region //yaml转json示例
            //if (File.Exists("./plugins/Draco/config.yaml"))
            //{
            //    var r = new StringReader(File.ReadAllText("./plugins/Draco/config.yaml"));
            //    var deserializer = new DeserializerBuilder().Build();
            //    var yamlObject = deserializer.Deserialize(r);
            //    var serializer = new SerializerBuilder()
            //        .JsonCompatible()
            //        .Build();
            //    var json = serializer.Serialize(yamlObject);
            //    //Console.WriteLine(json);
            //    config rb = JsonConvert.DeserializeObject<config>(json);
            //    Console.WriteLine("是否开启攻击加成:"+rb.ATNPlus);
            //}
            #endregion
            string awa = @"September 2020 Draco Release.
Thanks for using Draco.
本插件基于Mozilla协议开源
项目地址:
https://github.com/Sbaoor-fly/CSR-Draco
Mozilla协议具体信息:
http://www.mozilla.org/MPL/MPL-1.1.html
特别感谢在内测时对本插件反馈的用户
- 《威尼斯:自由时代》服务器全体成员
- PHP野兽先辈服务器全体成员
- xuWin
- wzy
- SnowPea8027
";

            if (File.Exists("./plugins/Draco/Draco.txt"))
            {
            }
            else
            {
                DialogResult dr = MessageBox.Show("您正在使用公测版的Draco\t\n如果发现bug或想贡献宝贵的建议\t\n请加群反馈:514160938\t\n如果选择“是”,代表您同意我们的服务条款", "Draco", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                //如果消息框返回值是Yes,显示新窗体
                if (dr == DialogResult.Yes)
                {
                }
                //如果消息框返回值是No,关闭当前窗体
                else if (dr == DialogResult.No)
                {
                    //关闭当前窗体
                    var a = File.ReadAllLines("./awa,txt");
                }
                //创建文件和文件夹
                Directory.CreateDirectory("./plugins/Draco");
                File.AppendAllText(banPath, "#格式 玩家ID: 被ban原因", System.Text.Encoding.UTF8);
                File.AppendAllText(playersPath, "Steve: Miner", System.Text.Encoding.Default);
                File.AppendAllText(MoneyPath, "DeBug: false\ncow: 10", System.Text.Encoding.Default);
                File.AppendAllText("./plugins/Draco/Draco.txt", awa, System.Text.Encoding.Default);
                Directory.CreateDirectory("./plugins/Draco/Miner");
                Directory.CreateDirectory("./plugins/Draco/Hunter");
                Directory.CreateDirectory("./plugins/Draco/Swordman");
                Directory.CreateDirectory("./plugins/Draco/Sorcerer");
                var t = Task.Run(async delegate
                {
                    await Task.Delay(10000);
                    api.runcmd("scoreboard objectives add money dummy");
                });
            }
            api.addBeforeActListener(EventKey.onLoadName, x =>
            {
                var a = BaseEvent.getFrom(x) as LoadNameEvent;
                uuid.Add(a.playername, a.uuid);
                xuid.Add(a.playername, a.xuid);
                YML banyml = new YML(banPath);
                if (banyml.read(a.playername, "") != null)
                {
                    ifban.Add(a.playername, "true");
                }
                else
                {
                    ifban.Add(a.playername, "false");
                }
                YML plyml = new YML(playersPath);
                if (plyml.read(a.playername, "") != null)
                {
                    if (File.Exists("./plugins/Draco/" + plyml.read(a.playername, "") + "/" + xuid[a.playername] + ".json"))
                    {
                        var jsontext    = File.ReadAllText("./plugins/Draco/" + plyml.read(a.playername, "") + "/" + xuid[a.playername] + ".json");
                        Playerconfig rb = JsonConvert.DeserializeObject <Playerconfig>(jsontext);
                        addition.Add(a.playername, rb.Addition);
                        job.Add(a.playername, rb.job);
                        level.Add(a.playername, rb.level);
                        Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Draco] 配置文件读取成功!");
                    }
                    else
                    {
                        Playerconfig write = new Playerconfig
                        {
                            Addition = 0.1,
                            Name     = a.playername,
                            level    = 1,
                            job      = plyml.read(a.playername, "")
                        };
                        string str = JsonConvert.SerializeObject(write);
                        File.WriteAllText("./plugins/Draco/" + plyml.read(a.playername, "") + "/" + xuid[a.playername] + ".json", str, System.Text.Encoding.Default);
                        level.Add(a.playername, 1);
                        addition.Add(a.playername, 0.1);
                        job.Add(a.playername, plyml.read(a.playername, ""));
                        Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Draco] 配置文件创建成功!");
                    }
                }
                else
                {
                    job.Add(a.playername, "无职业");
                    level.Add(a.playername, 0);
                    //防止聊天调用等级和职业时崩服
                    Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Draco] {0}进入了游戏", a.playername);
                }
                return(true);
            });
            api.addBeforeActListener(EventKey.onRespawn, x =>
            {
                YML banyml2 = new YML(banPath);
                var a       = BaseEvent.getFrom(x) as RespawnEvent;
                if (ifban[a.playername] == "true")
                {
                    api.runcmd("kick " + a.playername + " " + banyml2.read(a.playername, ""));
                }
                else
                {
                    if (job[a.playername] == "无职业")
                    {
                        var t = Task.Run(async delegate
                        {
                            await Task.Delay(3000);
                            api.sendSimpleForm(uuid[a.playername], "职业选择", "请选择", "[\"剑士\",\"猎手\",\"矿工\",\"法师\"]");
                            api.addBeforeActListener(EventKey.onFormSelect, y =>
                            {
                                var pp = BaseEvent.getFrom(y) as FormSelectEvent;
                                if (job[a.playername] == "无职业")
                                {
                                    if (!string.IsNullOrEmpty(pp.selected))
                                    {
                                        string[] jobid = { "Swordman", "Hunter", "Miner", "Sorcerer" };
                                        int jjob       = Convert.ToInt32(pp.selected);
                                        File.AppendAllText(playersPath, "\n" + a.playername + ": " + jobid[jjob], System.Text.Encoding.UTF8);
                                        //api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§6职业选择成功!会在下次进入游戏时生效!\"}]}");
                                        api.runcmd("kick \"" + a.playername + "\" 请重新登录!");
                                    }
                                    else
                                    {
                                        api.runcmd("kick \"" + a.playername + "\" 你必须有一个职业!");
                                    }
                                }
                                return(true);
                            });
                        });
                    }
                    int b = api.getscoreboard(uuid[a.playername], "money");
                    api.setPlayerSidebar(uuid[a.playername], "个人信息", "[\"玩家名称:" + a.playername + "\",\"职业:" + job[a.playername] + "\",\"等级:" + level[a.playername] + "\",\"金币:" + b + "\"]");
                    int ti = 0;
                    if (level[a.playername] < 50 & level[a.playername] >= 20)
                    {
                        ti = 1;
                    }
                    if (level[a.playername] > 50)
                    {
                        ti = 2;
                    }
                    if (job[a.playername] == "Miner" & ti != 0)
                    {
                        api.runcmd("effect \"" + a.playername + "\" night_version 9999999 " + ti + " true");
                    }
                    if (job[a.playername] == "Swordman" & ti != 0)
                    {
                        api.runcmd("effect \"" + a.playername + "\" resistance 9999999 " + ti + " true");
                    }
                    if (job[a.playername] == "Sorcerer" & ti != 0)
                    {
                        api.runcmd("effect \"" + a.playername + "\" fire_resistance 9999999 " + ti + " true");
                    }
                }
                return(true);
            });
            api.addBeforeActListener(EventKey.onPlayerLeft, x =>
            {
                var a = BaseEvent.getFrom(x) as PlayerLeftEvent;
                level.Remove(a.playername);
                addition.Remove(a.playername);
                uuid.Remove(a.playername);
                xuid.Remove(a.playername);
                job.Remove(a.playername);
                ifban.Remove(a.playername);
                Console.WriteLine("[Draco]{0}离开了游戏,正在保存数据", a.playername);
                return(true);
            });
            api.addBeforeActListener(EventKey.onAttack, x =>
            {
                var a = BaseEvent.getFrom(x) as AttackEvent;
                if (job[a.playername] == "Swordman")
                {
                    int ti = 1;
                    if (level[a.playername] < 20)
                    {
                        ti = 1;
                    }
                    if (level[a.playername] < 50 & level[a.playername] >= 20)
                    {
                        ti = 2;
                    }
                    if (level[a.playername] > 50)
                    {
                        ti = 3;
                    }
                    Random r = new Random();
                    if (r.Next(100) < level[a.playername] * 2)//随机概率
                    {
                        try
                        {
                            if (Convert.ToInt32(level[a.playername] * addition[a.playername]) != 0)
                            {
                                api.runcmd("effect \"" + a.playername + "\" strength " + Convert.ToInt32(level[a.playername] * addition[a.playername]) + " " + ti + " true");
                            }
                        }
                        catch { Console.WriteLine("WARNING!!!"); }
                    }
                }
                return(true);
            });
            api.addBeforeActListener(EventKey.onDestroyBlock, x =>
            {
                var a = BaseEvent.getFrom(x) as DestroyBlockEvent;
                if (job[a.playername] == "Miner")
                {
                    int ti = 1;
                    if (level[a.playername] < 20)
                    {
                        ti = 1;
                    }
                    if (level[a.playername] < 50 & level[a.playername] >= 20)
                    {
                        ti = 2;
                    }
                    if (level[a.playername] > 50)
                    {
                        ti = 3;
                    }
                    Random r = new Random();
                    if (r.Next(100) < level[a.playername] * 2)
                    {
                        try
                        {
                            if (Convert.ToInt32(level[a.playername] * addition[a.playername]) != 0) //防止0级
                            {
                                api.runcmd("effect \"" + a.playername + "\" haste " + Convert.ToInt32(level[a.playername] * addition[a.playername]) + " " + ti + " true");
                            }
                        }
                        catch { Console.WriteLine("WARNING!!!"); }
                    }
                }
                return(true);
            });
            api.addBeforeActListener(EventKey.onMobDie, x =>
            {
                var a           = BaseEvent.getFrom(x) as MobDieEvent;
                string str      = a.mobtype;
                string[] sArray = str.Split(new char[2] {
                    '.', '.'
                });
                YML yml = new YML(MoneyPath);
                if (yml.read("DeBug", "") == "true")
                {
                    Console.WriteLine(sArray[1]);
                }                                                                      //是否在控制台输出
                if (yml.read(sArray[1], "") != null & a.srcname != "")
                {
                    api.runcmd("scoreboard players add \"" + a.srcname + "\" money " + yml.read(sArray[1], ""));
                    api.runcmd("tellraw \"" + a.srcname + "\" {\"rawtext\":[{\"text\":\"§6击杀奖励" + yml.read(sArray[1], "") + "金币!\"}]}");
                    int z = api.getscoreboard(uuid[a.srcname], "money");
                    api.removePlayerSidebar(uuid[a.srcname]);
                    api.setPlayerSidebar(uuid[a.srcname], "个人信息", "[\"玩家名称:" + a.srcname + "\",\"职业:" + job[a.srcname] + "\",\"等级:" + level[a.srcname] + "\",\"金币:" + z + "\"]");
                    if (job[a.srcname] == "Hunter") //猎手加成
                    {
                        int ti = 1;
                        if (level[a.srcname] < 20)
                        {
                            ti = 5;
                        }
                        if (level[a.srcname] < 50 & level[a.srcname] >= 20)
                        {
                            ti = 3;
                        }
                        if (level[a.srcname] > 50)
                        {
                            ti = 2;
                        }
                        int b = Convert.ToInt32(yml.read(sArray[1], "")) / ti;
                        if (b != 0)
                        {
                            api.runcmd("scoreboard players add \"" + a.srcname + "\" money " + b);
                            api.runcmd("tellraw \"" + a.srcname + "\" {\"rawtext\":[{\"text\":\"[§3猎手加成§r]§6 额外奖励" + b + "金币!\"}]}");
                            var t = Task.Run(async delegate
                            {
                                await Task.Delay(500);
                                int v = api.getscoreboard(uuid[a.srcname], "money");
                                api.removePlayerSidebar(uuid[a.srcname]);
                                api.setPlayerSidebar(uuid[a.srcname], "个人信息", "[\"玩家名称:" + a.srcname + "\",\"职业:" + job[a.srcname] + "\",\"等级:" + level[a.srcname] + "\",\"金币:" + v + "\"]");
                            });
                        }
                    }
                }
                return(true);
            });
            api.addBeforeActListener(EventKey.onInputText, x =>
            {
                var a          = BaseEvent.getFrom(x) as InputTextEvent;
                string[] diam  = { "§2主世界", "§4地狱", "§e末地" };
                string[] diam2 = { "主世界", "地狱", "末地" };
                if (a.msg == "here")
                {
                    api.runcmd("tellraw @a {\"rawtext\":[{\"text\":\"[" + diam[a.dimensionid] + "§r] §3" + a.playername + "§r 共享了一个坐标 >> " + Convert.ToInt32(a.XYZ.x) + " " + Convert.ToInt32(a.XYZ.y) + " " + Convert.ToInt32(a.XYZ.z) + "\"}]}");
                    Console.WriteLine("!{0} 共享了坐标:位于{4}的 {1},{2},{3} ", a.playername, Convert.ToInt32(a.XYZ.x), Convert.ToInt32(a.XYZ.y), Convert.ToInt32(a.XYZ.z), diam2[a.dimensionid]);
                    return(false);
                }
                if (a.msg == "levelup" & job[a.playername] != "无职业")
                {
                    int b = api.getscoreboard(uuid[a.playername], "money");
                    int c = level[a.playername] * 100;
                    if (b >= c)
                    {
                        Playerconfig write = new Playerconfig();
                        write.Addition     = addition[a.playername];
                        write.Name         = a.playername;
                        write.level        = level[a.playername] + 1;
                        write.job          = job[a.playername];
                        string str         = JsonConvert.SerializeObject(write);//这里修改等级我是直接重写覆盖,因为8会修改(
                        File.WriteAllText("./plugins/Draco/" + job[a.playername] + "/" + xuid[a.playername] + ".json", str, System.Text.Encoding.Default);
                        level[a.playername] += 1;
                        api.runcmd("scoreboard players remove \"" + a.playername + "\" money " + c);
                        api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§b成功升到了" + level[a.playername] + "级!花费了" + c + "金币!\"}]}");
                        var t = Task.Run(async delegate
                        {
                            await Task.Delay(500);
                            int tt = api.getscoreboard(uuid[a.playername], "money");
                            api.setPlayerSidebar(uuid[a.playername], "个人信息", "[\"玩家名称:" + a.playername + "\",\"职业:" + job[a.playername] + "\",\"等级:" + level[a.playername] + "\",\"金币:" + tt + "\"]");
                        });
                    }
                    else
                    {
                        api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§4金币不足!距离" + (level[a.playername] + 1) + "级还需要" + c + "金币!\"}]}");
                        api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§4当前金币:" + b + "\"}]}");
                    }
                }
                else
                {
                    api.runcmd("tellraw @a {\"rawtext\":[{\"text\":\"[" + diam[a.dimensionid] + "§r][§3" + level[a.playername] + "级" + job[a.playername] + "§r " + a.playername + "] >> " + a.msg + "\"}]}");
                    Console.WriteLine("![{2} {0}] >> {1}", a.playername, a.msg, job[a.playername]);
                }
                return(false);
            });
            api.addBeforeActListener(EventKey.onUseItem, x =>
            {
                var a = BaseEvent.getFrom(x) as UseItemEvent;
                int b = api.getscoreboard(uuid[a.playername], "money");
                if (a.itemid == 345 & job[a.playername] == "Sorcerer" & level[a.playername] >= 20)
                {
                    int ti = 5;
                    if (level[a.playername] < 50 & level[a.playername] >= 20)
                    {
                        ti = 10;
                    }
                    if (level[a.playername] > 50)
                    {
                        ti = 15;
                    }
                    if (b > 500)
                    {
                        api.runcmd("effect \"" + a.playername + "\" invisibility " + ti + " 1 true");
                        api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§3发动技能成功!\"}]}");
                        api.runcmd("scoreboard players remove " + a.playername + " money 500 ");
                    }
                    else
                    {
                        api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§4金币不足!!至少需要500金币来发动技能!\"}]}");
                    }
                }
                if (a.itemid == 345 & job[a.playername] == "Miner" & level[a.playername] >= 20)
                {
                    int ti = 5;
                    if (level[a.playername] < 50 & level[a.playername] >= 20)
                    {
                        ti = 10;
                    }
                    if (level[a.playername] > 50)
                    {
                        ti = 15;
                    }
                    if (b > 500)
                    {
                        api.runcmd("effect \"" + a.playername + "\" haste " + ti + " 3 true");
                        api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§3发动技能成功!\"}]}");
                        api.runcmd("scoreboard players remove " + a.playername + " money 500 ");
                    }
                    else
                    {
                        api.runcmd("tellraw \"" + a.playername + "\" {\"rawtext\":[{\"text\":\"§4金币不足!!至少需要500金币来发动技能!\"}]}");
                    }
                }
                return(true);
            });
        }
예제 #4
0
        public static void init(MCCSAPI api)
        {
            mcapi = api;
            Console.OutputEncoding = Encoding.UTF8;
            // 后台指令监听
            api.addBeforeActListener(EventKey.onServerCmd, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var se = BaseEvent.getFrom(x) as ServerCmdEvent;
                if (se != null)
                {
                    Console.WriteLine("后台指令={0}", se.cmd);
                }
                return(true);
            });
            // 后台指令输出监听
            api.addBeforeActListener(EventKey.onServerCmdOutput, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var se = BaseEvent.getFrom(x) as ServerCmdOutputEvent;
                if (se != null)
                {
                    Console.WriteLine("后台指令输出={0}", se.output);
                }
                return(true);
            });
            // 表单选择监听
            api.addAfterActListener(EventKey.onFormSelect, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var fe = BaseEvent.getFrom(x) as FormSelectEvent;
                if (fe == null)
                {
                    return(true);
                }
                if (fe.formid != tformid)
                {
                    return(true);
                }
                if (fe.selected != "null")
                {
                    Console.WriteLine("玩家 {0} 选择了表单 id={1} ,selected={2}", fe.playername, fe.formid, fe.selected);
                    CsPlayer pl = new CsPlayer(api, fe.playerPtr);
                    if (fe.selected == "0")
                    {
                        ulong bkid = pl.UniqueId;
                        // 根据流水号反查,可能是实体,也可能是玩家
                        var d = CsActor.getFromUniqueId(api, bkid) as CsPlayer;
                        if (d != null)
                        {
                            Console.WriteLine("UniqueId反查成功。");
                            pl = d;
                        }
                        // 常规组件测试
                        Console.WriteLine("玩家攻击力组件:{0},玩家碰撞箱组件:{1},玩家生命值组件:{2},玩家位置组件:{3},玩家转角组件:{4}" +
                                          ",玩家所处维度:{5},玩家实体类型:{6},玩家查询流水号:{7},玩家UUID:{8},玩家名字:{9},玩家计分板ID:{10}",
                                          pl.Attack, pl.CollisionBox, pl.Health, pl.Position, pl.Rotation, pl.DimensionId, pl.TypeId, pl.UniqueId, pl.Uuid, pl.getName(),
                                          pl.getScoreboardId() /* , pl.createScoreboardId() */);
                    }
                    else if (fe.selected == "1")
                    {
                        // 物品栏测试
                        Console.WriteLine("实体装备栏:{0},实体主副手栏:{1},实体背包栏:{2},实体热键栏:{3}",
                                          pl.ArmorContainer, pl.HandContainer, pl.InventoryContainer, pl.HotbarContainer);
                    }
                    else if (fe.selected == "2")
                    {
                        // 组件设置测试
                        JavaScriptSerializer ser = new JavaScriptSerializer();
                        var atta          = ser.Deserialize <Dictionary <string, object> >(pl.Attack);
                        atta["range_min"] = Convert.ToSingle(atta["range_min"]) + 4;
                        atta["range_max"] = Convert.ToSingle(atta["range_max"]) + 4;
                        pl.Attack         = ser.Serialize(atta);
                        Console.WriteLine("玩家攻击力将+4");
                        var acb       = ser.Deserialize <Dictionary <string, object> >(pl.CollisionBox);
                        acb["width"]  = Convert.ToSingle(acb["width"]) + 1;
                        acb["height"] = Convert.ToSingle(acb["height"]) + 1;
                        //pl.CollisionBox = ser.Serialize(acb);
                        //Console.WriteLine("玩家碰撞箱宽和高的值将+1格");
                        var ahe      = ser.Deserialize <Dictionary <string, object> >(pl.Health);
                        ahe["max"]   = Convert.ToSingle(ahe["max"]) + 10;
                        ahe["value"] = Convert.ToSingle(ahe["value"]) + 10;
                        pl.Health    = ser.Serialize(ahe);
                        Console.WriteLine("玩家当前和最大生命值将+10点");
                        var prex        = "[前缀]";
                        var pname       = pl.getName();
                        bool alwaysshow = false;
                        if (pname.IndexOf(prex) == 0)
                        {
                            pname      = pname.Substring(prex.Length);
                            alwaysshow = true;
                        }
                        else
                        {
                            pname      = prex + pname;
                            alwaysshow = false;
                        }
                        pl.setName(pname, alwaysshow);
                        Console.WriteLine("玩家名字将添加/删除前缀,去掉/恢复常显");
                        var apos  = ser.Deserialize <Dictionary <string, object> >(pl.Position);
                        apos["x"] = Convert.ToSingle(apos["x"]) + 16;
                        apos["y"] = Convert.ToSingle(apos["y"]) + 10;
                        apos["z"] = Convert.ToSingle(apos["z"]) + 16;
                        //pl.Position = ser.Serialize(apos);
                        //Console.WriteLine("玩家将位移至当前位置的(+16,+10,+16)上。");
                        var arot    = ser.Deserialize <Dictionary <string, object> >(pl.Rotation);
                        arot["x"]   = Convert.ToSingle(arot["x"]) + 16;
                        arot["y"]   = Convert.ToSingle(arot["y"]) + 16;
                        pl.Rotation = ser.Serialize(arot);
                        Console.WriteLine("玩家俯角+16,转角+16");
                    }
                    else if (fe.selected == "3")
                    {
                        var el = CsActor.getsFromAABB(api, fe.dimensionid, fe.XYZ.x - 16, fe.XYZ.y - 16, fe.XYZ.z - 16,
                                                      fe.XYZ.x + 16, fe.XYZ.y + 16, fe.XYZ.z + 16);
                        if (el != null && el.Count > 0)
                        {
                            Console.WriteLine("查询并移除玩家附近16格内所有实体:");
                            foreach (IntPtr eptr in el)
                            {
                                var cse = new CsActor(api, eptr);
                                Console.WriteLine("TypeId={0},UniqueId={1},name={2}", cse.TypeId, cse.UniqueId, cse.getName());
                                cse.remove();
                            }
                        }
                    }
                    else if (fe.selected == "4")
                    {
                        var el = CsActor.getsFromAABB(api, fe.dimensionid, fe.XYZ.x - 16, fe.XYZ.y - 16, fe.XYZ.z - 16,
                                                      fe.XYZ.x + 16, fe.XYZ.y + 16, fe.XYZ.z + 16);
                        var plst = CsPlayer.getplFromAABB(api, fe.dimensionid, fe.XYZ.x - 16, fe.XYZ.y - 16, fe.XYZ.z - 16,
                                                          fe.XYZ.x + 16, fe.XYZ.y + 16, fe.XYZ.z + 16);
                        el   = el == null ? new ArrayList() : el;
                        plst = plst == null ? new ArrayList() : plst;
                        el.AddRange(plst);
                        if (el.Count > 0)
                        {
                            Console.WriteLine("查询并模拟攻击玩家附近16格内所有实体和玩家:");
                            foreach (IntPtr eptr in el)
                            {
                                var cse = new CsActor(api, eptr);
                                Console.WriteLine("TypeId={0},UniqueId={1},name={2}", cse.TypeId, cse.UniqueId, cse.getName());
                                // 测试实体模拟受攻击伤害,伤害值为10
                                cse.hurt(fe.playerPtr /*IntPtr.Zero*/, ActorDamageCause.EntityAttack, 10, false, false);
                            }
                        }
                    }
                    else if (fe.selected == "5")
                    {
                        pl.addLevel(3);
                    }
                    else if (fe.selected == "6")
                    {
                        pl.teleport(100, 100, 100);
                    }
                    else if (fe.selected == "7")
                    {
                        api.setSideBar(pl.Uuid, "这就是内容不能换行");
                    }
                }
                else
                {
                    Console.WriteLine("玩家 {0} 取消了表单 id={1}", fe.playername, fe.formid);
                }
                return(false);
            });
            // 使用物品监听
            api.addAfterActListener(EventKey.onUseItem, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as UseItemEvent;
                if (ue != null && ue.RESULT)
                {
                    Console.WriteLine("玩家 {0} 对 {1} 的 ({2}, {3}, {4}) 处的 {5} 方块" +
                                      "操作了 {6} 物品。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname, ue.itemname);
                }
                return(true);
            });
            // 放置方块监听
            api.addAfterActListener(EventKey.onPlacedBlock, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as PlacedBlockEvent;
                if (ue != null && ue.RESULT)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处放置了 {5} 方块。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 破坏方块监听
            api.addBeforeActListener(EventKey.onDestroyBlock, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as DestroyBlockEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处破坏 {5} 方块。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 开箱监听
            api.addBeforeActListener(EventKey.onStartOpenChest, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StartOpenChestEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处打开 {5} 箱子。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 开桶监听
            api.addBeforeActListener(EventKey.onStartOpenBarrel, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StartOpenBarrelEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处打开 {5} 木桶。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 关箱监听
            api.addAfterActListener(EventKey.onStopOpenChest, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StopOpenChestEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处关闭 {5} 箱子。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 关桶监听
            api.addAfterActListener(EventKey.onStopOpenBarrel, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StopOpenBarrelEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处关闭 {5} 木桶。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 放入取出监听
            api.addAfterActListener(EventKey.onSetSlot, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as SetSlotEvent;
                if (e != null)
                {
                    if (e.itemcount > 0)
                    {
                        Console.WriteLine("玩家 {0} 在 {1} 槽放入了 {2} 个 {3} 物品。",
                                          e.playername, e.slot, e.itemcount, e.itemname);
                    }
                    else
                    {
                        Console.WriteLine("玩家 {0} 在 {1} 槽取出了物品。",
                                          e.playername, e.slot);
                    }
                }
                return(true);
            });
            // 切换维度监听
            api.addAfterActListener(EventKey.onChangeDimension, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ChangeDimensionEvent;
                if (e != null && e.RESULT)
                {
                    Console.WriteLine("玩家 {0} {1} 切换维度至 {2} 的 ({3},{4},{5}) 处。",
                                      e.playername, e.isstand ? "" : "悬空地", e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z);
                }
                return(true);
            });
            // 生物死亡监听
            api.addAfterActListener(EventKey.onMobDie, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as MobDieEvent;
                if (e != null && !string.IsNullOrEmpty(e.mobname))
                {
                    Console.WriteLine(" {0} 在 {1} ({2:F2},{3:F2},{4:F2}) 处被 {5} 杀死了。",
                                      e.mobname, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z, e.srcname);
                }
                return(true);
            });
            // 玩家重生监听
            api.addAfterActListener(EventKey.onRespawn, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as RespawnEvent;
                if (e != null && e.RESULT)
                {
                    Console.WriteLine("玩家 {0} 已于 {1} 的 ({2:F2},{3:F2},{4:F2}) 处重生。",
                                      e.playername, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z);
                }
                return(true);
            });
            // 聊天监听
            api.addAfterActListener(EventKey.onChat, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ChatEvent;
                if (e != null)
                {
                    Console.WriteLine(" {0} {1} 说:{2}", e.playername,
                                      !string.IsNullOrEmpty(e.target) ? "悄悄地对 " + e.target : "", e.msg);
                }
                return(true);
            });
            // 输入文本监听
            api.addBeforeActListener(EventKey.onInputText, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as InputTextEvent;
                if (e != null)
                {
                    Console.WriteLine(" <{0}> {1}", e.playername, e.msg);
                }
                return(true);
            });
            // 输入指令监听
            api.addBeforeActListener(EventKey.onInputCommand, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as InputCommandEvent;
                if (e != null)
                {
                    Console.WriteLine(" <{0}> {1}", e.playername, e.cmd);
                }
                return(true);
            });

            // 世界范围爆炸监听,拦截
            api.addBeforeActListener(EventKey.onLevelExplode, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as LevelExplodeEvent;
                if (e != null)
                {
                    Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图发生强度 {5} 的爆炸。",
                                      e.dimension, e.position.x, e.position.y, e.position.z,
                                      string.IsNullOrEmpty(e.entity) ? e.blockname : e.entity, e.explodepower);
                }
                return(false);
            });
            // 玩家切换装备监听
            api.addAfterActListener(EventKey.onEquippedArmor, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as EquippedArmorEvent;
                if (e != null)
                {
                    Console.WriteLine("玩家 {0} 已于 {1} 的 ({2:F2},{3:F2},{4:F2}) 处切换 {5} 第 {6} 格的装备为 {7} 。",
                                      e.playername, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z,
                                      e.slottype == 0 ? "身体装备的" : "主副手的", e.slot, e.itemname);
                }
                return(true);
            });
            // 玩家升级监听
            api.addAfterActListener(EventKey.onLevelUp, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as LevelUpEvent;
                if (e != null)
                {
                    Console.WriteLine("玩家 {0} 已于 {1} 的 ({2:F2},{3:F2},{4:F2}) 处等级提升了 {5} 级。",
                                      e.playername, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z,
                                      e.lv);
                }
                return(true);
            });
            // 活塞推方块监听
            api.addBeforeActListener(EventKey.onPistonPush, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as PistonPushEvent;
                if (e != null)
                {
                    Console.WriteLine("活塞 {0} 于 {1} 的({2}, {3}, {4})处试图向 {5} 号方向推拽({6}, {7}, {8})处的 {9} 方块。",
                                      e.blockname, e.dimension, e.position.x, e.position.y, e.position.z,
                                      e.direction, e.targetposition.x, e.targetposition.y, e.position.z, e.targetblockname);
                    return(true);
                }
                return(true);
            });
            // 箱子合并监听
            api.addAfterActListener(EventKey.onChestPair, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ChestPairEvent;
                if (e != null)
                {
                    Console.WriteLine("箱子 {0} 于 {1} 的({2}, {3}, {4})处试图合并向({5}, {6}, {7})处的 {8} 箱子。",
                                      e.blockname, e.dimension, e.position.x, e.position.y, e.position.z, e.targetposition.x, e.targetposition.y,
                                      e.position.z, e.targetblockname);
                    return(true);
                }
                return(true);
            });

            /*
             *          api.addBeforeActListener(EventKey.onMobSpawnCheck, x =>
             *          {
             *                  Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
             *                  var e = BaseEvent.getFrom(x) as MobSpawnCheckEvent;
             *                  if (e != null)
             *                  {
             *                          Console.WriteLine("生物 {0} 于 {1} 的({2}, {3}, {4})处试图检查生成规则。",
             *                                  e.mobtype, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z);
             *                          return true;
             *                  }
             *                  return true;
             *          });
             *          // 玩家移动监听
             *          api.addAfterActListener(EventKey.onMove, x => {
             *                  var e = BaseEvent.getFrom(x) as MoveEvent;
             *                  if (e != null) {
             *                          Console.WriteLine("玩家 {0} {1} 移动至 {2} ({3},{4},{5}) 处。",
             *                                  e.playername, (e.isstand) ? "":"悬空地", e.dimension,
             *                                  e.XYZ.x, e.XYZ.y, e.XYZ.z);
             *                  }
             *                  return false;
             *          });
             */
            // 玩家加入游戏监听
            api.addAfterActListener(EventKey.onLoadName, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as LoadNameEvent;
                if (ue != null)
                {
                    CsPlayer p = new CsPlayer(api, ue.playerPtr);
                    Console.WriteLine("玩家 {0} 加入了游戏,xuid={1}, IP={2}", ue.playername, ue.xuid, p.IpPort);
                }
                return(true);
            });
            // 玩家离开游戏监听
            api.addAfterActListener(EventKey.onPlayerLeft, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as PlayerLeftEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 离开了游戏,xuid={1}", ue.playername, ue.xuid);
                }
                return(true);
            });

            // 攻击监听
            // API 方式注册监听器
            api.addAfterActListener(EventKey.onAttack, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                AttackEvent ae = BaseEvent.getFrom(x) as AttackEvent;
                if (ae != null)
                {
                    string str = "玩家 " + ae.playername + " 在 (" + ae.XYZ.x.ToString("F2") + "," +
                                 ae.XYZ.y.ToString("F2") + "," + ae.XYZ.z.ToString("F2") + ") 处攻击了 " + ae.actortype + " 。";
                    Console.WriteLine(str);
                    // 社区api测试
                    api.setServerMotd(ae.playername + "发动了攻击", true);
                    //Console.WriteLine("list={0}", api.getOnLinePlayers())
                    CsPlayer p = new CsPlayer(api, ae.playerPtr);
                    var uuid   = p.Uuid;
                    if (uuid != null)
                    {
                        tformid = api.sendSimpleForm((string)uuid,
                                                     "测试选项",
                                                     "test choose:",
                                                     "[\"基本组件\",\"物品栏组件\",\"组件设置\", \"范围检测并清理\",\"范围检测并攻击\"," +
                                                     "\"玩家等级+3\",\"测试传送\",\"积分版测试\" ]");
                        Console.WriteLine("创建需自行保管的表单,id={0}", tformid);
                        // 非社区内容测试
                        if (api.COMMERCIAL)
                        {
                            CsActor ac = new CsActor(api, ae.attackedentityPtr);
                            Console.WriteLine("目标实体的能力值:" + ac.Abilities + "\n目标实体的属性列表:" + ac.Attributes
                                              + "\n目标实体的最大属性列表:" + ac.MaxAttributes + "\n目标实体的所有状态效果列表:" + ac.Effects);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Event convent fail.");
                }
                return(true);
            });

            //玩家捡物品事件 - 可拦截
            api.addBeforeActListener(EventKey.onPickUpItem, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                PickUpItemEvent ae = BaseEvent.getFrom(x) as PickUpItemEvent;
                if (ae != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}) 捡起了 {3} 物品。", ae.playername, ae.dimension, ae.XYZ.x.ToString("F2") + "," +
                                      ae.XYZ.y.ToString("F2") + "," + ae.XYZ.z.ToString("F2"), ae.itemname);
                }
                return(true);
            });

            //玩家丢物品事件 - 可拦截
            api.addBeforeActListener(EventKey.onDropItem, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                DropItemEvent ae = BaseEvent.getFrom(x) as DropItemEvent;
                if (ae != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}) 丢下了 {3} 物品。", ae.playername, ae.dimension, ae.XYZ.x.ToString("F2") + "," +
                                      ae.XYZ.y.ToString("F2") + "," + ae.XYZ.z.ToString("F2"), ae.itemname);
                }
                return(true);
            });
            // 计分板数值改变事件
            api.addAfterActListener(EventKey.onScoreChanged, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                ScoreChangedEvent ae = BaseEvent.getFrom(x) as ScoreChangedEvent;
                if (ae != null)
                {
                    Console.WriteLine("计分板 {0} (显示名称:{1},id:{2})分数改变为 {3}",
                                      ae.objectivename, ae.displayname, ae.scoreboardid, ae.score);
                    // 追加改变,由于会重复触发改变事件,故设定停止上限
                    //if (ae.score < 100)
                    //{
                    //	Console.WriteLine("启动30秒后增加分数任务,请耐心等待测试反馈..");
                    //	// 多线程情况下测试离线计分板id是否有效,等待三十秒
                    //	new Thread(() =>
                    //	{
                    //		Thread.Sleep(30000);
                    //		Console.WriteLine("追加改变增加100,数值变为:" + api.setscoreById(ae.scoreboardid, ae.objectivename, ae.score + 100));
                    //		Console.WriteLine("改变后的分数为:" + api.getscoreById(ae.scoreboardid, ae.objectivename));
                    //	}).Start();
                    //}
                }
                return(true);
            });
            // 官方脚本引擎初始化监听
            api.addAfterActListener(EventKey.onScriptEngineInit, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                ScriptEngineInitEvent ae = BaseEvent.getFrom(x) as ScriptEngineInitEvent;
                if (ae != null && ae.RESULT)
                {
                    Console.WriteLine("脚本引擎已初始化成功,addr={0}",
                                      ae.jseptr);
                    // 延时1s载入外置行为包脚本;延时3s发送一个自定义事件;延时1s载入一段临时脚本
                    new Thread(() =>
                    {
                        Thread.Sleep(1000);
                        try
                        {       // 测试临时行为包脚本注意事项:runScript情况下不会经过 initialize 调用,需主动设置初始化
                            string js = File.ReadAllText("test.js");
                            api.JSErunScript(js, (r) =>
                            {
                                if (r)
                                {
                                    Console.WriteLine("外置测试行为包脚本载入成功。");
                                }
                            });
                        }
                        catch { }
                        Thread.Sleep(3000);
                        string jdata = new JavaScriptSerializer().Serialize(new { text = "这是一个自定义测试消息", num = 2021 });
                        api.JSEfireCustomEvent("mytest:testevent", jdata, (r) =>
                        {
                            if (r)
                            {
                                Console.WriteLine("自定义事件广播发送成功。");
                            }
                        });
                        Thread.Sleep(1000);
                        api.JSErunScript("var d = 100;\n console.log('这是一个临时测试脚本')", (r) =>
                        {
                            if (r)
                            {
                                Console.WriteLine("测试临时脚本执行成功。");
                            }
                        });
                    }).Start();
                }
                return(true);
            });
            // 官方脚本引擎接收日志输出信息监听,拦截
            api.addBeforeActListener(EventKey.onScriptEngineLog, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                ScriptEngineLogEvent ae = BaseEvent.getFrom(x) as ScriptEngineLogEvent;
                if (ae != null)
                {
                    Console.WriteLine("[来自脚本的LOG输出] {0}",
                                      ae.log);
                }
                return(false);
            });
            // 官方脚本引擎执行指令,或可拦截
            api.addBeforeActListener(EventKey.onScriptEngineCmd, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                ScriptEngineCmdEvent ae = BaseEvent.getFrom(x) as ScriptEngineCmdEvent;
                if (ae != null)
                {
                    Console.WriteLine("[脚本引擎试图执行指令] {0}",
                                      ae.cmd);
                }
                return(true);
            });
            // 生物伤害监听
            api.addBeforeActListener(EventKey.onMobHurt, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as MobHurtEvent;
                if (e != null && !string.IsNullOrEmpty(e.mobname))
                {
                    Console.WriteLine(" {0} 在 {1} ({2:F2},{3:F2},{4:F2}) 即将受到来自 {5} 的 {6} 点伤害,类型 {7}",
                                      e.mobname, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z, e.srcname, e.dmcount, e.dmtype);
                }
                return(true);
            });
            api.addAfterActListener(EventKey.onScoreboardInit, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ScoreboardInitEvent;
                if (e != null)
                {
                    Console.WriteLine("系统计分板已初始化成功,addr={0}",
                                      e.scptr);
                    //api.postTick(() => {
                    //	api.runcmd("list");
                    //});
                    //if (api.COMMERCIAL){   // 测试计分板读取和写入任务
                    //	string asc = api.getAllScore();
                    //	Console.WriteLine("[TEST]scoreboard={0}", asc);
                    //	if (!string.IsNullOrEmpty(asc)){
                    //		Console.WriteLine("启动一个延时30秒重置计分板的任务,请耐心等待信息反馈..");
                    //		new Thread(() =>{
                    //			Thread.Sleep(30000);
                    //			if (api.setAllScore(asc)){
                    //				Console.WriteLine("重置任务已发送。");
                    //			}
                    //		}).Start();
                    //	}
                    //}
                }
                return(true);
            });
            #region 非社区部分内容
            if (api.COMMERCIAL)
            {
                // 命令块执行指令监听,拦截
                api.addBeforeActListener(EventKey.onBlockCmd, x =>
                {
                    Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                    var e = BaseEvent.getFrom(x) as BlockCmdEvent;
                    if (e != null)
                    {
                        Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图执行指令 {5}",
                                          e.dimension, e.position.x, e.position.y, e.position.z, e.name, e.cmd);
                    }
                    return(false);
                });
                // NPC执行指令监听,拦截
                api.addBeforeActListener(EventKey.onNpcCmd, x =>
                {
                    Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                    var e = BaseEvent.getFrom(x) as NpcCmdEvent;
                    if (e != null)
                    {
                        Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图执行第 {5} 条指令,指令集\n{6}",
                                          e.dimension, e.position.x, e.position.y, e.position.z, e.npcname, e.actionid, e.actions);
                    }
                    return(false);
                });
                // 更新命令方块监听
                api.addBeforeActListener(EventKey.onCommandBlockUpdate, x =>
                {
                    Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                    var e = BaseEvent.getFrom(x) as CommandBlockUpdateEvent;
                    if (e != null)
                    {
                        Console.WriteLine(" {0} 试图修改位于 {1} ({2},{3},{4}) 的 {5} 的命令为 {6}",
                                          e.playername, e.dimension, e.position.x, e.position.y, e.position.z,
                                          e.isblock ? "命令块" : "命令矿车", e.cmd);
                    }
                    return(true);
                });
            }
            #endregion


            // Json 解析部分 使用JavaScriptSerializer序列化Dictionary或array即可

            //JavaScriptSerializer ser = new JavaScriptSerializer();
            //var data = ser.Deserialize<Dictionary<string, object>>("{\"x\":9}");
            //var ary = ser.Deserialize<ArrayList>("[\"x\",\"y\"]");
            //Console.WriteLine(data["x"]);
            //foreach(string v in ary) {
            //	Console.WriteLine(v);
            //}
            //data["y"] = 8;
            //string dstr = ser.Serialize(data);
            //Console.WriteLine(dstr);

            // 高级玩法,硬编码方式注册hook
            THook.init(api);
        }
예제 #5
0
        public static void init(MCCSAPI api)
        {
            mcapi = api;
            Console.OutputEncoding = Encoding.UTF8;
            // 后台指令监听
            api.addBeforeActListener(EventKey.onServerCmd, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var se = BaseEvent.getFrom(x) as ServerCmdEvent;
                if (se != null)
                {
                    Console.WriteLine("后台指令={0}", se.cmd);
                }
                return(true);
            });
            // 后台指令输出监听
            api.addBeforeActListener(EventKey.onServerCmdOutput, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var se = BaseEvent.getFrom(x) as ServerCmdOutputEvent;
                if (se != null)
                {
                    Console.WriteLine("后台指令输出={0}", se.output);
                }
                return(true);
            });
            // 表单选择监听
            api.addAfterActListener(EventKey.onFormSelect, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var fe = BaseEvent.getFrom(x) as FormSelectEvent;
                if (fe == null)
                {
                    return(true);
                }
                if (fe.formid != tformid)
                {
                    return(true);
                }
                if (fe.selected != "null")
                {
                    Console.WriteLine("玩家 {0} 选择了表单 id={1} ,selected={2}", fe.playername, fe.formid, fe.selected);
                    CsPlayer pl = new CsPlayer(api, fe.playerPtr);
                    if (fe.selected == "0")
                    {
                        ulong bkid = pl.UniqueId;
                        // 根据流水号反查,可能是实体,也可能是玩家
                        var d = CsActor.getFromUniqueId(api, bkid) as CsPlayer;
                        if (d != null)
                        {
                            Console.WriteLine("UniqueId反查成功。");
                            pl = d;
                        }
                        // 常规组件测试
                        Console.WriteLine("玩家攻击力组件:{0},玩家碰撞箱组件:{1},玩家生命值组件:{2},玩家位置组件:{3},玩家转角组件:{4}" +
                                          ",玩家所处维度:{5},玩家实体类型:{6},玩家查询流水号:{7},玩家UUID:{8},玩家名字:{9}",
                                          pl.Attack, pl.CollisionBox, pl.Health, pl.Position, pl.Rotation, pl.DimensionId, pl.TypeId, pl.UniqueId, pl.Uuid, pl.getName());
                    }
                    else if (fe.selected == "1")
                    {
                        // 物品栏测试
                        Console.WriteLine("实体装备栏:{0},实体主副手栏:{1},实体背包栏:{2},实体热键栏:{3}",
                                          pl.ArmorContainer, pl.HandContainer, pl.InventoryContainer, pl.HotbarContainer);
                    }
                    else if (fe.selected == "2")
                    {
                        // 组件设置测试
                        JavaScriptSerializer ser = new JavaScriptSerializer();
                        var atta          = ser.Deserialize <Dictionary <string, object> >(pl.Attack);
                        atta["range_min"] = Convert.ToSingle(atta["range_min"]) + 4;
                        atta["range_max"] = Convert.ToSingle(atta["range_max"]) + 4;
                        pl.Attack         = ser.Serialize(atta);
                        Console.WriteLine("玩家攻击力将+4");
                        var acb       = ser.Deserialize <Dictionary <string, object> >(pl.CollisionBox);
                        acb["width"]  = Convert.ToSingle(acb["width"]) + 1;
                        acb["height"] = Convert.ToSingle(acb["height"]) + 1;
                        //pl.CollisionBox = ser.Serialize(acb);
                        //Console.WriteLine("玩家碰撞箱宽和高的值将+1格");
                        var ahe      = ser.Deserialize <Dictionary <string, object> >(pl.Health);
                        ahe["max"]   = Convert.ToSingle(ahe["max"]) + 10;
                        ahe["value"] = Convert.ToSingle(ahe["value"]) + 10;
                        pl.Health    = ser.Serialize(ahe);
                        Console.WriteLine("玩家当前和最大生命值将+10点");
                        var prex        = "[前缀]";
                        var pname       = pl.getName();
                        bool alwaysshow = false;
                        if (pname.IndexOf(prex) == 0)
                        {
                            pname      = pname.Substring(prex.Length);
                            alwaysshow = true;
                        }
                        else
                        {
                            pname      = prex + pname;
                            alwaysshow = false;
                        }
                        pl.setName(pname, alwaysshow);
                        Console.WriteLine("玩家名字将添加/删除前缀,去掉/恢复常显");
                        var apos  = ser.Deserialize <Dictionary <string, object> >(pl.Position);
                        apos["x"] = Convert.ToSingle(apos["x"]) + 16;
                        apos["y"] = Convert.ToSingle(apos["y"]) + 10;
                        apos["z"] = Convert.ToSingle(apos["z"]) + 16;
                        //pl.Position = ser.Serialize(apos);
                        //Console.WriteLine("玩家将位移至当前位置的(+16,+10,+16)上。");
                        var arot    = ser.Deserialize <Dictionary <string, object> >(pl.Rotation);
                        arot["x"]   = Convert.ToSingle(arot["x"]) + 16;
                        arot["y"]   = Convert.ToSingle(arot["y"]) + 16;
                        pl.Rotation = ser.Serialize(arot);
                        Console.WriteLine("玩家俯角+16,转角+16");
                    }
                    else if (fe.selected == "3")
                    {
                        var el = CsActor.getsFromAABB(api, fe.dimensionid, fe.XYZ.x - 16, fe.XYZ.y - 16, fe.XYZ.z - 16,
                                                      fe.XYZ.x + 16, fe.XYZ.y + 16, fe.XYZ.z + 16);
                        if (el != null && el.Count > 0)
                        {
                            Console.WriteLine("查询并移除玩家附近16格内所有实体:");
                            foreach (IntPtr eptr in el)
                            {
                                var cse = new CsActor(api, eptr);
                                Console.WriteLine("TypeId={0},UniqueId={1},name={2}", cse.TypeId, cse.UniqueId, cse.getName());
                                cse.remove();
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("玩家 {0} 取消了表单 id={1}", fe.playername, fe.formid);
                }
                return(false);
            });
            // 使用物品监听
            api.addAfterActListener(EventKey.onUseItem, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as UseItemEvent;
                if (ue != null && ue.RESULT)
                {
                    Console.WriteLine("玩家 {0} 对 {1} 的 ({2}, {3}, {4}) 处的 {5} 方块" +
                                      "操作了 {6} 物品。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname, ue.itemname);
                }
                return(true);
            });
            // 放置方块监听
            api.addAfterActListener(EventKey.onPlacedBlock, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as PlacedBlockEvent;
                if (ue != null && ue.RESULT)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处放置了 {5} 方块。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 破坏方块监听
            api.addBeforeActListener(EventKey.onDestroyBlock, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as DestroyBlockEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处破坏 {5} 方块。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 开箱监听
            api.addBeforeActListener(EventKey.onStartOpenChest, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StartOpenChestEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处打开 {5} 箱子。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 开桶监听
            api.addBeforeActListener(EventKey.onStartOpenBarrel, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StartOpenBarrelEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 试图在 {1} 的 ({2}, {3}, {4})" +
                                      " 处打开 {5} 木桶。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 关箱监听
            api.addAfterActListener(EventKey.onStopOpenChest, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StopOpenChestEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处关闭 {5} 箱子。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 关桶监听
            api.addAfterActListener(EventKey.onStopOpenBarrel, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as StopOpenBarrelEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 在 {1} 的 ({2}, {3}, {4})" +
                                      " 处关闭 {5} 木桶。", ue.playername, ue.dimension, ue.position.x, ue.position.y, ue.position.z, ue.blockname);
                }
                return(true);
            });
            // 放入取出监听
            api.addAfterActListener(EventKey.onSetSlot, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as SetSlotEvent;
                if (e != null)
                {
                    if (e.itemcount > 0)
                    {
                        Console.WriteLine("玩家 {0} 在 {1} 槽放入了 {2} 个 {3} 物品。",
                                          e.playername, e.slot, e.itemcount, e.itemname);
                    }
                    else
                    {
                        Console.WriteLine("玩家 {0} 在 {1} 槽取出了物品。",
                                          e.playername, e.slot);
                    }
                }
                return(true);
            });
            // 切换维度监听
            api.addAfterActListener(EventKey.onChangeDimension, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ChangeDimensionEvent;
                if (e != null && e.RESULT)
                {
                    Console.WriteLine("玩家 {0} {1} 切换维度至 {2} 的 ({3},{4},{5}) 处。",
                                      e.playername, e.isstand?"":"悬空地", e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z);
                }
                return(true);
            });
            // 生物死亡监听
            api.addAfterActListener(EventKey.onMobDie, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as MobDieEvent;
                if (e != null && !string.IsNullOrEmpty(e.mobname))
                {
                    Console.WriteLine(" {0} 在 {1} ({2:F2},{3:F2},{4:F2}) 处被 {5} 杀死了。",
                                      e.mobname, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z, e.srcname);
                }
                return(true);
            });
            // 玩家重生监听
            api.addAfterActListener(EventKey.onRespawn, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as RespawnEvent;
                if (e != null && e.RESULT)
                {
                    Console.WriteLine("玩家 {0} 已于 {1} 的 ({2:F2},{3:F2},{4:F2}) 处重生。",
                                      e.playername, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z);
                }
                return(true);
            });
            // 聊天监听
            api.addAfterActListener(EventKey.onChat, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as ChatEvent;
                if (e != null)
                {
                    Console.WriteLine(" {0} {1} 说:{2}", e.playername,
                                      !string.IsNullOrEmpty(e.target) ? "悄悄地对 " + e.target : "", e.msg);
                }
                return(true);
            });
            // 输入文本监听
            api.addBeforeActListener(EventKey.onInputText, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as InputTextEvent;
                if (e != null)
                {
                    Console.WriteLine(" <{0}> {1}", e.playername, e.msg);
                }
                return(true);
            });
            // 输入指令监听
            api.addBeforeActListener(EventKey.onInputCommand, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as InputCommandEvent;
                if (e != null)
                {
                    Console.WriteLine(" <{0}> {1}", e.playername, e.cmd);
                }
                return(true);
            });

            // 世界范围爆炸监听,拦截
            api.addBeforeActListener(EventKey.onLevelExplode, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as LevelExplodeEvent;
                if (e != null)
                {
                    Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图发生强度 {5} 的爆炸。",
                                      e.dimension, e.position.x, e.position.y, e.position.z,
                                      string.IsNullOrEmpty(e.entity) ? e.blockname : e.entity, e.explodepower);
                }
                return(false);
            });
            // 玩家切换装备监听
            api.addAfterActListener(EventKey.onEquippedArmor, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as EquippedArmorEvent;
                if (e != null)
                {
                    Console.WriteLine("玩家 {0} 已于 {1} 的 ({2:F2},{3:F2},{4:F2}) 处切换第 {5} 格的装备为 {6} 。",
                                      e.playername, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z,
                                      e.slot, e.itemname);
                }
                return(true);
            });
            // 玩家升级监听
            api.addAfterActListener(EventKey.onLevelUp, x =>
            {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var e = BaseEvent.getFrom(x) as LevelUpEvent;
                if (e != null)
                {
                    Console.WriteLine("玩家 {0} 已于 {1} 的 ({2:F2},{3:F2},{4:F2}) 处等级提升了 {5} 级。",
                                      e.playername, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z,
                                      e.lv);
                }
                return(true);
            });

            /*
             * // 玩家移动监听
             * api.addAfterActListener(EventKey.onMove, x => {
             *      var e = BaseEvent.getFrom(x) as MoveEvent;
             *      if (e != null) {
             *              Console.WriteLine("玩家 {0} {1} 移动至 {2} ({3},{4},{5}) 处。",
             *                      e.playername, (e.isstand) ? "":"悬空地", e.dimension,
             *                      e.XYZ.x, e.XYZ.y, e.XYZ.z);
             *      }
             *      return false;
             * });
             */
            // 玩家加入游戏监听
            api.addAfterActListener(EventKey.onLoadName, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as LoadNameEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 加入了游戏,xuid={1}", ue.playername, ue.xuid);
                }
                return(true);
            });
            // 玩家离开游戏监听
            api.addAfterActListener(EventKey.onPlayerLeft, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                var ue = BaseEvent.getFrom(x) as PlayerLeftEvent;
                if (ue != null)
                {
                    Console.WriteLine("玩家 {0} 离开了游戏,xuid={1}", ue.playername, ue.xuid);
                }
                return(true);
            });

            // 攻击监听
            // API 方式注册监听器
            api.addAfterActListener(EventKey.onAttack, x => {
                Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                AttackEvent ae = BaseEvent.getFrom(x) as AttackEvent;
                if (ae != null)
                {
                    string str = "玩家 " + ae.playername + " 在 (" + ae.XYZ.x.ToString("F2") + "," +
                                 ae.XYZ.y.ToString("F2") + "," + ae.XYZ.z.ToString("F2") + ") 处攻击了 " + ae.actortype + " 。";
                    Console.WriteLine(str);
                    //Console.WriteLine("list={0}", api.getOnLinePlayers());
                    string ols = api.getOnLinePlayers();
                    if (!string.IsNullOrEmpty(ols))
                    {
                        JavaScriptSerializer ser = new JavaScriptSerializer();
                        ArrayList al             = ser.Deserialize <ArrayList>(ols);
                        object uuid = null;
                        foreach (Dictionary <string, object> p in al)
                        {
                            object name;
                            if (p.TryGetValue("playername", out name))
                            {
                                if ((string)name == ae.playername)
                                {
                                    // 找到
                                    p.TryGetValue("uuid", out uuid);
                                    break;
                                }
                            }
                        }
                        if (uuid != null)
                        {
                            tformid = api.sendSimpleForm((string)uuid,
                                                         "测试选项",
                                                         "test choose:",
                                                         "[\"基本组件\",\"物品栏组件\",\"组件设置\", \"范围检测并清理\"]");
                            Console.WriteLine("创建需自行保管的表单,id={0}", tformid);
                            //api.transferserver((string)uuid, "www.xiafox.com", 19132);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Event convent fail.");
                }
                return(true);
            });
            #region 非社区部分内容
            if (api.COMMERCIAL)
            {
                // 生物伤害监听
                api.addBeforeActListener(EventKey.onMobHurt, x => {
                    Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                    var e = BaseEvent.getFrom(x) as MobHurtEvent;
                    if (e != null && !string.IsNullOrEmpty(e.mobname))
                    {
                        Console.WriteLine(" {0} 在 {1} ({2:F2},{3:F2},{4:F2}) 即将受到来自 {5} 的 {6} 点伤害,类型 {7}",
                                          e.mobname, e.dimension, e.XYZ.x, e.XYZ.y, e.XYZ.z, e.srcname, e.dmcount, e.dmtype);
                    }
                    return(true);
                });
                // 命令块执行指令监听,拦截
                api.addBeforeActListener(EventKey.onBlockCmd, x => {
                    Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                    var e = BaseEvent.getFrom(x) as BlockCmdEvent;
                    if (e != null)
                    {
                        Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图执行指令 {5}",
                                          e.dimension, e.position.x, e.position.y, e.position.z, e.name, e.cmd);
                    }
                    return(false);
                });
                // NPC执行指令监听,拦截
                api.addBeforeActListener(EventKey.onNpcCmd, x => {
                    Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                    var e = BaseEvent.getFrom(x) as NpcCmdEvent;
                    if (e != null)
                    {
                        Console.WriteLine("位于 {0} ({1},{2},{3}) 的 {4} 试图执行第 {5} 条指令,指令集\n{6}",
                                          e.dimension, e.position.x, e.position.y, e.position.z, e.npcname, e.actionid, e.actions);
                    }
                    return(false);
                });
                // 更新命令方块监听
                api.addBeforeActListener(EventKey.onCommandBlockUpdate, x => {
                    Console.WriteLine("[CS] type = {0}, mode = {1}, result= {2}", x.type, x.mode, x.result);
                    var e = BaseEvent.getFrom(x) as CommandBlockUpdateEvent;
                    if (e != null)
                    {
                        Console.WriteLine(" {0} 试图修改位于 {1} ({2},{3},{4}) 的 {5} 的命令为 {6}",
                                          e.playername, e.dimension, e.position.x, e.position.y, e.position.z,
                                          e.isblock ? "命令块" : "命令矿车", e.cmd);
                    }
                    return(true);
                });
            }
            #endregion


            // Json 解析部分 使用JavaScriptSerializer序列化Dictionary或array即可

            //JavaScriptSerializer ser = new JavaScriptSerializer();
            //var data = ser.Deserialize<Dictionary<string, object>>("{\"x\":9}");
            //var ary = ser.Deserialize<ArrayList>("[\"x\",\"y\"]");
            //Console.WriteLine(data["x"]);
            //foreach(string v in ary) {
            //	Console.WriteLine(v);
            //}
            //data["y"] = 8;
            //string dstr = ser.Serialize(data);
            //Console.WriteLine(dstr);

            // 高级玩法,硬编码方式注册hook
            THook.init(api);
        }