bedrockws
Version:
A JavaScript/TypeScript library for Minecraft Bedrock Edition WebSocket API
140 lines (91 loc) โข 4.24 kB
Markdown
# BedrockWS
[](https://www.npmjs.com/package/bedrockws)
[](./LICENSE)
[](https://www.npmjs.com/package/bedrockws)
[](https://discord.gg/TXU5NjDRPQ)
**BedrockWS** is a JavaScript library for creating and handling a Minecraft Bedrock WebSocket server.
It simplifies interaction with the Bedrock WebSocket API by automatically subscribing to events when you listen with `server.on`, and provides convenient utilities for sending commands back to the game.
### โ ๏ธ This library is still in developing and more features coming in future
## ๐ Table of Contents
- [โจ Features](#%E2%9C%A8-features)
- [๐ฆ Installation](#%F0%9F%93%A6-installation)
- [๐ Usage](#%F0%9F%9A%80-usage)
- [Create a server](#create-a-server)
- [Event Subscription](#%F0%9F%94%84-event-subscription)
- [Sending Commands](#%F0%9F%8E%AE-sending-commands)
- [๐ก Available Events](#%F0%9F%93%A1-available-events)
- [๐ Development](#%F0%9F%9B%A0-development)
- [๐ Project Structure](#%F0%9F%93%82-project-structure)
- [๐ License](#%F0%9F%93%9C-license)
## โจ Features
- ๐ **Easy Setup** โ Create a WebSocket server with a single function call.
- ๐ **Automatic Event Subscription** โ No need to manually subscribe to events. `server.on` does it for you.
- ๐ฎ **Command Sending** โ Send Minecraft commands directly to connected clients.
- ๐ก **Many Event Support** โ Supports most Bedrock WebSocket events, such as `PlayerMessage`, `BlockPlaced`, and more(Not every events are tested and some may not work as expected).
- โก **Lightweight & Flexible** โ Works out of the box without heavy dependencies.
## ๐ฆ Installation
Install via npm:
```sh
npm install bedrockws
```
## ๐ Usage
### Create a server
```js
import bedrockws from "bedrockws";
// Create a WebSocket server on port 19132
const server = bedrockws.createServer(19132);
// Listen for a Bedrock event
server.on("PlayerTravelled", (data, ws) => {
console.log(Player travelled: ${data.playerName});
// Send a command back to the game
server.sendCommand(/say Hello ${data.playerName}, ws);
});
```
### ๐ Event Subscription
You donโt need to manually subscribe to events.
When you call:
```js
server.on("EventName", (data, ws) => {
console.log(data);
});
```
The server automatically subscribes to that event for all current and future clients.
### ๐ฎ Sending Commands
You can send Minecraft commands to a client from inside any event callback:
```js
server.sendCommand(command, ws);
```
- **command**: A valid Minecraft command string (e.g. `"/say Hello"`).
- **ws**: The WebSocket client to send the command to (provided in the event callback).
This allows you to react to in-game events dynamically, such as teleporting players, changing the time, or sending custom chat messages.
## ๐ก Available Events
Most Bedrock WebSocket events are supported. Common examples include:
- **PlayerMessage** โ Triggered when a player sends a chat message.
- **PlayerTravelled** โ Fired when a player moves.
- **BlockPlaced** โ Triggered when a block is placed.
- **BlockBroken** โ Triggered when a block is destroyed.
- **SlashCommandExecuted** โ Fired when a player runs a slash command.
For the full event list, see [src/events.js](./src/events.js).
## ๐ Development
### Run tests
There are example test files inside the [test](./test) directory.
Run the tests with:
```sh
npm test
```
## ๐ Project Structure
- [src/](./src) โ Core library code.
- [src/events.js](./src/events.js) โ Contains supported Bedrock events.
- [test/](./test) โ Example usage and test cases.
## ๐ License
[MIT](./LICENSE)