Enforce server auth, ping on createClient, update docs (#68)

* Add createServer, ping on createClient, update README

* fix createClient keepalive

* resort readme, fix node 14

* Enforce auth on server connections, fix close/connect issues

* add type definitions, update readme, docs

* Wait some time before closing connection, update docs

* wait for server close in tests, fix race bug

* export a ping api

* Rename api.md to API.md

* add ping example
This commit is contained in:
extremeheat 2021-04-18 09:19:59 -04:00 committed by GitHub
commit 999bfd3569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 580 additions and 135 deletions

View file

@ -0,0 +1,17 @@
/* eslint-disable */
const bedrock = require('bedrock-protocol')
const client = bedrock.createClient({
host: 'localhost', // optional
port: 19132, // optional, default 19132
username: 'Notch', // the username you want to join as, optional if online mode
offline: false // optional, default false. if true, do not login with Xbox Live. You will not be asked to sign-in if set to true.
})
client.on('text', (packet) => { // Listen for chat messages and echo them back.
if (packet.source_name != client.options.username) {
client.queue('text', {
type: 'chat', needs_translation: false, source_name: client.username, xuid: '', platform_chat_id: '',
message: `${packet.source_name} said: ${packet.message} on ${new Date().toLocaleString()}`
})
}
})

View file

@ -9,6 +9,7 @@ async function test () {
// You can specify version by adding :
// version: '1.16.210'
})
client.connect()
client.once('resource_packs_info', (packet) => {
client.write('resource_pack_client_response', {

5
examples/ping.js Normal file
View file

@ -0,0 +1,5 @@
const { ping } = require('bedrock-protocol')
ping({ host: 'play.cubecraft.net', port: 19132 }).then(res => {
console.log(res)
})

View file

@ -0,0 +1,14 @@
/* eslint-disable */
const bedrock = require('bedrock-protocol')
const server = new bedrock.createServer({
host: '0.0.0.0', // optional
port: 19132, // optional
version: '1.16.220' // The server version
})
server.on('connect', client => {
client.on('join', () => { // The client has joined the server.
const d = new Date() // Once client is in the server, send a colorful kick message
client.disconnect(`Good ${d.getHours() < 12 ? '§emorning§r' : '§3afternoon§r'} :)\n\nMy time is ${d.toLocaleString()} !`)
})
})

View file

@ -71,25 +71,25 @@ async function startServer (version = '1.16.210', ok) {
client.queue('inventory_slot', { window_id: 120, slot: 0, item: new Item().toBedrock() })
}
client.write('player_list', get('player_list'))
client.write('start_game', get('start_game'))
client.write('item_component', { entries: [] })
client.write('set_spawn_position', get('set_spawn_position'))
client.write('set_time', { time: 5433771 })
client.write('set_difficulty', { difficulty: 1 })
client.write('set_commands_enabled', { enabled: true })
client.write('adventure_settings', get('adventure_settings'))
client.write('biome_definition_list', get('biome_definition_list'))
client.write('available_entity_identifiers', get('available_entity_identifiers'))
client.write('update_attributes', get('update_attributes'))
client.write('creative_content', get('creative_content'))
client.write('inventory_content', get('inventory_content'))
client.write('player_hotbar', { selected_slot: 3, window_id: 'inventory', select_slot: true })
client.write('crafting_data', get('crafting_data'))
client.write('available_commands', get('available_commands'))
client.write('chunk_radius_update', { chunk_radius: 1 })
client.write('game_rules_changed', get('game_rules_changed'))
client.write('respawn', get('respawn'))
client.queue('player_list', get('player_list'))
client.queue('start_game', get('start_game'))
client.queue('item_component', { entries: [] })
client.queue('set_spawn_position', get('set_spawn_position'))
client.queue('set_time', { time: 5433771 })
client.queue('set_difficulty', { difficulty: 1 })
client.queue('set_commands_enabled', { enabled: true })
client.queue('adventure_settings', get('adventure_settings'))
client.queue('biome_definition_list', get('biome_definition_list'))
client.queue('available_entity_identifiers', get('available_entity_identifiers'))
client.queue('update_attributes', get('update_attributes'))
client.queue('creative_content', get('creative_content'))
client.queue('inventory_content', get('inventory_content'))
client.queue('player_hotbar', { selected_slot: 3, window_id: 'inventory', select_slot: true })
client.queue('crafting_data', get('crafting_data'))
client.queue('available_commands', get('available_commands'))
client.queue('chunk_radius_update', { chunk_radius: 1 })
client.queue('game_rules_changed', get('game_rules_changed'))
client.queue('respawn', get('respawn'))
for (const chunk of chunks) {
client.queue('level_chunk', chunk)