@nxg-org/mineflayer-util-plugin 1.2.4 → 1.3.1

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,8 +28,12 @@ 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
+ xzIntersectsRay(origin: Vec3, direction: Vec3): {
32
+ x: number;
33
+ z: number;
34
+ } | null;
31
35
  intersectsRay(origin: Vec3, direction: Vec3): Vec3 | null;
32
- distanceFromRay(origin: Vec3, direction: Vec3): number;
36
+ distanceFromRay(origin: Vec3, direction: Vec3, xz?: boolean): number;
33
37
  equals(other: AABB): boolean;
34
38
  distanceTo(pos: Vec3, heightOffset?: number): number;
35
39
  }
package/lib/calcs/aabb.js CHANGED
@@ -125,24 +125,24 @@ class AABB {
125
125
  this.minZ < other.maxZ &&
126
126
  this.maxZ > other.minZ);
127
127
  }
128
+ xzIntersectsRay(origin, direction) {
129
+ const d = this.distanceFromRay(origin, direction);
130
+ return d === Infinity ? null : { x: origin.x + direction.x * d, z: origin.z + direction.z * d };
131
+ }
128
132
  intersectsRay(origin, direction) {
129
133
  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
- }
134
+ return d === Infinity ? null : new vec3_1.Vec3(origin.x + direction.x * d, origin.y + direction.y * d, origin.z + direction.z * d);
136
135
  }
137
- distanceFromRay(origin, direction) {
136
+ distanceFromRay(origin, direction, xz = false) {
138
137
  const ro = origin.toArray();
139
138
  const rd = direction.normalize().toArray();
140
139
  const aabb = this.toMinAndMaxArrays();
141
140
  const dims = ro.length; // will change later.
141
+ const dif = xz ? 2 : 1;
142
142
  let lo = -Infinity;
143
143
  let hi = +Infinity;
144
144
  // let test = origin.clone()
145
- for (let i = 0; i < dims; i++) {
145
+ for (let i = 0; i < dims; i += dif) {
146
146
  let dimLo = (aabb[0][i] - ro[i]) / rd[i];
147
147
  let dimHi = (aabb[1][i] - ro[i]) / rd[i];
148
148
  if (dimLo > dimHi) {
@@ -150,21 +150,7 @@ class AABB {
150
150
  dimLo = dimHi;
151
151
  dimHi = tmp;
152
152
  }
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
153
  if (dimHi < lo || dimLo > hi) {
167
- console.log("fuck", dimHi < lo, dimLo > hi);
168
154
  return Infinity;
169
155
  }
170
156
  if (dimLo > lo)
@@ -75,9 +75,9 @@ class EntityFunctions {
75
75
  * @returns
76
76
  */
77
77
  getHealthChange(packetMetadata, entity) {
78
- const oldMetadata = entity.metadata;
79
- const newMetadata = this.parseMetadata(packetMetadata, oldMetadata);
80
- return this.getHealthFromMetadata(newMetadata) - this.getHealthFromMetadata(oldMetadata);
78
+ const oldHealth = this.getHealthFromMetadata(entity.metadata);
79
+ const newHealth = this.getHealthFromMetadata(this.parseMetadata(packetMetadata, entity.metadata));
80
+ return -(oldHealth - newHealth);
81
81
  }
82
82
  getDistanceToEntity(entity) {
83
83
  return this.getDistanceBetweenEntities(this.bot.entity, entity);
@@ -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.4",
3
+ "version": "1.3.1",
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
  },