Skip to content

HRTK92/mcws.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mcws.js

npm npm GitHub Release

mcws.js is a library that makes it easy to start a Minecraft WebSocket server.

Install

$ yarn add @hrtk92/mcwsjs

Usage

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

Example

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

Events

onReady

Called when the server is started

onConnect

Called when connected to Minecraft

onDisconnect

Called when the connection is disconnected

on(event, callback)

Called when the event specified by event occurs

Note
I used this gist as a reference to create an event list.

Command

mcserver.sendCommand('say hello')

You can send commands.

Warning
There is no need to add a / at the beginning.