bedrock-portal 0.8.8 → 0.8.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,46 @@ main();
59
59
 
60
60
  Modules are used to extend the functionality of the BedrockPortal class.
61
61
 
62
+ ### ServerFormList
63
+
64
+ Instead of redirecting players to a server, the portal will display a list of servers to join from a form. `#.use(Modules.serverFormList, options);`
65
+
66
+ Options:
67
+ - **form**: Form - The form to display to the player. At the moment only simple forms are supported. (required)
68
+ - **timeout**: number - The time in milliseconds before the player is kicked from the session if they don't select a server (default: 60000ms)
69
+ - **timeoutMessage**: string - The message to display to the player when they are kicked from the session (default: 'You took too long to select a server!')
70
+
71
+ The form will be sent to the player when they join the server and every 5 seconds after they clost the form.
72
+
73
+ ```js
74
+ const { BedrockPortal, Joinability, Modules } = require('bedrock-portal')
75
+ const { Authflow, Titles } = require('prismarine-auth')
76
+
77
+ const main = async () => {
78
+ const auth = new Authflow('example', './', { authTitle: Titles.XboxAppIOS, deviceType: 'iOS', flow: 'sisu' })
79
+
80
+ const portal = new BedrockPortal(auth)
81
+
82
+ portal.use(Modules.ServerFromList, {
83
+ form: {
84
+ title: '§l§aServer Form List',
85
+ content: '§7Please select a server to join',
86
+ buttons: [
87
+ { text: '§8Anarchy Server\n§7Click Here§!', ip: 'bedrock.opblocks.com', port: 19132 },
88
+ { text: '§7Creative Server\n§7Click Here§', ip: 'bedrock.opblocks.com', port: 19132 },
89
+ { text: '§6Survival Server\n§7Click Here§!', ip: 'bedrock.opblocks.com', port: 19132 },
90
+ ],
91
+ },
92
+ timeout: 60000,
93
+ timeoutMessage: '§cYou took too long to select a server!',
94
+ })
95
+
96
+ await portal.start()
97
+ }
98
+
99
+ main()
100
+ ```
101
+
62
102
  ### MultipleAccounts
63
103
 
64
104
  Allows the portal to use multiple accounts to redirect players to the server. `#.use(Modules.MultipleAccounts, options);`
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import type { Message } from './types/xblmessaging';
3
3
  import { EventResponse } from 'xbox-rta';
4
4
  import { Authflow } from 'prismarine-auth';
5
5
  import { TypedEmitter } from 'tiny-typed-emitter';
6
+ import { Server } from 'bedrock-portal-nethernet';
6
7
  import Host from './classes/Host';
7
8
  import Player from './classes/Player';
8
9
  import Module from './classes/Module';
@@ -13,6 +14,7 @@ import RedirectFromRealm from './modules/redirectFromRealm';
13
14
  import MultipleAccounts from './modules/multipleAccounts';
14
15
  import AutoFriendAccept from './modules/autoFriendAccept';
15
16
  import UpdateMemberCount from './modules/updateMemberCount';
17
+ import ServerFromList from './modules/serverFormList';
16
18
  type BedrockPortalOptions = {
17
19
  /**
18
20
  * The ip of the server to redirect users to.
@@ -103,7 +105,8 @@ export declare class BedrockPortal extends TypedEmitter<PortalEvents> {
103
105
  };
104
106
  players: Map<string, Player>;
105
107
  modules: Map<string, Module>;
106
- constructor(authflow: Authflow, options: Partial<BedrockPortalOptions>);
108
+ server: Server;
109
+ constructor(authflow: Authflow, options?: Partial<BedrockPortalOptions>);
107
110
  validateOptions(options: BedrockPortalOptions): void;
108
111
  /**
109
112
  * Starts the BedrockPortal instance.
@@ -148,6 +151,7 @@ export declare class BedrockPortal extends TypedEmitter<PortalEvents> {
148
151
  * })
149
152
  */
150
153
  use(mod: ExtendedModule, options?: {}): void;
154
+ onServerConnection: (client: any) => void;
151
155
  private createAndPublishSession;
152
156
  private createSessionBody;
153
157
  }
@@ -160,5 +164,6 @@ declare const Modules: {
160
164
  MultipleAccounts: typeof MultipleAccounts;
161
165
  AutoFriendAccept: typeof AutoFriendAccept;
162
166
  UpdateMemberCount: typeof UpdateMemberCount;
167
+ ServerFromList: typeof ServerFromList;
163
168
  };
164
169
  export { Modules };
package/dist/index.js CHANGED
@@ -19,6 +19,7 @@ const redirectFromRealm_1 = __importDefault(require("./modules/redirectFromRealm
19
19
  const multipleAccounts_1 = __importDefault(require("./modules/multipleAccounts"));
20
20
  const autoFriendAccept_1 = __importDefault(require("./modules/autoFriendAccept"));
21
21
  const updateMemberCount_1 = __importDefault(require("./modules/updateMemberCount"));
22
+ const serverFormList_1 = __importDefault(require("./modules/serverFormList"));
22
23
  const start_game_1 = require("./common/start_game");
23
24
  const util_1 = require("./common/util");
24
25
  const debug = (0, debug_1.default)('bedrock-portal');
@@ -29,10 +30,11 @@ class BedrockPortal extends tiny_typed_emitter_1.TypedEmitter {
29
30
  session;
30
31
  players;
31
32
  modules = new Map();
32
- constructor(authflow, options) {
33
+ server = new bedrock_portal_nethernet_1.Server();
34
+ constructor(authflow, options = {}) {
33
35
  super();
34
36
  this.options = {
35
- ip: '',
37
+ ip: 'bedrock.opblocks.com',
36
38
  port: 19132,
37
39
  joinability: constants_1.Joinability.FriendsOfFriends,
38
40
  webRTCNetworkId: (0, util_1.getRandomUint64)(),
@@ -54,10 +56,6 @@ class BedrockPortal extends tiny_typed_emitter_1.TypedEmitter {
54
56
  this.modules = new Map();
55
57
  }
56
58
  validateOptions(options) {
57
- if (!options.ip)
58
- throw new Error('No IP provided');
59
- if (!options.port)
60
- throw new Error('No port provided');
61
59
  if (!Object.values(constants_1.Joinability).includes(options.joinability))
62
60
  throw new Error('Invalid joinability - Expected one of ' + Object.keys(constants_1.Joinability).join(', '));
63
61
  }
@@ -68,22 +66,13 @@ class BedrockPortal extends tiny_typed_emitter_1.TypedEmitter {
68
66
  this.players = new Map();
69
67
  await this.host.connect();
70
68
  this.session.name = (0, uuid_1.v4)();
71
- const server = new bedrock_portal_nethernet_1.Server({
72
- version: '1.21.30', // The server version
73
- compressionAlgorithm: 'none',
74
- });
75
- server.listen(this.authflow, this.options.webRTCNetworkId);
76
- server.on('connect', client => {
77
- client.on('join', () => {
78
- console.log(`Client ${client.id} has joined the server.`);
79
- client.write('start_game', start_game_1.start_game);
80
- client.once('set_player_game_type', () => {
81
- client.write('transfer', { server_address: this.options.ip, port: this.options.port });
82
- });
83
- });
84
- });
69
+ await this.server.listen(this.authflow, this.options.webRTCNetworkId);
70
+ this.server.on('connect', (client) => this.onServerConnection(client));
85
71
  const session = await this.createAndPublishSession();
86
- this.host.rta.on('event', (event) => (0, Event_1.default)(this, event));
72
+ this.host.rta.on('event', (event) => {
73
+ (0, Event_1.default)(this, event)
74
+ .catch(e => debug('Failed to handle event', e));
75
+ });
87
76
  if (this.modules) {
88
77
  for (const mod of this.modules.values()) {
89
78
  mod.run(this)
@@ -174,6 +163,14 @@ class BedrockPortal extends tiny_typed_emitter_1.TypedEmitter {
174
163
  constructed.applyOptions(options);
175
164
  this.modules.set(constructed.name, constructed);
176
165
  }
166
+ onServerConnection = (client) => {
167
+ client.once('join', () => {
168
+ client.write('start_game', start_game_1.start_game);
169
+ client.once('set_player_game_type', () => {
170
+ client.write('transfer', { server_address: this.options.ip, port: this.options.port });
171
+ });
172
+ });
173
+ };
177
174
  async createAndPublishSession() {
178
175
  await this.updateSession(this.createSessionBody());
179
176
  debug(`Created session, name: ${this.session.name}, ID: ${this.options.webRTCNetworkId}`);
@@ -257,5 +254,6 @@ const Modules = {
257
254
  MultipleAccounts: multipleAccounts_1.default,
258
255
  AutoFriendAccept: autoFriendAccept_1.default,
259
256
  UpdateMemberCount: updateMemberCount_1.default,
257
+ ServerFromList: serverFormList_1.default,
260
258
  };
261
259
  exports.Modules = Modules;
@@ -0,0 +1,10 @@
1
+ import { BedrockPortal } from '..';
2
+ import Module from '../classes/Module';
3
+ export default class ServerFromList extends Module {
4
+ private n;
5
+ constructor();
6
+ run(portal: BedrockPortal): Promise<void>;
7
+ private sendForm;
8
+ private handleJoin;
9
+ private handleFormResponse;
10
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const Module_1 = __importDefault(require("../classes/Module"));
7
+ const start_game_1 = require("../common/start_game");
8
+ class ServerFromList extends Module_1.default {
9
+ n;
10
+ constructor() {
11
+ super('serverFromList', 'Allows players to join the server from a list of servers');
12
+ this.options = {
13
+ form: {
14
+ title: '§l§aServer Form List',
15
+ content: '§7Please select a server to join',
16
+ buttons: [
17
+ { text: '§8Anarchy Server\n§7Click Here§!', ip: 'bedrock.opblocks.com', port: 19132 },
18
+ { text: '§7Creative Server\n§7Click Here§', ip: 'bedrock.opblocks.com', port: 19132 },
19
+ { text: '§6Survival Server\n§7Click Here§!', ip: 'bedrock.opblocks.com', port: 19132 },
20
+ ],
21
+ },
22
+ timeout: 60000,
23
+ timeoutMessage: '§cYou took too long to respond',
24
+ };
25
+ this.n = 0;
26
+ }
27
+ async run(portal) {
28
+ portal.onServerConnection = (client) => {
29
+ client.once('join', () => this.handleJoin(client));
30
+ client.on('spawn', () => {
31
+ this.sendForm(client);
32
+ });
33
+ setTimeout(() => {
34
+ client.disconnect(this.options.timeoutMessage);
35
+ }, this.options.timeout);
36
+ };
37
+ }
38
+ sendForm(client) {
39
+ client.write('modal_form_request', {
40
+ form_id: this.n++,
41
+ data: JSON.stringify({
42
+ type: 'form',
43
+ title: this.options.form.title,
44
+ content: this.options.form.content,
45
+ buttons: this.options.form.buttons.map((button) => ({ text: button.text })),
46
+ }),
47
+ });
48
+ }
49
+ handleJoin(client) {
50
+ client.write('resource_packs_info', { must_accept: false, has_addons: false, has_scripts: false, texture_packs: [], resource_pack_links: [] });
51
+ client.write('resource_pack_stack', { must_accept: false, behavior_packs: [], resource_packs: [], game_version: '', experiments: [], experiments_previously_used: false });
52
+ client.once('resource_pack_client_response', async () => {
53
+ client.write('start_game', start_game_1.start_game);
54
+ client.write('play_status', { status: 'player_spawn' });
55
+ });
56
+ client.on('modal_form_response', (p) => this.handleFormResponse(p, client));
57
+ }
58
+ handleFormResponse(response, client) {
59
+ if (response.has_cancel_reason) {
60
+ setInterval(() => this.sendForm(client), 5000);
61
+ return;
62
+ }
63
+ const server = this.options.form.buttons[parseInt(response.data)];
64
+ client.write('transfer', { server_address: server.ip, port: server.port });
65
+ }
66
+ }
67
+ exports.default = ServerFromList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bedrock-portal",
3
- "version": "0.8.8",
3
+ "version": "0.8.9",
4
4
  "description": "Creates a joinable session in Minecraft Bedrock Edition that can route players to their a specified host",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",