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

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,20 @@
1
+ import { Bot } from "mineflayer";
2
+ import { Block } from "prismarine-block";
3
+ import { Vec3 } from "vec3";
4
+ declare type Iteration = {
5
+ x: number;
6
+ y: number;
7
+ z: number;
8
+ face: number;
9
+ };
10
+ declare type BlockAndIterations = {
11
+ block: Block | null;
12
+ iterations: Iteration[];
13
+ };
14
+ export declare class InterceptFunctions {
15
+ private bot;
16
+ constructor(bot: Bot);
17
+ check(from: Vec3, to: Vec3): BlockAndIterations;
18
+ raycast(from: Vec3, direction: Vec3, range: number): BlockAndIterations;
19
+ }
20
+ export {};
@@ -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,10 +1,15 @@
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
+ const mineflayer_pathfinder_1 = require("mineflayer-pathfinder");
5
6
  function inject(bot) {
7
+ if (!bot.pathfinder)
8
+ bot.loadPlugin(mineflayer_pathfinder_1.pathfinder);
6
9
  bot.util = new utilFunctions_1.UtilFunctions(bot);
7
10
  }
8
11
  exports.default = inject;
9
12
  var aabb_1 = require("./calcs/aabb");
10
13
  Object.defineProperty(exports, "AABB", { enumerable: true, get: function () { return aabb_1.AABB; } });
14
+ var intercept_1 = require("./calcs/intercept");
15
+ Object.defineProperty(exports, "InterceptFunctions", { enumerable: true, get: function () { return intercept_1.InterceptFunctions; } });
@@ -90,18 +90,17 @@ class InventoryFunctions {
90
90
  }
91
91
  findItemByID(itemId, metadata) {
92
92
  var _a, _b;
93
- const potentialMatches = this.getAllItems().filter((item) => item.type === itemId);
94
93
  if (metadata)
95
- return (_a = potentialMatches.find((item) => item.metadata === metadata)) !== null && _a !== void 0 ? _a : null;
96
- return (_b = potentialMatches[0]) !== null && _b !== void 0 ? _b : null;
94
+ return (_a = this.getAllItems().find((item) => item.type === itemId && item.metadata === metadata)) !== null && _a !== void 0 ? _a : null;
95
+ else
96
+ return (_b = this.getAllItems().find((item) => item.type === itemId)) !== null && _b !== void 0 ? _b : null;
97
97
  }
98
98
  findItem(name, metadata) {
99
99
  var _a, _b;
100
- //[...this.getAllItems(), this.bot.inventory.selectedItem!]
101
- const potentialMatches = this.getAllItems().filter((item) => item === null || item === void 0 ? void 0 : item.name.includes(name));
102
100
  if (metadata)
103
- return (_a = potentialMatches.find((item) => item.metadata === metadata)) !== null && _a !== void 0 ? _a : null;
104
- return (_b = potentialMatches[0]) !== null && _b !== void 0 ? _b : null;
101
+ return (_a = this.getAllItems().find((item) => item.name === name && item.metadata === metadata)) !== null && _a !== void 0 ? _a : null;
102
+ else
103
+ return (_b = this.getAllItems().find((item) => item.name === name)) !== null && _b !== void 0 ? _b : null;
105
104
  }
106
105
  //alias.
107
106
  has(name, metadata) {
@@ -100,7 +100,7 @@ class MovementFunctions {
100
100
  if (update) {
101
101
  // this.bot.entity.yaw = yaw;
102
102
  // this.bot.entity.pitch = pitch
103
- this.bot.lookAt(pos, true);
103
+ this.bot.look(yaw, pitch, true);
104
104
  }
105
105
  }
106
106
  }
@@ -91,15 +91,21 @@ const AIR_BLOCK = { type: 0 };
91
91
  class PredictiveFunctions {
92
92
  constructor(bot) {
93
93
  this.bot = bot;
94
- this.damageMultiplier = 7; // for 1.12+ 8 for 1.8 TODO check when the change occur (likely 1.9)
95
94
  this.resistanceIndex = "11";
96
- if (require('minecraft-data')(bot.version).isNewerOrEqualTo("1.16")) {
95
+ const version = Number(bot.version.split(".")[1]);
96
+ if (version === 16) {
97
97
  this.armorToughnessKey = "generic.armorToughness";
98
98
  this.armorProtectionKey = "generic.armor";
99
99
  }
100
100
  else {
101
- this.armorToughnessKey = "generic.armorToughness";
102
- this.armorProtectionKey = "generic.armor";
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;
103
109
  }
104
110
  const effects = (0, minecraft_data_1.default)(bot.version).effects;
105
111
  for (const effectId in effects) {
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.9",
4
4
  "description": "mineflayer utils for NextGEN mineflayer plugins.",
5
5
  "keywords": [
6
6
  "mineflayer",
@@ -28,7 +28,7 @@
28
28
  "vec3": "^0.1.7"
29
29
  },
30
30
  "devDependencies": {
31
- "@types/node": "^16.11.11",
31
+ "@types/node": "^17.0.4",
32
32
  "typescript": "^4.5.2"
33
33
  }
34
34
  }