@nxg-org/mineflayer-util-plugin 1.5.2 → 1.6.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.
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,95 @@
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;
package/lib/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import type { Bot } from "mineflayer";
2
2
  import { UtilFunctions } from "./utilFunctions";
3
+ import md from "minecraft-data";
3
4
  declare module "mineflayer" {
4
- type PrioGroups = "inventory" | "movement";
5
5
  interface Bot {
6
6
  util: UtilFunctions;
7
+ registry: md.IndexedData;
7
8
  }
8
9
  }
9
10
  export default function inject(bot: Bot): void;
package/lib/index.js CHANGED
@@ -2,10 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MathUtils = exports.AABBUtils = exports.BlockFace = exports.RaycastIterator = exports.InterceptFunctions = exports.AABB = void 0;
4
4
  const utilFunctions_1 = require("./utilFunctions");
5
- const mineflayer_pathfinder_1 = require("mineflayer-pathfinder");
6
5
  function inject(bot) {
7
- if (!bot.hasPlugin(mineflayer_pathfinder_1.pathfinder))
8
- bot.loadPlugin(mineflayer_pathfinder_1.pathfinder);
9
6
  bot.util = new utilFunctions_1.UtilFunctions(bot);
10
7
  }
11
8
  exports.default = inject;
@@ -1,42 +1,8 @@
1
1
  import type { Bot } from "mineflayer";
2
2
  import type { Vec3 } from "vec3";
3
- import type { Entity } from "prismarine-entity";
4
- import { goals, Movements } from "mineflayer-pathfinder";
5
3
  export declare class MovementFunctions {
6
4
  private bot;
7
- goalArray: goals.GoalCompositeAll;
8
5
  constructor(bot: Bot);
9
- set movements(movements: Movements);
10
- get movements(): Movements;
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
6
  forceLook(yaw: number, pitch: number, update?: boolean, onGround?: boolean): void;
41
7
  forceLookAt(pos: Vec3, update?: boolean, onGround?: boolean): void;
42
8
  lazyTeleport(endPos: Vec3): void;
@@ -1,96 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.MovementFunctions = void 0;
7
- const mineflayer_pathfinder_1 = require("mineflayer-pathfinder");
8
- const minecraft_data_1 = __importDefault(require("minecraft-data"));
9
- const { GoalCompositeAll, GoalInvert, GoalFollow } = mineflayer_pathfinder_1.goals;
10
4
  class MovementFunctions {
11
5
  constructor(bot) {
12
6
  this.bot = bot;
13
- this.goalArray = new GoalCompositeAll();
14
- this.movements = new mineflayer_pathfinder_1.Movements(bot, (0, minecraft_data_1.default)(bot.version));
15
- }
16
- set movements(movements) {
17
- this.bot.pathfinder.setMovements(movements);
18
- }
19
- get movements() {
20
- return this.movements;
21
- }
22
- /**
23
- * Fuckin' mineflayer-pathfinder still doesn't have typings.
24
- * Pain in my goddamn ass.
25
- * @returns have the goal changed
26
- */
27
- addGoal(goal) {
28
- if (this.goalArray.goals.find((inGoal) => inGoal === goal))
29
- return false;
30
- this.goalArray.push(goal);
31
- return true;
32
- }
33
- /**
34
- * Sets current goal and clears all others.
35
- * @param goal any type of mineflayer-pathfinder goal.
36
- * @returns have the goal changed
37
- */
38
- setOnlyGoal(goal, dynamic = false) {
39
- const goalArr = [goal];
40
- if (this.goalArray.goals === goalArr)
41
- return false;
42
- this.goalArray.goals = goalArr;
43
- this.bot.pathfinder.setGoal(this.goalArray, dynamic);
44
- return true;
45
- }
46
- /**
47
- * Reset all goals inside the goal array to none.
48
- * @returns have the goals changed
49
- */
50
- stop() {
51
- if (this.goalArray.goals.length === 0)
52
- return false;
53
- this.goalArray.goals = [];
54
- this.bot.pathfinder.setGoal(null);
55
- return true;
56
- }
57
- /**
58
- * Retreat from current entity.
59
- * @param entity Prismarine-Entity Entity
60
- * @returns have the goals changed.
61
- */
62
- retreatFromEntity(entity, distance, dynamic = true) {
63
- const oldGoals = this.goalArray.goals.length;
64
- 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; });
65
- if (oldGoals !== this.goalArray.goals.length || this.goalArray.goals.length === 0) {
66
- this.goalArray.push(new GoalFollow(entity, distance));
67
- this.goalArray.push(new GoalInvert(new GoalFollow(entity, distance - 1)));
68
- this.bot.pathfinder.setGoal(this.goalArray, dynamic);
69
- return true;
70
- }
71
- return false;
72
- }
73
- /**
74
- * Follow entity with a specific range. Will not approach past a certain distance either.
75
- * @param entity Prismarine-Entity Entity
76
- * @returns have the goals changed
77
- */
78
- followEntityWithRespectRange(entity, followDistance, invertDistance) {
79
- const oldGoals = this.goalArray.goals.length;
80
- this.goalArray.goals = this.goalArray.goals.filter((goal) => {
81
- var _a;
82
- return ((_a = goal.entity) === null || _a === void 0 ? void 0 : _a.id) === entity.id && goal.rangeSq === followDistance * followDistance;
83
- });
84
- if (oldGoals !== this.goalArray.goals.length || !this.bot.pathfinder.isMoving() || this.goalArray.goals.length === 0) {
85
- if (this.goalArray.goals.length > 0) {
86
- this.goalArray.goals = [];
87
- }
88
- this.goalArray.push(new GoalFollow(entity, followDistance));
89
- this.goalArray.push(new GoalInvert(new GoalFollow(entity, invertDistance !== null && invertDistance !== void 0 ? invertDistance : followDistance - 0.5)));
90
- this.bot.pathfinder.setGoal(this.goalArray, true);
91
- return true;
92
- }
93
- return false;
94
7
  }
95
8
  forceLook(yaw, pitch, update = false, onGround) {
96
9
  const notchianYawAndPitch = { yaw: this.bot.util.math.toNotchianYaw(yaw), pitch: this.bot.util.math.toNotchianPitch(pitch) };
@@ -1,6 +1,6 @@
1
1
  import { Bot } from "mineflayer";
2
- import { Vec3 } from "vec3";
3
2
  import type { Entity } from "prismarine-entity";
3
+ import { Vec3 } from "vec3";
4
4
  export declare class RayTraceFunctions {
5
5
  private bot;
6
6
  constructor(bot: Bot);
@@ -1,6 +1,6 @@
1
1
  import type { Block } from "prismarine-block";
2
- import { AABB } from "../calcs/aabb";
3
2
  import type { Vec3 } from "vec3";
3
+ import { AABB } from "../calcs/aabb";
4
4
  export declare namespace AABBUtils {
5
5
  function getBlockAABB(block: Block, height?: number): AABB;
6
6
  function getBlockPosAABB(block: Vec3, height?: number): AABB;
@@ -61,15 +61,7 @@ var MathUtils;
61
61
  MathUtils.getYaw = getYaw;
62
62
  //Scuffed.
63
63
  function yawPitchAndSpeedToDir(yaw, pitch, speed) {
64
- const thetaY = PI + yaw;
65
- const thetaP = pitch;
66
- const x = speed * Math.sin(thetaY);
67
- const y = speed * Math.sin(thetaP);
68
- const z = speed * Math.cos(thetaY);
69
- const VxMag = Math.sqrt(x * x + z * z);
70
- const VxRatio = Math.sqrt(VxMag * VxMag - y * y);
71
- const allRatio = VxRatio / VxMag;
72
- return new vec3_1.Vec3(x * allRatio, y, z * allRatio);
64
+ return new vec3_1.Vec3(-Math.sin(yaw) * Math.cos(pitch), Math.sin(pitch), -Math.cos(yaw) * Math.cos(pitch)).scale(speed);
73
65
  }
74
66
  MathUtils.yawPitchAndSpeedToDir = yawPitchAndSpeedToDir;
75
67
  })(MathUtils = exports.MathUtils || (exports.MathUtils = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-util-plugin",
3
- "version": "1.5.2",
3
+ "version": "1.6.1",
4
4
  "description": "mineflayer utils for NextGEN mineflayer plugins.",
5
5
  "keywords": [
6
6
  "mineflayer",
@@ -17,15 +17,12 @@
17
17
  "scripts": {
18
18
  "build": "npx tsc",
19
19
  "prepublish": "npm run build",
20
- "test": "node --trace-warnings lib/example.js"
21
- },
22
- "dependencies": {
23
- "mineflayer-pathfinder": "^2.3.3",
24
- "vec3": "^0.1.7"
20
+ "test": "ts-node examples/example.ts"
25
21
  },
26
22
  "devDependencies": {
27
23
  "@types/node": "^17.0.4",
28
- "mineflayer": "^4.4.0",
29
- "typescript": "^4.5.2"
24
+ "mineflayer": "^4.6.0",
25
+ "typescript": "^4.5.2",
26
+ "vec3": "^0.1.7"
30
27
  }
31
28
  }
package/lib/example.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/lib/example.js DELETED
@@ -1,96 +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
- var _a, _b;
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- const mineflayer_1 = require("mineflayer");
17
- const index_1 = __importDefault(require("./index"));
18
- const vec3_1 = require("vec3");
19
- let target;
20
- const bot = (0, mineflayer_1.createBot)({
21
- username: "utilTesting",
22
- host: (_a = process.argv[2]) !== null && _a !== void 0 ? _a : "localhost",
23
- port: (_b = Number(process.argv[3])) !== null && _b !== void 0 ? _b : 25565,
24
- });
25
- bot.loadPlugin(index_1.default);
26
- const emptyVec = new vec3_1.Vec3(0, 0, 0);
27
- bot.on("chat", (username, message) => __awaiter(void 0, void 0, void 0, function* () {
28
- var _c;
29
- const split = message.split(" ");
30
- switch (split[0]) {
31
- case "look":
32
- target = bot.nearestEntity((e) => { var _a; return ((_a = e.username) !== null && _a !== void 0 ? _a : e.name) === split[1]; });
33
- if (!target)
34
- return console.log("no entity");
35
- bot.util.move.forceLookAt(target.position, true);
36
- break;
37
- case "equip":
38
- const item = bot.util.inv.getAllItems().find((i) => i.name.includes(split[1]));
39
- if (!item)
40
- return console.log("no item");
41
- if (["hand", "off-hand"].includes(split[2])) {
42
- bot.util.inv.customEquip(item, split[2]);
43
- }
44
- break;
45
- case "health":
46
- const health = bot.util.entity.getHealth();
47
- bot.chat(`${health}`);
48
- break;
49
- case "whatAmILookingAt":
50
- target = bot.nearestEntity((e) => { var _a; return ((_a = e.username) !== null && _a !== void 0 ? _a : e.name) === username; });
51
- if (!target)
52
- return console.log("no entity");
53
- const player = bot.util.raytrace.entityAtEntityCursor(target, 256);
54
- if (player) {
55
- console.log(player);
56
- bot.chat(`${(_c = player.username) !== null && _c !== void 0 ? _c : player.name} at ${player.position}`);
57
- }
58
- else {
59
- const block = bot.util.raytrace.blockAtEntityCursor(target, 256); //includes face and intersect. That's very nice.
60
- if (block) {
61
- console.log(block);
62
- bot.chat(`${block.name} at ${block.position}`);
63
- }
64
- else {
65
- bot.chat("You're not looking at anything.");
66
- }
67
- }
68
- break;
69
- case "come":
70
- target = bot.nearestEntity((e) => { var _a; return ((_a = e.username) !== null && _a !== void 0 ? _a : e.name) === username; });
71
- if (!target)
72
- return console.log("no entity");
73
- if (!bot.pathfinder)
74
- bot.chat("pathfinder is not loaded!");
75
- bot.util.move.followEntityWithRespectRange(target, 1);
76
- break;
77
- case "follow":
78
- target = bot.nearestEntity((e) => { var _a; return ((_a = e.username) !== null && _a !== void 0 ? _a : e.name) === split[1]; });
79
- if (!target)
80
- return console.log("no entity");
81
- if (!bot.pathfinder)
82
- bot.chat("pathfinder is not loaded!");
83
- bot.util.move.followEntityWithRespectRange(target, 1);
84
- break;
85
- case "stop":
86
- if (!bot.pathfinder)
87
- bot.chat("pathfinder is not loaded!");
88
- bot.util.move.stop();
89
- break;
90
- default:
91
- console.log(username, bot.entity.username);
92
- if (username !== bot.entity.username)
93
- bot.chat("unknown command: " + message);
94
- break;
95
- }
96
- }));