使用事件(我的世界基岩 API)将玩家信息从客户端发送到服务器



我一直在尝试使用BETA API创建一个Minecraft BEDROCK Windows 10 MOD。 我目前正在关注{源代码}和Steven Blom的教程,但这些教程已经过时了,而且数量不多。

现在,我已经创建了一个客户端脚本,而不是每秒发送一个自定义事件。

// CLIENT SCRIPT :
// function called once
clientSystem.initialize = function ()
{
let customData = {}; // I would like this to be a player object but for now it's just an empty object
clientSystem.registerEventData("myScript:replaceStone", customData); // register a custom event
}
let tps = 20;           // tick per seconds
let tickNumber = 0;     // counter
let tickMax = 1 * tps;  // when tickNumber reach this % limit, an event is sent
// function called every tick so 20 times per second :
clientSystem.update = function()
{
if (!(++tickNumber % tickMax)) // each second, this condition is true so we enter the if
{
// we create a specific object for replace stone event
let d = clientSystem.createEventData("myScript:replaceStone");
// we call the event and send the specific object with it. (in this case, an empty object)
clientSystem.broadcastEvent("myScript:replaceStone", d);
}
}
// THEN in my server.js script :
/*
** server register the custom event then, listen for it.
** everything works so far.
*/
serverSystem.initialize = function ()
{
// server register to custom event
let rpStoneData = {};
serverSystem.registerEventData("myScript:replaceStone", rpStoneData);
serverSystem.listenForEvent("myScript:replaceStone", eventData => serverSystem.replaceStone(eventData));
}
/*
** Then the function replaceStone is called and a debug message event is called. 
*/
serverSystem.replaceStone = function(eventData)
{
let chatEventData = serverSystem.createEventData("minecraft:display_chat_event");
chatEventData.data.message = "ReplaceStone has been called";
serverSystem.broadcastEvent("minecraft:display_chat_event", chatEventData);
}

好吧,我想通过自定义事件"替换石头"将玩家信息从客户端发送到服务器端。这意味着与其将空对象发送到事件,不如发送客户端的玩家信息。例如玩家的位置。 在替换石头中,我可以执行一个名为"minecraft:execute_command"的事件并执行一个特定的命令,如"/setblock x y-1 z stone",x,y,z 将是玩家的位置! 希望足够清楚,谢谢你的帮助!

您可以在自定义事件中发送数据,如下所示:

if (!(++tickNumber % tickMax)) // each second, this condition is true so we enter the if
{          
let d = clientSystem.createEventData("myScript:replaceStone");
d.data.playerPosition = {x: 0, y: 0, z: 0};
clientSystem.broadcastEvent("myScript:replaceStone", d);
}

并在注册中定义事件,如下所示:

clientSystem.registerEventData("myScript:replaceStone", {playerPosition: null}); 

顺便说一下,没有必要注册"myScript:replaceStone"两次。您是在客户端和服务器上注册它,但在发送事件的端注册就足够了。在这种情况下,请在客户端注册它,因为它是发件人。服务器无需注册即可侦听事件。

相关内容

  • 没有找到相关文章

最新更新