Lv.1
https://shequ.codemao.cn/community/463495
在 谁能得到圣诞礼物?创作节提名名单出炉! 中回复
冲名单失败x3imgsrc="https://static.cod喵/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E4%BC%A4%E5%BF%83.gif"alt="emotion_编程猫_伤心"
2021-12-23T11:39:29 点赞:0
在 关于神岛首页改版的建议征集 中回复
能改回去吗,把贡献图留下来,否则有一些新出的产品玩家看都不看一眼,导致新出的地图一定要上首页才有浏览次数和评论,我就是这样,现在一些地图都要靠拉人来装浏览量了
2022-02-13T15:47:04 点赞:2
在 【教程】背包教程(自带SQL) 中回复
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();//游客玩家直接建立新背包
}
})2022-06-14T09:53:05 点赞:0
在 【教程】背包教程(自带SQL) 中回复
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存档的时候存这个
}2022-06-14T09:54:52 点赞:0
在 【教程】背包教程(自带SQL) 中回复
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 {//如果没存过2022-06-14T09:57:02 点赞:0
在 【教程】背包教程(自带SQL) 中回复
this.thing[item] = num;//直接赋值
this.saveObject.thing[item] = num;//直接赋值
} this.now += num;//物品的数量也要加
return this.thing[item];//返回物品数量
} del(item = '', num = 1) {//删除物品,默认减少一个 2022-06-14T09:58:46 点赞:0
在 【教程】背包教程(自带SQL) 中回复
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() {//对玩家展示背包2022-06-14T09:59:12 点赞:0
在 【教程】背包教程(自带SQL) 中回复
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)]];//返回选择了的物品
}
}2022-06-14T09:59:38 点赞:1