Skip to content

MinecraftPlayground/server-management-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Server Management Client

A Minecraft Server JSON-RPC TypeScript Client.

Run Linter Run Unit Tests

Usage

Basics

import { Client } from './client.ts'


const client = new Client('ws://localhost:25576', {
  token: 'my-secret-token'
});

// Call methods
const allowlist = await client.call('minecraft:allowlist');
console.log('Whitelisted players:', allowlist);

// Listen for notifications
client.addNotificationListener('minecraft:notification/players/joined', ({ player }) => {
  console.log(`${player.name} joined the server`);
});

Getting and setting values

import { Client } from './client.ts'


const client = new Client('ws://localhost:25576', {
  token: 'my-secret-token'
});

// Get current difficulty
const difficulty = await client.call('minecraft:serversettings/difficulty');
console.log('Current difficulty:', difficulty);

// Set difficulty to hard
await client.call('minecraft:serversettings/difficulty/set', { difficulty: 'hard' });
import { Client } from './client.ts'


const client = new Client('ws://localhost:25576', {
  token: 'my-secret-token'
});

// Get all connected players
const players = await client.call('minecraft:players');

// Add player to allowlist
await client.call('minecraft:allowlist/add', {
  add: [{ id: 'uuid-here', name: 'PlayerName' }]
});

// Kick a player
await client.call('minecraft:players/kick', {
  kick: [{
    player: { id: 'uuid', name: 'PlayerName' },
    message: { literal: 'You have been kicked' }
  }]
});

Defining custom methods and notifications

Methods

import { Client } from './client.ts'
import type { MethodObjectDefinition } from './schema/index.ts'
import type { minecraft } from './definitions/index.ts'


/**
 * Greet a player on the server
 */
type CustomPlayerGreetMethod = MethodObjectDefinition<
  'custom:player/greet',
  [{
    player : minecraft.schema.PlayerObject,
    message : string
  }],
  /** player */
  minecraft.schema.PlayerObject
>

const client = new Client<minecraft.Extend<CustomPlayerGreetMethod>>('ws://localhost:25576', {
  token: 'token-from-server.properties'
});

client.call('custom:player/greet', {
  player: { id: 'uuid', name: 'PlayerName' },
  message: 'Hello Player!'
}).then(({ player }) => { console.log(`Greeted player ${player.name}`) })

Notifications

import { Client } from './client.ts'
import type { NotificationObjectDefinition } from './schema/index.ts'
import type { minecraft } from './definitions/index.ts'

/**
 * Get notified when the server greets a player
 */
type CustomPlayerGreetNotification = MethodObjectDefinition<
  'custom:notification/player/greeted',
  [{ player : PlayerObject }]
>

const client = new Client<minecraft.Extend<CustomPlayerGreetNotification>>('ws://localhost:25576', {
  token: 'token-from-server.properties'
});

client.addNotificationListener('custom:notification/player/greeted', ({ player }) => {
  console.log(`Greeted player ${player.name}`)
})

About

A Minecraft Server JSON-RPC TypeScript Client.

Topics

Resources

License

Stars

Watchers

Forks