猫史档案馆


【教程】背包教程(自带SQL)

用户:萌新大佬萌新大佬查看:37 回复:39 评论:37 创建时间:2022-06-02T11:55:04


constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要喵回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(constiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 constp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', options:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } } 有bug发评论区


回复

上一页1 页 / 共 2下一页
萌新大佬萌新大佬

吃排版大师是吧,三个贴的排版都吃了

点赞0


评论


萌新大佬萌新大佬

constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要喵回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(constiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 constp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', options:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } } 代码👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆

点赞0


评论


萌新大佬萌新大佬

constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要喵回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(constiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 constp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', options:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } }

点赞0


评论


萌新大佬萌新大佬

constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要喵回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(constiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 constp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', options:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } }

点赞0


评论


萌新大佬萌新大佬

constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要喵回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(constiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 constp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', options:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } }

点赞0


评论


萌新大佬萌新大佬

c喵tInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 c喵trows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 c喵tnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 c喵tructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(c喵tiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要加 returnthis.thing[item];//返回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(c喵tiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 c喵tp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', opti喵:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } }

点赞0


评论


某只sans喵呀某只sans喵呀

编程猫最喜欢吃排版了

点赞0


评论


某只sans喵呀某只sans喵呀

emm, 开在我之前被你拉着测了半小时bug, 来帮我建图!!! https://box3.fun/e/cf6be2352b836fd2940d?p=Y4H%2FoFG5lhoYFmAojwG8EaeGGyvPt5mPViv%2F8tqN3VA4z1rD6SMAVndWVzdA%3D%3D

点赞0


评论


白洞awa白洞awa

。。。

点赞0


评论


千从千从

你用那个js的插入代码

点赞0


评论


SKQASKQA

草排版排不出来()算了()

点赞0


评论


萌新大佬萌新大佬

我就不信有人能在这里成功排版!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

点赞0


评论


白洞awa白洞awa

用代码模式可以不吞格式

点赞0


评论


_function__function_

constInitCap=10;//这里写背包的初始容量db.sql`CREATETABLEIFNOTEXISTSbag(idCHAR(16)NOTNULL,--这里存userKeyrecordTEXTNOTNULL--这里存背包)`//建立sqlworld.onPlayerJoin(async({entity})=>{//当玩家进入地图functioninit(){//建立初始背包entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造}if(entity.player.userKey){//如果玩家非游客constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行if(rows&&!rows.length){//如果这一行不存在init();//那么建立初始背包db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql}else{//如果玩家有数据constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在nowentity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包}}elseinit();//游客玩家直接建立新背包})world.onPlayerLeave(({entity})=>{entity.bag.save();//玩家离开时保存})classBag{//创建背包类constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数this.owner=owner;this.thing=thing;this.capacity=capacity;this.now=0;for(constiofObject.keys(this.thing)){this.now+=this.thing[i];}//now是物品数量this.saveObject={thing:this.thing,capacity:this.capacity}//sql存档的时候存这个}save(){//保存if(!this.owner.player.userKey)return;//如果游客账号就不保存db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存}add(item='',num=1){//添加物品,默认添加一个if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefinedif(!!this.thing[item]){//如果此物品存过this.thing[item]+=num;//增加numthis.saveObject.thing[item]+=num;//保存对象也要同步}else{//如果没存过this.thing[item]=num;//直接赋值this.saveObject.thing[item]=num;//直接赋值}this.now+=num;//物品的数量也要喵回物品数量}del(item='',num=1){//删除物品,默认减少一个if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber)if(this.thing[item]<num)returnundefined;//如果不够减就返回undefunedthis.thing[item]-=num;//减去相应的量this.saveObject.thing[item]-=num;//同步保存对象this.now-=num;//当前物品数也要减returnthis.thing[item];//返回数量}setCap(cap=0){//设置容量if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiendthis.capacity=cap;//直接设置容量returnthis.capacity;//返回值}asyncshow(){//对玩家展示背包varop=[];for(constiofObject.keys(this.thing)){op.push(`${i}×${this.thing[i]}`);}//将物品×数量放进选项列表constp=awaitthis.owner.player.dialog({//建立对话框type:Box3DialogType.SELECT,title:'背包',//具体细节如标题,内容等自己改content:'以下是你的背包',options:op,});returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品}}有bug发评论区

点赞0


评论


萌新大佬萌新大佬

编程猫吞排版功能 orz orz orz orz orz orz

点赞0


评论


浮生awa浮生awa

constInitCap = 10; db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL, recordTEXTNOTNULL )` world.onPlayerJoin(async ({ entity }) => {
    functioninit(){
    entity.bag = newBag(en
tity, {}, InitCap);
    } if (entity.player.userKey) {
        constrows = awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`; if (rows && !rows.length) {
            init(); db.sql`IN
ERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;
        } else {
            constnow = JSON.parse(rows[0].record); now entity.bag = newBag(entity, now.thing, now.capacity);
        }
    } elseinit();
}) world.onPlayerLeave(({ entity }) => { entity.bag.save(); }) classBag{
    constructor(owner = newBox3Entity, thing = {}, capacity = 0){
    this.owner = owner; this.thin
        g = thing; this.capacity = capacity; this.now = 0; for (constiofObject.keys(this.thing)) {
        this.now += this.thing[i]; this.saveObject = {
            thing: this.thing, capacity: this.cap
 acity
        }
        } save(){
            if (!this.owner.player.userKey) return; db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;
        } add(item = '', num = 1){
            if (this.now + num > this.capacity) returnundefined; undefined if (!!this.thing[item]) {
            this.thing[item] += num; this.saveObject.thing[item] += num
                ;
            } else { this.thing[item] = num; this.saveObject.thing[item] = num; } this.now += num; returnthis.thing[item];
        } del(item = '', num = 1){
            if (!this.thing[item]) returnNaN;
            if (this.thing[item] < num) returnundef
            ined; undefuned this.thing[item] -= num; this.saveObject.thing[item] -= num; this.now -= num; returnthis.thing[item];
        } setCap(cap = 0){
            if (cap < this.now) returnundefined;
            this.capacity = cap; returnthis.capacity;
        } asyncshow(){
            varop = []; for (constiofObject.keys(this.thing)) { op.push(`${i}×${this.thing[i]}`); }
            constp = awaitthis.owner.player.dialog({ type: Box3DialogType.SELECT, title: '背包', content: '以下是你的背包', options: op, }); returnthis.thing[Object.keys(t
  his.thing)[op.indexOf(p)]];
        }
    }

点赞0


评论


萌新大佬萌新大佬

constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要加 returnthis.thing[item];//返回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(constiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 constp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', options:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } }

点赞0


评论


萌新大佬萌新大佬

constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[item]+=num;//保存对象也要同步 }else{//如果没存过 this.thing[item]=num;//直接赋值 this.saveObject.thing[item]=num;//直接赋值 } this.now+=num;//物品的数量也要加 returnthis.thing[item];//返回物品数量 } del(item='',num=1){//删除物品,默认减少一个 if(!this.thing[item])returnNaN;//如果此物品不存在直接返回NaN(NotaNumber) if(this.thing[item]<num)returnundefined;//如果不够减就返回undefuned this.thing[item]-=num;//减去相应的量 this.saveObject.thing[item]-=num;//同步保存对象 this.now-=num;//当前物品数也要减 returnthis.thing[item];//返回数量 } setCap(cap=0){//设置容量 if(cap<this.now)returnundefined;//如果背包里的物品比目标容量多则返回undefiend this.capacity=cap;//直接设置容量 returnthis.capacity;//返回值 } asyncshow(){//对玩家展示背包 varop=[]; for(constiofObject.keys(this.thing)){ op.push(`${i}×${this.thing[i]}`); }//将物品×数量放进选项列表 constp=awaitthis.owner.player.dialog({//建立对话框 type:Box3DialogType.SELECT, title:'背包',//具体细节如标题,内容等自己改 content:'以下是你的背包', options:op, }); returnthis.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品 } }

点赞0


评论


Star_Ink_sansStar_Ink_sans

是这样的()你发布帖子的时候可以看到上面有一排按钮)按最后一个试试()效果:()

world.say("就像这样")

点赞0


评论


阳光猫阳光猫

center_imagecenter_imagecenter_imagecenter_imagecenter_image

点赞1


评论


全能代码师全能代码师

芜湖虽然看不清楚排版但是yyds)

点赞0


评论


一只野生的小柒一只野生的小柒

constInitCap = 10;//这里写背包的初始容量
db.sql`
    CREATE TABLE IF NOT EXISTS bag( 
        id CHAR(16) NOT NULL,--这里存userKey
        record TEXT NOT NULL--这里存背包
    )`//建立sql
world.onPlayerJoin(async ({ entity }) => {//当玩家进入地图
    function init() {//建立初始背包
        entity.bag = new Bag(entity,
            {/*如果有初始物品在这里写*/ },
            InitCap
        );//初始背包的构造
    }
    if (entity.player.userKey) {//如果玩家非游客 
        const rows = await db.sql`SELECT*FROM bag WHERE id=${entity.player.userKey}`;//获取玩家数据的那一行
        if (rows && !rows.length) {//如果这一行不存在
            init();
            //那么建立初始背包
            db.sql`INSERT IN TO bag VALUES(
            ${JSON.stringify(entity.bag.saveObject)}
            ${entity.player.userKey},
            )
            `;//将初始背包插入sql
        } else {//如果玩家有数据
            constnow = JSON.parse(rows[0].record);//将读取到的数据临时存放在now 
            entity.bag = new Bag(entity, now.thing, now.capacity);//
        }
    }
    else {
        init();//游客玩家直接建立新背包 
    }

})

点赞0


评论


一只野生的小柒一只野生的小柒

world.onPlayerLeave(({ entity }) => {
    entity.bag.save();//玩家离开时保存 
})
class Bag {//创建背包类
    constructor(owner = new Box3Entity, thing = {}, capacity = 0) {
        //构造函数
        this.owner = owner;
        this.thing = thing;
        this.capacity = capacity;
        this.now = 0;
        for (const i of Object.keys(this.thing)) {
            this.now += this.thing[i];
        }//now是物品数量
        this.saveObject = {
            thing: this.thing, capacity: this.capacity
        }//sql存档的时候存这个 
    }

点赞0


评论


一只野生的小柒一只野生的小柒

save() {//保存
        if (!this.owner.player.userKey) return;//如果游客账号就不保存
        db.sql`UPDATE bag SET record=${JSON.stringify(this.saveObject)} WHEREid = ${this.owner.player.userKey}`;//保存
        if (this.now + num > this.capacity) return undefined;//如果数量数量超过了就返回
        if (!!this.thing[item]) {//如果此物品存过
            this.thing[item] += num;//增加num
            this.saveObject.thing[item] += num;//保存对象也要同步
        } else {//如果没存过

点赞0


评论


一只野生的小柒一只野生的小柒

this.thing[item] = num;//直接赋值 
            this.saveObject.thing[item] = num;//直接赋值
        } this.now += num;//物品的数量也要加
        return this.thing[item];//返回物品数量
    } del(item = '', num = 1) {//删除物品,默认减少一个 

点赞0


评论


一只野生的小柒一只野生的小柒

if (!this.thing[item]) return NaN;//如果此物品不存在直接返回
        if (this.thing[item] < num) return undefined;//如果不够减就返undefuned 
        this.thing[item] -= num;//减去相应的量
        this.saveObject.thing[item] -= num;//同步保存对象
        this.now -= num;//当前物品数也要减
        return this.thing[item];//返回数量
    } setCap(cap = 0) {//设置容量
        if (cap < this.now) return undefined;//如果背包里的物品比目标容量多则返 
        this.capacity = cap;//直接设置容量
        return this.capacity;//返回值
    } async show() {//对玩家展示背包

点赞0


评论


一只野生的小柒一只野生的小柒

varop = [];
        for (const i of Object.keys(this.thing)) {
            op.push(`${i}×${this.thing[i]}`);
        }//将物品×数量放进选项列表
        const p = await this.owner.player.dialog({//建立对话框 
            type: Box3DialogType.SELECT, title: '背包',//具体细节如标题,        
            content: '以下是你的背包', options: op,
        });
        return this.thing[Object.keys(this.thing)[op.indexOf(p)]];//返回选择了的物品
    }
}

点赞1


评论


一只野生的小柒一只野生的小柒

啊作者,我刚刚测试有一个bug

点赞1


评论


好动的花卷卷mYXx好动的花卷卷mYXx

问个问题组合好了怎么调用

点赞0


评论


究极石头究极石头

constInitCap=10;//这里写背包的初始容量 db.sql`CREATETABLEIFNOTEXISTSbag( idCHAR(16)NOTNULL,--这里存userKey recordTEXTNOTNULL--这里存背包 )`//建立sql world.onPlayerJoin(async({entity})=>{//当玩家进入地图 functioninit(){//建立初始背包 entity.bag=newBag(entity,{/*如果有初始物品在这里写*/},InitCap);//初始背包的构造 } if(entity.player.userKey){//如果玩家非游客 constrows=awaitdb.sql`SELECT*FROMbagWHEREid=${entity.player.userKey}`;//获取玩家数据的那一行 if(rows&&!rows.length){//如果这一行不存在 init();//那么建立初始背包 db.sql`INSERTINTObagVALUES(${entity.player.userKey},${getRecord.call(entity)})`;//将初始背包插入sql }else{//如果玩家有数据 constnow=JSON.parse(rows[0].record);//将读取到的数据临时存放在now entity.bag=newBag(entity,now.thing,now.capacity);//将now拆分开并建立玩家的背包 } }elseinit();//游客玩家直接建立新背包 }) world.onPlayerLeave(({entity})=>{ entity.bag.save();//玩家离开时保存 }) classBag{//创建背包类 constructor(owner=newBox3Entity,thing={},capacity=0){//构造函数 this.owner=owner; this.thing=thing; this.capacity=capacity; this.now=0; for(constiofObject.keys(this.thing)){ this.now+=this.thing[i]; }//now是物品数量 this.saveObject={ thing:this.thing, capacity:this.capacity }//sql存档的时候存这个 } save(){//保存 if(!this.owner.player.userKey)return;//如果游客账号就不保存 db.sql`UPDATEbagSETrecord=${JSON.stringify(this.saveObject)}WHEREid=${this.owner.player.userKey}`;//保存 } add(item='',num=1){//添加物品,默认添加一个 if(this.now+num>this.capacity)returnundefined;//如果数量数量超过了就返回undefined if(!!this.thing[item]){//如果此物品存过 this.thing[item]+=num;//增加num this.saveObject.thing[

点赞0


评论