@nxg-org/mineflayer-util-plugin 1.6.3 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { Vec3 } from "vec3";
2
- declare type AABBPoints = [minX: number, minY: number, minZ: number, maxX: number, maxY: number, maxZ: number];
3
- declare type MinAndMaxPoints = [min: [x: number, y: number, z: number], max: [x: number, y: number, z: number]];
4
- declare type Vec3AABB = [min: Vec3, max: Vec3];
2
+ type AABBPoints = [minX: number, minY: number, minZ: number, maxX: number, maxY: number, maxZ: number];
3
+ type MinAndMaxPoints = [min: [x: number, y: number, z: number], max: [x: number, y: number, z: number]];
4
+ type Vec3AABB = [min: Vec3, max: Vec3];
5
5
  export declare class AABB {
6
6
  minX: number;
7
7
  minY: number;
@@ -14,15 +14,32 @@ export declare class AABB {
14
14
  static fromBlock(min: Vec3): AABB;
15
15
  set(x0: number, y0: number, z0: number, x1: number, y1: number, z1: number): void;
16
16
  clone(): AABB;
17
+ minPoint(): Vec3;
18
+ maxPoint(): Vec3;
19
+ bottomMiddlePoint(): Vec3;
20
+ heightAndWidths(): Vec3;
17
21
  toArray(): AABBPoints;
18
- toMinAndMaxArrays(): MinAndMaxPoints;
22
+ minAndMaxArrays(): MinAndMaxPoints;
19
23
  toVecs(): Vec3AABB;
24
+ /**
25
+ * Compatible with Iterators from prismarine-world.
26
+ * Used like a block for prismarine-world.
27
+ * @returns {number[][]} single element long array of shapes.
28
+ */
29
+ toShapeFromMin(): AABBPoints[];
30
+ /**
31
+ * Compatible with Iterators from prismarine-world.
32
+ * Used with the entity's actual position for prismarine-world.
33
+ * @returns {number[][]} single element long array of shapes.
34
+ */
35
+ toShapeFromBottomMiddle(): AABBPoints[];
20
36
  toVertices(): Vec3[];
21
37
  floor(): this;
22
38
  extend(dx: number, dy: number, dz: number): this;
23
39
  contract(x: number, y: number, z: number): this;
24
40
  expand(x: number, y: number, z: number): this;
25
41
  offset(x: number, y: number, z: number): this;
42
+ offsetVec(vec: Vec3): this;
26
43
  computeOffsetX(other: AABB, offsetX: number): number;
27
44
  computeOffsetY(other: AABB, offsetY: number): number;
28
45
  computeOffsetZ(other: AABB, offsetZ: number): number;
package/lib/calcs/aabb.js CHANGED
@@ -31,15 +31,48 @@ class AABB {
31
31
  clone() {
32
32
  return new AABB(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ);
33
33
  }
34
+ minPoint() {
35
+ return new vec3_1.Vec3(this.minX, this.minY, this.minZ);
36
+ }
37
+ maxPoint() {
38
+ return new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ);
39
+ }
40
+ bottomMiddlePoint() {
41
+ return new vec3_1.Vec3(lerp(0.5, this.minX, this.maxX), this.minY, lerp(0.5, this.minZ, this.maxZ));
42
+ }
43
+ heightAndWidths() {
44
+ return new vec3_1.Vec3(this.maxX - this.minX, this.maxY - this.minY, this.maxZ - this.minZ);
45
+ }
34
46
  toArray() {
35
47
  return [this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ];
36
48
  }
37
- toMinAndMaxArrays() {
38
- return [[this.minX, this.minY, this.minZ], [this.maxX, this.maxY, this.maxZ]];
49
+ minAndMaxArrays() {
50
+ return [
51
+ [this.minX, this.minY, this.minZ],
52
+ [this.maxX, this.maxY, this.maxZ],
53
+ ];
39
54
  }
40
55
  toVecs() {
41
56
  return [new vec3_1.Vec3(this.minX, this.minY, this.minZ), new vec3_1.Vec3(this.maxX, this.maxY, this.maxZ)];
42
57
  }
58
+ /**
59
+ * Compatible with Iterators from prismarine-world.
60
+ * Used like a block for prismarine-world.
61
+ * @returns {number[][]} single element long array of shapes.
62
+ */
63
+ toShapeFromMin() {
64
+ return [[0, 0, 0, this.maxX - this.minX, this.maxY - this.minY, this.maxZ - this.minZ]];
65
+ }
66
+ /**
67
+ * Compatible with Iterators from prismarine-world.
68
+ * Used with the entity's actual position for prismarine-world.
69
+ * @returns {number[][]} single element long array of shapes.
70
+ */
71
+ toShapeFromBottomMiddle() {
72
+ const wx = lerp(0.5, this.minX, this.maxX);
73
+ const wz = lerp(0.5, this.minX, this.maxX);
74
+ return [[-wx, 0, -wz, wx, this.maxY - this.minY, wz]];
75
+ }
43
76
  toVertices() {
44
77
  return [
45
78
  new vec3_1.Vec3(this.minX, this.minY, this.minZ),
@@ -103,6 +136,15 @@ class AABB {
103
136
  this.maxZ += z;
104
137
  return this;
105
138
  }
139
+ offsetVec(vec) {
140
+ this.minX += vec.x;
141
+ this.minY += vec.y;
142
+ this.minZ += vec.z;
143
+ this.maxX += vec.x;
144
+ this.maxY += vec.y;
145
+ this.maxZ += vec.z;
146
+ return this;
147
+ }
106
148
  computeOffsetX(other, offsetX) {
107
149
  if (other.maxY > this.minY && other.minY < this.maxY && other.maxZ > this.minZ && other.minZ < this.maxZ) {
108
150
  if (offsetX > 0.0 && other.maxX <= this.minX) {
@@ -167,7 +209,7 @@ class AABB {
167
209
  distanceFromRay(origin, direction, xz = false) {
168
210
  const ro = origin.toArray();
169
211
  const rd = direction.clone().normalize().toArray();
170
- const aabb = this.toMinAndMaxArrays();
212
+ const aabb = this.minAndMaxArrays();
171
213
  const dims = ro.length; // will change later.
172
214
  const dif = xz ? 2 : 1;
173
215
  let lo = -Infinity;
@@ -262,7 +304,7 @@ class AABB {
262
304
  return this.collidesCoords(aABB.minX, aABB.minY, aABB.minZ, aABB.maxX, aABB.maxY, aABB.maxZ);
263
305
  }
264
306
  collidesCoords(d, d2, d3, d4, d5, d6) {
265
- return this.minX <= d4 && this.maxX >= d && this.minY <= d5 && this.maxY >= d2 && this.minZ <= d6 && this.maxZ >= d3;
307
+ return (this.minX <= d4 && this.maxX >= d && this.minY <= d5 && this.maxY >= d2 && this.minZ <= d6 && this.maxZ >= d3);
266
308
  }
267
309
  getCenter() {
268
310
  return new vec3_1.Vec3(lerp(0.5, this.minX, this.maxX), lerp(0.5, this.minY, this.maxY), lerp(0.5, this.minZ, this.maxZ));
@@ -1,13 +1,13 @@
1
1
  import { Bot } from "mineflayer";
2
2
  import { Block } from "prismarine-block";
3
3
  import { Vec3 } from "vec3";
4
- declare type Iteration = {
4
+ type Iteration = {
5
5
  x: number;
6
6
  y: number;
7
7
  z: number;
8
8
  face: number;
9
9
  };
10
- declare type BlockAndIterations = {
10
+ type BlockAndIterations = {
11
11
  block: Block | null;
12
12
  iterations: Iteration[];
13
13
  };
@@ -1,7 +1,5 @@
1
1
  import type { Bot } from "mineflayer";
2
2
  import type { Entity } from "prismarine-entity";
3
- import type { Vec3 } from "vec3";
4
- import { AABB } from "./calcs/aabb";
5
3
  export declare class EntityFunctions {
6
4
  bot: Bot;
7
5
  healthSlot: number;
@@ -38,22 +36,8 @@ export declare class EntityFunctions {
38
36
  * @returns
39
37
  */
40
38
  getHealthChange(packetMetadata: any, entity: Entity): number;
41
- getDistanceToEntity(entity: Entity): number;
42
- getEyeDistanceToEntity(entity: Entity): number;
43
- getEyeDistanceBetweenEntities(first: Entity, second: Entity): number;
44
- getEntityAABB(entity: {
45
- type: string;
46
- position: Vec3;
47
- height: number;
48
- width?: number;
49
- }): AABB;
50
- getPlayerAABB(entity: {
51
- position: Vec3;
52
- }): AABB;
53
- getEntityAABBRaw(entity: {
54
- position: Vec3;
55
- height: number;
56
- width?: number;
57
- }): AABB;
39
+ entityDistance(entity: Entity): number;
40
+ eyeDistanceToEntity(entity: Entity): number;
41
+ eyeDistanceBetweenEntities(first: Entity, second: Entity): number;
58
42
  private parseMetadata;
59
43
  }
@@ -1,23 +1,12 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.EntityFunctions = void 0;
13
- const aabb_1 = require("./calcs/aabb");
4
+ const static_1 = require("./static");
14
5
  class EntityFunctions {
15
6
  constructor(bot) {
16
7
  this.bot = bot;
17
8
  this.healthSlot = 7;
18
- this.bot.on("spawn", () => __awaiter(this, void 0, void 0, function* () {
19
- this.healthSlot = Number(this.bot.version.split(".")[1]) <= 16 ? 7 : 9;
20
- }));
9
+ this.healthSlot = Number(this.bot.version.split(".")[1]) <= 16 ? 7 : 9;
21
10
  }
22
11
  /**
23
12
  * TODO: Version specific right now. Generalize. Unknown method.
@@ -70,32 +59,14 @@ class EntityFunctions {
70
59
  const newHealth = this.getHealthFromMetadata(this.parseMetadata(packetMetadata, entity.metadata));
71
60
  return newHealth - oldHealth;
72
61
  }
73
- getDistanceToEntity(entity) {
62
+ entityDistance(entity) {
74
63
  return this.bot.entity.position.distanceTo(entity.position);
75
64
  }
76
- getEyeDistanceToEntity(entity) {
77
- return this.getEntityAABB(entity).distanceToVec(this.bot.entity.position.offset(0, 1.62, 0));
65
+ eyeDistanceToEntity(entity) {
66
+ return static_1.AABBUtils.getEntityAABB(entity).distanceToVec(this.bot.entity.position.offset(0, this.bot.entity.height, 0));
78
67
  }
79
- getEyeDistanceBetweenEntities(first, second) {
80
- return this.getEntityAABB(second).distanceToVec(first.position.offset(0, first.height, 0));
81
- }
82
- getEntityAABB(entity) {
83
- switch (entity.type) {
84
- case "player":
85
- return this.getPlayerAABB({ position: entity.position });
86
- case "mob":
87
- default:
88
- //TODO: Implement better AABBs. However, this may just be correct.
89
- return this.getEntityAABBRaw({ position: entity.position, height: entity.height, width: entity.width });
90
- }
91
- }
92
- getPlayerAABB(entity) {
93
- return this.getEntityAABBRaw({ position: entity.position, height: 1.8, width: 0.6 });
94
- }
95
- getEntityAABBRaw(entity) {
96
- const w = entity.width ? entity.width / 2 : entity.height / 2;
97
- const { x, y, z } = entity.position;
98
- return new aabb_1.AABB(-w, 0, -w, w, entity.height, w).offset(x, y, z);
68
+ eyeDistanceBetweenEntities(first, second) {
69
+ return static_1.AABBUtils.getEntityAABB(second).distanceToVec(first.position.offset(0, first.height, 0));
99
70
  }
100
71
  parseMetadata(packetMetadata, entityMetadata = {}) {
101
72
  if (packetMetadata !== undefined) {
package/lib/index.d.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import type { Bot } from "mineflayer";
2
2
  import { UtilFunctions } from "./utilFunctions";
3
- import md from "minecraft-data";
4
3
  declare module "mineflayer" {
5
4
  interface Bot {
6
5
  util: UtilFunctions;
7
- registry: md.IndexedData;
6
+ }
7
+ }
8
+ declare module "prismarine-entity" {
9
+ interface Entity {
10
+ attributes: {
11
+ [index: string]: {
12
+ value: number;
13
+ modifiers: any[];
14
+ };
15
+ };
8
16
  }
9
17
  }
10
18
  export default function inject(bot: Bot): void;
@@ -1,9 +1,10 @@
1
1
  import type { Bot } from "mineflayer";
2
- import type { Vec3 } from "vec3";
2
+ import { Vec3 } from "vec3";
3
+ export declare function lerp(f: number, f2: number, f3: number): number;
3
4
  export declare class MovementFunctions {
4
5
  private bot;
5
6
  constructor(bot: Bot);
6
7
  forceLook(yaw: number, pitch: number, update?: boolean, onGround?: boolean): void;
7
8
  forceLookAt(pos: Vec3, update?: boolean, onGround?: boolean): void;
8
- lazyTeleport(endPos: Vec3): void;
9
+ lazyTeleport(endPos: Vec3, steps?: number, update?: boolean): void;
9
10
  }
@@ -1,25 +1,42 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MovementFunctions = void 0;
3
+ exports.MovementFunctions = exports.lerp = void 0;
4
+ const vec3_1 = require("vec3");
5
+ const static_1 = require("./static");
6
+ function lerp(f, f2, f3) {
7
+ return f2 + f * (f3 - f2);
8
+ }
9
+ exports.lerp = lerp;
10
+ function* interpolate(start, end, steps = 1) {
11
+ for (let i = 0; i < steps; i++) {
12
+ 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));
13
+ }
14
+ }
4
15
  class MovementFunctions {
5
16
  constructor(bot) {
6
17
  this.bot = bot;
7
18
  }
8
19
  forceLook(yaw, pitch, update = false, onGround) {
9
- const notchianYawAndPitch = { yaw: this.bot.util.math.toNotchianYaw(yaw), pitch: this.bot.util.math.toNotchianPitch(pitch) };
20
+ const notchianYawAndPitch = { yaw: static_1.MathUtils.toNotchianYaw(yaw), pitch: static_1.MathUtils.toNotchianPitch(pitch) };
10
21
  this.bot._client.write("look", Object.assign(Object.assign({}, notchianYawAndPitch), { onGround: onGround !== null && onGround !== void 0 ? onGround : this.bot.entity.onGround }));
11
22
  if (update) {
12
23
  this.bot.look(yaw, pitch, true);
13
24
  }
14
25
  }
15
26
  forceLookAt(pos, update = false, onGround) {
16
- const { yaw, pitch } = this.bot.util.math.pointToYawAndPitch(this.bot, pos);
17
- const nyp = { yaw: this.bot.util.math.toNotchianYaw(yaw), pitch: this.bot.util.math.toNotchianPitch(pitch) };
27
+ const { yaw, pitch } = static_1.MathUtils.pointToYawAndPitch(this.bot, pos);
28
+ const nyp = { yaw: static_1.MathUtils.toNotchianYaw(yaw), pitch: static_1.MathUtils.toNotchianPitch(pitch) };
18
29
  this.bot._client.write("look", Object.assign(Object.assign({}, nyp), { onGround: onGround !== null && onGround !== void 0 ? onGround : this.bot.entity.onGround }));
19
30
  if (update) {
20
31
  this.bot.look(yaw, pitch, true);
21
32
  }
22
33
  }
23
- lazyTeleport(endPos) { }
34
+ lazyTeleport(endPos, steps = 1, update = false) {
35
+ for (const pos of interpolate(this.bot.entity.position, endPos, steps)) {
36
+ this.bot._client.write("position", Object.assign(Object.assign({}, pos), { onGround: this.bot.entity.onGround }));
37
+ }
38
+ if (update)
39
+ this.bot.entity.position.set(endPos.x, endPos.y, endPos.z);
40
+ }
24
41
  }
25
42
  exports.MovementFunctions = MovementFunctions;
@@ -2,7 +2,6 @@ import type { Bot } from "mineflayer";
2
2
  import type { Entity } from "prismarine-entity";
3
3
  import { Vec3 } from "vec3";
4
4
  import { Overwrites, PredictiveWorld } from "./worldRelated/predictiveWorld";
5
- export declare const ARMOR_THOUGHNESS_KEY = "generic.armorToughness";
6
5
  export declare class PredictiveFunctions {
7
6
  private bot;
8
7
  private damageMultiplier;
@@ -1,12 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PredictiveFunctions = exports.ARMOR_THOUGHNESS_KEY = void 0;
7
- const minecraft_data_1 = __importDefault(require("minecraft-data"));
3
+ exports.PredictiveFunctions = void 0;
8
4
  const vec3_1 = require("vec3");
9
5
  const predictiveWorld_1 = require("./worldRelated/predictiveWorld");
6
+ const static_1 = require("./static");
10
7
  const armorPieces = ["head", "torso", "legs", "feet"];
11
8
  // https://minecraft.fandom.com/wiki/Explosion
12
9
  // Use bot.world, there's no typing yet.
@@ -33,6 +30,32 @@ function calcExposure(playerPos, explosionPos, world) {
33
30
  }
34
31
  return exposed / sampled;
35
32
  }
33
+ function calcExposureAABB(entityBB, explosionPos, world /* prismarine-world*/) {
34
+ const xWidth = entityBB.maxX - entityBB.minX;
35
+ const yWidth = entityBB.maxY - entityBB.minY;
36
+ const zWidth = entityBB.maxZ - entityBB.minZ;
37
+ const dx = 1 / (xWidth * 2 + 1);
38
+ const dy = 1 / (yWidth * 2 + 1);
39
+ const dz = 1 / (zWidth * 2 + 1);
40
+ const d3 = (1 - Math.floor(1 / dx) * dx) / 2;
41
+ const d4 = (1 - Math.floor(1 / dz) * dz) / 2;
42
+ let sampled = 0;
43
+ let exposed = 0;
44
+ const pos = new vec3_1.Vec3(0, 0, 0);
45
+ for (pos.y = entityBB.minY; pos.y <= entityBB.maxY; pos.y += yWidth * dy) {
46
+ for (pos.x = entityBB.minX + d3; pos.x <= entityBB.maxX; pos.x += xWidth * dx) {
47
+ for (pos.z = entityBB.minZ + d4; pos.z <= entityBB.maxZ; pos.z += zWidth * dz) {
48
+ const dir = pos.minus(explosionPos);
49
+ const range = dir.norm();
50
+ if (world.raycast(explosionPos, dir.normalize(), range) === null) {
51
+ exposed++;
52
+ }
53
+ sampled++;
54
+ }
55
+ }
56
+ }
57
+ return exposed / sampled;
58
+ }
36
59
  // https://minecraft.fandom.com/wiki/Armor#Damage_protection
37
60
  function getDamageAfterAbsorb(damages, armorValue, toughness) {
38
61
  const var3 = 2 + toughness / 4;
@@ -86,35 +109,23 @@ const DIFFICULTY_VALUES = {
86
109
  normal: 2,
87
110
  hard: 3,
88
111
  };
89
- exports.ARMOR_THOUGHNESS_KEY = "generic.armorToughness";
90
- const AIR_BLOCK = { type: 0 };
91
112
  class PredictiveFunctions {
92
113
  constructor(bot) {
93
114
  this.bot = bot;
94
- this.resistanceIndex = "11";
95
- const version = Number(bot.version.split(".")[1]);
96
- if (version === 16) {
97
- this.armorToughnessKey = "generic.armorToughness";
98
- this.armorProtectionKey = "generic.armor";
99
- }
100
- else {
101
- this.armorToughnessKey = "minecraft:generic.armor_toughness";
102
- this.armorProtectionKey = "minecraft:generic.armor";
103
- }
104
- if (version > 9) {
105
- this.damageMultiplier = 8;
106
- }
107
- else {
108
- this.damageMultiplier = 7;
109
- }
110
- const effects = (0, minecraft_data_1.default)(bot.version).effects;
115
+ this.resistanceIndex = 11;
116
+ const effects = bot.registry.effects;
111
117
  for (const effectId in effects) {
112
118
  const effect = effects[effectId];
113
- if (effect.name.includes("resistance")) {
114
- this.resistanceIndex = effectId;
119
+ if (effect.name.includes("esistance")) {
120
+ this.resistanceIndex = Number(effectId);
115
121
  break;
116
122
  }
117
123
  }
124
+ this.damageMultiplier = bot.registry.version[">="]("1.9") ? 8 : 7; // for 1.12+ 8 for 1.8 TODO check when the change occur (likely 1.9)
125
+ this.armorToughnessKey = bot.registry.version[">="]("1.16")
126
+ ? "minecraft:generic.armor_toughness"
127
+ : "generic.armorToughness"; // was renamed in 1.16
128
+ this.armorProtectionKey = bot.registry.version[">="]("1.16") ? "minecraft:generic.armor" : "generic.armor"; // was renamed in 1.16
118
129
  this.world = new predictiveWorld_1.PredictiveWorld(bot);
119
130
  }
120
131
  //There's a mistyping in mineflayer. Effect[] is not accurate. You cannot map over it.
@@ -144,8 +155,7 @@ class PredictiveFunctions {
144
155
  const armor = getAttributeValue(this.bot.entity.attributes[this.armorProtectionKey]);
145
156
  const armorToughness = getAttributeValue(this.bot.entity.attributes[this.armorToughnessKey]);
146
157
  damages = getDamageAfterAbsorb(damages, armor, armorToughness);
147
- const equipment = armorPieces.map((piece) => this.bot.inventory.slots[this.bot.getEquipmentDestSlot(piece)]);
148
- damages = getDamageWithEnchantments(damages, equipment);
158
+ damages = getDamageWithEnchantments(damages, this.bot.entity.equipment);
149
159
  damages = this.getDamageWithEffects(damages, this.bot.entity.effects);
150
160
  damages *= DIFFICULTY_VALUES[this.bot.game.difficulty] * 0.5;
151
161
  }
@@ -156,7 +166,7 @@ class PredictiveFunctions {
156
166
  const radius = 2 * power;
157
167
  if (distance >= radius)
158
168
  return 0;
159
- const exposure = calcExposure(targetEntity.position, sourcePos, this.world);
169
+ const exposure = calcExposureAABB(static_1.AABBUtils.getEntityAABB(targetEntity), sourcePos, this.world);
160
170
  const impact = (1 - distance / radius) * exposure;
161
171
  let damages = Math.floor((impact * impact + impact) * this.damageMultiplier * power + 1);
162
172
  // The following modifiers are constant for the input targetEntity and doesnt depend
@@ -1,15 +1,21 @@
1
1
  import { Bot } from "mineflayer";
2
+ import type { Block } from "prismarine-block";
2
3
  import type { Entity } from "prismarine-entity";
3
4
  import { Vec3 } from "vec3";
5
+ import AABB from "./calcs/aabb";
6
+ import { BlockFace } from "./calcs/iterators";
7
+ type RayAddtion = {
8
+ intersect: Vec3;
9
+ face: BlockFace;
10
+ };
11
+ export type EntityRaycastReturn = (Block | Entity) & RayAddtion;
4
12
  export declare class RayTraceFunctions {
5
13
  private bot;
6
14
  constructor(bot: Bot);
7
- blockAtEntityCursor({ position, height, yaw, pitch }: {
8
- position: Vec3;
9
- height: number;
10
- yaw: number;
11
- pitch: number;
12
- }, maxDistance?: number, matcher?: null): any;
13
- entityAtCursor(maxDistance?: number): Entity | null;
15
+ entityRaytrace(startPos: Vec3, dir: Vec3, maxDistance?: number, matcher?: (e: Entity) => boolean): EntityRaycastReturn | null;
16
+ entityRaytraceRaw(startPos: Vec3, dir: Vec3, aabbMap: {
17
+ [id: string]: AABB;
18
+ }, maxDistance?: number, matcher?: (e: Entity) => boolean): EntityRaycastReturn | null;
14
19
  entityAtEntityCursor(entity: Entity, maxDistance?: number): Entity | null;
15
20
  }
21
+ export {};
@@ -3,28 +3,58 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RayTraceFunctions = void 0;
4
4
  const vec3_1 = require("vec3");
5
5
  const iterators_1 = require("./calcs/iterators");
6
- function getViewDirection(pitch, yaw) {
7
- const csPitch = Math.cos(pitch);
8
- const snPitch = Math.sin(pitch);
9
- const csYaw = Math.cos(yaw);
10
- const snYaw = Math.sin(yaw);
11
- return new vec3_1.Vec3(-snYaw * csPitch, snPitch, -csYaw * csPitch);
12
- }
6
+ const static_1 = require("./static");
13
7
  class RayTraceFunctions {
14
8
  constructor(bot) {
15
9
  this.bot = bot;
16
10
  }
17
- blockAtEntityCursor({ position, height, yaw, pitch }, maxDistance = 256, matcher = null) {
18
- const eyePosition = position.offset(0, height, 0);
19
- const viewDirection = getViewDirection(pitch, yaw);
20
- return this.bot.world.raycast(eyePosition, viewDirection, maxDistance, matcher);
11
+ entityRaytrace(startPos, dir, maxDistance = 3.5, matcher) {
12
+ const aabbMap = {};
13
+ Object.values(this.bot.entities)
14
+ .filter((e) => e.username !== this.bot.entity.username)
15
+ .forEach((e) => (aabbMap[e.id] = static_1.AABBUtils.getEntityAABB(e)));
16
+ return this.entityRaytraceRaw(startPos, dir, aabbMap, maxDistance, matcher);
21
17
  }
22
- entityAtCursor(maxDistance = 3.5) {
23
- return this.entityAtEntityCursor(this.bot.entity, maxDistance);
18
+ entityRaytraceRaw(startPos, dir, aabbMap, maxDistance = 3.5, matcher) {
19
+ var _a;
20
+ matcher || (matcher = (e) => true);
21
+ Object.entries(aabbMap).forEach(([id, bb]) => {
22
+ if (bb.distanceToVec(eyePos) > maxDistance)
23
+ delete aabbMap[id];
24
+ });
25
+ dir = dir.normalize();
26
+ let returnVal = this.bot.world.raycast(startPos, dir, maxDistance);
27
+ if (returnVal && Object.keys(aabbMap).length === 0)
28
+ return returnVal;
29
+ maxDistance = (_a = returnVal === null || returnVal === void 0 ? void 0 : returnVal.intersect.distanceTo(startPos)) !== null && _a !== void 0 ? _a : maxDistance;
30
+ const eyePos = this.bot.entity.position.offset(0, this.bot.entity.height, 0);
31
+ const iterator = new iterators_1.RaycastIterator(startPos, dir, maxDistance);
32
+ for (const id in aabbMap) {
33
+ const aabb = aabbMap[id];
34
+ const pt = aabb.bottomMiddlePoint();
35
+ const entity = this.bot.entities[id];
36
+ const intersect = iterator.intersect(aabb.toShapeFromBottomMiddle(), pt);
37
+ if (intersect) {
38
+ const entityDir = pt.minus(eyePos);
39
+ const sign = Math.sign(entityDir.dot(dir));
40
+ if (sign !== -1) {
41
+ const dist = eyePos.distanceTo(intersect.pos);
42
+ if (dist <= maxDistance) {
43
+ maxDistance = dist;
44
+ if (matcher(entity)) {
45
+ returnVal = entity;
46
+ returnVal.intersect = intersect.pos;
47
+ returnVal.face = intersect.face;
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ return returnVal;
24
54
  }
25
55
  entityAtEntityCursor(entity, maxDistance = 3.5) {
26
56
  var _a;
27
- const block = this.blockAtEntityCursor(entity, maxDistance);
57
+ const block = this.bot.blockAtCursor(maxDistance);
28
58
  maxDistance = (_a = block === null || block === void 0 ? void 0 : block.intersect.distanceTo(this.bot.entity.position)) !== null && _a !== void 0 ? _a : maxDistance;
29
59
  const entities = Object.values(this.bot.entities).filter((e) => e.type !== "object" && e.username !== entity.username && e.position.distanceTo(entity.position) <= maxDistance);
30
60
  const dir = new vec3_1.Vec3(-Math.sin(entity.yaw) * Math.cos(entity.pitch), Math.sin(entity.pitch), -Math.cos(entity.yaw) * Math.cos(entity.pitch));
@@ -33,7 +63,7 @@ class RayTraceFunctions {
33
63
  let targetDist = maxDistance;
34
64
  for (let i = 0; i < entities.length; i++) {
35
65
  const e = entities[i];
36
- const w = e.height >= 1.62 && e.height <= 1.99 || e.height === 2.9 ? 0.3 : e.height / 2;
66
+ const w = (e.height >= 1.62 && e.height <= 1.99) || e.height === 2.9 ? 0.3 : e.height / 2;
37
67
  const shapes = [[-w, 0, -w, w, e.height + (e.height === 1.62 ? 0.18 : 0), w]];
38
68
  const intersect = iterator.intersect(shapes, e.position);
39
69
  if (intersect) {
@@ -12,7 +12,10 @@ export declare namespace AABBUtils {
12
12
  }): AABB;
13
13
  function getPlayerAABB(entity: {
14
14
  position: Vec3;
15
+ height?: number;
16
+ width?: number;
15
17
  }): AABB;
18
+ function getPlayerAABBRaw(position: Vec3, height?: number): AABB;
16
19
  function getEntityAABBRaw(entity: {
17
20
  position: Vec3;
18
21
  height: number;
@@ -17,7 +17,7 @@ var AABBUtils;
17
17
  function getEntityAABB(entity) {
18
18
  switch (entity.type) {
19
19
  case "player":
20
- return getPlayerAABB({ position: entity.position });
20
+ return getEntityAABBRaw({ position: entity.position, height: entity.height + 0.18, width: entity.width });
21
21
  case "mob":
22
22
  default: //TODO: Implement better AABBs. However, this may just be correct.
23
23
  return getEntityAABBRaw({ position: entity.position, height: entity.height, width: entity.width });
@@ -25,9 +25,15 @@ var AABBUtils;
25
25
  }
26
26
  AABBUtils.getEntityAABB = getEntityAABB;
27
27
  function getPlayerAABB(entity) {
28
- return getEntityAABBRaw({ position: entity.position, height: 1.8, width: 0.6 });
28
+ const w = entity.width ? entity.width / 2 : 0.6;
29
+ const { x, y, z } = entity.position;
30
+ return new aabb_1.AABB(-w, 0, -w, w, entity.height ? entity.height + 0.18 : 1.8, w).offset(x, y, z);
29
31
  }
30
32
  AABBUtils.getPlayerAABB = getPlayerAABB;
33
+ function getPlayerAABBRaw(position, height = 1.8) {
34
+ return new aabb_1.AABB(-0.6, 0, -0.6, 0.6, height, 0.6).offsetVec(position);
35
+ }
36
+ AABBUtils.getPlayerAABBRaw = getPlayerAABBRaw;
31
37
  function getEntityAABBRaw(entity) {
32
38
  const w = entity.width ? entity.width / 2 : entity.height / 2;
33
39
  const { x, y, z } = entity.position;
@@ -59,7 +59,6 @@ var MathUtils;
59
59
  return yaw;
60
60
  }
61
61
  MathUtils.getYaw = getYaw;
62
- //Scuffed.
63
62
  function yawPitchAndSpeedToDir(yaw, pitch, speed) {
64
63
  return new vec3_1.Vec3(-Math.sin(yaw) * Math.cos(pitch), Math.sin(pitch), -Math.cos(yaw) * Math.cos(pitch)).scale(speed);
65
64
  }
@@ -1,12 +1,9 @@
1
- /// <reference types="node" />
2
1
  import type { Bot } from "mineflayer";
3
2
  import { EntityFunctions } from "./entityFunctions";
4
3
  import { FilterFunctions } from "./filterFunctions";
5
4
  import { InventoryFunctions } from "./inventoryFunctions";
6
5
  import { MovementFunctions } from "./movementFunctions";
7
6
  import { PredictiveFunctions } from "./predictiveFunctions";
8
- import { MathFunctions } from "./mathUtil";
9
- import { WorldFunctions } from "./WorldFunctions";
10
7
  import { RayTraceFunctions } from "./rayTracingFunctions";
11
8
  /**
12
9
  * I don't believe I need any locks, as I'm only going to have one instance of this per bot.
@@ -30,9 +27,6 @@ export declare class UtilFunctions {
30
27
  entity: EntityFunctions;
31
28
  predict: PredictiveFunctions;
32
29
  filters: FilterFunctions;
33
- math: MathFunctions;
34
- world: WorldFunctions;
35
30
  raytrace: RayTraceFunctions;
36
31
  constructor(bot: Bot);
37
- sleep: typeof import("timers/promises").setTimeout;
38
32
  }
@@ -5,10 +5,7 @@ const entityFunctions_1 = require("./entityFunctions");
5
5
  const filterFunctions_1 = require("./filterFunctions");
6
6
  const inventoryFunctions_1 = require("./inventoryFunctions");
7
7
  const movementFunctions_1 = require("./movementFunctions");
8
- const util_1 = require("util");
9
8
  const predictiveFunctions_1 = require("./predictiveFunctions");
10
- const mathUtil_1 = require("./mathUtil");
11
- const WorldFunctions_1 = require("./WorldFunctions");
12
9
  const rayTracingFunctions_1 = require("./rayTracingFunctions");
13
10
  /**
14
11
  * I don't believe I need any locks, as I'm only going to have one instance of this per bot.
@@ -28,15 +25,12 @@ const rayTracingFunctions_1 = require("./rayTracingFunctions");
28
25
  class UtilFunctions {
29
26
  constructor(bot) {
30
27
  this.bot = bot;
31
- this.sleep = (0, util_1.promisify)(setTimeout);
32
28
  this.inv = new inventoryFunctions_1.InventoryFunctions(bot);
33
29
  this.move = new movementFunctions_1.MovementFunctions(bot);
34
30
  this.entity = new entityFunctions_1.EntityFunctions(bot);
35
31
  this.predict = new predictiveFunctions_1.PredictiveFunctions(bot);
36
32
  this.filters = new filterFunctions_1.FilterFunctions(bot);
37
- this.world = new WorldFunctions_1.WorldFunctions(bot);
38
33
  this.raytrace = new rayTracingFunctions_1.RayTraceFunctions(bot);
39
- this.math = new mathUtil_1.MathFunctions();
40
34
  }
41
35
  }
42
36
  exports.UtilFunctions = UtilFunctions;
@@ -1,7 +1,8 @@
1
1
  import type { Bot } from "mineflayer";
2
2
  import type { Block } from "prismarine-block";
3
3
  import { Vec3 } from "vec3";
4
- export declare type Overwrites = {
4
+ import AABB from "../calcs/aabb";
5
+ export type Overwrites = {
5
6
  [coord: string]: Block | null;
6
7
  };
7
8
  /**
@@ -10,8 +11,8 @@ export declare type Overwrites = {
10
11
  * Currently, this class can predict explosion damages of crystals using a custom world.
11
12
  */
12
13
  export declare class PredictiveWorld {
13
- bot: Bot;
14
14
  private blocks;
15
+ private originalWorld;
15
16
  constructor(bot: Bot);
16
17
  raycast(from: Vec3, direction: Vec3, range: number, matcher?: ((block: Block) => boolean) | null): Block | null;
17
18
  /**
@@ -25,7 +26,7 @@ export declare class PredictiveWorld {
25
26
  setBlocks(blocks: Overwrites): void;
26
27
  /**
27
28
  * @param {Vec3} pos
28
- * @returns {Block} Block at position.
29
+ * @returns {Block | null} Block at position.
29
30
  */
30
31
  getBlock(pos: Vec3): Block | null;
31
32
  removeBlock(pos: Vec3, force: boolean): void;
@@ -36,6 +37,6 @@ export declare class PredictiveWorld {
36
37
  * @param block bot.block
37
38
  * @returns List of affected blocks that potentially protect the entity.
38
39
  */
39
- getExplosionAffectedBlocks(playerPos: Vec3, explosionPos: Vec3): Overwrites;
40
- loadExplosionAffectedBlocks(playerPos: Vec3, explosionPos: Vec3): void;
40
+ getExplosionAffectedBlocks(entityBB: AABB, explosionPos: Vec3): Overwrites;
41
+ loadProtectiveBlocks(playerPos: Vec3, explosionPos: Vec3): void;
41
42
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PredictiveWorld = void 0;
4
4
  const vec3_1 = require("vec3");
5
5
  const iterators_1 = require("../calcs/iterators");
6
+ const static_1 = require("../static");
6
7
  /**
7
8
  * A class dedicated to predictive logic.
8
9
  *
@@ -10,8 +11,8 @@ const iterators_1 = require("../calcs/iterators");
10
11
  */
11
12
  class PredictiveWorld {
12
13
  constructor(bot) {
13
- this.bot = bot;
14
14
  this.blocks = {};
15
+ this.originalWorld = bot.world;
15
16
  }
16
17
  raycast(from, direction, range, matcher = null) {
17
18
  const iter = new iterators_1.RaycastIterator(from, direction, range);
@@ -51,20 +52,21 @@ class PredictiveWorld {
51
52
  }
52
53
  /**
53
54
  * @param {Vec3} pos
54
- * @returns {Block} Block at position.
55
+ * @returns {Block | null} Block at position.
55
56
  */
56
57
  getBlock(pos) {
58
+ var _a;
57
59
  const pblock = this.blocks[pos.toString()];
58
60
  if (pblock !== undefined && pblock !== null)
59
61
  return pblock;
60
- return this.bot.blockAt(pos);
62
+ return (_a = this.originalWorld.getBlock(pos)) !== null && _a !== void 0 ? _a : null;
61
63
  }
62
64
  removeBlock(pos, force) {
63
65
  if (force) {
64
66
  delete this.blocks[pos.toString()];
65
67
  }
66
68
  else {
67
- const realBlock = this.bot.blockAt(pos);
69
+ const realBlock = this.originalWorld.getBlock(pos);
68
70
  if (realBlock)
69
71
  this.blocks[pos.toString()] = realBlock;
70
72
  else
@@ -80,17 +82,18 @@ class PredictiveWorld {
80
82
  * @param block bot.block
81
83
  * @returns List of affected blocks that potentially protect the entity.
82
84
  */
83
- getExplosionAffectedBlocks(playerPos, explosionPos) {
85
+ getExplosionAffectedBlocks(entityBB, explosionPos) {
84
86
  let blocks = {};
85
- const dx = 1 / (0.6 * 2 + 1);
86
- const dy = 1 / (1.8 * 2 + 1);
87
- const dz = 1 / (0.6 * 2 + 1);
87
+ const { x: xWidth, y: yWidth, z: zWidth } = entityBB.heightAndWidths();
88
+ const dx = 1 / (xWidth * 2 + 1);
89
+ const dy = 1 / (yWidth * 2 + 1);
90
+ const dz = 1 / (zWidth * 2 + 1);
88
91
  const d3 = (1 - Math.floor(1 / dx) * dx) / 2;
89
92
  const d4 = (1 - Math.floor(1 / dz) * dz) / 2;
90
93
  const pos = new vec3_1.Vec3(0, 0, 0);
91
- for (pos.y = playerPos.y; pos.y <= playerPos.y + 1.8; pos.y += 1.8 * dy) {
92
- for (pos.x = playerPos.x - 0.3 + d3; pos.x <= playerPos.x + 0.3; pos.x += 0.6 * dx) {
93
- for (pos.z = playerPos.z - 0.3 + d4; pos.z <= playerPos.z + 0.3; pos.z += 0.6 * dz) {
94
+ for (pos.y = entityBB.minY; pos.y <= entityBB.maxY; pos.y += yWidth * dy) {
95
+ for (pos.x = entityBB.minX + d3; pos.x <= entityBB.maxX; pos.x += xWidth * dx) {
96
+ for (pos.z = entityBB.minZ + d4; pos.z <= entityBB.maxZ; pos.z += zWidth * dz) {
94
97
  const dir = pos.minus(explosionPos);
95
98
  const range = dir.norm();
96
99
  const potentialBlock = this.raycast(explosionPos, dir.normalize(), range);
@@ -101,8 +104,8 @@ class PredictiveWorld {
101
104
  }
102
105
  return blocks;
103
106
  }
104
- loadExplosionAffectedBlocks(playerPos, explosionPos) {
105
- this.setBlocks(this.getExplosionAffectedBlocks(playerPos, explosionPos));
107
+ loadProtectiveBlocks(playerPos, explosionPos) {
108
+ this.setBlocks(this.getExplosionAffectedBlocks(static_1.AABBUtils.getPlayerAABBRaw(playerPos), explosionPos));
106
109
  }
107
110
  }
108
111
  exports.PredictiveWorld = PredictiveWorld;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-util-plugin",
3
- "version": "1.6.3",
3
+ "version": "1.7.0",
4
4
  "description": "mineflayer utils for NextGEN mineflayer plugins.",
5
5
  "keywords": [
6
6
  "mineflayer",
@@ -19,6 +19,7 @@
19
19
  "prepublish": "npm run build",
20
20
  "test": "ts-node examples/example.ts"
21
21
  },
22
+ "dependencies": {},
22
23
  "devDependencies": {
23
24
  "@types/node": "^17.0.4",
24
25
  "mineflayer": "^4.7.0",