Skip to content

Commit 4bc0e0a

Browse files
fix support for pathfinder, aka backwards compatibility
1 parent c9ed7cb commit 4bc0e0a

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

examples/pathfinder/gps.js

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
11
// This is an example that uses mineflayer-pathfinder to showcase how simple it is to walk to goals
22

3-
const mineflayer = require('../../')
4-
const { pathfinder, Movements, goals: { GoalNear } } = require('mineflayer-pathfinder')
3+
const mineflayer = require("../../");
4+
const {
5+
pathfinder,
6+
Movements,
7+
goals: { GoalNear },
8+
} = require("mineflayer-pathfinder");
59

610
if (process.argv.length < 4 || process.argv.length > 6) {
7-
console.log('Usage : node gps.js <host> <port> [<name>] [<password>]')
8-
process.exit(1)
11+
console.log("Usage : node gps.js <host> <port> [<name>] [<password>]");
12+
process.exit(1);
913
}
1014

1115
const bot = mineflayer.createBot({
1216
host: process.argv[2],
1317
port: parseInt(process.argv[3]),
14-
username: process.argv[4] ? process.argv[4] : 'gps',
15-
password: process.argv[5]
16-
})
18+
username: process.argv[4] ? process.argv[4] : "gps",
19+
password: process.argv[5],
20+
});
1721

18-
const RANGE_GOAL = 1 // get within this radius of the player
22+
const RANGE_GOAL = 1; // get within this radius of the player
1923

20-
bot.loadPlugin(pathfinder)
24+
bot.loadPlugin(pathfinder);
2125

22-
bot.once('spawn', () => {
23-
const defaultMove = new Movements(bot)
26+
bot.once("spawn", () => {
27+
const defaultMove = new Movements(bot);
2428

25-
bot.on('chat', (username, message) => {
26-
if (username === bot.username) return
27-
if (message !== 'come') return
28-
const target = bot.players[username]?.entity
29-
if (!target) {
30-
bot.chat("I don't see you !")
31-
return
32-
}
33-
const { x: playerX, y: playerY, z: playerZ } = target.position
29+
bot.on("chat", (username, message) => {
30+
console.log("Chat message received: ", username, message);
31+
if (username === bot.username) return;
32+
33+
const [cmd, ...args] = message.split(" ");
34+
35+
const target = bot.players[username]?.entity;
36+
if (!target) {
37+
bot.chat("I don't see you !");
38+
return;
39+
}
3440

35-
bot.pathfinder.setMovements(defaultMove)
36-
bot.pathfinder.setGoal(new GoalNear(playerX, playerY, playerZ, RANGE_GOAL))
37-
})
38-
})
41+
switch (cmd) {
42+
case "come": {
43+
44+
const { x: playerX, y: playerY, z: playerZ } = target.position;
45+
46+
bot.pathfinder.setMovements(defaultMove);
47+
bot.pathfinder.setGoal(new GoalNear(playerX, playerY, playerZ, RANGE_GOAL));
48+
}
49+
case "forward": {
50+
bot.lookAt(target.position);
51+
bot.setControlState("forward", true)
52+
}
53+
}
54+
});
55+
});
3956

40-
bot.on('error', err => console.log(err))
57+
bot.on("error", (err) => console.log(err));

examples/pathfinder/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "0.0.0",
44
"private": true,
55
"dependencies": {
6-
"mineflayer-pathfinder": "^2.4.5",
7-
"mineflayer": "file:../../"
6+
"mineflayer": "file:../../",
7+
"mineflayer-pathfinder": "^2.4.5"
88
},
99
"description": "A mineflayer example"
1010
}

lib/plugins/physics.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const math = require('../math')
44
const conv = require('../conversions')
55
const { performance } = require('perf_hooks')
66
const { createDoneTask, createTask } = require('../promise_utils')
7-
const { BotcraftPhysics, EPhysicsCtx, PhysicsWorldSettings, initSetup } = require('@nxg-org/mineflayer-physics-util')
7+
const { EntityPhysics, EPhysicsCtx, PhysicsWorldSettings, initSetup } = require('@nxg-org/mineflayer-physics-util')
88

99
module.exports = inject
1010

@@ -22,7 +22,7 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
2222
const PHYSICS_CATCHUP_TICKS = maxCatchupTicks ?? 4
2323
const world = { getBlock: (pos) => { return bot.blockAt(pos, false) } }
2424

25-
const physics = new BotcraftPhysics(bot.registry)
25+
const physics = new EntityPhysics(bot.registry)
2626

2727
initSetup(bot.registry)
2828

@@ -300,11 +300,16 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
300300
// compatibility for mineflayer-pathfinder
301301
// define method 'simulatePlayer'
302302

303-
obj.simulatePlayer = function (...args) {
304-
ectx.state.update(bot)
305-
Object.assign(ectx, settings.overrides)
303+
obj.simulatePlayer = function (state, world) {
304+
const eType = bot.registry.entitiesByName["player"] ?? bot.registry.entitiesByName["minecraft:player"]
305+
if (!eType) {
306+
throw new Error("Player entity type not found in registry")
307+
}
306308

307-
physics.simulate(ectx, world).apply(bot)
309+
const ectx = EPhysicsCtx.FROM_ENTITY_STATE(physics, state, eType, settings)
310+
Object.assign(ectx, settings.overrides)
311+
312+
physics.simulate(ectx, world)
308313
}
309314

310315
bot.physics = obj

0 commit comments

Comments
 (0)