cubbot/src/modules/State.ts

96 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-04-19 17:02:07 +00:00
import Module from '../utils/Module'
import { IDelta, IPosition, IState } from '../utils/types'
// MAYBE: split
2020-04-17 14:26:33 +00:00
interface IDifficulty {
level: number,
locked: boolean
}
interface IAbilities {
2020-04-19 17:02:07 +00:00
// TODO: Flags Byte Bit mask. 0x08: damage disabled (god mode), 0x04: can fly, 0x02: is flying, 0x01: is Creative
2020-04-17 14:26:33 +00:00
flyingSpeed: number,
walkingSpeed: number
}
2020-04-19 17:02:07 +00:00
interface IPositionPacket extends IPosition {
flags: number
2020-04-17 14:26:33 +00:00
teleportId: number
}
2020-04-19 17:02:07 +00:00
// TODO: player_info
2020-04-17 14:26:33 +00:00
2020-04-19 17:02:07 +00:00
/** Handle client state and position */
export default class State extends Module<{ positionConfirm: boolean }> {
2020-04-17 14:26:33 +00:00
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
}
2020-04-19 17:02:07 +00:00
public translate(delta: IDelta, onGround: boolean = true) {
this.client.write('position', {
onGround,
x: this.position.x += delta.dX,
y: this.position.y += delta.dY,
z: this.position.z += delta.dZ,
})
}
protected mount() {
2020-04-17 14:26:33 +00:00
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)
2020-04-19 17:02:07 +00:00
this.client.on('position', (data: IPositionPacket) => {
2020-04-17 14:26:33 +00:00
if (this._position == null) {
if (data.flags) {
2020-04-19 17:02:07 +00:00
this.logger.error('first position is relative')
2020-04-17 14:26:33 +00:00
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),
}
}
2020-04-19 17:02:07 +00:00
if (this.conf.positionConfirm) {
2020-04-17 14:26:33 +00:00
this.client.write('teleport_confirm', { teleportId: data.teleportId })
}
this.logger.debug({ type: 'teleported', position: this.position })
})
}
2020-04-19 17:02:07 +00:00
protected getConf() {
return { positionConfirm: true }
}
2020-04-17 14:26:33 +00:00
}
2020-04-19 17:02:07 +00:00
// TODO: vehicule_move