cubbot/src/modules/State.ts

103 lines
2.7 KiB
TypeScript

import { Client } from 'minecraft-protocol'
import Module from './Module'
//MAYBE: split
interface IState {
entityId: number,
gameMode: 0|1|2|3,
dimension: number,
hashedSeed: number[],
maxPlayers: number,
levelType: string,
viewDistance: number,
reducedDebugInfo: boolean,
enableRespawnScreen: boolean
}
interface IDifficulty {
level: number,
locked: boolean
}
interface IAbilities {
//TODO: Flags Byte Bit mask. 0x08: damage disabled (god mode), 0x04: can fly, 0x02: is flying, 0x01: is Creative
flyingSpeed: number,
walkingSpeed: number
}
interface IPosition {
x: number,
y: number,
z: number,
yaw: number,
pitch: number,
flags: number,
teleportId: number
}
//TODO: entity_status {entityId, entityStatus} aka animations
//TODO: player_info
export default class State extends Module {
public positionConfirm: boolean = true
private _state!: IState
public get state() {
return this._state
}
private _difficulty!: IDifficulty
public get difficulty() {
return this._difficulty
}
private _abilities!: IDifficulty
public get abilities() {
return this._abilities
}
private _position!: IPosition
public get position() {
return this._position
}
public mount() {
this.client.on('login', data => this._state = data)
this.client.on('difficulty', data => this._difficulty = {
level: data.difficulty, locked: data.difficultyLocked,
})
this.client.on('abilities', data => this._abilities = data)
this.client.on('position', (data: IPosition) => {
if (this._position == null) {
if (data.flags) {
this.logger.error('first postion is relative')
return
} else {
this._position = data
}
} else {
// tslint:disable: no-bitwise
this._position = {
x: data.x + (data.flags & 0x01 ? this._position.x : 0),
y: data.y + (data.flags & 0x02 ? this._position.y : 0),
z: data.z + (data.flags & 0x04 ? this._position.z : 0),
yaw: data.yaw + (data.flags & 0x08 ? this._position.yaw : 0),
pitch: data.pitch + (data.flags & 0x10 ? this._position.pitch : 0),
flags: 0,
teleportId : -1,
}
}
if (this.positionConfirm) {
this.client.write('teleport_confirm', { teleportId: data.teleportId })
}
this.logger.debug({ type: 'teleported', position: this.position })
})
}
}