Power Tools For Minecraft Bedrock

Here's a little javascript to enable any object to act like a tool that performs a /command on interaction with a block. It's a server script so it goes in the /scripts/server folder of your bedrock behavior pack as .js file.

Eventually as more of the dev API rolls out there'll be more elegant solutions, but I think this one is pretty rad.

/** Stellario's "Power Tool" Server Script
Run any command from any item by right-clicking on a block like a tool.
Edit the list with the minecraft:item_id and /command you want that item to run.
Remove or comment out any example ids and command you don't use.

Example: 
["minecraft:item_id_name", "/command"], 

Save this script to "behavior_pack_name/scripts/server/"

Command Replacements: 
    PLAYER => player name
    PP_X, PP_Y, PP_Z => player's X,Y,Z coordinates
    ROT_X, ROT_Y => player's head rotation   
    THE_ITEM => minecraft ID of item used
    POS_X,POS_Y,_POS_Z => interacted block coordinates
**/


var itemCommands = new Map([
    ["minecraft:stick", "/say Hello PLAYER at (PP_X, PP_Y, PP_Z) with head rotation (ROT_X, ROT_Y) interacting with THE_ITEM on a block at (POS_X, POS_Y, POS_Z) how are you?"],
    ["my_behavior_pack:ultra_short_range_teleporter", "/tp PLAYER POS_X POS_Y POS_Z"], //remember the commas
    ["my_behavior_pack:cookie_jar", "/give PLAYER cookie"] //no comma here on last pair
]);

//No Need to edit after this part.

const serverSystem = server.registerSystem(0, 0);

//Initialies the entire script, tells it to watch out for players interacting with blocks (right-click)
serverSystem.initialize = function () {
    this.listenForEvent("minecraft:block_interacted_with", eventData => this.onBlockInteraction(eventData));
};

//Callback Function
serverSystem.commandCallback = function (commandResultData) {
    //If there's an error, send it to chat.
    if (parseInt(commandResultData.data.statusCode) != 0) {
        serverSystem.say(commandResultData);
    }
};

//Function to send message to chat mostly for debugging
serverSystem.say = function (message) {
    let chatMessage = this.createEventData("minecraft:display_chat_event");
    let messageToSend = message;
    if (typeof (message) == "object") { //legibly print object to chat if object
        messageToSend = JSON.stringify(message, null, 2);
    } else { //otherwise just print message
        messageToSend = message;
    }
    chatMessage.data.message = messageToSend;
    this.broadcastEvent("minecraft:display_chat_event", chatMessage);
}


//Whenever a player left interacts with a block, if its one of the list items, execute command.
serverSystem.onBlockInteraction = function (eventData) {
    //Find what's in the player's hand
    let handContainer = serverSystem.getComponent(eventData.data.player, "minecraft:hand_container");
    let mainHandItem = handContainer.data[0].item;

    //check if it's an item from our list 
    if (itemCommands.has(mainHandItem)) {
        //Get theappropriate command from the list
        let theCommand = itemCommands.get(mainHandItem);

        //Do a bunch of string replacements on the command for added functionality
        theCommand = theCommand.replace(/THE_ITEM/g, mainHandItem);
        theCommand = theCommand.replace(/POS_X/g, eventData.data.block_position.x);
        theCommand = theCommand.replace(/POS_Y/g, eventData.data.block_position.y);
        theCommand = theCommand.replace(/POS_Z/g, eventData.data.block_position.z);
        let theComponent = serverSystem.getComponent(eventData.data.player, "minecraft:nameable");
        theCommand = theCommand.replace(/PLAYER/g, theComponent.data.name);
        theComponent = serverSystem.getComponent(eventData.data.player, "minecraft:position");
        theCommand = theCommand.replace(/PP_X/g, theComponent.data.x.toFixed(2));
        theCommand = theCommand.replace(/PP_Y/g, theComponent.data.y.toFixed(2));
        theCommand = theCommand.replace(/PP_Z/g, theComponent.data.z.toFixed(2));
        theComponent = serverSystem.getComponent(eventData.data.player, "minecraft:rotation");
        theCommand = theCommand.replace(/ROT_X/g, theComponent.data.x.toFixed(3));
        theCommand = theCommand.replace(/ROT_Y/g, theComponent.data.y.toFixed(3));

        //execute the command
        serverSystem.executeCommand(theCommand, (commandResultData) => this.commandCallback(commandResultData));
    }
        
};

Comments

  1. Dedicated Minecraft Servers are safer from attacks as the gaming world is located in your own server setting. Take for instance, if your Minecraft gaming is hosted in a shared server, other clients on the same server may be on the receiving end of a DDOS assault. In this scenario, the server is struggling with traffic, negatively affecting game play. https://serverbrowse.com/

    ReplyDelete
  2. If editing is your main aim, use Dreamweaver 3, 1st Page 2000, Adobe Golive, Topstyle, and Webber. Natnit Blog

    ReplyDelete
  3. You have a real ability to write a content that is helpful for us. Thank you for your efforts in sharing such blogs to us. Servers For Minecraft

    ReplyDelete
  4. I read this article, it is really informative one. Your way of writing and making things clear is very impressive. Thanking you for such an informative article.minecraft coding course for kids

    ReplyDelete
  5. I really appreciate your work which you have shared here about the Replacement Battery for Makita Power Tools. The article you have shared here is very informative and the points you have mentioned are very helpful. Thank you so much.12V NiMH Replacement Battery for Makita Power Tools

    ReplyDelete
  6. I’m impressed with your blog. It's an interesting post. I will definitely share this kind of informative post with my friends. It's a valuable post for everyone. I hope you post again soon. Thank you so much for the useful info. I recommend you the best machine tools in dubai

    ReplyDelete
  7. Great job for publishing such a nice article. Your article isn’t only useful but it is additionally really informative. Thank you because you have been willing to share information with us.PhD thesis writers India Site.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hello World