@nxg-org/mineflayer-util-plugin
Version:
mineflayer utils for NextGEN mineflayer plugins.
141 lines (140 loc) • 6.29 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MovementFunctions = exports.lerp = void 0;
const vec3_1 = require("vec3");
const static_1 = require("./static");
const LOOK_EPSILON = 1e-2;
function lerp(f, f2, f3) {
return f2 + f * (f3 - f2);
}
exports.lerp = lerp;
function* interpolate(start, end, steps = 1) {
for (let i = 0; i < steps; i++) {
yield new vec3_1.Vec3(lerp(i / steps, start.x, end.x), lerp(i / steps, start.y, end.y), lerp(i / steps, start.z, end.z));
}
}
function deltaRad(a, b) {
const pi = Math.PI;
const tau = Math.PI * 2;
let delta = (a - b) % tau;
if (delta < -pi)
delta += tau;
else if (delta > pi)
delta -= tau;
return delta;
}
function getHorizontalCollision(bot) {
var _a;
return !!((_a = bot.entity.horizontalCollision) !== null && _a !== void 0 ? _a : bot.entity.isCollidedHorizontally);
}
class MovementFunctions {
constructor(bot) {
this.bot = bot;
this.lastSentYaw = NaN;
this.lastSentPitch = NaN;
this.pendingLookSync = null;
this.trackSentRotation = this.captureSentRotation.bind(this);
this.bot.on("spawn", this.trackSentRotation); // should clear the sent rotation when the bot respawns,
this.bot.on("move", this.trackSentRotation);
}
captureSentRotation() {
this.lastSentYaw = this.bot.entity.yaw;
this.lastSentPitch = this.bot.entity.pitch;
this.resolvePendingLookSync();
}
isRotationSynced(yaw, pitch, epsilon = LOOK_EPSILON) {
if (!Number.isFinite(this.lastSentYaw) || !Number.isFinite(this.lastSentPitch))
return false;
return (Math.abs(deltaRad(yaw, this.lastSentYaw)) < epsilon &&
Math.abs(deltaRad(pitch, this.lastSentPitch)) < epsilon);
}
resolvePendingLookSync() {
const pending = this.pendingLookSync;
if (!pending)
return;
if (!this.isRotationSynced(pending.yaw, pending.pitch, pending.epsilon))
return;
this.pendingLookSync = null;
pending.resolve();
}
waitForLookSync(yaw, pitch, epsilon = LOOK_EPSILON) {
if (this.pendingLookSync) {
const error = new Error("MovementFunctions lookSync was overwritten by a newer lookSync/lookAtSync call before it synced.");
const pending = this.pendingLookSync;
this.pendingLookSync = null;
pending.reject(error);
}
if (this.isRotationSynced(yaw, pitch, epsilon))
return Promise.resolve();
return new Promise((resolve, reject) => {
const pending = { yaw, pitch, epsilon, resolve, reject };
this.pendingLookSync = pending;
this.resolvePendingLookSync();
});
}
lookSync(yaw, pitch, force = true, epsilon = LOOK_EPSILON) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const activeRequest = { yaw, pitch, epsilon };
const sync = this.waitForLookSync(yaw, pitch, epsilon);
try {
console.time("lookSync");
yield this.bot.look(yaw, pitch, force);
yield sync;
console.timeEnd("lookSync");
}
catch (error) {
if (((_a = this.pendingLookSync) === null || _a === void 0 ? void 0 : _a.yaw) === activeRequest.yaw &&
((_b = this.pendingLookSync) === null || _b === void 0 ? void 0 : _b.pitch) === activeRequest.pitch &&
((_c = this.pendingLookSync) === null || _c === void 0 ? void 0 : _c.epsilon) === activeRequest.epsilon) {
this.pendingLookSync = null;
}
throw error;
}
});
}
lookAtSync(pos, force = true, epsilon = LOOK_EPSILON) {
return __awaiter(this, void 0, void 0, function* () {
const { yaw, pitch } = static_1.MathUtils.pointToYawAndPitch(this.bot, pos);
yield this.lookSync(yaw, pitch, force, epsilon);
});
}
forceLook(yaw, pitch, update = false, onGround) {
const packet = { yaw: static_1.MathUtils.toNotchianYaw(yaw), pitch: static_1.MathUtils.toNotchianPitch(pitch) };
packet.onGround = onGround !== null && onGround !== void 0 ? onGround : this.bot.entity.onGround;
packet.flags = {
onGround: packet.onGround,
hasHorizontalCollision: getHorizontalCollision(this.bot),
};
this.bot._client.write("look", packet);
if (update) {
this.bot.look(yaw, pitch, true);
this.bot.entity.pitch = pitch;
this.bot.entity.yaw = yaw;
}
}
forceLookAt(pos, update = false, onGround) {
const { yaw, pitch } = static_1.MathUtils.pointToYawAndPitch(this.bot, pos);
return this.forceLook(yaw, pitch, update, onGround);
}
lazyTeleport(endPos, steps = 1, update = false) {
for (const pos of interpolate(this.bot.entity.position, endPos, steps)) {
const block = this.bot.blockAt(pos.offset(0, -0.01, 0));
if (!block)
break;
this.bot._client.write("position", Object.assign(Object.assign({}, pos), { onGround: block.transparent }));
}
if (update)
this.bot.entity.position.set(endPos.x, endPos.y, endPos.z);
}
}
exports.MovementFunctions = MovementFunctions;