PrismarineJS / mineflayer

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

chat.js plugin doesn't work on non-vanilla servers #163

Closed urlci closed 10 years ago

urlci commented 10 years ago

Hello.

I found this project by a chance and love it. I've been working on it and trying to understand how it works for few days so I can build my own bot. However I am having few issues with non-vanilla server which doesn't occur on vanilla servers. At first I was not even able to stay connected to the server because of the chat.js plugin just like this issue https://github.com/superjoe30/mineflayer/issues/159. I guess bot were getting disconnected due to server's welcome message or something else. Luckily I made a change on the line of chat.js plugin as zuazo stated; https://github.com/superjoe30/mineflayer/commit/fe2a7f1. Now I can stay connected to the server, but bot does not react anything depending bot.on(chat) event. But the weird thing is it works completely as expected on my vanilla server. My guess about on this issue is modified player names. As you know on vanilla servers when a player sends a global message, server prints it on chat GUI as;

< PlayerA > Hello world.

However on the sever which I am trying to make my bot work, it works this way;

PlayerA : Hello world.

So I assume characters (<,>) causes this problem. I have no idea if this can be modified or username type is changeable.

I hope you could tell me a simple solution for me. Thank you.

JonnyD commented 10 years ago

I'm having the same problem, is there a workaround for this?

''' jonathan@jonathan-VirtualBox:~$ node test.js I logged in. settings { chat: 'enabled', colorsEnabled: true, viewDistance: 'far', difficulty: 2, showCape: true } current time { day: 10393, age: 306536895 }

/home/jonathan/node_modules/mineflayer/lib/plugins/chat.js:17 if (jsonMsg.translate.match(/^chat./)) { ^ TypeError: Cannot call method 'match' of undefined at Client. (/home/jonathan/node_modules/mineflayer/lib/plugins/chat.js:17:27) at Client.EventEmitter.emit (events.js:95:17) at Socket. (/home/jonathan/node_modules/mineflayer/node_modules/minecraft-protocol/lib/client.js:41:12) at Socket.EventEmitter.emit (events.js:95:17) at Socket. (_streamreadable.js:746:14) at Socket.EventEmitter.emit (events.js:92:17) at emitReadable (_stream_readable.js:408:10) at emitReadable (_stream_readable.js:404:5) at readableAddChunk (_stream_readable.js:165:9) at Socket.Readable.push (_stream_readable.js:127:10) '''

andrewrk commented 10 years ago

Feel free to contribute. I'd gladly accept a pull request that solved this problem cleanly.

JonnyD commented 10 years ago

Yeah I am interested. I'm new to Node.js so was looking for a quick solution at first. I found you can solve this problem by removing that IF statement but then chat probably wont work (don't need it yet anyway).

zuazo commented 10 years ago
/home/jonathan/node_modules/mineflayer/lib/plugins/chat.js:17
if (jsonMsg.translate.match(/^chat./)) {
^
TypeError: Cannot call method 'match' of undefined

@JonnyD, This TypeError is fixed in master (maybe I should release a version with this fix rather than wait for the chat parsing to be improved). But some non-vanilla chat formats are not supported, so it is possible that the chat is not correctly parsed and chat events not triggered.

Anyway, you can always wait for "message" events and parse the chat message on your own:

https://github.com/superjoe30/mineflayer/blob/a35a2d83d37890c402cd8a3ba4bb17803a2f6807/lib/plugins/chat.js#L56-L60

JonnyD commented 10 years ago

Thanks zuazo, that's very helpful! :+1:

JonnyD commented 10 years ago

@zuazo there's a problem with that code at line 56: message is not a variable.

JonnyD commented 10 years ago

Fix it by replacing message with jsonMsg.

Is there any support for messages sent by the server?

zuazo commented 10 years ago

@zuazo there's a problem with that code at line 56: message is not a variable.

Thanks for testing :smile: I think I fixed it in master.

Is there any support for messages sent by the server?

AFAIK, you should be able to read them listening to "message" events. Other than that, are not supported.

JonnyD commented 10 years ago

Thanks! Actually server messages do work.

How could I change these regular expressions to support Herochat:

A spoken message with Herochat should be "Player: Hello". I got that work by removing the surrounding '<' and '>'. A whisper with Herochat should be "From Player: Hello". I added 'From' into the regular expression but it still matches the first one?

if (match = legalContent.match(/^(?:.+? )?(.+?) (.*)$/)) {
        // spoken chat
        username = match[1];
        content = match[2];
        bot.emit('chat', username, content, message);
      } else if (match = legalContent.match(/^(From ?:.+? )?(.+?) (.*)$/)) {
        // whispered chat
        username = match[1];
        content = match[2];
        bot.emit('whisper', username, content, message);
      }
zuazo commented 10 years ago

I think you could test the whisper regex before the normal chat regex. Something like the following:

if (match = legalContent.match(/^From (?:.+? )?(.+?) (.*)$/)) {
  // whispered chat
  username = match[1];
  content = match[2];
  bot.emit('whisper', username, content, message);
} else if (match = legalContent.match(/^(?:.+? )?(.+?) (.*)$/)) {
  // spoken chat
  username = match[1];
  content = match[2];
  bot.emit('chat', username, content, message);
}
nevercast commented 10 years ago

Will be handled by https://github.com/andrewrk/mineflayer/issues/175 Full support will be available in 1.7