UNPKG

@nxg-org/mineflayer-physics-util

Version:

Provides functionality for more accurate entity and projectile tracking.

133 lines (132 loc) 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlayerControls = exports.ControlStateHandler = void 0; const mineflayer_util_plugin_1 = require("@nxg-org/mineflayer-util-plugin"); ; class ControlStateHandler { constructor(forward, back, left, right, jump, sprint, sneak) { this.forward = forward; this.back = back; this.left = left; this.right = right; this.jump = jump; this.sprint = sprint; this.sneak = sneak; } static DEFAULT() { return new ControlStateHandler(false, false, false, false, false, false, false); } static COPY_BOT(bot) { return new ControlStateHandler(bot.getControlState("forward"), bot.getControlState("back"), bot.getControlState("left"), bot.getControlState("right"), bot.getControlState("jump"), bot.getControlState("sprint"), bot.getControlState("sneak")); } static COPY_STATE(state) { return new ControlStateHandler(state.control.forward, state.control.back, state.control.left, state.control.right, state.control.jump, state.control.sprint, state.control.sneak); } set(state, wanted) { this[state] = wanted; return this; } get(state) { return this[state]; } clear(state) { this[state] = false; return this; } reset() { this.forward = false; this.back = false; this.left = false; this.right = false; this.jump = false; this.sprint = false; this.sneak = false; return this; } clone() { return new ControlStateHandler(this.forward, this.back, this.left, this.right, this.jump, this.sprint, this.sneak); } equals(other) { return this.forward == other.forward && this.back == other.back && this.left == other.left && this.right == other.right && this.jump == other.jump && this.sprint == other.sprint && this.sneak == other.sneak; } applyControls(bot) { for (const move of this) { bot.setControlState(move[0], move[1]); } } *[Symbol.iterator]() { yield ["forward", this.forward]; yield ["back", this.back]; yield ["left", this.left]; yield ["right", this.right]; yield ["jump", this.jump]; yield ["sprint", this.sprint]; yield ["sneak", this.sneak]; } } exports.ControlStateHandler = ControlStateHandler; class PlayerControls extends ControlStateHandler { constructor(forward, back, left, right, jump, sprint, sneak, leftClick, rightClick, yaw, pitch, force = false) { super(forward, back, left, right, jump, sprint, sneak); this.leftClick = leftClick; this.rightClick = rightClick; this.yaw = yaw; this.pitch = pitch; this.force = force; this.leftClick = leftClick; this.rightClick = rightClick; } static DEFAULT() { return new PlayerControls(false, false, false, false, false, false, false, false, false, NaN, NaN); } static LOOK(yaw, pitch, force) { return new PlayerControls(false, false, false, false, false, false, false, false, false, yaw, pitch, force); } static LOOKAT(pos, force) { const info = mineflayer_util_plugin_1.MathUtils.dirToYawAndPitch(pos); return new PlayerControls(false, false, false, false, false, false, false, false, false, info.yaw, info.pitch, force); } static COPY_BOT(bot) { return new PlayerControls(bot.controlState.forward, bot.controlState.back, bot.controlState.left, bot.controlState.right, bot.controlState.jump, bot.controlState.sprint, bot.controlState.sneak, bot.util.entity.isMainHandActive(), bot.util.entity.isOffHandActive(), bot.entity.yaw, bot.entity.pitch, false); } static COPY_STATE(state) { return new PlayerControls(state.control.forward, state.control.back, state.control.left, state.control.right, state.control.jump, state.control.sprint, state.control.sneak, state.isUsingMainHand, state.isUsingOffHand, state.yaw, state.pitch, false); } clone() { return new PlayerControls(this.forward, this.back, this.left, this.right, this.jump, this.sprint, this.sneak, this.leftClick, this.rightClick, this.yaw, this.pitch, this.force); } setRot(dir) { const tmp = mineflayer_util_plugin_1.MathUtils.dirToYawAndPitch(dir); this.yaw = tmp.yaw; this.pitch = tmp.pitch; } setRotRaw(yaw, pitch) { this.yaw = yaw; this.pitch = pitch; } *movements() { yield this.forward; yield this.back; yield this.left; yield this.right; yield this.jump; yield this.sprint; yield this.sneak; } *linkedMovements() { yield ["forward", this.forward]; yield ["back", this.back]; yield ["left", this.left]; yield ["right", this.right]; yield ["jump", this.jump]; yield ["sprint", this.sprint]; yield ["sneak", this.sneak]; } } exports.PlayerControls = PlayerControls;