@nxg-org/mineflayer-util-plugin 1.0.0

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.
Files changed (45) hide show
  1. package/LICENSE +674 -0
  2. package/lib/WorldFunctions.d.ts +10 -0
  3. package/lib/WorldFunctions.js +18 -0
  4. package/lib/calcs/aabb.d.ts +36 -0
  5. package/lib/calcs/aabb.js +194 -0
  6. package/lib/commonSense.d.ts +22 -0
  7. package/lib/commonSense.js +220 -0
  8. package/lib/customImplementations/inventory.d.ts +10 -0
  9. package/lib/customImplementations/inventory.js +95 -0
  10. package/lib/entityFunctions.d.ts +36 -0
  11. package/lib/entityFunctions.js +78 -0
  12. package/lib/filterFunctions.d.ts +23 -0
  13. package/lib/filterFunctions.js +45 -0
  14. package/lib/index.d.ts +27 -0
  15. package/lib/index.js +7 -0
  16. package/lib/inventoryFunctions.d.ts +45 -0
  17. package/lib/inventoryFunctions.js +208 -0
  18. package/lib/mathUtil.d.ts +23 -0
  19. package/lib/mathUtil.js +49 -0
  20. package/lib/movementFunctions.d.ts +43 -0
  21. package/lib/movementFunctions.js +109 -0
  22. package/lib/predictiveFunctions.d.ts +25 -0
  23. package/lib/predictiveFunctions.js +172 -0
  24. package/lib/utilFunctions.d.ts +50 -0
  25. package/lib/utilFunctions.js +140 -0
  26. package/lib/worldRelated/predictiveWorld.d.ts +41 -0
  27. package/lib/worldRelated/predictiveWorld.js +108 -0
  28. package/lib/worldRelated/raycastIterator.d.ts +45 -0
  29. package/lib/worldRelated/raycastIterator.js +114 -0
  30. package/package.json +33 -0
  31. package/src/WorldFunctions.ts +19 -0
  32. package/src/calcs/aabb.ts +218 -0
  33. package/src/commonSense.ts +189 -0
  34. package/src/customImplementations/inventory.ts +90 -0
  35. package/src/entityFunctions.ts +71 -0
  36. package/src/filterFunctions.ts +54 -0
  37. package/src/index.ts +29 -0
  38. package/src/inventoryFunctions.ts +187 -0
  39. package/src/mathUtil.ts +56 -0
  40. package/src/movementFunctions.ts +116 -0
  41. package/src/predictiveFunctions.ts +187 -0
  42. package/src/utilFunctions.ts +152 -0
  43. package/src/worldRelated/predictiveWorld.ts +109 -0
  44. package/src/worldRelated/raycastIterator.ts +136 -0
  45. package/tsconfig.json +17 -0
@@ -0,0 +1,78 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.EntityFunctions = void 0;
13
+ const aabb_1 = require("./calcs/aabb");
14
+ class EntityFunctions {
15
+ constructor(bot) {
16
+ this.bot = bot;
17
+ this.healthSlot = 7;
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
+ this.healthSlot = Number(this.bot.version.split(".")[1]) <= 16 ? 7 : 9;
25
+ }));
26
+ }
27
+ /**
28
+ * TODO: Version specific right now. Generalize. Unknown method.
29
+ *
30
+ * Checks if main hand is activated.
31
+ * @returns boolean
32
+ */
33
+ isMainHandActive(entity) {
34
+ return (entity !== null && entity !== void 0 ? entity : this.bot.entity).metadata[6] === 1; //as any & (1 | 0)) === (1 | 0);
35
+ }
36
+ /**
37
+ * TODO: Version specific right now. Generalize. Unknown method.
38
+ *
39
+ * Checks if offhand is activated.
40
+ * @returns boolean
41
+ */
42
+ isOffHandActive(entity) {
43
+ return (entity !== null && entity !== void 0 ? entity : this.bot.entity).metadata[6] === 1; //& (1 | 2)) === (1 | 2);
44
+ }
45
+ /**
46
+ * TODO: Version specific right now. Generalize. Unknown method.
47
+ * @param metadata metadata from Prismarine-Entity Entity.
48
+ * @returns
49
+ */
50
+ getHealth(entity) {
51
+ var _a, _b;
52
+ entity = entity !== null && entity !== void 0 ? entity : this.bot.entity;
53
+ let metadata = entity.metadata;
54
+ //console.log(entity.metadata, this.healthSlot, this.bot.entity.health)
55
+ let healthSlot = this.healthSlot; //metadata[this.healthSlot] ? this.healthSlot : metadata.findIndex((met) => Number(met) > 1 && Number(met) <= 20);
56
+ // return Number(metadata[healthSlot]) + (Number(metadata[healthSlot + 4]) ?? 0);
57
+ let health = Number(metadata[healthSlot]);
58
+ if (!health || health === 0)
59
+ health = entity === this.bot.entity ? (_a = this.bot.entity.health) !== null && _a !== void 0 ? _a : 0 : 0;
60
+ if (health === 0)
61
+ console.log(this.healthSlot, entity.metadata);
62
+ // console.log(health + (Number(metadata[this.healthSlot + 4]) ?? 0))
63
+ return health + ((_b = Number(metadata[this.healthSlot + 4])) !== null && _b !== void 0 ? _b : 0);
64
+ }
65
+ getDistanceToEntity(entity) {
66
+ return this.getDistanceBetweenEntities(this.bot.entity, entity);
67
+ }
68
+ getDistanceBetweenEntities(first, second) {
69
+ return first.position.distanceTo(second.position);
70
+ }
71
+ getEntityAABB(entity) {
72
+ var _a;
73
+ const w = (_a = entity.width) !== null && _a !== void 0 ? _a : entity.height / 2;
74
+ const { x, y, z } = entity.position;
75
+ return new aabb_1.AABB(-w, 0, -w, w, entity.height, w).offset(x, y, z);
76
+ }
77
+ }
78
+ exports.EntityFunctions = EntityFunctions;
@@ -0,0 +1,23 @@
1
+ import type { Entity } from "prismarine-entity";
2
+ import type { Bot } from "mineflayer";
3
+ import type { Vec3 } from "vec3";
4
+ /**
5
+ * TODO: Inherit other bot names. May need to communciate to a server for this one. Or perhaps reference this once?
6
+ */
7
+ export declare class FilterFunctions {
8
+ private bot;
9
+ specificNames: string[];
10
+ botNames: string[];
11
+ constructor(bot: Bot);
12
+ addBotName(name: string): void;
13
+ getNearestEntity(func: (entity: Entity, ...args: any[]) => boolean, ...args: any[]): Entity | null;
14
+ static getNearestEntity(bot: Bot, func: (entity: Entity, ...args: any[]) => boolean, ...args: any[]): Entity | null;
15
+ allButOtherBotsFilter(): Entity | null;
16
+ static allButOtherBotsFilter(bot: Bot, ...names: string[]): Entity | null;
17
+ specificNamesFilter(): Entity | null;
18
+ static specificNamesFilter(bot: Bot, ...names: string[]): Entity | null;
19
+ nearestCrystalFilter(): Entity | null;
20
+ static nearestCrystalFilter(bot: Bot): Entity | null;
21
+ entityAtPosition(position: Vec3): Entity | null;
22
+ static entityAtPosition(bot: Bot, position: Vec3): void;
23
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FilterFunctions = void 0;
4
+ /**
5
+ * TODO: Inherit other bot names. May need to communciate to a server for this one. Or perhaps reference this once?
6
+ */
7
+ class FilterFunctions {
8
+ constructor(bot) {
9
+ this.bot = bot;
10
+ this.specificNames = [];
11
+ this.botNames = [];
12
+ }
13
+ addBotName(name) {
14
+ this.botNames.push(name);
15
+ }
16
+ getNearestEntity(func, ...args) {
17
+ return this.bot.nearestEntity((entity) => func(entity, ...args));
18
+ }
19
+ static getNearestEntity(bot, func, ...args) {
20
+ return bot.nearestEntity((entity) => func(entity, ...args));
21
+ }
22
+ allButOtherBotsFilter() {
23
+ return this.getNearestEntity((e) => { var _a, _b, _c; return e.type === "player" && ((_c = !((_a = this.botNames) === null || _a === void 0 ? void 0 : _a.includes((_b = e.username) !== null && _b !== void 0 ? _b : ""))) !== null && _c !== void 0 ? _c : true); });
24
+ }
25
+ static allButOtherBotsFilter(bot, ...names) {
26
+ return FilterFunctions.getNearestEntity(bot, (e) => { var _a; return e.type === "player" && !names.includes((_a = e.username) !== null && _a !== void 0 ? _a : ""); });
27
+ }
28
+ specificNamesFilter() {
29
+ return this.getNearestEntity((e) => { var _a, _b, _c; return e.type === "player" && ((_c = (_a = this.specificNames) === null || _a === void 0 ? void 0 : _a.includes((_b = e.username) !== null && _b !== void 0 ? _b : "")) !== null && _c !== void 0 ? _c : true); });
30
+ }
31
+ static specificNamesFilter(bot, ...names) {
32
+ return FilterFunctions.getNearestEntity(bot, (e) => { var _a; return e.type === "player" && names.includes((_a = e.username) !== null && _a !== void 0 ? _a : ""); });
33
+ }
34
+ nearestCrystalFilter() {
35
+ return this.getNearestEntity((e) => e.name === "ender_crystal");
36
+ }
37
+ static nearestCrystalFilter(bot) {
38
+ return FilterFunctions.getNearestEntity(bot, (e) => e.name === "ender_crystal");
39
+ }
40
+ entityAtPosition(position) {
41
+ return this.getNearestEntity((entity) => entity.position.offset(-0.5, -1, -0.5).equals(position));
42
+ }
43
+ static entityAtPosition(bot, position) { }
44
+ }
45
+ exports.FilterFunctions = FilterFunctions;
package/lib/index.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type { Bot } from "mineflayer";
2
+ import { UtilFunctions } from "./utilFunctions";
3
+ declare module "mineflayer" {
4
+ type PrioGroups = "inventory" | "movement";
5
+ interface Bot {
6
+ util: UtilFunctions;
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
+ interface Entity {
19
+ attributes: {
20
+ [index: string]: {
21
+ value: number;
22
+ modifiers: any[];
23
+ };
24
+ };
25
+ }
26
+ }
27
+ export default function inject(bot: Bot): void;
package/lib/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utilFunctions_1 = require("./utilFunctions");
4
+ function inject(bot) {
5
+ bot.util = new utilFunctions_1.UtilFunctions(bot);
6
+ }
7
+ exports.default = inject;
@@ -0,0 +1,45 @@
1
+ import { Bot, EquipmentDestination } from "mineflayer";
2
+ import { Item } from "prismarine-item";
3
+ import { BuiltInPriorityOptions } from "./utilFunctions";
4
+ /**
5
+ * Error codes:
6
+ *
7
+ * 0 = completed successfully.
8
+ *
9
+ * 1 = Item is already equipped.
10
+ *
11
+ * 2 = Item could not be found.
12
+ *
13
+ * 3 = We are currently equipping an item.
14
+ */
15
+ export declare class InventoryFunctions {
16
+ bot: Bot;
17
+ private _equippingMainHand;
18
+ private _equippingOffHand;
19
+ private _equippingOtherSlot;
20
+ usingMainHand: boolean;
21
+ usingOffHand: boolean;
22
+ constructor(bot: Bot);
23
+ set equippingOtherSlot(value: boolean);
24
+ get equippingOtherSlot(): boolean;
25
+ set equippingMainHand(value: boolean);
26
+ get equippingMainHand(): boolean;
27
+ set equippingOffHand(value: boolean);
28
+ get equippingOffHand(): boolean;
29
+ getAllItems(): Item[];
30
+ getAllItemsExceptCurrent(current: EquipmentDestination): Item[];
31
+ getHandWithItem(offhand?: boolean): Item | null;
32
+ getHand(offhand?: boolean): "hand" | "off-hand";
33
+ findItemByID(itemId: number, metadata?: number): Item | null;
34
+ findItem(name: string, metadata?: number): Item | null;
35
+ has(name: string, metadata?: number): boolean;
36
+ 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
+ customEquip(item: Item, destination: EquipmentDestination, retries?: number): Promise<boolean>;
45
+ }
@@ -0,0 +1,208 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.InventoryFunctions = void 0;
13
+ const util_1 = require("util");
14
+ const sleep = (0, util_1.promisify)(setTimeout);
15
+ /**
16
+ * Error codes:
17
+ *
18
+ * 0 = completed successfully.
19
+ *
20
+ * 1 = Item is already equipped.
21
+ *
22
+ * 2 = Item could not be found.
23
+ *
24
+ * 3 = We are currently equipping an item.
25
+ */
26
+ class InventoryFunctions {
27
+ constructor(bot) {
28
+ this.bot = bot;
29
+ this._equippingMainHand = false;
30
+ this._equippingOffHand = false;
31
+ this._equippingOtherSlot = false;
32
+ this.usingMainHand = false;
33
+ this.usingOffHand = false;
34
+ }
35
+ set equippingOtherSlot(value) {
36
+ if (this._equippingOtherSlot === value)
37
+ return;
38
+ if (value)
39
+ this.bot.emit("startedEquippingOtherSlot");
40
+ else
41
+ this.bot.emit("stoppedEquippingOtherSlot");
42
+ this._equippingOtherSlot = value;
43
+ }
44
+ get equippingOtherSlot() {
45
+ return this._equippingOtherSlot;
46
+ }
47
+ set equippingMainHand(value) {
48
+ if (this._equippingMainHand === value)
49
+ return;
50
+ if (value)
51
+ this.bot.emit("startedEquippingMainHand");
52
+ else
53
+ this.bot.emit("stoppedEquippingMainHand");
54
+ this._equippingMainHand = value;
55
+ }
56
+ get equippingMainHand() {
57
+ return this._equippingMainHand;
58
+ }
59
+ set equippingOffHand(value) {
60
+ if (this._equippingOffHand === value)
61
+ return;
62
+ if (value)
63
+ this.bot.emit("startedEquippingOffHand");
64
+ else
65
+ this.bot.emit("stoppedEquippingOffHand");
66
+ this._equippingOffHand = value;
67
+ }
68
+ get equippingOffHand() {
69
+ return this._equippingOffHand;
70
+ }
71
+ getAllItems() {
72
+ return [
73
+ ...this.bot.inventory.items(),
74
+ ...["hand", "head", "torso", "legs", "feet", "off-hand"].map((name) => this.bot.inventory.slots[this.bot.getEquipmentDestSlot(name)]),
75
+ ].filter((e) => !!e);
76
+ }
77
+ getAllItemsExceptCurrent(current) {
78
+ return [
79
+ ...this.bot.inventory.items(),
80
+ ...["hand", "head", "torso", "legs", "feet", "off-hand"]
81
+ .filter((name) => name !== current)
82
+ .map((name) => this.bot.inventory.slots[this.bot.getEquipmentDestSlot(name)]),
83
+ ].filter((e) => !!e);
84
+ }
85
+ getHandWithItem(offhand) {
86
+ return this.bot.inventory.slots[this.bot.getEquipmentDestSlot(this.getHand(offhand))];
87
+ }
88
+ getHand(offhand = false) {
89
+ return offhand ? "off-hand" : "hand";
90
+ }
91
+ findItemByID(itemId, metadata) {
92
+ var _a, _b;
93
+ const potentialMatches = this.getAllItems().filter((item) => item.type === itemId);
94
+ 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;
97
+ }
98
+ findItem(name, metadata) {
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
+ 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;
105
+ }
106
+ //alias.
107
+ has(name, metadata) {
108
+ return !!this.findItem(name, metadata);
109
+ }
110
+ equipItemRaw(item, dest) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ if (this.bot.inventory.slots[this.bot.getEquipmentDestSlot(dest)] === item)
113
+ return true;
114
+ yield this.bot.equip(item, dest);
115
+ return false;
116
+ });
117
+ }
118
+ equipItem(name, dest, options) {
119
+ var _a;
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ if ((_a = this.bot.inventory.slots[this.bot.getEquipmentDestSlot(dest)]) === null || _a === void 0 ? void 0 : _a.name.includes(name))
122
+ return 1;
123
+ const item = this.getAllItemsExceptCurrent(dest).find((item) => item === null || item === void 0 ? void 0 : item.name.includes(name));
124
+ if (!!item) {
125
+ yield this.bot.util.builtInsPriority(options, this.bot.equip, item, dest);
126
+ return 0;
127
+ }
128
+ return 2;
129
+ });
130
+ }
131
+ equipMainHand(name, options = { group: "inventory", priority: 1 }) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ while (this._equippingMainHand) {
134
+ yield sleep(0);
135
+ }
136
+ return yield this.equipMainHandNoWait(name, options);
137
+ });
138
+ }
139
+ equipOffHand(name, options = { group: "inventory", priority: 1 }) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ while (this._equippingOffHand) {
142
+ yield sleep(0);
143
+ }
144
+ return yield this.equipOffHandNoWait(name, options);
145
+ });
146
+ }
147
+ equipSlot(name, destination, options = { group: "inventory", priority: 1 }) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ while (this._equippingOtherSlot) {
150
+ yield sleep(0);
151
+ }
152
+ return yield this.equipSlotNoWait(name, destination, options);
153
+ });
154
+ }
155
+ equipMainHandNoWait(name, options) {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ if (this._equippingMainHand)
158
+ return 3;
159
+ this._equippingMainHand = true;
160
+ const result = yield this.equipItem(name, "hand", options);
161
+ this._equippingMainHand = false;
162
+ return result;
163
+ });
164
+ }
165
+ equipOffHandNoWait(name, options) {
166
+ return __awaiter(this, void 0, void 0, function* () {
167
+ if (this._equippingOffHand)
168
+ return 3;
169
+ this.equippingOffHand = true;
170
+ const result = yield this.equipItem(name, "off-hand", options);
171
+ this.equippingOffHand = false;
172
+ return result;
173
+ });
174
+ }
175
+ equipSlotNoWait(name, dest, options) {
176
+ return __awaiter(this, void 0, void 0, function* () {
177
+ if (this._equippingOtherSlot)
178
+ return 3;
179
+ this.equippingOtherSlot = true;
180
+ const result = yield this.equipItem(name, dest, options);
181
+ this.equippingOtherSlot = false;
182
+ return result;
183
+ });
184
+ }
185
+ customEquip(item, destination, retries = 1) {
186
+ return __awaiter(this, void 0, void 0, function* () {
187
+ for (let i = 0; i < retries; i++) {
188
+ try {
189
+ yield this.bot.equip(item, destination);
190
+ return true;
191
+ }
192
+ catch (error) {
193
+ if (this.bot.inventory.selectedItem) {
194
+ const slot = this.bot.inventory.firstEmptyInventorySlot(false) || -999;
195
+ try {
196
+ yield this.bot.clickWindow(slot, 0, 0);
197
+ }
198
+ catch (error) {
199
+ return false;
200
+ }
201
+ }
202
+ }
203
+ }
204
+ return false;
205
+ });
206
+ }
207
+ }
208
+ exports.InventoryFunctions = InventoryFunctions;
@@ -0,0 +1,23 @@
1
+ import { Bot } from "mineflayer";
2
+ import { Vec3 } from "vec3";
3
+ export declare class MathFunctions {
4
+ constructor();
5
+ toNotchianYaw: (yaw: number) => number;
6
+ toNotchianPitch: (pitch: number) => number;
7
+ fromNotchianYawByte: (yaw: number) => number;
8
+ fromNotchianPitchByte: (pitch: number) => number;
9
+ euclideanMod(numerator: number, denominator: number): number;
10
+ toRadians(degrees: number): number;
11
+ toDegrees(radians: number): number;
12
+ fromNotchianYaw(yaw: number): number;
13
+ fromNotchianPitch(pitch: number): number;
14
+ fromNotchVelocity(vel: Vec3): Vec3;
15
+ pointToYawAndPitch(bot: Bot, point: Vec3): {
16
+ yaw: number;
17
+ pitch: number;
18
+ };
19
+ dirToYawAndPitch(dir: Vec3): {
20
+ yaw: number;
21
+ pitch: number;
22
+ };
23
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MathFunctions = void 0;
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
+ class MathFunctions {
13
+ constructor() {
14
+ this.toNotchianYaw = (yaw) => this.toDegrees(PI - yaw);
15
+ this.toNotchianPitch = (pitch) => this.toDegrees(-pitch);
16
+ this.fromNotchianYawByte = (yaw) => this.fromNotchianYaw(yaw * FROM_NOTCH_BYTE);
17
+ this.fromNotchianPitchByte = (pitch) => this.fromNotchianPitch(pitch * FROM_NOTCH_BYTE);
18
+ }
19
+ euclideanMod(numerator, denominator) {
20
+ const result = numerator % denominator;
21
+ return result < 0 ? result + denominator : result;
22
+ }
23
+ toRadians(degrees) {
24
+ return TO_RAD * degrees;
25
+ }
26
+ toDegrees(radians) {
27
+ return TO_DEG * radians;
28
+ }
29
+ fromNotchianYaw(yaw) {
30
+ return this.euclideanMod(PI - this.toRadians(yaw), PI_2);
31
+ }
32
+ fromNotchianPitch(pitch) {
33
+ return this.euclideanMod(this.toRadians(-pitch) + PI, PI_2) - PI;
34
+ }
35
+ fromNotchVelocity(vel) {
36
+ return new vec3_1.Vec3(vel.x * FROM_NOTCH_VEL, vel.y * FROM_NOTCH_VEL, vel.z * FROM_NOTCH_VEL);
37
+ }
38
+ pointToYawAndPitch(bot, point) {
39
+ const delta = point.minus(bot.entity.position.offset(0, bot.entity.height, 0));
40
+ return this.dirToYawAndPitch(delta);
41
+ }
42
+ dirToYawAndPitch(dir) {
43
+ const yaw = Math.atan2(-dir.x, -dir.z);
44
+ const groundDistance = Math.sqrt(dir.x * dir.x + dir.z * dir.z);
45
+ const pitch = Math.atan2(dir.y, groundDistance);
46
+ return { yaw: yaw, pitch: pitch };
47
+ }
48
+ }
49
+ exports.MathFunctions = MathFunctions;
@@ -0,0 +1,43 @@
1
+ import type { Bot } from "mineflayer";
2
+ import type { Entity } from "prismarine-entity";
3
+ import type { Vec3 } from "vec3";
4
+ import { goals } from "mineflayer-pathfinder";
5
+ export declare class MovementFunctions {
6
+ bot: Bot;
7
+ goalArray: goals.GoalCompositeAll;
8
+ lastYaw: number;
9
+ lastPitch: number;
10
+ constructor(bot: Bot);
11
+ /**
12
+ * Fuckin' mineflayer-pathfinder still doesn't have typings.
13
+ * Pain in my goddamn ass.
14
+ * @returns have the goal changed
15
+ */
16
+ addGoal(goal: any): boolean;
17
+ /**
18
+ * Sets current goal and clears all others.
19
+ * @param goal any type of mineflayer-pathfinder goal.
20
+ * @returns have the goal changed
21
+ */
22
+ setOnlyGoal(goal: any, dynamic?: boolean): boolean;
23
+ /**
24
+ * Reset all goals inside the goal array to none.
25
+ * @returns have the goals changed
26
+ */
27
+ stop(): boolean;
28
+ /**
29
+ * Retreat from current entity.
30
+ * @param entity Prismarine-Entity Entity
31
+ * @returns have the goals changed.
32
+ */
33
+ retreatFromEntity(entity: Entity, distance: number, dynamic?: boolean): boolean;
34
+ /**
35
+ * Follow entity with a specific range. Will not approach past a certain distance either.
36
+ * @param entity Prismarine-Entity Entity
37
+ * @returns have the goals changed
38
+ */
39
+ followEntityWithRespectRange(entity: Entity, followDistance: number, invertDistance?: number): boolean;
40
+ forceLook(yaw: number, pitch: number, update?: boolean): void;
41
+ forceLookAt(pos: Vec3, update?: boolean, trueForce?: boolean): void;
42
+ lazyTeleport(endPos: Vec3): void;
43
+ }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MovementFunctions = void 0;
4
+ const mineflayer_pathfinder_1 = require("mineflayer-pathfinder");
5
+ const { GoalCompositeAll, GoalInvert, GoalFollow } = mineflayer_pathfinder_1.goals;
6
+ class MovementFunctions {
7
+ constructor(bot) {
8
+ this.bot = bot;
9
+ this.goalArray = new GoalCompositeAll();
10
+ this.lastYaw = 0;
11
+ this.lastPitch = 0;
12
+ }
13
+ /**
14
+ * Fuckin' mineflayer-pathfinder still doesn't have typings.
15
+ * Pain in my goddamn ass.
16
+ * @returns have the goal changed
17
+ */
18
+ addGoal(goal) {
19
+ if (this.goalArray.goals.find((inGoal) => inGoal === goal))
20
+ return false;
21
+ this.goalArray.push(goal);
22
+ return true;
23
+ }
24
+ /**
25
+ * Sets current goal and clears all others.
26
+ * @param goal any type of mineflayer-pathfinder goal.
27
+ * @returns have the goal changed
28
+ */
29
+ setOnlyGoal(goal, dynamic = false) {
30
+ const goalArr = [goal];
31
+ if (this.goalArray.goals === goalArr)
32
+ return false;
33
+ this.goalArray.goals = goalArr;
34
+ this.bot.pathfinder.setGoal(this.goalArray, dynamic);
35
+ return true;
36
+ }
37
+ /**
38
+ * Reset all goals inside the goal array to none.
39
+ * @returns have the goals changed
40
+ */
41
+ stop() {
42
+ if (this.goalArray.goals.length === 0)
43
+ return false;
44
+ this.goalArray.goals = [];
45
+ this.bot.pathfinder.setGoal(null);
46
+ return true;
47
+ }
48
+ /**
49
+ * Retreat from current entity.
50
+ * @param entity Prismarine-Entity Entity
51
+ * @returns have the goals changed.
52
+ */
53
+ retreatFromEntity(entity, distance, dynamic = true) {
54
+ const oldGoals = this.goalArray.goals.length;
55
+ this.goalArray.goals = this.goalArray.goals.filter((goal) => { var _a, _b, _c; return ((_b = (_a = goal.goal) === null || _a === void 0 ? void 0 : _a.entity) === null || _b === void 0 ? void 0 : _b.id) === entity.id && ((_c = goal.goal) === null || _c === void 0 ? void 0 : _c.rangeSq) === distance * distance; });
56
+ if (oldGoals !== this.goalArray.goals.length || this.goalArray.goals.length === 0) {
57
+ this.goalArray.push(new GoalFollow(entity, distance));
58
+ this.goalArray.push(new GoalInvert(new GoalFollow(entity, distance - 1)));
59
+ this.bot.pathfinder.setGoal(this.goalArray, dynamic);
60
+ return true;
61
+ }
62
+ return false;
63
+ }
64
+ /**
65
+ * Follow entity with a specific range. Will not approach past a certain distance either.
66
+ * @param entity Prismarine-Entity Entity
67
+ * @returns have the goals changed
68
+ */
69
+ followEntityWithRespectRange(entity, followDistance, invertDistance) {
70
+ const oldGoals = this.goalArray.goals.length;
71
+ this.goalArray.goals = this.goalArray.goals.filter((goal) => {
72
+ var _a;
73
+ return ((_a = goal.entity) === null || _a === void 0 ? void 0 : _a.id) === entity.id && goal.rangeSq === followDistance * followDistance;
74
+ });
75
+ if (oldGoals !== this.goalArray.goals.length || !this.bot.pathfinder.isMoving() || this.goalArray.goals.length === 0) {
76
+ if (this.goalArray.goals.length > 0) {
77
+ this.goalArray.goals = [];
78
+ }
79
+ this.goalArray.push(new GoalFollow(entity, followDistance));
80
+ this.goalArray.push(new GoalInvert(new GoalFollow(entity, invertDistance !== null && invertDistance !== void 0 ? invertDistance : followDistance - 0.5)));
81
+ this.bot.pathfinder.setGoal(this.goalArray, true);
82
+ return true;
83
+ }
84
+ return false;
85
+ }
86
+ forceLook(yaw, pitch, update = false) {
87
+ const notchianYawAndPitch = { yaw: this.bot.util.math.toNotchianYaw(yaw), pitch: this.bot.util.math.toNotchianPitch(pitch) };
88
+ this.bot._client.write("look", Object.assign(Object.assign({}, notchianYawAndPitch), { onGround: false }));
89
+ if (update) {
90
+ // this.bot.entity.yaw = yaw;
91
+ // this.bot.entity.pitch = pitch
92
+ this.bot.look(yaw, pitch, true);
93
+ }
94
+ }
95
+ forceLookAt(pos, update = false, trueForce = false) {
96
+ const { yaw, pitch } = this.bot.util.math.pointToYawAndPitch(this.bot, pos);
97
+ const nyp = { yaw: this.bot.util.math.toNotchianYaw(yaw), pitch: this.bot.util.math.toNotchianPitch(pitch) };
98
+ if (nyp.yaw !== this.lastYaw || nyp.pitch !== this.lastPitch || trueForce) {
99
+ this.bot._client.write("look", Object.assign(Object.assign({}, nyp), { onGround: false }));
100
+ if (update) {
101
+ // this.bot.entity.yaw = yaw;
102
+ // this.bot.entity.pitch = pitch
103
+ this.bot.lookAt(pos, true);
104
+ }
105
+ }
106
+ }
107
+ lazyTeleport(endPos) { }
108
+ }
109
+ exports.MovementFunctions = MovementFunctions;