Send player information from client to server with event (minecraft bedrock API)

Viewed 828

I've been trying to create a Minecraft BEDROCK Windows 10 MOD with the BETA API . I'm currently following tutorials of {The source Code} and Steven Blom but those tutorials are outdated and they are not so many of them.

For now I've created a client script than send an custom event every second.

// 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);
}

Allright, I want to send the player information from the client to the server side via the custom event "replace stone". It means than instead of having an EMPTY OBJECT to send to the event, I would like to send the client's player information. Like the player's position for example. In Replace stone, I could execute an event called "minecraft:execute_command" and execute a specific command like "/setblock x y-1 z stone", x,y,z would be the player's position ! I hope it's clear enough, thank you for your help !

1 Answers

You can send data in a custom event like this:

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);
    }

And define the event on the registration like this:

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

By the way, it is not necessary to register "myScript:replaceStone" twice. You are registering it on client and server, but doing it on the side that sends the event is enough. In this case, register it on the client side because it's the sender. The server can listen to the event without registration.

Related