PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.96k stars 904 forks source link

SyntaxError: Unexpected token in JSON at position 38 #1169

Closed kars0nn closed 4 years ago

kars0nn commented 4 years ago

Versions

Issue : I havent been writing js for very long, so any help would be amazing. I started coding the bot, everything i have so far works great. The bot connects to minecraft, it sends embedded minecraft chat messages to discord, but this only happens for about 2 seconds before the console logs an error.

Here is the console log:

BaaaaBaaaa Online!
BaaaaBaaaa is in minecraft!
undefined:1
{"text":"§7§l§o5BUILDERS\n§7§l§o5TOOLS
                                      ^

SyntaxError: Unexpected token
 in JSON at position 38
    at JSON.parse (<anonymous>)
    at Client.<anonymous> (C:\Users\Master\node_modules\mineflayer\lib\plugins\tablist.js:18:49)
    at Client.emit (events.js:315:20)
    at FullPacketParser.<anonymous> (C:\Users\Master\node_modules\minecraft-protocol\src\client.js:88:12)
    at FullPacketParser.emit (events.js:315:20)
    at addChunk (C:\Users\Master\node_modules\readable-stream\lib\_stream_readable.js:298:12)
    at readableAddChunk (C:\Users\Master\node_modules\readable-stream\lib\_stream_readable.js:280:11)
    at FullPacketParser.Readable.push (C:\Users\Master\node_modules\readable-stream\lib\_stream_readable.js:241:10)
    at FullPacketParser.Transform.push (C:\Users\Master\node_modules\readable-stream\lib\_stream_transform.js:139:32)
    at FullPacketParser._transform (C:\Users\Master\node_modules\protodef\src\serializer.js:82:10)

I would expect for it to consistantly stay online in both minecraft and discord. I would also expect for it to keep sending chat messages to discord (which it has been doing for the 3 seconds it's online)

Any help is appreciated!

TheDudeFromCI commented 4 years ago

Can you show us your code?

kars0nn commented 4 years ago

Can you show us your code?

https://srcb.in/6f1b8d66d0

wvffle commented 4 years ago

Mind to share the tablist screenshot?

kars0nn commented 4 years ago

Mind to share the tablist screenshot?

2020-07-21_15 38 04

wvffle commented 4 years ago

are you sure, you're using the newest version of mineflayer? This seems like an old bug that has already been fixed.

kars0nn commented 4 years ago

are you sure, you're using the newest version of mineflayer? This seems like an old bug that has already been fixed.

how can I check / update?

rom1504 commented 4 years ago

we discussed this but nobody fixed it could you open a PR adding a try-catch at https://github.com/PrismarineJS/mineflayer/blob/master/lib/plugins/tablist.js#L18?

kars0nn commented 4 years ago

we discussed this but nobody fixed it could you open a PR adding a try-catch at https://github.com/PrismarineJS/mineflayer/blob/master/lib/plugins/tablist.js#L18?

how would I do that? Could you help me with that?

wvffle commented 4 years ago

we discussed this but nobody fixed it could you open a PR adding a try-catch at https://github.com/PrismarineJS/mineflayer/blob/master/lib/plugins/tablist.js#L18?

escapeValueNewlines is supposed to take care of it. Adding try/catch is not a solution.

wvffle commented 4 years ago

The correct solution is to change match.replace('\n', '\\n') to match.replace(/\n/g, '\\n')

kars0nn commented 4 years ago

The correct solution is to change match.replace('\n', '\\n') to match.replace(/\n/g, '\\n')

It worked!!!! Thank you so much, you guys are the best!

rom1504 commented 4 years ago

Ah right I remember now. Can you please pr it ?

On Tue, Jul 21, 2020, 23:05 karson-d notifications@github.com wrote:

The correct solution is to change match.replace('\n', '\n') to match.replace(/\n/g, '\n')

It worked!!!! Thank you so much, you guys are the best!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/PrismarineJS/mineflayer/issues/1169#issuecomment-662106029, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR437WMAQKFLD3TXP6CODLR4X7KBANCNFSM4PDHH2EA .