Minecraft基岩版 @minecraft/server 模块的beforechat事件

huangapple go评论83阅读模式
英文:

Minecraft bedrock edition @minecraft/server module beforechat event

问题

{
    "module_name": "@minecraft/server",
    "version": "1.2.0-beta"
}
import * as server from "@minecraft/server";

// 这是监听聊天事件的旧版本(1.1.0-beta):
server.world.events.beforeChat.subscribe(function (eventData) {
    let Player = eventData.sender;
    let Message = eventData.message;
    Player.tell($`[${Player}]: ${Message}`);
}

"server.world.events.beforeChat.subscribe" 导致错误:
TypeError: 无法读取未定义的属性 'subscribe'

我希望服务器能够监听聊天消息事件,但我不确定如何在新版本中实现。我已查看了Microsoft的文档:

Microsoft没有提供任何使用示例,我找不到其他资源。

英文:

Manifest dependencies

{
	"module_name": "@minecraft/server",
	"version": "1.2.0-beta"
},

Main.js

import * as server from "@minecraft/server"

// This is the old version for listening to a chat event (1.1.0-beta):
server.world.events.beforeChat.subscribe(function (eventData) {
	let Player = eventData.sender
	let Message = eventData.message
	Player.tell($`[${Player}]: ${Message}`)
}

"server.world.events.beforeChat.subscribe" is causing the error:
TypeError: cannot read property 'subscribe' of undefined

I want the server to listen to chat message events but I'm not sure how I do that with the new version. I've looked at the Microsoft documentation:

Microsoft has not provided any usage examples and I cannot find any other resources.

答案1

得分: 1

以下是您提供的代码的中文翻译:

// 对于最新版本的 @minecraft-server,我更改了您的脚本如下:

import * as server from "@minecraft/server"

server.world.beforeEvents.chatSend.subscribe(function (eventData) {
    let player = eventData.sender
    let message = eventData.message

    switch (message) {
        case "hello":
            player.sendMessage("你好!")
            break
        case "!creative":
            eventData.cancel = true
            player.runCommand("gamemode c")
            break;
    }
})

// 我只是替换了函数,并使用 switch 语句来检测玩家是否在聊天中发送了 "hello"。请记住,使用 sendMessage 函数将始终在玩家的消息之前发送。要修复这个问题,只需将 beforeEvents 更改为 afterEvents。

// 在第二种情况下,我加入了 "eventData.cancel = true" 来删除玩家的消息,类似于命令。

// 请记得将您的 @minecraft-server 版本更新到 manifest.json 中的 1.3.0-beta 版本!

"modules": [
    {
        "type": "script",
        "language": "javascript",
        "uuid": /*在此处插入UUID*/,
        "entry": "scripts/main.js",
        "version": [
            1, 0, 0
        ]
    }
],
"dependencies": [
    {
        "module_name": "@minecraft/server",
        "version": "1.3.0-beta"
    }
]

如果您有任何问题,请随时告诉我。

英文:

For the latest version of @minecraft-server, I changed up your script to look like this:

import * as server from "@minecraft/server"

server.world.beforeEvents.chatSend.subscribe(function (eventData) {
    let player = eventData.sender
    let message = eventData.message

    switch (message) {
        case "hello":
            player.sendMessage("Hello!")
            break
        case "!creative":
            eventData.cancel = true
            player.runCommand("gamemode c")
            break;
    }
})

I just replace the function and put in a switch statement to detect if a player sends a hello in chat. Keep in mind, using the sendMessage function will always guarantee to send before the players message. To fix this just change beforeEvents to afterEvents.

In the second case I put "eventData.cancel = true" to delete the players message similar to commands.

Remember to update your @minecraft-server version to 1.3.0-beta in your manifest.json!

"modules": [
	{
		"type": "script",
		"language": "javascript",
		"uuid": /*INSERT A UUID HERE*/,
		"entry": "scripts/main.js",
		"version": [
			1, 0, 0
		]
	}
],
"dependencies": [
	{
		"module_name": "@minecraft/server",
		"version": "1.3.0-beta"
	}
]

I've only just started learning scripting in Bedrock so if there's any issues here let me know.

huangapple
  • 本文由 发表于 2023年6月5日 17:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定