@nxg-org/mineflayer-util-plugin 1.3.5 → 1.3.6

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.
@@ -0,0 +1,41 @@
1
+ import { Bot } from "mineflayer";
2
+ import { Block } from "prismarine-block";
3
+ import { Vec3 } from "vec3";
4
+ export declare class InterceptFunctions {
5
+ bot: Bot;
6
+ constructor(bot: Bot);
7
+ check(from: Vec3, to: Vec3): {
8
+ block: Block;
9
+ iterations: ({
10
+ x: number;
11
+ y: number;
12
+ z: number;
13
+ face: number;
14
+ } | null)[];
15
+ } | {
16
+ block: null;
17
+ iterations: ({
18
+ x: number;
19
+ y: number;
20
+ z: number;
21
+ face: number;
22
+ } | null)[];
23
+ };
24
+ raycast(from: Vec3, direction: Vec3, range: number): {
25
+ block: Block;
26
+ iterations: ({
27
+ x: number;
28
+ y: number;
29
+ z: number;
30
+ face: number;
31
+ } | null)[];
32
+ } | {
33
+ block: null;
34
+ iterations: ({
35
+ x: number;
36
+ y: number;
37
+ z: number;
38
+ face: number;
39
+ } | null)[];
40
+ };
41
+ }
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InterceptFunctions = void 0;
4
+ const vec3_1 = require("vec3");
5
+ class InterceptFunctions {
6
+ constructor(bot) {
7
+ this.bot = bot;
8
+ }
9
+ //Old ver grabs range between the two + direction from... from to... to.
10
+ //calls raycast.
11
+ check(from, to) {
12
+ //old code:
13
+ const range = from.distanceTo(to);
14
+ const direction = to.minus(from).normalize();
15
+ if (isNaN(range))
16
+ return { block: null, iterations: [] };
17
+ return this.raycast(from, direction, range);
18
+ }
19
+ raycast(from, direction, range) {
20
+ const iterations = [];
21
+ const iter = new RaycastIterator(from, direction, range);
22
+ let pos = iter.next();
23
+ while (pos) {
24
+ iterations.push(pos);
25
+ const position = new vec3_1.Vec3(pos.x, pos.y, pos.z);
26
+ const block = this.bot.blockAt(position);
27
+ if (block) {
28
+ const intersect = iter.intersect(block.shapes, position);
29
+ if (intersect) {
30
+ return {
31
+ block,
32
+ iterations,
33
+ };
34
+ }
35
+ }
36
+ pos = iter.next();
37
+ if (range > 20 || (iterations.length >= 1000 && iterations.length % 1000 === 0)) {
38
+ // console.trace("too much");
39
+ console.log(from, direction, range, iterations, block, position, pos);
40
+ }
41
+ // console.log(range, iterations.length);
42
+ }
43
+ return {
44
+ block: null,
45
+ iterations,
46
+ };
47
+ }
48
+ }
49
+ exports.InterceptFunctions = InterceptFunctions;
50
+ var BlockFace;
51
+ (function (BlockFace) {
52
+ BlockFace[BlockFace["UNKNOWN"] = -999] = "UNKNOWN";
53
+ BlockFace[BlockFace["BOTTOM"] = 0] = "BOTTOM";
54
+ BlockFace[BlockFace["TOP"] = 1] = "TOP";
55
+ BlockFace[BlockFace["NORTH"] = 2] = "NORTH";
56
+ BlockFace[BlockFace["SOUTH"] = 3] = "SOUTH";
57
+ BlockFace[BlockFace["WEST"] = 4] = "WEST";
58
+ BlockFace[BlockFace["EAST"] = 5] = "EAST";
59
+ })(BlockFace || (BlockFace = {}));
60
+ class RaycastIterator {
61
+ constructor(pos, dir, maxDistance) {
62
+ this.block = {
63
+ x: Math.floor(pos.x),
64
+ y: Math.floor(pos.y),
65
+ z: Math.floor(pos.z),
66
+ face: BlockFace.UNKNOWN,
67
+ };
68
+ this.blockVec = new vec3_1.Vec3(Math.floor(pos.x), Math.floor(pos.y), Math.floor(pos.z));
69
+ this.pos = pos;
70
+ this.dir = dir;
71
+ this.invDirX = dir.x === 0 ? Number.MAX_VALUE : 1 / dir.x;
72
+ this.invDirY = dir.y === 0 ? Number.MAX_VALUE : 1 / dir.y;
73
+ this.invDirZ = dir.z === 0 ? Number.MAX_VALUE : 1 / dir.z;
74
+ this.stepX = Math.sign(dir.x);
75
+ this.stepY = Math.sign(dir.y);
76
+ this.stepZ = Math.sign(dir.z);
77
+ this.tDeltaX = dir.x === 0 ? Number.MAX_VALUE : Math.abs(1 / dir.x);
78
+ this.tDeltaY = dir.y === 0 ? Number.MAX_VALUE : Math.abs(1 / dir.y);
79
+ this.tDeltaZ = dir.z === 0 ? Number.MAX_VALUE : Math.abs(1 / dir.z);
80
+ this.tMaxX = dir.x === 0 ? Number.MAX_VALUE : Math.abs((this.block.x + (dir.x > 0 ? 1 : 0) - pos.x) / dir.x);
81
+ this.tMaxY = dir.y === 0 ? Number.MAX_VALUE : Math.abs((this.block.y + (dir.y > 0 ? 1 : 0) - pos.y) / dir.y);
82
+ this.tMaxZ = dir.z === 0 ? Number.MAX_VALUE : Math.abs((this.block.z + (dir.z > 0 ? 1 : 0) - pos.z) / dir.z);
83
+ this.maxDistance = maxDistance;
84
+ }
85
+ // Returns null if none of the shapes is intersected, otherwise returns intersect pos and face
86
+ // shapes are translated by offset
87
+ //[x0: number,y0: number,z0: number,x1:number,y1:number,z1:number][]
88
+ intersect(shapes, offset) {
89
+ // Shapes is an array of shapes, each in the form of: [x0, y0, z0, x1, y1, z1]
90
+ let t = Number.MAX_VALUE;
91
+ let f = BlockFace.UNKNOWN;
92
+ const p = this.pos.minus(offset);
93
+ for (const shape of shapes) {
94
+ let tmin = (shape[this.invDirX > 0 ? 0 : 3] - p.x) * this.invDirX;
95
+ let tmax = (shape[this.invDirX > 0 ? 3 : 0] - p.x) * this.invDirX;
96
+ const tymin = (shape[this.invDirY > 0 ? 1 : 4] - p.y) * this.invDirY;
97
+ const tymax = (shape[this.invDirY > 0 ? 4 : 1] - p.y) * this.invDirY;
98
+ let face = this.stepX > 0 ? BlockFace.WEST : BlockFace.EAST;
99
+ if (tmin > tymax || tymin > tmax)
100
+ continue;
101
+ if (tymin > tmin) {
102
+ tmin = tymin;
103
+ face = this.stepY > 0 ? BlockFace.BOTTOM : BlockFace.TOP;
104
+ }
105
+ if (tymax < tmax)
106
+ tmax = tymax;
107
+ const tzmin = (shape[this.invDirZ > 0 ? 2 : 5] - p.z) * this.invDirZ;
108
+ const tzmax = (shape[this.invDirZ > 0 ? 5 : 2] - p.z) * this.invDirZ;
109
+ if (tmin > tzmax || tzmin > tmax)
110
+ continue;
111
+ if (tzmin > tmin) {
112
+ tmin = tzmin;
113
+ face = this.stepZ > 0 ? BlockFace.NORTH : BlockFace.SOUTH;
114
+ }
115
+ if (tzmax < tmax)
116
+ tmax = tzmax;
117
+ if (tmin < t) {
118
+ t = tmin;
119
+ f = face;
120
+ }
121
+ }
122
+ if (t === Number.MAX_VALUE)
123
+ return null;
124
+ return { pos: this.pos.plus(this.dir.scaled(t)), face: f };
125
+ }
126
+ next() {
127
+ if (Math.min(Math.min(this.tMaxX, this.tMaxY), this.tMaxZ) > this.maxDistance) {
128
+ return null;
129
+ }
130
+ if (this.tMaxX < this.tMaxY) {
131
+ if (this.tMaxX < this.tMaxZ) {
132
+ this.block.x += this.stepX;
133
+ this.tMaxX += this.tDeltaX;
134
+ this.block.face = this.stepX > 0 ? BlockFace.WEST : BlockFace.EAST;
135
+ }
136
+ else {
137
+ this.block.z += this.stepZ;
138
+ this.tMaxZ += this.tDeltaZ;
139
+ this.block.face = this.stepZ > 0 ? BlockFace.NORTH : BlockFace.SOUTH;
140
+ }
141
+ }
142
+ else {
143
+ if (this.tMaxY < this.tMaxZ) {
144
+ this.block.y += this.stepY;
145
+ this.tMaxY += this.tDeltaY;
146
+ this.block.face = this.stepY > 0 ? BlockFace.BOTTOM : BlockFace.TOP;
147
+ }
148
+ else {
149
+ this.block.z += this.stepZ;
150
+ this.tMaxZ += this.tDeltaZ;
151
+ this.block.face = this.stepZ > 0 ? BlockFace.NORTH : BlockFace.SOUTH;
152
+ }
153
+ }
154
+ if (isNaN(this.block.x) || isNaN(this.block.y) || isNaN(this.block.z))
155
+ return null;
156
+ return this.block;
157
+ }
158
+ }
package/lib/index.d.ts CHANGED
@@ -26,3 +26,4 @@ declare module "prismarine-entity" {
26
26
  }
27
27
  export default function inject(bot: Bot): void;
28
28
  export { AABB } from "./calcs/aabb";
29
+ export { InterceptFunctions } from "./calcs/intercept";
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AABB = void 0;
3
+ exports.InterceptFunctions = exports.AABB = void 0;
4
4
  const utilFunctions_1 = require("./utilFunctions");
5
5
  function inject(bot) {
6
6
  bot.util = new utilFunctions_1.UtilFunctions(bot);
@@ -8,3 +8,5 @@ function inject(bot) {
8
8
  exports.default = inject;
9
9
  var aabb_1 = require("./calcs/aabb");
10
10
  Object.defineProperty(exports, "AABB", { enumerable: true, get: function () { return aabb_1.AABB; } });
11
+ var intercept_1 = require("./calcs/intercept");
12
+ Object.defineProperty(exports, "InterceptFunctions", { enumerable: true, get: function () { return intercept_1.InterceptFunctions; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-util-plugin",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "mineflayer utils for NextGEN mineflayer plugins.",
5
5
  "keywords": [
6
6
  "mineflayer",