用户:
翎with珝查看:0 回复:4 评论:0 创建时间:2024-07-21T20:17:40
依据API文档编写的临时聊天频道,已亲身体验,游玩端、创作端均可使用
注:若使用此代码发现聊天框上方没有“临时”,请重新进入地图
效果展示





代码
c喵ole.clear()
world.getTempChats().then(chatIds => {
let chats = chatIds.join(',').split(',')
world.destroyTempChat(chats).then(failedChatIds => {
if (!failedChatIds.length) {
c喵ole.log(`地图内所有聊天室(${chats})销毁成功`)
} else {
c喵ole.log(`以下聊天室销毁失败:${failedChatIds.join(',')}`)
}
})
})
var chatTempId = []
var chatTempName = []
var chatTempCreatePlayer = []
world.onPlayerJoin(async ({ entity }) => {
entity.createChatId = []
entity.joinChatId = []
entity.isInChat = false
entity.isCreateChat = false
})
world.onPress(async ({ entity, button }) => {
if (button == GameButtonType.ACTION1) {
let menu = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '菜单',
content: `请进行有关于临时聊天频道的操作${entity.isInChat == true ? `\n我加入的频道:${entity.joinChat[0]}(${entity.joinChat[1]})` : `\n我加入的频道: 无`}`,
opti喵: entity.isInChat == true ? entity.isCreateChat == true ? ['管理我创建的临时聊天频道', '退出我所加入的临时聊天频道'] : ['退出我所加入的临时聊天频道'] : ['创建一个临时聊天频道', '加入一个临时聊天频道']
})
if (!menu || menu === null) return;
if (menu) {
if (menu.value == '退出我所加入的临时聊天频道') {
if (entity.isCreateChat == false) {
let outChat = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '退出我所加入的临时聊天频道',
content: '是否要退出加入的临时聊天频道',
opti喵: ['是的', '取消']
})
if (!outChat || outChat === null) return;
if (outChat) {
if (outChat.value == '是的') {
world.removeTempChatPlayer(entity.joinChat[1], [entity.player.userId]).then(userIds => {
entity.player.dialog({
type: GameDialogType.TEXT,
content: '你已退出了频道'
})
world.querySelectorAll('player').forEach(async (e) => {
if (e.createChat[1] == entity.joinChat[1]) {
e.player.dialog({
type: GameDialogType.TEXT,
content: `@${entity.player.name} 退出了你的频道`
})
}
})
entity.createChatId = []
entity.joinChatId = []
entity.isInChat = false
entity.isCreateChat = false
})
}
}
} else {
let outChat = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '退出我所加入的临时聊天频道',
content: `该临时聊天频道由你创建\n如需退出即为销毁此临时聊天频道,且踢出该频道内的所有玩家\n是否要销毁自己的临时聊天频道`,
opti喵: ['是的', '取消']
})
if (!outChat || outChat === null) return;
if (outChat) {
if (outChat.value == '是的') {
world.getTempChatUsers(entity.createChat[1]).then(userIds => {
let players = userIds.join(',').split(',')
for (let i = 0; i < players.length; i++) {
world.querySelectorAll('player').forEach(async (e) => {
if (e.player.userId == players[i]) {
e.player.dialog({
type: GameDialogType.TEXT,
content: `频道创建人@${entity.player.name} 删除了频道`
})
e.createChatId = []
e.joinChatId = []
e.isInChat = false
e.isCreateChat = false
}
})
}
})
world.destroyTempChat([entity.createChat[1]]).then(failedChatIds => {
if (!failedChatIds.length) {
entity.player.dialog({
type: GameDialogType.TEXT,
content: '你已销毁了频道'
})
let n = chatTempCreatePlayer.indexOf(entity.player.name)
chatTempCreatePlayer.splice(n, 1)
chatTempId.splice(n, 1)
chatTempName.splice(n, 1)
entity.createChatId = []
entity.joinChatId = []
entity.isInChat = false
entity.isCreateChat = false
} else {
entity.player.dialog({
type: GameDialogType.TEXT,
content: '频道销毁失败 请重试'
})
}
})
}
}
}
} else if (menu.value == '创建一个临时聊天频道') {
let create = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '创建一个临时聊天频道',
content: `是否要创建自己的临时聊天频道(创建后不能加入其它频道)`,
options: ['是的', '取消']
})
if (!create || create === null) return;
if (create) {
if (create.value == '是的') {
let creatName = await entity.player.dialog({
type: GameDialogType.INPUT,
title: '创建一个临时聊天频道',
content: `请为你所要创建的频道命名`,
confirmText: '确定',
placeholder: '名字长度2~13个字 (例:一起聊天, 交流讨论, 你干嘛哈哈诶呦~)'
})
if (!creatName || creatName === null) return;
if (creatName) {
if (creatName.length >= 2 && creatName.length <= 13) {
world.createTempChat().then(chatId => {
world.addTempChatPlayer(chatId, [entity.player.userId]).then(userIds => {
entity.player.dialog({
type: GameDialogType.TEXT,
content: `你创建了频道 ${creatName}\n频道创建人 @${entity.player.name}(${entity.player.userId})\n你已加入此频道`
})
entity.createChat = [creatName, chatId]
entity.joinChat = [creatName, chatId]
entity.isInChat = true
entity.isCreateChat = true
chatTempId.push(chatId)
chatTempName.push(creatName)
chatTempCreatePlayer.push(entity.player.name)
world.say(`@${entity.player.name} 创建了名为“${creatName}”的临时聊天频道\n该频道Id为${chatId}`)
})
})
} else {
entity.player.dialog({
type: GameDialogType.TEXT,
content: creatName.length < 2 ? '名称过短' : '名称过长'
})
}
}
}
}
} else if (menu.value == '加入一个临时聊天频道') {
if (chatTempId.length !== 0) {
let chatList = []
for (let i = 0; i < chatTempName.length; i++) {
chatList.push(`${chatTempName[i]}(${chatTempCreatePlayer[i]})`)
}
let joinTempChat = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '加入一个临时聊天频道',
content: '请选择你想加入的临时聊天频道',
options: chatList
})
if (!joinTempChat || joinTempChat === null) return;
if (joinTempChat) {
world.addTempChatPlayer(chatTempId[joinTempChat.index], [entity.player.userId]).then(userIds => {
entity.player.dialog({
type: GameDialogType.TEXT,
content: `你加入了频道 ${chatTempName[joinTempChat.index]}\n频道创建人 @${chatTempCreatePlayer[joinTempChat.index]}`
})
world.querySelectorAll('player').forEach(async (e) => {
if (e.player.name == chatTempCreatePlayer[joinTempChat.index]) {
e.player.dialog({
type: GameDialogType.TEXT,
content: `@${entity.player.name} 加入了你的频道 ${chatTempName[joinTempChat.index]}`
})
}
})
entity.createChat = []
entity.joinChat = [chatTempName[joinTempChat.index], chatTempId[joinTempChat.index]]
entity.isInChat = true
entity.isCreateChat = false
})
}
} else {
entity.player.dialog({
type: GameDialogType.TEXT,
content: `当前暂无频道`
})
}
} else if (menu.value == '管理我创建的临时聊天频道') {
let checkMyChat = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '管理我创建的临时聊天频道',
content: '请进行操作',
options: ['踢出玩家']
})
if (!checkMyChat || checkMyChat === null) return;
if (checkMyChat) {
if (checkMyChat.value == '踢出玩家') {
world.getTempChatUsers(entity.createChat[1]).then(async (userIds) => {
let playersId = userIds.join(',').split(',')
let playersName = []
for (let i = 0; i < playersId.length; i++) {
world.querySelectorAll('player').forEach(async (e) => {
if (e.player.userId == playersId[i] && e.player.userId !== entity.player.userId) {
playersName.push(e.player.name)
}
})
}
let kickPlayer = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '管理我创建的临时聊天频道',
content: '请选择你要踢出的玩家',
options: playersName
})
if (!kickPlayer || kickPlayer === null) return;
if (kickPlayer) {
let ifKickPlayer = await entity.player.dialog({
type: GameDialogType.SELECT,
title: '管理我创建的临时聊天频道',
content: '你是否要踢出该玩家',
options: ['是的', '取消']
})
if (!ifKickPlayer || ifKickPlayer === null) return;
if (ifKickPlayer) {
if (ifKickPlayer.value == '是的') {
world.querySelectorAll('player').forEach(async (e) => {
if (e.player.name == playersName[kickPlayer.index]) {
world.removeTempChatPlayer(entity.createChat[1], [e.player.userId]).then(userIds => {
e.player.dialog({
type: GameDialogType.TEXT,
content: `频道主@${entity.player.name} 把你踢出了临时聊天频道 ${entity.createChat[0]}`
})
entity.player.dialog({
type: GameDialogType.TEXT,
content: `你已将玩家@${e.player.name} 踢出了临时聊天频道 ${entity.createChat[0]}`
})
e.createChatId = []
e.joinChatId = []
e.isInChat = false
e.isCreateChat = false
})
}
})
}
}
}
})
}
}
}
}
}
})
world.onPlayerLeave(async ({ entity }) => {
if (entity.isInChat && entity.isCreateChat) {
world.getTempChatUsers(entity.createChat[1]).then(userIds => {
let players = userIds.join(',').split(',')
for (let i = 0; i < players.length; i++) {
world.querySelectorAll('player').forEach(async (e) => {
if (e.player.userId == players[i]) {
e.player.dialog({
type: GameDialogType.TEXT,
content: `频道创建人@${entity.player.name} 退出了地图`
})
e.createChatId = []
e.joinChatId = []
e.isInChat = false
e.isCreateChat = false
}
})
}
})
world.destroyTempChat([entity.createChat[1]]).then(failedChatIds => {
if (!failedChatIds.length) {
let n = chatTempCreatePlayer.indexOf(entity.player.name)
chatTempCreatePlayer.splice(n, 1)
chatTempId.splice(n, 1)
chatTempName.splice(n, 1)
entity.createChatId = []
entity.joinChatId = []
entity.isInChat = false
entity.isCreateChat = false
} else return;
})
} else if (entity.isInChat && !entity.isCreateChat) {
world.removeTempChatPlayer(entity.joinChat[1], [entity.player.userId]).then(userIds => {
world.querySelectorAll('player').forEach(async (e) => {
if (e.createChat[1] == entity.joinChat[1]) {
e.player.dialog({
type: GameDialogType.TEXT,
content: `@${entity.player.name} 退出了你的频道`
})
}
})
entity.createChatId = []
entity.joinChatId = []
entity.isInChat = false
entity.isCreateChat = false
})
} else {
entity.createChatId = []
entity.joinChatId = []
entity.isInChat = false
entity.isCreateChat = false
}
})
注*2:理解代码可以使用吉PT插件,十分细节