@nxg-org/mineflayer-util-plugin 1.5.0 → 1.5.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.
@@ -32,7 +32,7 @@ export declare class RaycastIterator {
32
32
  tMaxZ: number;
33
33
  maxDistance: number;
34
34
  constructor(pos: Vec3, dir: Vec3, maxDistance: number);
35
- intersect(shapes: [x0: BlockFace, y0: BlockFace, z0: BlockFace, x1: BlockFace, y1: BlockFace, z1: BlockFace][], offset: Vec3): {
35
+ intersect(shapes: [x0: number, y0: number, z0: number, x1: number, y1: number, z1: number][], offset: Vec3): {
36
36
  pos: Vec3;
37
37
  face: BlockFace;
38
38
  } | null;
@@ -39,7 +39,8 @@ export declare class EntityFunctions {
39
39
  */
40
40
  getHealthChange(packetMetadata: any, entity: Entity): number;
41
41
  getDistanceToEntity(entity: Entity): number;
42
- getDistanceBetweenEntities(first: Entity, second: Entity): number;
42
+ getEyeDistanceToEntity(entity: Entity): number;
43
+ getEyeDistanceBetweenEntities(first: Entity, second: Entity): number;
43
44
  getEntityAABB(entity: {
44
45
  type: string;
45
46
  position: Vec3;
@@ -16,11 +16,6 @@ class EntityFunctions {
16
16
  this.bot = bot;
17
17
  this.healthSlot = 7;
18
18
  this.bot.on("spawn", () => __awaiter(this, void 0, void 0, function* () {
19
- // await this.bot.util.sleep(1000)
20
- // const slot = this.bot.entity.metadata.slice(5).findIndex((data) => Number(data) === 20);
21
- // if (slot > 0) {
22
- // this.healthSlot = slot + 5;
23
- // }
24
19
  this.healthSlot = Number(this.bot.version.split(".")[1]) <= 16 ? 7 : 9;
25
20
  }));
26
21
  }
@@ -51,11 +46,9 @@ class EntityFunctions {
51
46
  var _a, _b;
52
47
  entity !== null && entity !== void 0 ? entity : (entity = this.bot.entity);
53
48
  const metadata = entity.metadata;
54
- const healthSlot = this.healthSlot; //metadata[this.healthSlot] ? this.healthSlot : metadata.findIndex((met) => Number(met) > 1 && Number(met) <= 20);
55
- let health = Number(metadata[healthSlot]);
49
+ let health = Number(metadata[this.healthSlot]);
56
50
  if (!health || health === 0)
57
51
  health = entity === this.bot.entity ? (_a = this.bot.entity.health) !== null && _a !== void 0 ? _a : 0 : 0;
58
- // console.log(health + (Number(metadata[this.healthSlot + 4]) ?? 0))
59
52
  return health + ((_b = Number(metadata[this.healthSlot + 4])) !== null && _b !== void 0 ? _b : 0);
60
53
  }
61
54
  /**
@@ -75,23 +68,25 @@ class EntityFunctions {
75
68
  getHealthChange(packetMetadata, entity) {
76
69
  const oldHealth = this.getHealthFromMetadata(entity.metadata);
77
70
  const newHealth = this.getHealthFromMetadata(this.parseMetadata(packetMetadata, entity.metadata));
78
- return -(oldHealth - newHealth);
71
+ return newHealth - oldHealth;
79
72
  }
80
73
  getDistanceToEntity(entity) {
81
- return this.getDistanceBetweenEntities(this.bot.entity, entity);
74
+ return this.bot.entity.position.distanceTo(entity.position);
82
75
  }
83
- getDistanceBetweenEntities(first, second) {
84
- return first.position.distanceTo(second.position);
76
+ getEyeDistanceToEntity(entity) {
77
+ return this.getEntityAABB(entity).distanceToVec(this.bot.entity.position.offset(0, 1.62, 0));
78
+ }
79
+ getEyeDistanceBetweenEntities(first, second) {
80
+ return this.getEntityAABB(second).distanceToVec(first.position.offset(0, first.height, 0));
85
81
  }
86
82
  getEntityAABB(entity) {
87
- var _a;
88
83
  switch (entity.type) {
89
84
  case "player":
90
85
  return this.getPlayerAABB({ position: entity.position });
91
86
  case "mob":
92
87
  default:
93
88
  //TODO: Implement better AABBs. However, this may just be correct.
94
- return this.getEntityAABBRaw({ position: entity.position, height: entity.height, width: (_a = entity.width) !== null && _a !== void 0 ? _a : entity.height });
89
+ return this.getEntityAABBRaw({ position: entity.position, height: entity.height, width: entity.width });
95
90
  }
96
91
  }
97
92
  getPlayerAABB(entity) {
package/lib/example.js CHANGED
@@ -21,7 +21,6 @@ const bot = (0, mineflayer_1.createBot)({
21
21
  username: "utilTesting",
22
22
  host: (_a = process.argv[2]) !== null && _a !== void 0 ? _a : "localhost",
23
23
  port: (_b = Number(process.argv[3])) !== null && _b !== void 0 ? _b : 25565,
24
- version: "1.17.1",
25
24
  });
26
25
  bot.loadPlugin(index_1.default);
27
26
  const emptyVec = new vec3_1.Vec3(0, 0, 0);
@@ -47,7 +46,7 @@ bot.on("chat", (username, message) => __awaiter(void 0, void 0, void 0, function
47
46
  const health = bot.util.entity.getHealth();
48
47
  bot.chat(`${health}`);
49
48
  break;
50
- case "WhatAmILookingAt":
49
+ case "whatAmILookingAt":
51
50
  target = bot.nearestEntity((e) => { var _a; return ((_a = e.username) !== null && _a !== void 0 ? _a : e.name) === username; });
52
51
  if (!target)
53
52
  return console.log("no entity");
@@ -18,6 +18,5 @@ export declare class FilterFunctions {
18
18
  static specificNamesFilter(bot: Bot, ...names: string[]): Entity | null;
19
19
  nearestCrystalFilter(): Entity | null;
20
20
  static nearestCrystalFilter(bot: Bot): Entity | null;
21
- entityAtPosition(position: Vec3): Entity | null;
22
- static entityAtPosition(bot: Bot, position: Vec3): void;
21
+ crystalAtPosition(position: Vec3): Entity | null;
23
22
  }
@@ -37,9 +37,8 @@ class FilterFunctions {
37
37
  static nearestCrystalFilter(bot) {
38
38
  return FilterFunctions.getNearestEntity(bot, (e) => e.name === "ender_crystal");
39
39
  }
40
- entityAtPosition(position) {
40
+ crystalAtPosition(position) {
41
41
  return this.getNearestEntity((entity) => entity.position.offset(-0.5, -1, -0.5).equals(position));
42
42
  }
43
- static entityAtPosition(bot, position) { }
44
43
  }
45
44
  exports.FilterFunctions = FilterFunctions;
package/lib/index.d.ts CHANGED
@@ -5,18 +5,9 @@ declare module "mineflayer" {
5
5
  interface Bot {
6
6
  util: UtilFunctions;
7
7
  }
8
- interface BotEvents {
9
- startedEquippingMainHand: () => void;
10
- startedEquippingOffHand: () => void;
11
- stoppedEquippingMainHand: () => void;
12
- stoppedEquippingOffHand: () => void;
13
- startedEquippingOtherSlot: () => void;
14
- stoppedEquippingOtherSlot: () => void;
15
- }
16
- }
17
- declare module "prismarine-entity" {
18
8
  }
19
9
  export default function inject(bot: Bot): void;
20
10
  export { AABB } from "./calcs/aabb";
21
11
  export { InterceptFunctions } from "./calcs/intercept";
12
+ export { RaycastIterator, BlockFace } from "./calcs/iterators";
22
13
  export { AABBUtils, MathUtils } from "./static";
package/lib/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MathUtils = exports.AABBUtils = exports.InterceptFunctions = exports.AABB = void 0;
3
+ exports.MathUtils = exports.AABBUtils = exports.BlockFace = exports.RaycastIterator = exports.InterceptFunctions = exports.AABB = void 0;
4
4
  const utilFunctions_1 = require("./utilFunctions");
5
5
  const mineflayer_pathfinder_1 = require("mineflayer-pathfinder");
6
6
  function inject(bot) {
7
- if (!bot.pathfinder)
7
+ if (!bot.hasPlugin(mineflayer_pathfinder_1.pathfinder))
8
8
  bot.loadPlugin(mineflayer_pathfinder_1.pathfinder);
9
9
  bot.util = new utilFunctions_1.UtilFunctions(bot);
10
10
  }
@@ -13,6 +13,9 @@ var aabb_1 = require("./calcs/aabb");
13
13
  Object.defineProperty(exports, "AABB", { enumerable: true, get: function () { return aabb_1.AABB; } });
14
14
  var intercept_1 = require("./calcs/intercept");
15
15
  Object.defineProperty(exports, "InterceptFunctions", { enumerable: true, get: function () { return intercept_1.InterceptFunctions; } });
16
+ var iterators_1 = require("./calcs/iterators");
17
+ Object.defineProperty(exports, "RaycastIterator", { enumerable: true, get: function () { return iterators_1.RaycastIterator; } });
18
+ Object.defineProperty(exports, "BlockFace", { enumerable: true, get: function () { return iterators_1.BlockFace; } });
16
19
  var static_1 = require("./static");
17
20
  Object.defineProperty(exports, "AABBUtils", { enumerable: true, get: function () { return static_1.AABBUtils; } });
18
21
  Object.defineProperty(exports, "MathUtils", { enumerable: true, get: function () { return static_1.MathUtils; } });
@@ -1,6 +1,5 @@
1
1
  import { Bot, EquipmentDestination } from "mineflayer";
2
2
  import { Item } from "prismarine-item";
3
- import { BuiltInPriorityOptions } from "./utilFunctions";
4
3
  /**
5
4
  * Error codes:
6
5
  *
@@ -34,12 +33,5 @@ export declare class InventoryFunctions {
34
33
  findItem(name: string, metadata?: number): Item | null;
35
34
  has(name: string, metadata?: number): boolean;
36
35
  equipItemRaw(item: Item, dest: EquipmentDestination): Promise<boolean>;
37
- equipItem(name: string, dest: EquipmentDestination, options: BuiltInPriorityOptions): Promise<number>;
38
- equipMainHand(name: string, options?: BuiltInPriorityOptions): Promise<number>;
39
- equipOffHand(name: string, options?: BuiltInPriorityOptions): Promise<number>;
40
- equipSlot(name: string, destination: EquipmentDestination, options?: BuiltInPriorityOptions): Promise<number>;
41
- equipMainHandNoWait(name: string, options: BuiltInPriorityOptions): Promise<number>;
42
- equipOffHandNoWait(name: string, options: BuiltInPriorityOptions): Promise<number>;
43
- equipSlotNoWait(name: string, dest: EquipmentDestination, options: BuiltInPriorityOptions): Promise<number>;
44
36
  customEquip(item: Item, destination: EquipmentDestination, retries?: number): Promise<boolean>;
45
37
  }
@@ -35,10 +35,8 @@ class InventoryFunctions {
35
35
  set equippingOtherSlot(value) {
36
36
  if (this._equippingOtherSlot === value)
37
37
  return;
38
- if (value)
39
- this.bot.emit("startedEquippingOtherSlot");
40
- else
41
- this.bot.emit("stoppedEquippingOtherSlot");
38
+ // if (value) this.bot.emit("startedEquippingOtherSlot");
39
+ // else this.bot.emit("stoppedEquippingOtherSlot");
42
40
  this._equippingOtherSlot = value;
43
41
  }
44
42
  get equippingOtherSlot() {
@@ -47,10 +45,8 @@ class InventoryFunctions {
47
45
  set equippingMainHand(value) {
48
46
  if (this._equippingMainHand === value)
49
47
  return;
50
- if (value)
51
- this.bot.emit("startedEquippingMainHand");
52
- else
53
- this.bot.emit("stoppedEquippingMainHand");
48
+ // if (value) this.bot.emit("startedEquippingMainHand");
49
+ // else this.bot.emit("stoppedEquippingMainHand");
54
50
  this._equippingMainHand = value;
55
51
  }
56
52
  get equippingMainHand() {
@@ -59,10 +55,8 @@ class InventoryFunctions {
59
55
  set equippingOffHand(value) {
60
56
  if (this._equippingOffHand === value)
61
57
  return;
62
- if (value)
63
- this.bot.emit("startedEquippingOffHand");
64
- else
65
- this.bot.emit("stoppedEquippingOffHand");
58
+ // if (value) this.bot.emit("startedEquippingOffHand");
59
+ // else this.bot.emit("stoppedEquippingOffHand");
66
60
  this._equippingOffHand = value;
67
61
  }
68
62
  get equippingOffHand() {
@@ -77,8 +71,8 @@ class InventoryFunctions {
77
71
  getAllItemsExceptCurrent(current) {
78
72
  return [
79
73
  ...this.bot.inventory.items(),
80
- ...["hand", "head", "torso", "legs", "feet", "off-hand"]
81
- .filter((name) => name !== current)
74
+ ...(["hand", "head", "torso", "legs", "feet", "off-hand"]
75
+ .filter((name) => name !== current))
82
76
  .map((name) => this.bot.inventory.slots[this.bot.getEquipmentDestSlot(name)]),
83
77
  ].filter((e) => !!e);
84
78
  }
@@ -114,73 +108,6 @@ class InventoryFunctions {
114
108
  return false;
115
109
  });
116
110
  }
117
- equipItem(name, dest, options) {
118
- var _a;
119
- return __awaiter(this, void 0, void 0, function* () {
120
- if ((_a = this.bot.inventory.slots[this.bot.getEquipmentDestSlot(dest)]) === null || _a === void 0 ? void 0 : _a.name.includes(name))
121
- return 1;
122
- const item = this.getAllItemsExceptCurrent(dest).find((item) => item === null || item === void 0 ? void 0 : item.name.includes(name));
123
- if (!!item) {
124
- yield this.bot.util.builtInsPriority(options, this.bot.equip, item, dest);
125
- return 0;
126
- }
127
- return 2;
128
- });
129
- }
130
- equipMainHand(name, options = { group: "inventory", priority: 1 }) {
131
- return __awaiter(this, void 0, void 0, function* () {
132
- while (this._equippingMainHand) {
133
- yield sleep(0);
134
- }
135
- return yield this.equipMainHandNoWait(name, options);
136
- });
137
- }
138
- equipOffHand(name, options = { group: "inventory", priority: 1 }) {
139
- return __awaiter(this, void 0, void 0, function* () {
140
- while (this._equippingOffHand) {
141
- yield sleep(0);
142
- }
143
- return yield this.equipOffHandNoWait(name, options);
144
- });
145
- }
146
- equipSlot(name, destination, options = { group: "inventory", priority: 1 }) {
147
- return __awaiter(this, void 0, void 0, function* () {
148
- while (this._equippingOtherSlot) {
149
- yield sleep(0);
150
- }
151
- return yield this.equipSlotNoWait(name, destination, options);
152
- });
153
- }
154
- equipMainHandNoWait(name, options) {
155
- return __awaiter(this, void 0, void 0, function* () {
156
- if (this._equippingMainHand)
157
- return 3;
158
- this._equippingMainHand = true;
159
- const result = yield this.equipItem(name, "hand", options);
160
- this._equippingMainHand = false;
161
- return result;
162
- });
163
- }
164
- equipOffHandNoWait(name, options) {
165
- return __awaiter(this, void 0, void 0, function* () {
166
- if (this._equippingOffHand)
167
- return 3;
168
- this.equippingOffHand = true;
169
- const result = yield this.equipItem(name, "off-hand", options);
170
- this.equippingOffHand = false;
171
- return result;
172
- });
173
- }
174
- equipSlotNoWait(name, dest, options) {
175
- return __awaiter(this, void 0, void 0, function* () {
176
- if (this._equippingOtherSlot)
177
- return 3;
178
- this.equippingOtherSlot = true;
179
- const result = yield this.equipItem(name, dest, options);
180
- this.equippingOtherSlot = false;
181
- return result;
182
- });
183
- }
184
111
  customEquip(item, destination, retries = 1) {
185
112
  return __awaiter(this, void 0, void 0, function* () {
186
113
  for (let i = 0; i < retries; i++) {
@@ -1,5 +1,4 @@
1
1
  import type { Bot } from "mineflayer";
2
- import type { Entity } from "prismarine-entity";
3
2
  import type { Vec3 } from "vec3";
4
3
  import { goals, Movements } from "mineflayer-pathfinder";
5
4
  export declare class MovementFunctions {
@@ -30,13 +29,13 @@ export declare class MovementFunctions {
30
29
  * @param entity Prismarine-Entity Entity
31
30
  * @returns have the goals changed.
32
31
  */
33
- retreatFromEntity(entity: Entity, distance: number, dynamic?: boolean): boolean;
32
+ retreatFromEntity(entity: any, distance: number, dynamic?: boolean): boolean;
34
33
  /**
35
34
  * Follow entity with a specific range. Will not approach past a certain distance either.
36
35
  * @param entity Prismarine-Entity Entity
37
36
  * @returns have the goals changed
38
37
  */
39
- followEntityWithRespectRange(entity: Entity, followDistance: number, invertDistance?: number): boolean;
38
+ followEntityWithRespectRange(entity: any, followDistance: number, invertDistance?: number): boolean;
40
39
  forceLook(yaw: number, pitch: number, update?: boolean, onGround?: boolean): void;
41
40
  forceLookAt(pos: Vec3, update?: boolean, onGround?: boolean): void;
42
41
  lazyTeleport(endPos: Vec3): void;
@@ -15,13 +15,12 @@ var AABBUtils;
15
15
  }
16
16
  AABBUtils.getBlockPosAABB = getBlockPosAABB;
17
17
  function getEntityAABB(entity) {
18
- var _a;
19
18
  switch (entity.type) {
20
19
  case "player":
21
20
  return getPlayerAABB({ position: entity.position });
22
21
  case "mob":
23
22
  default: //TODO: Implement better AABBs. However, this may just be correct.
24
- return getEntityAABBRaw({ position: entity.position, height: entity.height, width: (_a = entity.width) !== null && _a !== void 0 ? _a : entity.height });
23
+ return getEntityAABBRaw({ position: entity.position, height: entity.height, width: entity.width });
25
24
  }
26
25
  }
27
26
  AABBUtils.getEntityAABB = getEntityAABB;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1,6 +1,10 @@
1
1
  import { Bot } from "mineflayer";
2
2
  import { Vec3 } from "vec3";
3
3
  export declare namespace MathUtils {
4
+ const TO_RAD: number;
5
+ const TO_DEG: number;
6
+ const FROM_NOTCH_BYTE: number;
7
+ const FROM_NOTCH_VEL: number;
4
8
  const toNotchianYaw: (yaw: number) => number;
5
9
  const toNotchianPitch: (pitch: number) => number;
6
10
  const fromNotchianYawByte: (yaw: number) => number;
@@ -2,30 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MathUtils = void 0;
4
4
  const vec3_1 = require("vec3");
5
- const PI = Math.PI;
6
- const PI_2 = Math.PI * 2;
7
- const TO_RAD = PI / 180;
8
- const TO_DEG = 1 / TO_RAD;
9
- const FROM_NOTCH_BYTE = 360 / 256;
10
- // From wiki.vg: Velocity is believed to be in units of 1/8000 of a block per server tick (50ms)
11
- const FROM_NOTCH_VEL = 1 / 8000;
12
5
  var MathUtils;
13
6
  (function (MathUtils) {
7
+ const PI = Math.PI;
8
+ const PI_2 = Math.PI * 2;
9
+ MathUtils.TO_RAD = PI / 180;
10
+ MathUtils.TO_DEG = 1 / MathUtils.TO_RAD;
11
+ MathUtils.FROM_NOTCH_BYTE = 360 / 256;
12
+ // From wiki.vg: Velocity is believed to be in units of 1/8000 of a block per server tick (50ms)
13
+ MathUtils.FROM_NOTCH_VEL = 1 / 8000;
14
14
  MathUtils.toNotchianYaw = (yaw) => toDegrees(PI - yaw);
15
15
  MathUtils.toNotchianPitch = (pitch) => toDegrees(-pitch);
16
- MathUtils.fromNotchianYawByte = (yaw) => fromNotchianYaw(yaw * FROM_NOTCH_BYTE);
17
- MathUtils.fromNotchianPitchByte = (pitch) => fromNotchianPitch(pitch * FROM_NOTCH_BYTE);
16
+ MathUtils.fromNotchianYawByte = (yaw) => fromNotchianYaw(yaw * MathUtils.FROM_NOTCH_BYTE);
17
+ MathUtils.fromNotchianPitchByte = (pitch) => fromNotchianPitch(pitch * MathUtils.FROM_NOTCH_BYTE);
18
18
  function euclideanMod(numerator, denominator) {
19
19
  const result = numerator % denominator;
20
20
  return result < 0 ? result + denominator : result;
21
21
  }
22
22
  MathUtils.euclideanMod = euclideanMod;
23
23
  function toRadians(degrees) {
24
- return TO_RAD * degrees;
24
+ return MathUtils.TO_RAD * degrees;
25
25
  }
26
26
  MathUtils.toRadians = toRadians;
27
27
  function toDegrees(radians) {
28
- return TO_DEG * radians;
28
+ return MathUtils.TO_DEG * radians;
29
29
  }
30
30
  MathUtils.toDegrees = toDegrees;
31
31
  function fromNotchianYaw(yaw) {
@@ -37,7 +37,7 @@ var MathUtils;
37
37
  }
38
38
  MathUtils.fromNotchianPitch = fromNotchianPitch;
39
39
  function fromNotchVelocity(vel) {
40
- return new vec3_1.Vec3(vel.x * FROM_NOTCH_VEL, vel.y * FROM_NOTCH_VEL, vel.z * FROM_NOTCH_VEL);
40
+ return new vec3_1.Vec3(vel.x * MathUtils.FROM_NOTCH_VEL, vel.y * MathUtils.FROM_NOTCH_VEL, vel.z * MathUtils.FROM_NOTCH_VEL);
41
41
  }
42
42
  MathUtils.fromNotchVelocity = fromNotchVelocity;
43
43
  function pointToYawAndPitch(bot, point) {
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import type { Bot, PrioGroups } from "mineflayer";
2
+ import type { Bot } from "mineflayer";
3
3
  import { EntityFunctions } from "./entityFunctions";
4
4
  import { FilterFunctions } from "./filterFunctions";
5
5
  import { InventoryFunctions } from "./inventoryFunctions";
@@ -8,18 +8,21 @@ import { PredictiveFunctions } from "./predictiveFunctions";
8
8
  import { MathFunctions } from "./mathUtil";
9
9
  import { WorldFunctions } from "./WorldFunctions";
10
10
  import { RayTraceFunctions } from "./rayTracingFunctions";
11
- export declare type BuiltInPriorityOptions = {
12
- group: PrioGroups;
13
- priority: number;
14
- returnIfRunning?: boolean;
15
- errCancel?: boolean;
16
- };
17
- export declare type CustomPriorityOptions = {
18
- priority: number;
19
- group?: PrioGroups;
20
- returnIfRunning?: boolean;
21
- errCancel?: boolean;
22
- };
11
+ /**
12
+ * I don't believe I need any locks, as I'm only going to have one instance of this per bot.
13
+ * This is added to bot context so multiple instances will exist in memory.
14
+ * Therefore, I don't need this. https://www.npmjs.com/package/async-lock
15
+ */
16
+ /**
17
+ * I may add listeners to this class, as halting until an item is equipped may be good.
18
+ */
19
+ /**
20
+ * I can't inherit from multiple classes. This language sucks.
21
+ * I'm not using mixins. Fuck you, fuck that.
22
+ * I'm just going to segregate these functions into separate categories
23
+ * because once again, fuck you.
24
+ *
25
+ */
23
26
  export declare class UtilFunctions {
24
27
  bot: Bot;
25
28
  inv: InventoryFunctions;
@@ -30,21 +33,6 @@ export declare class UtilFunctions {
30
33
  math: MathFunctions;
31
34
  world: WorldFunctions;
32
35
  raytrace: RayTraceFunctions;
33
- private builtInsPriorityStore;
34
- private customPriorityStore;
35
- private builtInCurrentExecuting;
36
- private customCurrentExecuting;
37
36
  constructor(bot: Bot);
38
37
  sleep: typeof import("timers/promises").setTimeout;
39
- isBuiltInsEmpty(name?: string): boolean;
40
- isCustomEmpty(name?: string): boolean;
41
- /**
42
- *
43
- * @param object \{priority, errCancel} => priority of function (highest order first), throw error if already running a function.
44
- * @param func any custom function.
45
- * @param args the arguments of passed in function.
46
- * @returns Error if errCancel and already executing, otherwise result of function.
47
- */
48
- customPriority<K extends (...args: any) => any>({ priority, group, returnIfRunning, errCancel }: CustomPriorityOptions, func: K, ...args: Parameters<K>): number | Promise<ReturnType<K> | Error>;
49
- builtInsPriority<K extends (...args: any) => any>({ group, priority, returnIfRunning, errCancel }: BuiltInPriorityOptions, func: K, ...args: Parameters<K>): number | Promise<ReturnType<K> | Error>;
50
38
  }
@@ -1,13 +1,4 @@
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.UtilFunctions = void 0;
13
4
  const entityFunctions_1 = require("./entityFunctions");
@@ -19,6 +10,21 @@ const predictiveFunctions_1 = require("./predictiveFunctions");
19
10
  const mathUtil_1 = require("./mathUtil");
20
11
  const WorldFunctions_1 = require("./WorldFunctions");
21
12
  const rayTracingFunctions_1 = require("./rayTracingFunctions");
13
+ /**
14
+ * I don't believe I need any locks, as I'm only going to have one instance of this per bot.
15
+ * This is added to bot context so multiple instances will exist in memory.
16
+ * Therefore, I don't need this. https://www.npmjs.com/package/async-lock
17
+ */
18
+ /**
19
+ * I may add listeners to this class, as halting until an item is equipped may be good.
20
+ */
21
+ /**
22
+ * I can't inherit from multiple classes. This language sucks.
23
+ * I'm not using mixins. Fuck you, fuck that.
24
+ * I'm just going to segregate these functions into separate categories
25
+ * because once again, fuck you.
26
+ *
27
+ */
22
28
  class UtilFunctions {
23
29
  constructor(bot) {
24
30
  this.bot = bot;
@@ -31,110 +37,6 @@ class UtilFunctions {
31
37
  this.world = new WorldFunctions_1.WorldFunctions(bot);
32
38
  this.raytrace = new rayTracingFunctions_1.RayTraceFunctions(bot);
33
39
  this.math = new mathUtil_1.MathFunctions();
34
- this.builtInsPriorityStore = {};
35
- this.customPriorityStore = {};
36
- this.builtInCurrentExecuting = {};
37
- this.customCurrentExecuting = {};
38
- }
39
- isBuiltInsEmpty(name) {
40
- var _a;
41
- if (name) {
42
- return !((_a = this.builtInsPriorityStore[name]) === null || _a === void 0 ? void 0 : _a.length) || !this.builtInCurrentExecuting[name];
43
- }
44
- else {
45
- return !Object.values(this.builtInsPriorityStore).length || !Object.values(this.builtInCurrentExecuting).length;
46
- }
47
- }
48
- isCustomEmpty(name) {
49
- var _a;
50
- if (name) {
51
- return !((_a = this.customPriorityStore[name]) === null || _a === void 0 ? void 0 : _a.length) && !this.customCurrentExecuting[name];
52
- }
53
- else {
54
- return !Object.values(this.customPriorityStore).length && !Object.values(this.customCurrentExecuting).length;
55
- }
56
- }
57
- /**
58
- *
59
- * @param object \{priority, errCancel} => priority of function (highest order first), throw error if already running a function.
60
- * @param func any custom function.
61
- * @param args the arguments of passed in function.
62
- * @returns Error if errCancel and already executing, otherwise result of function.
63
- */
64
- customPriority({ priority, group, returnIfRunning, errCancel }, func, ...args) {
65
- var _a, _b;
66
- var _c;
67
- const name = (_a = group !== null && group !== void 0 ? group : func.name) !== null && _a !== void 0 ? _a : "anonymous";
68
- const actionQueue = ((_b = (_c = this.customPriorityStore)[name]) !== null && _b !== void 0 ? _b : (_c[name] = []));
69
- // console.log("custom", group ?? func.name ?? "anonymous", actionQueue, this.isCustomEmpty(name))
70
- if (errCancel && actionQueue.length > 1)
71
- throw "already executing";
72
- if (returnIfRunning && !this.isCustomEmpty(name))
73
- return 1;
74
- // console.log("running.")
75
- return new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
76
- var _d, _e;
77
- const currentlyExecuting = actionQueue.shift();
78
- if (currentlyExecuting)
79
- this.customCurrentExecuting[(_d = group !== null && group !== void 0 ? group : currentlyExecuting[1].name) !== null && _d !== void 0 ? _d : "anonymous"] = currentlyExecuting;
80
- const index = actionQueue.findIndex(([prio]) => priority > prio);
81
- actionQueue.splice(index === -1 ? actionQueue.length : index, 0, [
82
- priority,
83
- () => __awaiter(this, void 0, void 0, function* () {
84
- var _f;
85
- try {
86
- res(yield func(...args));
87
- }
88
- catch (e) {
89
- rej(e);
90
- }
91
- actionQueue.shift();
92
- yield ((_f = actionQueue[0]) === null || _f === void 0 ? void 0 : _f[1]());
93
- }),
94
- ]);
95
- if (currentlyExecuting) {
96
- actionQueue.unshift(currentlyExecuting);
97
- this.customCurrentExecuting[(_e = group !== null && group !== void 0 ? group : currentlyExecuting[1].name) !== null && _e !== void 0 ? _e : "anonymous"] = undefined;
98
- }
99
- else
100
- yield actionQueue[0][1]();
101
- }));
102
- }
103
- builtInsPriority({ group, priority, returnIfRunning, errCancel }, func, ...args) {
104
- var _a;
105
- var _b;
106
- const actionQueue = ((_a = (_b = this.builtInsPriorityStore)[group]) !== null && _a !== void 0 ? _a : (_b[group] = []));
107
- // console.log("builtin", group, actionQueue)
108
- if (errCancel && !this.isBuiltInsEmpty(group))
109
- throw "already executing";
110
- if (returnIfRunning && !this.isBuiltInsEmpty(group))
111
- return 1;
112
- return new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
113
- const currentlyExecuting = actionQueue.shift();
114
- if (currentlyExecuting)
115
- this.customCurrentExecuting[group] = currentlyExecuting;
116
- const index = actionQueue.findIndex(([prio]) => priority > prio);
117
- actionQueue.splice(index === -1 ? actionQueue.length : index, 0, [
118
- priority,
119
- () => __awaiter(this, void 0, void 0, function* () {
120
- var _c;
121
- try {
122
- res(yield func.bind(this.bot)(...args));
123
- }
124
- catch (e) {
125
- rej(e);
126
- }
127
- actionQueue.shift();
128
- yield ((_c = actionQueue[0]) === null || _c === void 0 ? void 0 : _c[1]());
129
- }),
130
- ]);
131
- if (currentlyExecuting) {
132
- actionQueue.unshift(currentlyExecuting);
133
- this.builtInCurrentExecuting[group] = undefined;
134
- }
135
- else
136
- yield actionQueue[0][1]();
137
- }));
138
40
  }
139
41
  }
140
42
  exports.UtilFunctions = UtilFunctions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-util-plugin",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "mineflayer utils for NextGEN mineflayer plugins.",
5
5
  "keywords": [
6
6
  "mineflayer",
@@ -20,16 +20,12 @@
20
20
  "test": "node --trace-warnings lib/example.js"
21
21
  },
22
22
  "dependencies": {
23
- "minecraft-data": "^2.113.0",
24
- "mineflayer": "^4.0.0",
25
- "mineflayer-pathfinder": "^1.9.1",
26
- "prismarine-block": "^1.13.1",
27
- "prismarine-entity": "^2.0.0",
28
- "prismarine-item": "^1.11.2",
23
+ "mineflayer-pathfinder": "^2.3.3",
29
24
  "vec3": "^0.1.7"
30
25
  },
31
26
  "devDependencies": {
32
27
  "@types/node": "^17.0.4",
28
+ "mineflayer": "^4.4.0",
33
29
  "typescript": "^4.5.2"
34
30
  }
35
31
  }
@@ -1,10 +0,0 @@
1
- import type { Bot, EquipmentDestination } from "mineflayer";
2
- import type { Item } from "prismarine-item";
3
- export declare class CustomInventoryFunctions {
4
- private bot;
5
- armorSlots: any;
6
- constructor(bot: Bot);
7
- equip(item: Item, destination: EquipmentDestination): Promise<void>;
8
- setQuickBarSlot(slot: number): void;
9
- getDestSlot(destination: EquipmentDestination): any;
10
- }
@@ -1,95 +0,0 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.CustomInventoryFunctions = void 0;
16
- const assert_1 = __importDefault(require("assert"));
17
- const util_1 = require("util");
18
- const sleep = (0, util_1.promisify)(setTimeout);
19
- const QUICK_BAR_COUNT = 9;
20
- const QUICK_BAR_START = 36;
21
- let nextQuickBarSlot = 0;
22
- //lazy. will fix this later.
23
- class CustomInventoryFunctions {
24
- constructor(bot) {
25
- this.bot = bot;
26
- this.armorSlots = {
27
- head: 5,
28
- torso: 6,
29
- legs: 7,
30
- feet: 8,
31
- };
32
- if (!bot.supportFeature("doesntHaveOffHandSlot")) {
33
- this.armorSlots["off-hand"] = 45;
34
- }
35
- }
36
- equip(item, destination) {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- if (item == null || typeof item !== "object") {
39
- throw new Error("Invalid item object in equip (item is null or typeof item is not object)");
40
- }
41
- if (!destination || destination === null) {
42
- destination = "hand";
43
- }
44
- const sourceSlot = item.slot;
45
- let destSlot = this.getDestSlot(destination);
46
- if (sourceSlot === destSlot) {
47
- // don't need to do anything
48
- return;
49
- }
50
- if (destination !== "hand") {
51
- yield this.bot.moveSlotItem(sourceSlot, destSlot);
52
- return;
53
- }
54
- if (destSlot >= QUICK_BAR_START &&
55
- destSlot < QUICK_BAR_START + QUICK_BAR_COUNT &&
56
- sourceSlot >= QUICK_BAR_START &&
57
- sourceSlot < QUICK_BAR_START + QUICK_BAR_COUNT) {
58
- // all we have to do is change the quick bar selection
59
- this.bot.setQuickBarSlot(sourceSlot - QUICK_BAR_START);
60
- return;
61
- }
62
- // find an empty slot on the quick bar to put the source item in
63
- destSlot = this.bot.inventory.firstEmptySlotRange(QUICK_BAR_START, QUICK_BAR_START + QUICK_BAR_COUNT);
64
- if (destSlot == null) {
65
- // LRU cache for the quick bar items
66
- destSlot = QUICK_BAR_START + nextQuickBarSlot;
67
- nextQuickBarSlot = (nextQuickBarSlot + 1) % QUICK_BAR_COUNT;
68
- }
69
- this.setQuickBarSlot(destSlot - QUICK_BAR_START);
70
- yield this.bot.moveSlotItem(sourceSlot, destSlot);
71
- });
72
- }
73
- setQuickBarSlot(slot) {
74
- assert_1.default.ok(slot >= 0);
75
- assert_1.default.ok(slot < 9);
76
- if (this.bot.quickBarSlot === slot)
77
- return;
78
- this.bot.quickBarSlot = slot;
79
- this.bot._client.write("held_item_slot", {
80
- slotId: slot,
81
- });
82
- this.bot.updateHeldItem();
83
- }
84
- getDestSlot(destination) {
85
- if (destination === "hand") {
86
- return QUICK_BAR_START + this.bot.quickBarSlot;
87
- }
88
- else {
89
- const destSlot = this.armorSlots[destination];
90
- assert_1.default.ok(destSlot != null, `invalid destination: ${destination}`);
91
- return destSlot;
92
- }
93
- }
94
- }
95
- exports.CustomInventoryFunctions = CustomInventoryFunctions;