@nxg-org/mineflayer-physics-util 1.3.6 → 1.3.8

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 (51) hide show
  1. package/{lib → dist}/index.d.ts +1 -0
  2. package/{lib → dist}/index.js +3 -1
  3. package/{lib → dist}/physics/engines/IPhysics.d.ts +0 -0
  4. package/{lib → dist}/physics/engines/IPhysics.js +0 -0
  5. package/{lib → dist}/physics/engines/entityPhysics.d.ts +0 -0
  6. package/{lib → dist}/physics/engines/entityPhysics.js +0 -0
  7. package/{lib → dist}/physics/engines/index.d.ts +0 -0
  8. package/{lib → dist}/physics/engines/index.js +0 -0
  9. package/{lib → dist}/physics/info/attributes.d.ts +0 -0
  10. package/{lib → dist}/physics/info/attributes.js +0 -0
  11. package/{lib → dist}/physics/info/entity_physics.json +0 -0
  12. package/{lib → dist}/physics/info/features.json +0 -0
  13. package/{lib → dist}/physics/info/math.d.ts +0 -0
  14. package/{lib → dist}/physics/info/math.js +0 -0
  15. package/{lib → dist}/physics/player/index.d.ts +0 -0
  16. package/{lib → dist}/physics/player/index.js +0 -0
  17. package/{lib → dist}/physics/player/playerControls.d.ts +0 -0
  18. package/{lib → dist}/physics/player/playerControls.js +0 -0
  19. package/{lib → dist}/physics/settings/entityPhysicsCtx.d.ts +0 -0
  20. package/{lib → dist}/physics/settings/entityPhysicsCtx.js +0 -0
  21. package/{lib → dist}/physics/settings/index.d.ts +0 -0
  22. package/{lib → dist}/physics/settings/index.js +0 -0
  23. package/{lib → dist}/physics/settings/physicsSettings.d.ts +0 -0
  24. package/{lib → dist}/physics/settings/physicsSettings.js +0 -0
  25. package/{lib → dist}/physics/states/entityState.d.ts +0 -0
  26. package/{lib → dist}/physics/states/entityState.js +0 -0
  27. package/{lib → dist}/physics/states/index.d.ts +0 -0
  28. package/{lib → dist}/physics/states/index.js +0 -0
  29. package/{lib → dist}/physics/states/playerState.d.ts +0 -0
  30. package/{lib → dist}/physics/states/playerState.js +0 -0
  31. package/{lib → dist}/physics/states/poses.d.ts +0 -0
  32. package/{lib → dist}/physics/states/poses.js +0 -0
  33. package/{lib → dist}/simulators/baseSimulator.d.ts +1 -1
  34. package/{lib → dist}/simulators/baseSimulator.js +0 -0
  35. package/{lib → dist}/simulators/basicSim.d.ts +0 -0
  36. package/{lib → dist}/simulators/basicSim.js +0 -0
  37. package/{lib → dist}/simulators/index.d.ts +0 -0
  38. package/{lib → dist}/simulators/index.js +0 -0
  39. package/{lib → dist}/util/mathUtil.d.ts +0 -0
  40. package/{lib → dist}/util/mathUtil.js +0 -0
  41. package/{lib → dist}/util/physicsUtils.d.ts +0 -0
  42. package/{lib → dist}/util/physicsUtils.js +0 -0
  43. package/{lib → dist}/wrapper.d.ts +0 -0
  44. package/{lib → dist}/wrapper.js +0 -0
  45. package/package.json +4 -4
  46. package/tests/fakeWorld.ts +95 -0
  47. package/tests/util/testUtils.ts +68 -0
  48. package/lib/tests/fakeWorld.d.ts +0 -1
  49. package/lib/tests/fakeWorld.js +0 -77
  50. package/lib/tests/util/testUtils.d.ts +0 -33
  51. package/lib/tests/util/testUtils.js +0 -67
@@ -13,4 +13,5 @@ export { EPhysicsCtx, PhysicsSettings } from "./physics/settings";
13
13
  export { BaseSimulator } from "./simulators";
14
14
  export { EntityPhysics } from "./physics/engines";
15
15
  export { EntityState } from "./physics/states";
16
+ export { ControlStateHandler } from "./physics/player";
16
17
  export type { SimulationGoal, Controller, OnGoalReachFunction } from "./simulators";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EntityState = exports.EntityPhysics = exports.BaseSimulator = exports.PhysicsSettings = exports.EPhysicsCtx = exports.initSetup = void 0;
3
+ exports.ControlStateHandler = exports.EntityState = exports.EntityPhysics = exports.BaseSimulator = exports.PhysicsSettings = exports.EPhysicsCtx = exports.initSetup = void 0;
4
4
  const settings_1 = require("./physics/settings");
5
5
  const wrapper_1 = require("./wrapper");
6
6
  function loader(bot) {
@@ -24,3 +24,5 @@ var engines_1 = require("./physics/engines");
24
24
  Object.defineProperty(exports, "EntityPhysics", { enumerable: true, get: function () { return engines_1.EntityPhysics; } });
25
25
  var states_1 = require("./physics/states");
26
26
  Object.defineProperty(exports, "EntityState", { enumerable: true, get: function () { return states_1.EntityState; } });
27
+ var player_1 = require("./physics/player");
28
+ Object.defineProperty(exports, "ControlStateHandler", { enumerable: true, get: function () { return player_1.ControlStateHandler; } });
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -4,7 +4,7 @@ import { EntityState } from "../physics/states";
4
4
  import { IPhysics } from "../physics/engines";
5
5
  import { Entity } from "prismarine-entity";
6
6
  import { Vec3 } from "vec3";
7
- export declare type SimulationGoal = (state: EntityState, ticks?: number) => boolean;
7
+ export declare type SimulationGoal = (state: EntityState, ticks: number) => boolean | ((state: EntityState) => boolean);
8
8
  export declare type OnGoalReachFunction = (state: EntityState) => void;
9
9
  export declare type Controller = (state: EntityState, ticks: number) => void;
10
10
  export declare abstract class BaseSimulator {
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-physics-util",
3
- "version": "1.3.6",
3
+ "version": "1.3.8",
4
4
  "description": "Provides functionality for more accurate entity and projectile tracking.",
5
5
  "keywords": [
6
6
  "mineflayer",
@@ -13,12 +13,12 @@
13
13
  },
14
14
  "license": "GPL-3.0",
15
15
  "author": "generel_schwerz",
16
- "main": "lib/index.js",
17
- "types": "lib/index.d.ts",
16
+ "main": "dist/index.js",
17
+ "types": "dist/index.d.ts",
18
18
  "scripts": {
19
19
  "build": "npx tsc",
20
20
  "prepublish": "npm run build",
21
- "test": "node --trace-warnings lib/tests/fakeWorld.js"
21
+ "test": "ts-node -T tests/fakeWorld.ts"
22
22
  },
23
23
  "dependencies": {
24
24
  "@nxg-org/mineflayer-util-plugin": "^1.3.17",
@@ -0,0 +1,95 @@
1
+ import type { Entity } from "prismarine-entity";
2
+ import type { Effect } from "mineflayer";
3
+ import { EntityPhysics } from "../src/physics/engines";
4
+ import { EntityState } from "../src/physics/states";
5
+ import { EPhysicsCtx, PhysicsSettings } from "../src/physics/settings";
6
+ import { applyMdToNewEntity } from "../src/util/physicsUtils";
7
+ import { ControlStateHandler } from "../src/physics/player";
8
+ import { Vec3 } from "vec3";
9
+ import md from "minecraft-data";
10
+ import block from "prismarine-block";
11
+ import expect from "expect";
12
+
13
+ import { initSetup } from "../src/index";
14
+
15
+ const mcData = md("1.17.1");
16
+ const Block = (block as any)("1.17.1");
17
+
18
+ const fakeWorld = {
19
+ getBlock: (pos: { x: number; y: number; z: number }) => {
20
+ const type = pos.y < 60 ? mcData.blocksByName.stone.id : mcData.blocksByName.air.id;
21
+ const b = new Block(type, 0, 0);
22
+ b.position = pos;
23
+ return b;
24
+ },
25
+ };
26
+
27
+ function createFakePlayer(pos: Vec3) {
28
+ return {
29
+ entity: {
30
+ position: pos,
31
+ velocity: new Vec3(0, 0, 0),
32
+ onGround: false,
33
+ isInWater: false,
34
+ isInLava: false,
35
+ isInWeb: false,
36
+ isCollidedHorizontally: false,
37
+ isCollidedVertically: false,
38
+ yaw: 0,
39
+ effects: {} as Effect[],
40
+ } as unknown as Entity,
41
+ jumpTicks: 0,
42
+ jumpQueued: false,
43
+ version: "1.17.1",
44
+ inventory: {
45
+ slots: [],
46
+ },
47
+ };
48
+ }
49
+
50
+
51
+ //init (imports mcData to necessary modules).
52
+ initSetup(mcData);
53
+
54
+ //create fake bot
55
+ const playerType = mcData.entitiesByName["player"]; // specify type we will be simulating.
56
+ const fakePlayer = createFakePlayer(new Vec3(0, 80, 0)); // call function supplied by prismarine-physics
57
+ fakePlayer.entity = applyMdToNewEntity(EPhysicsCtx, playerType, fakePlayer.entity); // ensure compatibility.
58
+
59
+ // create physics context.
60
+ const physics = new EntityPhysics(mcData); // creates entity physics w/ environments specific to this entity.
61
+
62
+ // create entity-specific physics context.
63
+ const playerState = EntityState.CREATE_FROM_ENTITY(physics, fakePlayer.entity); // creates a simulation-compatible state.
64
+ const playerCtx = EPhysicsCtx.FROM_ENTITY_STATE(physics, playerState, playerType); // create wrapper context (supplies AABB, pose info, etc).
65
+
66
+ // set control state.
67
+ playerState.controlState = ControlStateHandler.DEFAULT(); // specific to players and mobs, specify control scheme to apply.
68
+ playerState.controlState.forward = true;
69
+
70
+ // simulate until on ground.
71
+ while (!playerCtx.state.onGround) {
72
+ physics.simulate(playerCtx, fakeWorld).applyToBot(fakePlayer as any); // (applyToBot since fakePlayer is supposed to be a bot)
73
+ // console.log(fakePlayer.entity.position, fakePlayer.entity.velocity);
74
+ }
75
+
76
+ if (playerState.controlState.forward) {
77
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, -3.4508449226731694)); // it works.
78
+ } else {
79
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, 0)); // it works.
80
+ }
81
+
82
+ playerCtx.state.controlState.set("jump", true);
83
+ for (let i = 0; i < 12; i++) {
84
+ physics.simulate(playerCtx, fakeWorld).applyToBot(fakePlayer as any); // (applyToBot since fakePlayer is supposed to be a bot)
85
+ // console.log(fakePlayer.entity.position, fakePlayer.entity.velocity);
86
+ }
87
+
88
+ if (playerState.controlState.forward) {
89
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, -5.788782872583908)); // it works.
90
+ } else {
91
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, 0)); // it works.
92
+ }
93
+
94
+
95
+ console.log(fakePlayer.entity.position); //manual run.
@@ -0,0 +1,68 @@
1
+ import { Effect } from "mineflayer";
2
+ import { Entity } from "prismarine-entity";
3
+ import { Vec3 } from "vec3";
4
+ import md from "minecraft-data";
5
+ import block from "prismarine-block";
6
+ import entity from "prismarine-entity"
7
+
8
+ export default function load(version: string) {
9
+
10
+ const mcData = md(version);
11
+ const Block = (block as any)(version);
12
+ const Entity = (entity as any)(version)
13
+
14
+
15
+ const fakeWorld = {
16
+ getBlock: (pos: { x: number; y: number; z: number }) => {
17
+ const type = pos.y < 60 ? mcData.blocksByName.stone.id : mcData.blocksByName.air.id;
18
+ const b = new Block(type, 0, 0);
19
+ b.position = pos;
20
+ return b;
21
+ },
22
+ };
23
+
24
+ const createFakeEntity = (name: string, pos: Vec3) => {
25
+ if (!mcData.entitiesByName[name!]) throw "invalid name"
26
+ const tmp = mcData.entitiesByName[name!]
27
+ return {
28
+ name: name,
29
+ type: tmp.type,
30
+ height: tmp.height,
31
+ width: tmp.width,
32
+ position: pos,
33
+ velocity: new Vec3(0, 0, 0),
34
+ onGround: false,
35
+ isInWater: false,
36
+ isInLava: false,
37
+ isInWeb: false,
38
+ isCollidedHorizontally: false,
39
+ isCollidedVertically: false,
40
+ yaw: 0,
41
+ effects: {} as Effect[],
42
+ metadata: [],
43
+ equipment: new Array(6)
44
+ } as unknown as Entity
45
+ }
46
+
47
+ const modifyEntity = (name: string, entity: Entity) => {
48
+ if (!mcData.entitiesByName[name!]) throw "invalid name"
49
+ const tmp = mcData.entitiesByName[name!]
50
+ entity.height = tmp.height ?? 0
51
+ entity.width = tmp.width ?? 0
52
+ }
53
+
54
+ const createFakePlayer = (pos: Vec3) => {
55
+ return {
56
+ entity: createFakeEntity("player", pos),
57
+ jumpTicks: 0,
58
+ jumpQueued: false,
59
+ version: "1.17.1",
60
+ inventory: {
61
+ slots: [],
62
+ },
63
+ world: fakeWorld
64
+ };
65
+ }
66
+
67
+ return { mcData, Block, Entity, fakeWorld, createFakePlayer, createFakeEntity, modifyEntity };
68
+ }
@@ -1 +0,0 @@
1
- export {};
@@ -1,77 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const engines_1 = require("../physics/engines");
7
- const states_1 = require("../physics/states");
8
- const settings_1 = require("../physics/settings");
9
- const physicsUtils_1 = require("../util/physicsUtils");
10
- const player_1 = require("../physics/player");
11
- const vec3_1 = require("vec3");
12
- const minecraft_data_1 = __importDefault(require("minecraft-data"));
13
- const prismarine_block_1 = __importDefault(require("prismarine-block"));
14
- const index_1 = require("../index");
15
- const mcData = (0, minecraft_data_1.default)("1.17.1");
16
- const Block = prismarine_block_1.default("1.17.1");
17
- const fakeWorld = {
18
- getBlock: (pos) => {
19
- const type = pos.y < 60 ? mcData.blocksByName.stone.id : mcData.blocksByName.air.id;
20
- const b = new Block(type, 0, 0);
21
- b.position = pos;
22
- return b;
23
- },
24
- };
25
- function createFakePlayer(pos) {
26
- return {
27
- entity: {
28
- position: pos,
29
- velocity: new vec3_1.Vec3(0, 0, 0),
30
- onGround: false,
31
- isInWater: false,
32
- isInLava: false,
33
- isInWeb: false,
34
- isCollidedHorizontally: false,
35
- isCollidedVertically: false,
36
- yaw: 0,
37
- effects: {},
38
- },
39
- jumpTicks: 0,
40
- jumpQueued: false,
41
- version: "1.17.1",
42
- inventory: {
43
- slots: [],
44
- },
45
- };
46
- }
47
- //init (imports mcData to necessary modules).
48
- (0, index_1.initSetup)(mcData);
49
- //create fake bot
50
- const playerType = mcData.entitiesByName["player"]; // specify type we will be simulating.
51
- const fakePlayer = createFakePlayer(new vec3_1.Vec3(0, 80, 0)); // call function supplied by prismarine-physics
52
- fakePlayer.entity = (0, physicsUtils_1.applyMdToNewEntity)(settings_1.EPhysicsCtx, playerType, fakePlayer.entity); // ensure compatibility.
53
- // create physics context.
54
- const physics = new engines_1.EntityPhysics(mcData); // creates entity physics w/ environments specific to this entity.
55
- // create entity-specific physics context.
56
- const playerState = states_1.EntityState.CREATE_FROM_ENTITY(physics, fakePlayer.entity); // creates a simulation-compatible state.
57
- const playerCtx = settings_1.EPhysicsCtx.FROM_ENTITY_STATE(physics, playerState, playerType); // create wrapper context (supplies AABB, pose info, etc).
58
- // set control state.
59
- playerState.controlState = player_1.ControlStateHandler.DEFAULT(); // specific to players and mobs, specify control scheme to apply.
60
- playerState.controlState.forward = true;
61
- // simulate until on ground.
62
- while (!playerCtx.state.onGround) {
63
- physics.simulate(playerCtx, fakeWorld).applyToBot(fakePlayer); // (applyToBot since fakePlayer is supposed to be a bot)
64
- console.log(fakePlayer.entity.position, fakePlayer.entity.velocity);
65
- }
66
- playerCtx.state.controlState.set("jump", true);
67
- for (let i = 0; i < 12; i++) {
68
- physics.simulate(playerCtx, fakeWorld).applyToBot(fakePlayer); // (applyToBot since fakePlayer is supposed to be a bot)
69
- console.log(fakePlayer.entity.position, fakePlayer.entity.velocity);
70
- }
71
- if (playerState.controlState.forward) {
72
- // expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, -3.4508449226731694)); // it works.
73
- }
74
- else {
75
- // expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, 0)); // it works.
76
- }
77
- console.log(fakePlayer.entity.position); //manual run.
@@ -1,33 +0,0 @@
1
- import { Entity } from "prismarine-entity";
2
- import { Vec3 } from "vec3";
3
- import md from "minecraft-data";
4
- export default function load(version: string): {
5
- mcData: md.IndexedData;
6
- Block: any;
7
- Entity: any;
8
- fakeWorld: {
9
- getBlock: (pos: {
10
- x: number;
11
- y: number;
12
- z: number;
13
- }) => any;
14
- };
15
- createFakePlayer: (pos: Vec3) => {
16
- entity: Entity;
17
- jumpTicks: number;
18
- jumpQueued: boolean;
19
- version: string;
20
- inventory: {
21
- slots: never[];
22
- };
23
- world: {
24
- getBlock: (pos: {
25
- x: number;
26
- y: number;
27
- z: number;
28
- }) => any;
29
- };
30
- };
31
- createFakeEntity: (name: string, pos: Vec3) => Entity;
32
- modifyEntity: (name: string, entity: Entity) => void;
33
- };
@@ -1,67 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const vec3_1 = require("vec3");
7
- const minecraft_data_1 = __importDefault(require("minecraft-data"));
8
- const prismarine_block_1 = __importDefault(require("prismarine-block"));
9
- const prismarine_entity_1 = __importDefault(require("prismarine-entity"));
10
- function load(version) {
11
- const mcData = (0, minecraft_data_1.default)(version);
12
- const Block = prismarine_block_1.default(version);
13
- const Entity = prismarine_entity_1.default(version);
14
- const fakeWorld = {
15
- getBlock: (pos) => {
16
- const type = pos.y < 60 ? mcData.blocksByName.stone.id : mcData.blocksByName.air.id;
17
- const b = new Block(type, 0, 0);
18
- b.position = pos;
19
- return b;
20
- },
21
- };
22
- const createFakeEntity = (name, pos) => {
23
- if (!mcData.entitiesByName[name])
24
- throw "invalid name";
25
- const tmp = mcData.entitiesByName[name];
26
- return {
27
- name: name,
28
- type: tmp.type,
29
- height: tmp.height,
30
- width: tmp.width,
31
- position: pos,
32
- velocity: new vec3_1.Vec3(0, 0, 0),
33
- onGround: false,
34
- isInWater: false,
35
- isInLava: false,
36
- isInWeb: false,
37
- isCollidedHorizontally: false,
38
- isCollidedVertically: false,
39
- yaw: 0,
40
- effects: {},
41
- metadata: [],
42
- equipment: new Array(6)
43
- };
44
- };
45
- const modifyEntity = (name, entity) => {
46
- var _a, _b;
47
- if (!mcData.entitiesByName[name])
48
- throw "invalid name";
49
- const tmp = mcData.entitiesByName[name];
50
- entity.height = (_a = tmp.height) !== null && _a !== void 0 ? _a : 0;
51
- entity.width = (_b = tmp.width) !== null && _b !== void 0 ? _b : 0;
52
- };
53
- const createFakePlayer = (pos) => {
54
- return {
55
- entity: createFakeEntity("player", pos),
56
- jumpTicks: 0,
57
- jumpQueued: false,
58
- version: "1.17.1",
59
- inventory: {
60
- slots: [],
61
- },
62
- world: fakeWorld
63
- };
64
- };
65
- return { mcData, Block, Entity, fakeWorld, createFakePlayer, createFakeEntity, modifyEntity };
66
- }
67
- exports.default = load;