Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/discover/src/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ export class Discover {
* @returns An unsubscribe function to deregister the service
*/
register<M = Record<string, any>>(type: string, meta?: M): () => void {
const _meta = meta ?? {}

const n = this.#nextInstanceIds.get(type) ?? 1
this.#nextInstanceIds.set(type, n + 1)

const hostname = n === 1
? `${type}.discover`
: `${type.replace(/^([^.]+)/, `$1-${n}`)}.discover`

const body = JSON.stringify({ meta: _meta })
const body = meta !== undefined ? JSON.stringify({ meta }) : '{}'

const post = (): void => {
const nonce = unique()
Expand All @@ -110,7 +108,7 @@ export class Discover {

const timer = system.runInterval(post, ms2ticks(this.#options.heartbeatInterval))

const registration: LocalRegistration = { fullname: hostname, meta: _meta, timer }
const registration: LocalRegistration = { fullname: hostname, meta, timer }
this.#localServices.set(hostname, registration)

return () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/ipc/src/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class Chunker {
}
this.#buffer.set(id, pending)
}
else if (pending.compressed !== compressed) {
this.#buffer.delete(id)
return { done: false }
}

if (pending.fragments[seq] !== undefined) {
return { done: false }
Expand Down
2 changes: 1 addition & 1 deletion packages/ipc/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class IPC {

const seq = url.searchParams.get('seq')
const total = url.searchParams.get('total')
const compressed = url.searchParams.get('c') !== null
const compressed = url.searchParams.get('c') === '1'

if (seq !== null && total !== null) {
this.#handleChunk(id, channel, Number(seq), Number(total), payload, compressed)
Expand Down
7 changes: 6 additions & 1 deletion packages/protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,14 @@ export class Protocol {
}
}

once(handler: (event: BedrockReceiveEvent) => void): () => void {
once(handler: (event: BedrockReceiveEvent) => void): () => void
once(handler: (event: BedrockReceiveEvent) => void, options: { sourceType?: ScriptEventSource }): () => void
once(handler: (event: BedrockReceiveEvent) => void, options?: { sourceType?: ScriptEventSource }): () => void {
let off: () => void
const wrapped = (event: BedrockReceiveEvent): void => {
if (options?.sourceType && event.sourceType !== options.sourceType) {
return
}
off()
handler(event)
}
Expand Down
14 changes: 11 additions & 3 deletions packages/rpc/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,19 @@ export class RPC {

try {
const parsed = JSON.parse(body)
if (parsed && typeof parsed === 'object' && 'error' in parsed) {
pending.reject(new Error(parsed.error as string))
if (parsed && typeof parsed === 'object') {
if ('error' in parsed) {
pending.reject(new Error(parsed.error as string))
}
else if ('data' in parsed) {
pending.resolve(parsed.data)
}
else {
pending.reject(new Error('RPC: invalid response format — expected {data} or {error}'))
}
}
else {
pending.resolve(parsed && 'data' in parsed ? parsed.data : undefined)
pending.reject(new Error('RPC: invalid response body'))
}
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm install @mcbe-mods/runtime
| `@mcbe-mods/log` | `Log` | `LogLevel`, `LogOptions` |
| `@mcbe-mods/protocol` | `Protocol` | `BedrockReceiveEvent`, `ProtocolCipher`, `ProtocolOptions` |
| `@mcbe-mods/rpc` | `RPC` | `RPCOptions` |
| `@mcbe-mods/utils` | `Base64`, `color`, `unique`, `ms2ticks`, … | (all types included) |
| `@mcbe-mods/utils` | `Base64`, `Color`, `unique`, `ms2ticks`, … | (all types included) |

```ts
import { Base64, Cipher, Discover, IPC, Log, Protocol, RPC } from '@mcbe-mods/runtime'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class Experience {
* @returns {this} self
*/
setLevel(level: number): this {
this.#xp = 0
this.#level = 0
this.addLevel(level)
return this
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export { Base64 } from './base64'
export { Color } from './color'
export type { StyleId, Stylizer } from './color'
export { Experience } from './Experience'
export { getCubeRange, getSphereRange } from './getCubeRange'
export type { Location } from './getCubeRange'
export { Experience } from './experience'
export { getRandomProbability } from './getRandomProbability'
export { getRandomRangeValue } from './getRandomRangeValue'
export { ms2ticks } from './ms2ticks'
export { getCubeRange, getSphereRange } from './range'
export type { Location } from './range'
export { splitGroups } from './splitGroups'
export { utf8Decode, utf8Encode } from './textCodec'
export { unique } from './unique'
12 changes: 0 additions & 12 deletions packages/utils/src/getCubeRange.ts → packages/utils/src/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ export interface Location {
z: number
}

/**
* With {@link location} as the center, get all coordinates within a cube of side length `(2*radius+1)`.
* @param location - Center position
* @param radius - Half side length (default 1)
* @returns Array of positions within the cube
*/
export function getCubeRange(location: Location, radius: number = 1): Location[] {
const centerX = location.x
const centerY = location.y
Expand All @@ -27,12 +21,6 @@ export function getCubeRange(location: Location, radius: number = 1): Location[]
return positions
}

/**
* With {@link location} as the center, get all coordinates within a sphere of radius {@link radius}.
* @param location - Center position
* @param radius - Sphere radius (default 1)
* @returns Array of positions within the sphere
*/
export function getSphereRange(location: Location, radius: number = 1): Location[] {
const centerX = location.x
const centerY = location.y
Expand Down
Loading