@nxg-org/mineflayer-util-plugin 1.2.6 → 1.3.3

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.
@@ -28,9 +28,19 @@ export declare class AABB {
28
28
  computeOffsetY(other: AABB, offsetY: number): number;
29
29
  computeOffsetZ(other: AABB, offsetZ: number): number;
30
30
  intersects(other: AABB): boolean;
31
- intersectsRay(origin: Vec3, direction: Vec3): Vec3 | null;
32
- distanceFromRay(origin: Vec3, direction: Vec3): number;
31
+ xzIntersectsRay(org: Vec3, dir: Vec3): {
32
+ x: number;
33
+ z: number;
34
+ } | null;
35
+ intersectsRay(org: Vec3, dir: Vec3): Vec3 | null;
36
+ xzIntersectsSegment(org: Vec3, dest: Vec3): {
37
+ x: number;
38
+ z: number;
39
+ } | null;
40
+ intersectsSegment(org: Vec3, dest: Vec3): Vec3 | null;
41
+ distanceFromRay(origin: Vec3, direction: Vec3, xz?: boolean): number;
33
42
  equals(other: AABB): boolean;
43
+ xzDistanceTo(pos: Vec3, heightOffset?: number): number;
34
44
  distanceTo(pos: Vec3, heightOffset?: number): number;
35
45
  }
36
46
  export default AABB;
package/lib/calcs/aabb.js CHANGED
@@ -125,24 +125,36 @@ class AABB {
125
125
  this.minZ < other.maxZ &&
126
126
  this.maxZ > other.minZ);
127
127
  }
128
- intersectsRay(origin, direction) {
129
- const d = this.distanceFromRay(origin, direction);
130
- if (d === Infinity) {
131
- return null;
132
- }
133
- else {
134
- return new vec3_1.Vec3(origin.x + direction.x * d, origin.y + direction.y * d, origin.z + direction.z * d);
135
- }
136
- }
137
- distanceFromRay(origin, direction) {
128
+ xzIntersectsRay(org, dir) {
129
+ const d = this.distanceFromRay(org, dir, true);
130
+ return d === Infinity ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
131
+ }
132
+ intersectsRay(org, dir) {
133
+ const d = this.distanceFromRay(org, dir);
134
+ return d === Infinity ? null : new vec3_1.Vec3(org.x + dir.x * d, org.y + dir.y * d, org.z + dir.z * d);
135
+ }
136
+ //TODO: Better check for hit reg of PLANES.
137
+ xzIntersectsSegment(org, dest) {
138
+ const dir = dest.clone().subtract(org).normalize();
139
+ const d = this.distanceFromRay(org, dir, true);
140
+ return d > dest.distanceTo(org) || d < 0 ? null : { x: org.x + dir.x * d, z: org.z + dir.z * d };
141
+ }
142
+ //TODO: Better check for hit reg of PLANES.
143
+ intersectsSegment(org, dest) {
144
+ const dir = dest.clone().subtract(org).normalize();
145
+ const d = this.distanceFromRay(org, dir);
146
+ return d > dest.distanceTo(org) || d < 0 ? null : new vec3_1.Vec3(org.x + dir.x * d, org.y + dir.y * d, org.z + dir.z * d);
147
+ }
148
+ distanceFromRay(origin, direction, xz = false) {
138
149
  const ro = origin.toArray();
139
- const rd = direction.normalize().toArray();
150
+ const rd = direction.clone().normalize().toArray();
140
151
  const aabb = this.toMinAndMaxArrays();
141
152
  const dims = ro.length; // will change later.
153
+ const dif = xz ? 2 : 1;
142
154
  let lo = -Infinity;
143
155
  let hi = +Infinity;
144
156
  // let test = origin.clone()
145
- for (let i = 0; i < dims; i++) {
157
+ for (let i = 0; i < dims; i += dif) {
146
158
  let dimLo = (aabb[0][i] - ro[i]) / rd[i];
147
159
  let dimHi = (aabb[1][i] - ro[i]) / rd[i];
148
160
  if (dimLo > dimHi) {
@@ -150,21 +162,7 @@ class AABB {
150
162
  dimLo = dimHi;
151
163
  dimHi = tmp;
152
164
  }
153
- // let num;
154
- // switch (i) {
155
- // case 0:
156
- // num = "x"
157
- // break;
158
- // case 1:
159
- // num = "y"
160
- // break;
161
- // case 2:
162
- // num = "z"
163
- // break;
164
- // }
165
- // console.log(num, aabb[0][i], aabb[1][i], ro[i], "highest overall:", lo, hi )
166
165
  if (dimHi < lo || dimLo > hi) {
167
- console.log("fuck", dimHi < lo, dimLo > hi);
168
166
  return Infinity;
169
167
  }
170
168
  if (dimLo > lo)
@@ -182,6 +180,12 @@ class AABB {
182
180
  this.maxY === other.maxY &&
183
181
  this.maxZ === other.maxZ);
184
182
  }
183
+ xzDistanceTo(pos, heightOffset = 0) {
184
+ const { x, y, z } = pos.offset(0, heightOffset, 0);
185
+ let dx = Math.max(this.minX - x, 0, x - this.maxX);
186
+ let dz = Math.max(this.minZ - z, 0, z - this.maxZ);
187
+ return Math.sqrt(dx * dx + dz * dz);
188
+ }
185
189
  distanceTo(pos, heightOffset = 0) {
186
190
  const { x, y, z } = pos.offset(0, heightOffset, 0);
187
191
  let dx = Math.max(this.minX - x, 0, x - this.maxX);
@@ -0,0 +1,15 @@
1
+ import { Bot } from "mineflayer";
2
+ import { Vec3 } from "vec3";
3
+ import type { Entity } from "prismarine-entity";
4
+ export declare class RayTraceFunctions {
5
+ private bot;
6
+ 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;
14
+ entityAtEntityCursor(entity: Entity, maxDistance?: number): Entity | null;
15
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RayTraceFunctions = void 0;
4
+ const vec3_1 = require("vec3");
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
+ }
13
+ class RayTraceFunctions {
14
+ constructor(bot) {
15
+ this.bot = bot;
16
+ }
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);
21
+ }
22
+ entityAtCursor(maxDistance = 3.5) {
23
+ return this.entityAtEntityCursor(this.bot.entity, maxDistance);
24
+ }
25
+ entityAtEntityCursor(entity, maxDistance = 3.5) {
26
+ var _a;
27
+ const block = this.blockAtEntityCursor(entity, maxDistance);
28
+ maxDistance = (_a = block === null || block === void 0 ? void 0 : block.intersect.distanceTo(this.bot.entity.position)) !== null && _a !== void 0 ? _a : maxDistance;
29
+ const entities = Object.values(this.bot.entities).filter((e) => e.type !== "object" && e.username !== entity.username && e.position.distanceTo(entity.position) <= maxDistance);
30
+ 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));
31
+ const iterator = new iterators_1.RaycastIterator(entity.position.offset(0, entity.height, 0), dir.normalize(), maxDistance);
32
+ let targetEntity = null;
33
+ let targetDist = maxDistance;
34
+ for (let i = 0; i < entities.length; i++) {
35
+ const e = entities[i];
36
+ const w = e.height >= 1.62 && e.height <= 1.99 || e.height === 2.9 ? 0.3 : e.height / 2;
37
+ const shapes = [[-w, 0, -w, w, e.height + (e.height === 1.62 ? 0.18 : 0), w]];
38
+ const intersect = iterator.intersect(shapes, e.position);
39
+ if (intersect) {
40
+ const entityDir = e.position.minus(entity.position); // Can be combined into 1 line
41
+ const sign = Math.sign(entityDir.dot(dir));
42
+ if (sign !== -1) {
43
+ const dist = entity.position.distanceTo(intersect.pos);
44
+ if (dist < targetDist) {
45
+ targetEntity = e;
46
+ targetDist = dist;
47
+ }
48
+ }
49
+ }
50
+ }
51
+ return targetEntity;
52
+ }
53
+ }
54
+ exports.RayTraceFunctions = RayTraceFunctions;
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -7,6 +7,7 @@ import { MovementFunctions } from "./movementFunctions";
7
7
  import { PredictiveFunctions } from "./predictiveFunctions";
8
8
  import { MathFunctions } from "./mathUtil";
9
9
  import { WorldFunctions } from "./WorldFunctions";
10
+ import { RayTraceFunctions } from "./rayTracingFunctions";
10
11
  export declare type BuiltInPriorityOptions = {
11
12
  group: PrioGroups;
12
13
  priority: number;
@@ -28,6 +29,7 @@ export declare class UtilFunctions {
28
29
  filters: FilterFunctions;
29
30
  math: MathFunctions;
30
31
  world: WorldFunctions;
32
+ raytrace: RayTraceFunctions;
31
33
  private builtInsPriorityStore;
32
34
  private customPriorityStore;
33
35
  private builtInCurrentExecuting;
@@ -18,6 +18,7 @@ const util_1 = require("util");
18
18
  const predictiveFunctions_1 = require("./predictiveFunctions");
19
19
  const mathUtil_1 = require("./mathUtil");
20
20
  const WorldFunctions_1 = require("./WorldFunctions");
21
+ const rayTracingFunctions_1 = require("./rayTracingFunctions");
21
22
  class UtilFunctions {
22
23
  constructor(bot) {
23
24
  this.bot = bot;
@@ -28,6 +29,7 @@ class UtilFunctions {
28
29
  this.predict = new predictiveFunctions_1.PredictiveFunctions(bot);
29
30
  this.filters = new filterFunctions_1.FilterFunctions(bot);
30
31
  this.world = new WorldFunctions_1.WorldFunctions(bot);
32
+ this.raytrace = new rayTracingFunctions_1.RayTraceFunctions(bot);
31
33
  this.math = new mathUtil_1.MathFunctions();
32
34
  this.builtInsPriorityStore = {};
33
35
  this.customPriorityStore = {};
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PredictiveWorld = void 0;
4
4
  const vec3_1 = require("vec3");
5
- const raycastIterator_1 = require("./raycastIterator");
5
+ const iterators_1 = require("../calcs/iterators");
6
6
  /**
7
7
  * A class dedicated to predictive logic.
8
8
  *
@@ -14,7 +14,7 @@ class PredictiveWorld {
14
14
  this.blocks = {};
15
15
  }
16
16
  raycast(from, direction, range, matcher = null) {
17
- const iter = new raycastIterator_1.RaycastIterator(from, direction, range);
17
+ const iter = new iterators_1.RaycastIterator(from, direction, range);
18
18
  let pos = iter.next();
19
19
  while (pos) {
20
20
  const position = new vec3_1.Vec3(pos.x, pos.y, pos.z);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-util-plugin",
3
- "version": "1.2.6",
3
+ "version": "1.3.3",
4
4
  "description": "mineflayer utils for NextGEN mineflayer plugins.",
5
5
  "keywords": [
6
6
  "mineflayer",
@@ -23,6 +23,7 @@
23
23
  "mineflayer": "^3.11.2",
24
24
  "mineflayer-pathfinder": "^1.8.0",
25
25
  "prismarine-block": "^1.10.3",
26
+ "prismarine-entity": "^1.2.0",
26
27
  "prismarine-item": "^1.11.1",
27
28
  "vec3": "^0.1.7"
28
29
  },