@nxg-org/mineflayer-physics-util 1.5.11 → 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.
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ declare module "mineflayer" {
8
8
  }
9
9
  export default function loader(bot: Bot): void;
10
10
  export declare function initSetup(data: IndexedData): void;
11
- export { EPhysicsCtx, PhysicsSettings } from "./physics/settings";
11
+ export { EPhysicsCtx, PhysicsSettings as PhysicsSettings } from "./physics/settings";
12
12
  export { BaseSimulator } from "./simulators";
13
13
  export { EntityPhysics } from "./physics/engines";
14
14
  export { EntityState } from "./physics/states";
package/dist/index.js CHANGED
@@ -12,7 +12,6 @@ function loader(bot) {
12
12
  exports.default = loader;
13
13
  function initSetup(data) {
14
14
  settings_1.EPhysicsCtx.loadData(data);
15
- settings_1.PhysicsSettings.loadData(data);
16
15
  }
17
16
  exports.initSetup = initSetup;
18
17
  var settings_2 = require("./physics/settings");
@@ -18,7 +18,8 @@
18
18
  }
19
19
  },
20
20
  "player": {
21
- "stepHeight": 0.6
21
+ "stepHeight": 0.6,
22
+ "airborneInertia": 0.91
22
23
  }
23
24
  },
24
25
  "projectiles": {
@@ -6,6 +6,7 @@ import { Vec3 } from "vec3";
6
6
  import { IPhysics } from "../engines/IPhysics";
7
7
  import { EntityState } from "../states/entityState";
8
8
  import { PlayerPoses } from "../states/poses";
9
+ import { PhysicsSettings } from "./physicsSettings";
9
10
  export declare const emptyVec: Vec3;
10
11
  type PlayerPoseContext = {
11
12
  [key in PlayerPoses]: {
@@ -27,6 +28,7 @@ export declare class EPhysicsCtx {
27
28
  static mcData: md.IndexedData;
28
29
  static entityData: md.IndexedData["entitiesByName"];
29
30
  static mobData: md.IndexedData["mobs"];
31
+ static globalSettings: typeof PhysicsSettings;
30
32
  /**
31
33
  * From minecraft's Player.java file.
32
34
  */
@@ -68,5 +70,6 @@ export declare class EPhysicsCtx {
68
70
  y: number;
69
71
  z: number;
70
72
  }): AABB;
73
+ updateFromBot(bot: Bot): void;
71
74
  }
72
75
  export {};
@@ -11,6 +11,7 @@ const physicsUtils_1 = require("../../util/physicsUtils");
11
11
  const entityState_1 = require("../states/entityState");
12
12
  const poses_1 = require("../states/poses");
13
13
  const entity_physics_json_1 = __importDefault(require("../info/entity_physics.json"));
14
+ const physicsSettings_1 = require("./physicsSettings");
14
15
  function getPose(entity) {
15
16
  const pose = entity.metadata.find((e) => (e === null || e === void 0 ? void 0 : e.type) === 18);
16
17
  return pose ? pose.value : poses_1.PlayerPoses.STANDING;
@@ -47,6 +48,7 @@ class EPhysicsCtx {
47
48
  };
48
49
  this.position = state.pos;
49
50
  this.velocity = state.vel;
51
+ // TODO cleanup all of this code.
50
52
  if (entityType.type === "player" || !!EPhysicsCtx.mobData[entityType.id]) {
51
53
  // @ts-expect-error
52
54
  const additional = entity_physics_json_1.default.living_entities[entityType.type];
@@ -187,9 +189,20 @@ class EPhysicsCtx {
187
189
  const halfWidth = this.entityType.width ? this.entityType.width / 2 : 0;
188
190
  return new mineflayer_util_plugin_1.AABB(position.x - halfWidth, position.y, position.z - halfWidth, position.x + halfWidth, position.y + ((_a = this.entityType.height) !== null && _a !== void 0 ? _a : 0), position.z + halfWidth);
189
191
  }
192
+ updateFromBot(bot) {
193
+ this.state.updateFromBot(bot);
194
+ this.pose = getPose(bot.entity);
195
+ switch (bot.game.gameMode) {
196
+ case "survival":
197
+ case "creative":
198
+ case "adventure":
199
+ case "spectator":
200
+ }
201
+ }
190
202
  }
191
203
  exports.EPhysicsCtx = EPhysicsCtx;
192
204
  EPhysicsCtx.loadData = load;
205
+ EPhysicsCtx.globalSettings = physicsSettings_1.PhysicsSettings;
193
206
  /**
194
207
  * From minecraft's Player.java file.
195
208
  */
@@ -5,6 +5,41 @@ type BubbleColumnInfo = {
5
5
  up: number;
6
6
  maxUp: number;
7
7
  };
8
+ export declare class OPhysicsSettings {
9
+ mcData: md.IndexedData;
10
+ entityData: md.IndexedData["entitiesByName"];
11
+ mobData: md.IndexedData["mobs"];
12
+ yawSpeed: number;
13
+ pitchSpeed: number;
14
+ playerSpeed: number;
15
+ stepHeight: number;
16
+ negligeableVelocity: number;
17
+ soulsandSpeed: number;
18
+ honeyblockSpeed: number;
19
+ honeyblockJumpSpeed: number;
20
+ ladderMaxSpeed: number;
21
+ ladderClimbSpeed: number;
22
+ waterInertia: number;
23
+ lavaInertia: number;
24
+ liquidAcceleration: number;
25
+ defaultSlipperiness: number;
26
+ outOfLiquidImpulse: number;
27
+ autojumpCooldown: number;
28
+ bubbleColumnSurfaceDrag: BubbleColumnInfo;
29
+ bubbleColumnDrag: BubbleColumnInfo;
30
+ slowFalling: number;
31
+ sprintingUUID: string;
32
+ jumpHeight: number;
33
+ airborneInertia: number;
34
+ airborneAcceleration: number;
35
+ sprintSpeed: number;
36
+ sneakSpeed: number;
37
+ usingItemSpeed: number;
38
+ constructor(mcData: md.IndexedData);
39
+ }
40
+ /**
41
+ * @deprecated Use PlayerPhysicsSettings instead
42
+ */
8
43
  export declare class PhysicsSettings {
9
44
  static loadData: (data: md.IndexedData) => void;
10
45
  static mcData: md.IndexedData;
@@ -1,11 +1,58 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PhysicsSettings = void 0;
3
+ exports.PhysicsSettings = exports.OPhysicsSettings = void 0;
4
+ class OPhysicsSettings {
5
+ constructor(mcData) {
6
+ this.yawSpeed = 3.0;
7
+ this.pitchSpeed = 3.0;
8
+ this.playerSpeed = 0.1;
9
+ this.stepHeight = 0.6; // how much height can the bot step on without jump
10
+ this.negligeableVelocity = 0.003; // actually 0.005 for 1.8; but seems fine
11
+ this.soulsandSpeed = 0.4;
12
+ this.honeyblockSpeed = 0.4;
13
+ this.honeyblockJumpSpeed = 0.4;
14
+ this.ladderMaxSpeed = 0.15;
15
+ this.ladderClimbSpeed = 0.2;
16
+ this.waterInertia = 0.8;
17
+ this.lavaInertia = 0.5;
18
+ this.liquidAcceleration = 0.02;
19
+ this.defaultSlipperiness = 0.6;
20
+ this.outOfLiquidImpulse = 0.3;
21
+ this.autojumpCooldown = 10; // ticks (0.5s)
22
+ this.bubbleColumnSurfaceDrag = {
23
+ down: 0.03,
24
+ maxDown: -0.9,
25
+ up: 0.1,
26
+ maxUp: 1.8,
27
+ };
28
+ this.bubbleColumnDrag = {
29
+ down: 0.03,
30
+ maxDown: -0.3,
31
+ up: 0.06,
32
+ maxUp: 0.7,
33
+ };
34
+ this.slowFalling = 0.125;
35
+ this.sprintingUUID = "662a6b8d-da3e-4c1c-8813-96ea6097278d"; // SPEED_MODIFIER_SPRINTING_UUID is from LivingEntity.java
36
+ this.jumpHeight = Math.fround(0.42);
37
+ this.airborneInertia = 0.99;
38
+ this.airborneAcceleration = 0.06;
39
+ this.sprintSpeed = Math.fround(0.3);
40
+ this.sneakSpeed = 0.3;
41
+ this.usingItemSpeed = 0.2;
42
+ this.mcData = mcData;
43
+ this.entityData = mcData["entitiesByName"];
44
+ this.mobData = mcData["mobs"];
45
+ }
46
+ }
47
+ exports.OPhysicsSettings = OPhysicsSettings;
4
48
  function load(data) {
5
49
  PhysicsSettings.mcData = data;
6
50
  PhysicsSettings.entityData = data["entitiesByName"];
7
51
  PhysicsSettings.mobData = data["mobs"];
8
52
  }
53
+ /**
54
+ * @deprecated Use PlayerPhysicsSettings instead
55
+ */
9
56
  class PhysicsSettings {
10
57
  }
11
58
  exports.PhysicsSettings = PhysicsSettings;
@@ -42,7 +89,7 @@ PhysicsSettings.slowFalling = 0.125;
42
89
  PhysicsSettings.sprintingUUID = "662a6b8d-da3e-4c1c-8813-96ea6097278d"; // SPEED_MODIFIER_SPRINTING_UUID is from LivingEntity.java
43
90
  PhysicsSettings.jumpHeight = Math.fround(0.42);
44
91
  PhysicsSettings.airborneInertia = 0.99;
45
- PhysicsSettings.airborneAcceleration = 0.06;
92
+ PhysicsSettings.airborneAcceleration = 0.02;
46
93
  PhysicsSettings.sprintSpeed = Math.fround(0.3);
47
94
  PhysicsSettings.sneakSpeed = 0.3;
48
95
  PhysicsSettings.usingItemSpeed = 0.2;
package/dist/wrapper.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { EntityPhysics, IPhysics } from "./physics/engines";
2
- import { EPhysicsCtx, PhysicsSettings } from "./physics/settings";
2
+ import { EPhysicsCtx } from "./physics/settings";
3
3
  import { Vec3 } from "vec3";
4
4
  import md from "minecraft-data";
5
5
  import type { Entity } from "prismarine-entity";
@@ -15,7 +15,6 @@ export declare enum SimulationTypes {
15
15
  export declare class PhysicsUtilWrapper {
16
16
  private bot;
17
17
  engine: IPhysics;
18
- readonly physicsSettings: typeof PhysicsSettings;
19
18
  readonly ePhysicsCtx: typeof EPhysicsCtx;
20
19
  readonly data: md.IndexedData;
21
20
  constructor(bot: Bot);
package/dist/wrapper.js CHANGED
@@ -16,10 +16,8 @@ var SimulationTypes;
16
16
  class PhysicsUtilWrapper {
17
17
  constructor(bot) {
18
18
  this.bot = bot;
19
- this.physicsSettings = settings_1.PhysicsSettings;
20
19
  this.ePhysicsCtx = settings_1.EPhysicsCtx;
21
20
  this.data = bot.registry;
22
- settings_1.PhysicsSettings.loadData(this.data);
23
21
  settings_1.EPhysicsCtx.loadData(this.data);
24
22
  this.engine = new engines_1.EntityPhysics(this.data);
25
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxg-org/mineflayer-physics-util",
3
- "version": "1.5.11",
3
+ "version": "1.6.1",
4
4
  "description": "Provides functionality for more accurate entity and projectile tracking.",
5
5
  "keywords": [
6
6
  "mineflayer",
@@ -25,7 +25,8 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "expect": "^29.5.0",
28
- "mineflayer": "^4.18.0",
28
+ "minecraft-data": "^3.83.1",
29
+ "mineflayer": "^4.22.0",
29
30
  "mineflayer-pathfinder": "^2.4.4",
30
31
  "prismarine-entity": "^2.4.0",
31
32
  "typescript": "^4.5.5"
@@ -8,6 +8,7 @@ const bot: Bot = createBot({
8
8
  host: process.argv[2],
9
9
  port: Number(process.argv[3]),
10
10
  username: "testingbot",
11
+ version: process.argv[4],
11
12
  });
12
13
 
13
14
  bot.once("spawn", async () => {
@@ -24,13 +25,44 @@ const rl = require("readline").createInterface({
24
25
 
25
26
  rl.on("line", (line: any) => bot.chat(line));
26
27
 
28
+ // print whenever bot hits the ground
29
+
30
+ let wasOnGround = false;
31
+ let printNextPos = false;
32
+ bot.on("move", (pos) => {
33
+ if (bot.entity.onGround && !wasOnGround) {
34
+ bot.chat("Hit the ground! " + bot.entity.position.toString());
35
+ }
36
+ wasOnGround = bot.entity.onGround;
37
+ });
38
+
39
+ // print whenever another player hits the ground
40
+ let lastPositions: Record<string, boolean> = {};
41
+ bot.on("entityMoved", (entity) => {
42
+ console.log(entity.username)
43
+ if (entity.username && entity.username !== bot.username) {
44
+ // check by seeing is y value is an integer
45
+ if (Math.floor(entity.position.y) === entity.position.y && !lastPositions[entity.username]) {
46
+ bot.chat(`${entity.username} hit the ground! ${entity.position.toString()}`);
47
+ lastPositions[entity.username] = true;
48
+ } else if (Math.floor(entity.position.y) !== entity.position.y) {
49
+ lastPositions[entity.username] = false;
50
+ }}
51
+ });
52
+
27
53
  bot.on("chat", (user, message) => {
28
54
  const [cmd, ...args] = message.split(" ");
29
55
  const author = bot.nearestEntity((e) => e.username === user);
30
56
 
31
57
  switch (cmd) {
58
+ case "control":
59
+ if (args.length !== 2) return bot.chat("Invalid control command!");
60
+ if (args[0] === "clear") return bot.clearControlStates();
61
+ bot.setControlState(args[0] as any, args[1] === "true");
62
+ break;
32
63
  case "original":
33
64
  bot.physics = new Physics(bot.registry, bot.world);
65
+ bot.chat("Switched to original physics!");
34
66
  break;
35
67
  case "new":
36
68
  const val = new EntityPhysics(bot.registry);
@@ -54,6 +86,7 @@ bot.on("chat", (user, message) => {
54
86
  return val.simulate(ctx, bot.world);
55
87
  return oldSim(...args);
56
88
  };
89
+ bot.chat("Switched to new physics!");
57
90
  break;
58
91
  case "jump":
59
92
  bot.setControlState("jump", true);
@@ -12,12 +12,14 @@ import expect from "expect";
12
12
 
13
13
  import { initSetup } from "../src/index";
14
14
 
15
- const mcData = md("1.17.1");
16
- const Block = (block as any)("1.17.1");
15
+ const mcData = md("1.12.2");
16
+ const Block = (block as any)("1.12.2");
17
+
18
+ const groundLevel = 4;
17
19
 
18
20
  const fakeWorld = {
19
21
  getBlock: (pos: { x: number; y: number; z: number }) => {
20
- const type = pos.y < 60 ? mcData.blocksByName.stone.id : mcData.blocksByName.air.id;
22
+ const type = pos.y < groundLevel ? mcData.blocksByName.stone.id : mcData.blocksByName.air.id;
21
23
  const b = new Block(type, 0, 0);
22
24
  b.position = pos;
23
25
  return b;
@@ -45,7 +47,7 @@ function createFakePlayer(pos: Vec3) {
45
47
  slots: [],
46
48
  },
47
49
 
48
- setControlState: (...args) => {}
50
+ setControlState: (...args: any) => {}
49
51
  };
50
52
  }
51
53
 
@@ -55,7 +57,7 @@ initSetup(mcData);
55
57
 
56
58
  //create fake bot
57
59
  const playerType = mcData.entitiesByName["player"]; // specify type we will be simulating.
58
- const fakePlayer = createFakePlayer(new Vec3(0, 80, 0)); // call function supplied by prismarine-physics
60
+ const fakePlayer = createFakePlayer(new Vec3(0, groundLevel + 20, 0)); // call function supplied by prismarine-physics
59
61
  fakePlayer.entity = applyMdToNewEntity(EPhysicsCtx, playerType, fakePlayer.entity); // ensure compatibility.
60
62
 
61
63
  // create physics context.
@@ -69,6 +71,7 @@ const playerCtx = EPhysicsCtx.FROM_ENTITY_STATE(physics, playerState, playerType
69
71
  playerState.control = ControlStateHandler.DEFAULT(); // specific to players and mobs, specify control scheme to apply.
70
72
  playerState.control.forward = true;
71
73
 
74
+
72
75
  // simulate until on ground.
73
76
  while (!playerCtx.state.onGround) {
74
77
  physics.simulate(playerCtx, fakeWorld).applyToBot(fakePlayer as any); // (applyToBot since fakePlayer is supposed to be a bot)
@@ -76,9 +79,9 @@ while (!playerCtx.state.onGround) {
76
79
  }
77
80
 
78
81
  if (playerState.control.forward) {
79
- expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, -3.4508449226731694)); // it works.
82
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, groundLevel, -3.4508449226731694)); // it works.
80
83
  } else {
81
- expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, 0)); // it works.
84
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, groundLevel, 0)); // it works.
82
85
  }
83
86
 
84
87
  playerCtx.state.control.set("jump", true);
@@ -88,9 +91,9 @@ for (let i = 0; i < 12; i++) {
88
91
  }
89
92
 
90
93
  if (playerState.control.forward) {
91
- expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, -5.788782872583908)); // it works.
94
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, groundLevel, -5.788782872583908)); // it works.
92
95
  } else {
93
- expect(fakePlayer.entity.position).toEqual(new Vec3(0, 60, 0)); // it works.
96
+ expect(fakePlayer.entity.position).toEqual(new Vec3(0, groundLevel, 0)); // it works.
94
97
  }
95
98
 
96
99