@nxg-org/mineflayer-util-plugin
Version:
mineflayer utils for NextGEN mineflayer plugins.
81 lines (80 loc) • 3.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityFunctions = void 0;
const static_1 = require("./static");
class EntityFunctions {
constructor(bot) {
this.bot = bot;
this.healthSlot = 7;
this.healthSlot = Number(this.bot.version.split(".")[1]) <= 16 ? 7 : 9;
}
/**
* TODO: Version specific right now. Generalize. Unknown method.
*
* Checks if main hand is activated.
* @returns boolean
*/
isMainHandActive(entity) {
return (entity !== null && entity !== void 0 ? entity : this.bot.entity).metadata[6] === 2; //as any & (1 | 0)) === (1 | 0);
}
/**
* TODO: Version specific right now. Generalize. Unknown method.
*
* Checks if offhand is activated.
* @returns boolean
*/
isOffHandActive(entity) {
return (entity !== null && entity !== void 0 ? entity : this.bot.entity).metadata[6] === 3; //& (1 | 2)) === (1 | 2);
}
/**
* TODO: Version specific right now. Generalize. Unknown method.
* @param metadata metadata from Prismarine-Entity Entity.
* @returns number
*/
getHealth(entity) {
var _a, _b;
entity !== null && entity !== void 0 ? entity : (entity = this.bot.entity);
const metadata = entity.metadata;
let health = Number(metadata[this.healthSlot]);
if (!health || health === 0)
health = entity === this.bot.entity ? (_a = this.bot.entity.health) !== null && _a !== void 0 ? _a : 0 : 0;
return health + ((_b = Number(metadata[this.healthSlot + 4])) !== null && _b !== void 0 ? _b : 0);
}
/**
*
* @param metadata Must be FULL metadata object.
* @returns number
*/
getHealthFromMetadata(metadata) {
var _a;
return (_a = Number(metadata[this.healthSlot]) + Number(metadata[this.healthSlot + 4])) !== null && _a !== void 0 ? _a : undefined;
}
/**
* TODO: Version specific right now. Generalize. Unknown method.
* @param metadata metadata from Prismarine-Entity Entity.
* @returns
*/
getHealthChange(packetMetadata, entity) {
const oldHealth = this.getHealthFromMetadata(entity.metadata);
const newHealth = this.getHealthFromMetadata(this.parseMetadata(packetMetadata, entity.metadata));
return newHealth - oldHealth;
}
entityDistance(entity) {
return this.bot.entity.position.distanceTo(entity.position);
}
eyeDistanceToEntity(entity) {
return static_1.AABBUtils.getEntityAABB(entity).distanceToVec(this.bot.entity.position.offset(0, this.bot.entity.height, 0));
}
eyeDistanceBetweenEntities(first, second) {
return static_1.AABBUtils.getEntityAABB(second).distanceToVec(first.position.offset(0, first.height, 0));
}
parseMetadata(packetMetadata, entityMetadata = {}) {
if (packetMetadata !== undefined) {
for (const { key, value } of packetMetadata) {
entityMetadata[key] = value;
}
}
return entityMetadata;
}
}
exports.EntityFunctions = EntityFunctions;