Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain call crash in Python-JavaScript bridge when accessing object properties. #144

Open
ghost opened this issue Sep 7, 2024 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 7, 2024

Context: The bug occurs when using the Python-JavaScript bridge and the mineflayer module.
Replay conditions: The crash occurs when methods are called in a chain, e.g. playerEntity.position.offset(...).

from javascript import require, On
mineflayer = require('mineflayer')

bot = mineflayer.createBot({
  'host': 'localhost',
  'port': 30034,
  'username': 'lookAt_Bot'
})

def lookAtNearestPlayer():
    playerEntity = bot.nearestEntity()
  
    if not playerEntity: 
        return
  
    pos = playerEntity.position.offset(0, playerEntity.height, 0)
    bot.lookAt(pos)
    
@On(bot, 'chat')
def chathandle(this, username, message, *args):
    if username == "sffkjsdfsfd" and message == "asdkasdjkadjskajsdj":
        bot.chat('asjdkjasdkasjd')

@On(bot, 'physicsTick')
def handle(*args):
    lookAtNearestPlayer()

Workaround: Assigning an intermediate value to a variable before calling a method prevents a crash.

pos = playerEntity.position
pos = pos.offset(0, playerEntity.height, 0)
bot.lookAt(pos)
  • Funny enough, "@on(bot, 'chat')" also affects the bug. If you remove this event, the script will not crash.
  • The script crashes on chat messages (even though the event should not affect anything), but I don't think this applies to JSPyBridge.
@ghost
Copy link
Author

ghost commented Sep 7, 2024

JS analog

const mineflayer = require('mineflayer')

const bot = mineflayer.createBot({
  host: 'localhost',
  port: 37269,
  username: 'lookAt_Bot'
})

function lookAtNearestPlayer () {
  const playerFilter = (entity) => entity.type === 'player'
  const playerEntity = bot.nearestEntity(playerFilter)
  
  if (!playerEntity) return
  
  const pos = playerEntity.position.offset(0, playerEntity.height, 0)
  bot.lookAt(pos)
}

bot.on('physicTick', lookAtNearestPlayer)

@ghost
Copy link
Author

ghost commented Sep 7, 2024

the screenshot and log are old, but the error is the same.

Error: https://imgur.com/Ww9h3ci
PrismarineJS/mineflayer#2996 (comment)

C:\Users\root\Desktop>python main.py [JSE] node:internal/process/promises:288 [JSE]             triggerUncaughtException(err, true /* fromPromise */); [JSE]             ^ [JSE] [JSE] [🐍  Python Error  JavaScript attempt to call 'some function' in Python failed: [JSE] > bot.emit('physicsTick') [JSE]   at Timeout.doPhysics [as _onTimeout] (C:\Users\root\AppData\Local\Programs\Python\Python39\lib\site-packages\javascript\js\node_modules\mineflayer\lib\plugins\physics.js:65:13) [JSE]   at listOnTimeout (node:internal/timers:569:17) [JSE]   at process.processTimers (node:internal/timers:512:7) [JSE] [JSE] ... across the bridge ... [JSE] [JSE] > lookAtNearestPlayer() [JSE]   at handle (C:\Users\root\Desktop\main.py:26) [JSE] > pos = playerEntity.position.offset(0, playerEntity.height, 0) [JSE]   at lookAtNearestPlayer (C:\Users\root\Desktop\main.py:16) [JSE] 🌉 javascript.errors.JavaScriptError: ('offset', "TypeError: Cannot read properties of undefined (reading 'offset')\n    at Bridge.call (C:\\Users\\root\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\javascript\\js\\bridge.js:136:35)\n    at Bridge.onMessage (C:\\Users\\root\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\javascript\\js\\bridge.js:226:25)\n    at Socket.<anonymous> (C:\\Users\\root\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\javascript\\js\\bridge.js:278:18)\n    at Socket.emit (node:events:513:28)\n    at addChunk (node:internal/streams/readable:324:12)\n    at readableAddChunk (node:internal/streams/readable:297:9)\n    at Readable.push (node:internal/streams/readable:234:10)\n    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)")] [JSE] [JSE] Node.js v18.15.0

@ghost
Copy link
Author

ghost commented Sep 13, 2024

@extremeheat

1 similar comment
@ghost
Copy link
Author

ghost commented Dec 11, 2024

@extremeheat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants