
Lv.1
面朝大海,春暖花开
签名:CODE()
在 【教程】背包教程(自带SQL) 中回复
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发评论区
2022-06-02T14:43:55 点赞:0
在 要怎么写踢人的代码??? 中回复
//这样也彳亍
world.querySelectorAll('player').forEach((e) => if(e.player.name == ''){e.player.kick()})
' '里填你想踢出玩家的姓名
2022-06-07T07:47:09 点赞:0
在 关于sql表的报错(大佬) 中回复
啊这位朋友
表格中数据的名字要一样啊
async function saveplayer(entity){
if(entity.player.userKey){
await db.sql`
INSERT INTO "player" (
"userkey",
"itemlist",
"dengj",
"hp",
"gold",
"exp"
) VALUES (
${entity.player.userKey},
${JSON.stringify(itemList)},
${entity.dengj},
${entity.hp},
${entity.gold},
${entity.exp}
)
ON CONFLICT("userkey")
DO UPDATE SET
"itemlist" = excluded."itemlist",
"dengj" = excluded."dengj",
"hp" = excluded."hp",
"gold" = excluded."gold",
"exp" = excluded."exp"
`
}
}2022-06-07T19:05:39 点赞:1
在 关于sql表的报错(大佬) 中回复
你可以自己改变,但是SQL里面每一个你要存储的数据相应的代码都是要一样的,不能比如说这个是“exp”,那个就变成了“EXP”
2022-06-07T19:06:23 点赞:1
在 宣传一下()勿举报() 中回复
啊啊啊!屑排版! http(防屏蔽)s://space.b(防屏蔽)ilibi(防屏蔽)li.com/547838430?spm_id_from=333.1007.0.0
2022-06-08T19:49:57 点赞:0
在 怎么用代码表示碰到冰块🧊破裂,等一会儿又复原 中回复
world.onVoxelContact(async ({ x, y, z, voxel }) => {
c喵t voxelName = voxels.name(voxel); // 将方块id转换名称
if (voxelName === 'ice') { // 如果方块名称是冰块
voxels.setVoxel(x, y, z, 0); // 将方块变成空气
await sleep(1000)
voxels.setVoxel(x, y, z, 'ice')
}
});2022-06-11T20:07:08 点赞:1
在 谁能告诉我一下 CEOの试炼的留言箱的代码? 中回复
global.Z = ['']//作者
global.H = ['']//合作者
db.sql`CREATE TABLE IF NOT EXISTS "advice"(//SQL
"title" TEXT NOT NULL
)`
world.querySelectorAll('.liuyan').forEach(async (e) => {//标签为“liuyan”
e.enableInteract = true
e.interactHint = '留言箱'
e.interactRadius = 5;
e.onInteract(async ({ entity }) => {
c喵t result_1 = await entity.player.dialog({
type: Box3DialogType.SELECT,
title: "留言箱",
content: `欢迎留言`,
opti喵: ['留言栏', '查看留言内容'],
});
//如果玩家点击了屏幕其他区域,取消对话框
if (!result_1 || result_1 === null) {
return;
}
//判断玩家选了什么选项
switch (result_1.index) {
case 0:
c喵t rows = await db.sql`SELECT * FROM "advice"`;
let title = '';
for (let i in rows) {
title += rows[i].title + '\n';
}
c喵t result = await entity.player.dialog({
type: Box3DialogType.INPUT,
title: '意见箱',
content: '举报&意见(字数<=20字)',
placeholder: '输入你想说的话...禁止骂人)',
})
if (result != null) {
if (result.length <= 20) {
db.sql`INSERT INTO "advice" VALUES(${entity.player.name + ':' + result})`;//插入新字段
entity.player.dialog({
type: Box3DialogType.TEXT,
title: '意见箱',
content: '发布成功,谢谢你的建议\n作者将会在一周内查看并回复你的留言\n请留意',
})
} else {
entity.player.dialog({
type: Box3DialogType.TEXT,
title: '意见箱',
content: '字数不能超过20个字哦~',
})
}
}
break
case 1:
if (Z.includes(entity.player.name) || H.includes(entity.player.name)) {
c喵t rows = await db.sql`SELECT * FROM "advice"`;
let title = '';
for (let i in rows) {
title += rows[i].title + '\n';
}
c喵t result = await entity.player.dialog({
type: Box3DialogType.TEXT,
title: '意见箱',
content: title,
})
} else {
entity.player.dialog({
type: 'text',
content: 'emm,你还没有权限去查看此内容'
})
}
default:
}
})
})2022-06-14T13:22:17 点赞:1
在 谁能给我个代码是关于碰到终点时,就能开启飞行 谢谢🙏 中回复
需要看你的个人需求:
如果是模型的话:
world.onEntityContact(({ entity, other }) => {
if (entity.id == '奖杯' && other.isPlayer) {//''种填入模型id
other.player.canFly = true
}
})
如果是区域的话:
const area = world.addZone({
selector: 'player',
bounds: {
lo: [, , ],//自己填区域
hi: [, , ],
},
})
area.onEnter(({ entity }) => {
entity.player.canFly = true
});
如果是方块的话:
world.onVoxelContact(({ entity, voxel }) => {
if (voxel == voxels.id('')) {//方块名称自己填入
entity.player.canFly = true
}
});2022-06-16T18:16:51 点赞:0
在 求问代码!!!! 中回复
world.querySelectorAll('player').forEach((e) => if(e.player.name == '你要禁言的人的名字'){e.player.muted = true})2022-06-18T19:34:47 点赞:0