用户:
芜湖咸鱼查看:7 回复:17 评论:7 创建时间:2020-06-29T16:07:21
好久不见的SJS又回来啦!
这次更新主要是让事件绑定支持多个事件处理
使用方法:
复制下面的代码
"use strict";const box3EventTypes=["PlayerJoin","Press","EntityContact","EntityCreate","EntityDestroy","EntitySeparate","FluidEnter","FluidLeave","VoxelContact","VoxelSeparate","Chat","Click","PlayerLeave","Release","Tick"];let sjs=class{constructor(t=!0,e=!0){this._initAttr(),this._initAttr=null,global.$=this,e&&console.clear(),this.debug=t,t?console.log("[SJS] 此地图开启了调试模式. 如果你正准备发布地图,请务必启用关闭调试模式,否则运行时会降低性能."):console.log("[SJS] 此地图关闭了调试模式. 如果你正在创作地图,请务必启用调试模式,否则就得不到警告,提示和错误.");for(let t of box3EventTypes)world["on"+t](e=>{for(let n of this.eventFunctionList)n.type==t&&n.func(e)})}_initAttr(){this.eventFunctionList=[],this.Admin=class{constructor(adminNameList,functions={chatDebug:!1}){this.adminNameList=adminNameList,functions.chatDebug&&$.on("Chat",({entity:entity,message:message})=>{if(-1!=this.adminNameList.indexOf(entity.player.name)&&"$"==message[0])try{world.say("~> "+eval(message.slice(1)))}catch(t){world.say("[!]"+t)}},"用于chatDebug功能(来自sjs.Admin)")}},this.EventFunction=class{constructor(t,e,n){this.type=t,this.func=e,this.token=n}},this._runtime={warn:t=>{if(!this.debug)return;let e=new Error(t);e.name="[SJS] 警告",console.warn(this._runtime.stackReplace(e.stack)+"\n")},error:(t,e=!1)=>{if(!this.debug)return;let n=new Error(t);n.name="[SJS] "+(e?"致命":"")+"错误";let i=this._runtime.stackReplace(n.stack)+"\n";if(console.error(i),e)throw"程序停止"},log:(t,e)=>{if(this.debug&&(console.log("[SJS]: "+t+"\n"),e)){let e=new Error(t);e.name="位置追溯",console.warn(this._runtime.stackReplace(e.stack)+"\n")}},stackReplace:t=>{t=t.split("\n");for(let e of t)-1==e.indexOf("<anonymous>")&&-1==e.indexOf("<isolated-vm>")||t.splice(t.indexOf(e));return t.join("\n")}}}on(t,e,n){if(-1!=box3EventTypes.indexOf(t)){if("function"==typeof e){var i=this.eventFunctionList.length;return this.eventFunctionList.push(new this.EventFunction(t,e,i)),this._runtime.log(`已注册事件:"${n||"<未指定描述信息>"}"\n类型:${t} 事件令牌:${i}`),this.eventFunctionList.length>=100&&this._runtime.warn("注册的事件过多(>=100个),可能会影响事件操作的性能,请适当合并事件处理函数."),i}this._runtime.error("事件处理函数必须是一个可调用的函数",!0)}else this._runtime.error('注册了不存在或者不支持的事件("'+t+'")',!0)}clearEvent(t){for(let e of this.eventFunctionList)e.token==t&&(this._runtime.log(`已清除事件处理函数:${t}(事件令牌)`),this.eventFunctionList.splice(this.eventFunctionList.indexOf(e)))}};module.exports=sjs;
然后在编辑器中新建一个JS文件 (打开代码编辑器,点击上面的"+")
然后把新建的文件重命名成'sjs' (右键编辑器上方的"script_01.js",然后点击"重命名")
把上面的代码粘贴进去 (可能会卡一会,耐心等待)
然后回到主程序(index.js)
输入下面的代码
var sjs = require('./sjs.js');
sjs = new sjs();
这样就导入了SJS,并完成了初始化
使用方法:
在使用"new"创建sjs对象的时候,就完成了初始化,并且会在全局注入变量"$"
你只需要使用$即可访问到sjs
事件绑定器:
使用sjs.on('事件名称',事件处理函数,描述信息)即可绑定事件
其中,描述信息是可选的
事件名称和默认的一样,只不过少了'on' (比如world.onPlayerJoin变为$.on('PlayerJoin'))
第二个参数就是一个处理器函数,默认的一致 (比如$.on("PlayerJoin",({entity})=>{}))
第三个参数是描述信息,主要用来在调试时区分
特点:
你可以绑定多个事件处理函数,这点和Box3默认的不同
Box3默认的API只允许一个事件存在一个处理器函数,前面的的会被之后的覆盖,而SJS允许绑定多个处理器函数
如何取消事件处理器:
有时候你可能需要清除掉某些事件处理器,你可以使用$.clearEvent(事件令牌)
事件令牌即事件处理器的标识,调用$.on时会作为返回值得到
实用工具:
Admin (管喵)
使用方法:
在基础的初始化代码后面加上
new sjs.Admin(管喵名称列表,功能选项)
示例:
new sjs.Admin(['shyfcka','昊然'],{
chatDebug:true //打开聊天调试
})
执行之后就可以立即生效
注意事项:
SJS有一些警告,提示和自定义错误,这些只会在调试模式显示,所以在发布时可以通过new sjs(false)来禁用调试模式以此提高性能
如果在使用的时候遇到莫名其妙的报错(SJS内部报错,并且没有中文提示)请回帖
七式草莓Error: error loading module: sjs.js, module not found
at ScriptResourceSync.loadModule(<isolated-vm>:13529:19
at require(<isolated-vm>:13586:25)
at eval(index.js:3:11)
点赞0
评论