用户:
灵清帝王吃爆一切厕所查看:21 回复:19 评论:21 创建时间:2022-05-08T11:26:59
备注:以下代码全由本人自己肝的()如有雷同,纯属巧合()
碰水回存档点代码:
world.onFluidEnter(async({ entity , voxel }) => {//当实体进入液体中时
if(entity.isPlayer) {//如果实体是玩家
if(voxel == voxels.id('water')) {//如果液体方块名称是water
entity.player.forceRespawn()//玩家重生
}
}else {
return//不处理非玩家实体
}
})
存档点代码(要先给存档点打上标签:存档点):
world.querySelectorAll('.存档点').forEach(async( mesh ) => {//搜索所有标签为存档点的实体
mesh.onEntityContact(async({ other }) => {//当它和另外一个实体碰撞时。注意onEntityContact参数要填other,而不是entity
if(other.isPlayer) {//如果另一个实体是玩家
if(other.player.spawnPoint.x != mesh.position.x && other.player.spawnPoint.y != mesh.position.y + 1 && other.player.spawnPoint.z != mesh.position.z) {//如果玩家的重生点和存档点的位置不一样
other.player.directMessage('到达新的重生点') // 给玩家发消息
other.player.spawnPoint.x = mesh.position.x;
other.player.spawnPoint.y = mesh.position.y + 1;//为了防止卡在方块里
other.player.spawnPoint.z = mesh.position.z;
}
}
})
})
碰到方块趋势,自己填方块名称:
world.onVoxelContact(async({ entity , voxel }) => {//当实体碰到方块时
if(entity.isPlyaer) {//如果实体是玩家
if(voxel == voxels.id('方块的名称')) {
entity.hurt(100);
for(let i = 0; i <= 3; i++) {
entity.player.directMessage(`复活倒计时:${String(3 - t)}秒`);
await sleep(1000);
}
entity.player.forceRespawn()
}
}
})
有时候可能跑酷太难()一次性跑不完,这时候就要用SQL了():
(async function() {
await createTable()
})();
world.onPlayerJoin(async({ entity }) => {
try {
await loadUser(entity)
}catch(error1) {
world.say(`loadUser sql error : ${error1}`)
};
})
async function createTable() {
await db.sql`
CREATE TABLE IF NOT EXISTS "player"(
"x" REAL NOT NULL,
"y" REAL NOT NULL,
"z" REAL NOT NULL,
"userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL
)
`
}
async function savePlayer(entity) {
if(entity.player.userKey) {
await db.sql`
INSERT INTO "player"(
"x",
"y",
"z",
"userKey"
)
VALUES(
${entity.position.x},
${entity.position.y},
${entity.position.z},
${entity.player.userKey}
)
ON CONFLICT("userKey")
DO UPDATE SET
"x" = excluded."x",
"y" = excluded."y",
"z" = excluded."z"
`
}
}
async function loadUser(user) {
const [data] = await db.sql`SELECT * FROM "player" WHERE "userKey" = ${user.player.userKey} LIMIT 1`;
if(data) {
user.position.x = data.x;
user.position.y = data.y;
user.position.z = data.z;
}
}
async function removeplayer (entity) {
await db.sql`DELETE FROM "player" WHERE "userKey" = ${entity.player.userKey}`
}
world.onPress(async({ entity , button }) => {
if(button == Box3ButtonType.ACTION1) {
if(entity.player.walkState == Box喵layerWalkState.CROUCH) {
const delete_player = await entity.player.dialog({
type:Box3DialogType.SELECT,
title:null,
content:'你要删除存档吗?',
options:['确定']
})
if(delete_player.value == '确定') {
await removeplayer(entity)
}
}
}
})
然后在存档点代码那里加载savePlayer,最后给出全部代码:
world.onFluidEnter(async({ entity , voxel }) => {//当实体进入液体中时
if(entity.isPlayer) {//如果实体是玩家
if(voxel == voxels.id('water')) {//如果液体方块名称是water
entity.player.forceRespawn()//玩家重生
}
}else {
return//不处理非玩家实体
}
})
world.querySelectorAll('.存档点').forEach(async( mesh ) => {//搜索所有标签为存档点的实体
mesh.onEntityContact(async({ other }) => {//当它和另外一个实体碰撞时。注意onEntityContact参数要填other,而不是entity
if(other.isPlayer) {//如果另一个实体时玩家
if(other.player.spawnPoint.x != mesh.position.x && other.player.spawnPoint.y != mesh.position.y + 1 && other.player.spawnPoint.z != mesh.position.z) {//如果玩家的重生点和存档点的位置不一样
other.player.directMessage('到达新的重生点') // 给玩家发消息
other.player.spawnPoint.x = mesh.position.x;
other.player.spawnPoint.y = mesh.position.y + 1;//为了防止卡在方块里
other.player.spawnPoint.z = mesh.position.z;
await savePlayer(other)
}
}
})
})
world.onVoxelContact(async({ entity , voxel }) => {//当实体碰到方块时
if(entity.isPlyaer) {//如果实体是玩家
if(voxel == voxels.id('方块的名称')) {
entity.hurt(100);
for(let i = 0; i <= 3; i++) {
entity.player.directMessage(`复活倒计时:${String(3 - t)}秒`);
await sleep(1000);
}
entity.player.forceRespawn()
}
}
})
(async function() {
await createTable()
})();
world.onPlayerJoin(async({ entity }) => {
try {
await loadUser(entity)
}catch(error1) {
world.say(`loadUser sql error : ${error1}`)
};
})
async function createTable() {
await db.sql`
CREATE TABLE IF NOT EXISTS "player"(
"x" REAL NOT NULL,
"y" REAL NOT NULL,
"z" REAL NOT NULL,
"userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL
)
`
}
async function savePlayer(entity) {
if(entity.player.userKey) {
await db.sql`
INSERT INTO "player"(
"x",
"y",
"z",
"userKey"
)
VALUES(
${entity.position.x},
${entity.position.y},
${entity.position.z},
${entity.player.userKey}
)
ON CONFLICT("userKey")
DO UPDATE SET
"x" = excluded."x",
"y" = excluded."y",
"z" = excluded."z"
`
}
}
async function loadUser(user) {
const [data] = await db.sql`SELECT * FROM "player" WHERE "userKey" = ${user.player.userKey} LIMIT 1`;
if(data) {
user.position.x = data.x;
user.position.y = data.y;
user.position.z = data.z;
}
}
async function removeplayer (entity) {
await db.sql`DELETE FROM "player" WHERE "userKey" = ${entity.player.userKey}`
}
world.onPress(async({ entity , button }) => {
if(button == Box3ButtonType.ACTION1) {
if(entity.player.walkState == Box喵layerWalkState.CROUCH) {
const delete_player = await entity.player.dialog({
type:Box3DialogType.SELECT,
title:null,
content:'你要删除存档吗?',
options:['确定']
})
if(delete_player.value == '确定') {
await removeplayer(entity)
}
}
}
})
堂主の犬存档这样好像更简单一些()()()
world.onPlayerJoinn(({entity})=>{
entity.cundang = []
})
world.querySelectorAll('.存档点').forEach(async( mesh ) => {//搜索所有标签为存档点的实体
mesh.onEntityContact(async({ other }) => {//当它和另外一个实体碰撞时。注意onEntityContact参数要填other,而不是entity
if(other.isPlayer) {//如果另一个实体时玩家
if(other.cundang == mesh.id){return}//防止玩家重复存档
other.player.directMessage('到达新的重生点') // 给玩家发消息
other.player.spawnPoint.x = mesh.position.x;
other.player.spawnPoint.y = mesh.position.y + 1;//为了防止卡在方块里
other.player.spawnPoint.z = mesh.position.z;
other.cundang = mesh.id
await savePlayer(other)
}
}
})
})
点赞0
评论
扣血
world.onVoxelContact(async({ entity , voxel }) => {//当实体碰到方块时
if(entity.isPlyaer) {//如果实体是玩家
if(voxel == voxels.id('lava02')) {
entity.hurt(100);
for(let i = 0; i <= 3; i++) {
entity.player.directMessage(`复活倒计时:${String(3 - t)}秒`);
await sleep(1000);
}
entity.player.forceRespawn()
}
}
})
这有点问题,扣不了血!
点赞0
评论
ssss1457收藏点赞imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"
点赞0
评论