mcws.js is a library that makes it easy to start a Minecraft WebSocket server.
$ yarn add @hrtk92/mcwsjs
Here is a detailed explanation of how to use this library:
- Step 1: Import the library in your project
- Step 2: Create an instance of the
mcws
class, passing in the host and port as arguments - Step 3: Call the
createServer()
method to start the server - Step 4: Register event listeners using the
on()
method to receive events from the Minecraft server - Step 5: Use the
sendCommand()
method to send commands to the Minecraft server
import { mcws, Events } from '@hrtk92/mcwsjs'
const mcserver = new mcws('localhost', 8000)
mcserver.onReady((host, port) => {
console.log('Server started')
console.log(`/wsserver ${host}:${port}`)
})
mcserver.onConnection(() => {
console.log('Connected to Minecraft')
mcserver.sendCommand('say Connected') // send command
mcserver.subscribe(Events.PlayerMessage) // register events to receive
})
mcserver.on(Events.PlayerMessage, (data) => {
console.log(`${data.body.message} by ${data.body.sender}`)
})
mcserver.onDisconnect(() => {
console.log('Disconnected')
})
mcserver.createServer() // start the server
Called when the server is started
Called when connected to Minecraft
Called when the connection is disconnected
Called when the event specified by event occurs
Note
I used this gist as a reference to create an event list.
mcserver.sendCommand('say hello')
You can send commands.
Warning
There is no need to add a/
at the beginning.