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

Plugin message custom payload for "MC|Brand" encoded incorrectly #253

Closed deathcap closed 8 years ago

deathcap commented 9 years ago

lib/plugins/game.js:

    bot.client.write('custom_payload', {channel: 'MC|Brand', data: new Buffer('vanilla')});

from http://wiki.vg/Plugin_channel#MC.7CBrand: this is "vanilla" (encoded as a UTF-8 string).

Mineflayer does not encode the buffer as a UTF-8 string (i.e., varint length prefix; writeString() in node-minecraft-protocol). This causes at least the Glowstone server to choke:

20:18:26 [SEVERE] Error while handling PluginMessage(channel=MC|Brand, data=[118, 97, 110, 105, 108, 108, 97]) (handler: PluginMessageHandler)
java.lang.IndexOutOfBoundsException: readerIndex(1) + length(118) exceeds writerIndex(7): UnpooledHeapByteBuf(ridx: 1, widx: 7, cap: 7/7)
    at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1171)
    at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:677)
    at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:685)
    at com.flowpowered.networking.util.ByteBufUtils.readUTF8(ByteBufUtils.java:46)
    at net.glowstone.net.handler.play.game.PluginMessageHandler.handleInternal(PluginMessageHandler.java:74)
    at net.glowstone.net.handler.play.game.PluginMessageHandler.handle(PluginMessageHandler.java:39)
    at net.glowstone.net.handler.play.game.PluginMessageHandler.handle(PluginMessageHandler.java:20)
    at com.flowpowered.networking.session.BasicSession.handleMessage(BasicSession.java:80)
    at com.flowpowered.networking.session.BasicSession.messageReceived(BasicSession.java:139)
    at net.glowstone.net.GlowSession.pulse(GlowSession.java:408)
    at net.glowstone.net.SessionRegistry.pulse(SessionRegistry.java:23)
    at net.glowstone.scheduler.GlowScheduler.pulse(GlowScheduler.java:174)
    at net.glowstone.scheduler.GlowScheduler.access$100(GlowScheduler.java:25)
    at net.glowstone.scheduler.GlowScheduler$2.run(GlowScheduler.java:111)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

tested with node examples/echo.js localhost against a Glowstone server. This can be fixed by manually adding the varint length:

    bot.client.write('custom_payload', {channel: 'MC|Brand', data: new Buffer('\x07vanilla')});

but there ought to be a better way

deathcap commented 9 years ago

The payloads for plugin channel messages are all structured differently, should this be something node-minecraft-protocol handles encoding/decoding instead of mineflayer? I think so. For reference: https://github.com/GlowstoneMC/Glowstone/blob/master/src/main/java/net/glowstone/net/handler/play/game/PluginMessageHandler.java#L45 (MC|Brand, MC|BEdit, and MC|BSign payload decoding).

roblabla commented 9 years ago

This should be reported as a glowstone bug too IMO

deathcap commented 9 years ago

Why? It's correctly reporting bad packet structure (at least, it does recover - can still connect)

roblabla commented 9 years ago

Oh, I thought it disconnected you. NVM then.

roblabla commented 9 years ago

Related : PrismarineJS/node-minecraft-protocol#152

deathcap commented 8 years ago

Closing, merged in https://github.com/PrismarineJS/mineflayer/pull/254