How to Write a Client

From wiki.vg
Revision as of 15:18, 7 February 2011 by Thejoshwolfe (talk | contribs) (→‎Getting the map chunks: it's ok to ignore the initialization chunk fragments)
Jump to navigation Jump to search

This tutorial is being created to document what it takes to write a stand-alone client to interact with a notchian server (version Beta 1.2_01). The tutorial is incomplete but will be updated whenever more information is discovered.

Before You Get Started

  • Make sure you don't want to join or fork an existing project instead.
  • Ponder how much work it will be.
  • Download the latest official minecraft server and run it with authentication turned off. Bind it to localhost if you want.

Parsing the messages

Main article: Protocol

Sorry, but you have to be able to parse all the messages. Write all the message parsing code. If you don't, your client will effectively crash if the server sends you any message you can't parse.

Login

Connect to the server at localhost, port 25565.

Main article: Protocol FAQ

Paraphrase (with no authentication): send a 0x02, get a 0x02, send a 0x01, get a 0x01. Then you'll eventually get a 0x0D, and that's when the game really begins.

To test if this worked, connect to your localhost server with the notchian client and see if you can see your custom client appear and float in the air.

Getting the map chunks

You have to convince the server that you deserve to know about all the map chunks that are around you.

  • Every 200 milliseconds, send one of 0x0A - 0x0D.

To test if this worked, count the number of 0x33 map chunk messages you get that are full-sized chunks (16,128,16). You should get hundreds.

If you're not doing it right, you might only see bits and pieces of the map loading really far away, or possibly just one chunk load where you're standing. It seems notchian servers send several partial chunks very far away from spawn when clients first log in. The chunks these fragments belong to will eventually be loaded completely, so it's probably safe to ignore the initial fragments.

Moving around

Send the server updates on your position (0x0A - 0x0D) every 200 milliseconds. This section was written experimenting with sending only 0x0D messages.

The server will mostly keep quiet about your position, which means that your movements are acceptable. If the server sends you your position, it means you've done something wrong. In this situation, you must apologize by sending back identical data or else the server will begin ignoring any future position updates. The jerking effect that happens in this event can be seen in notchian clients when trying to walk across missing chunks.

The walking animation is automatically sent to or in some other way displayed in notchian clients when you move around on the ground. There's no walking animation when you move straight up and down though.

Restrictions

  • You cannot move to inside of a solid block.

If you break any of the above rules, the server will send you your corrected position. Corrected positions preserve your on-ground status, even if you were trying to fall through the top of a solid block.

Allowed super-human abilities

  • Flying - move about in the air. Make sure on-ground is false or else you might get stuck to the ground.
  • Walking in the air - walk off a cliff with on-ground set to true. The avatar will continue walking as if there were ground bellow him. Tested over shallow holes and across water at sea level.
  • Teleporting - put yourself anywhere instantly. Tested with a teleport distance of 1000m, which exceeds the bounds of the loaded map. Only tested with on-ground set to false.
  • Moving arbitrarily fast - due to being able to teleport, simply update your position to move as far as you like with each step to move arbitrarily fast.

Testing/Debugging

To test if moving around is working, watch your avatar move around from a notchian client.

If your avatar remains still despite your position update messages, then maybe you didn't apologize in response to the server correcting your position. Reconnect to give yourself another chance.

If your avatar is invisible to notchian clients despite the server sending you reasonable spawn coordinates, then maybe you died. Watch for 0x08 health updates to verify this cause. Try respawning or deleting the server's player file (world/players/<username>.dat) to recover from this situation.

TODO: how is fall damage calculated and how to avoid it.

TODO: what is the effect of your stance?

TODO: experiment with and document Look stuff.

Digging

Requirements from the server to break a block

  • The dig modifier is '3'
  • The distance from the players eyes to the block is less than 16
  • For the server to process 0 and 1 modifiers, you must be within 6.0 of the center of the block you are mining
  • Waiting between packets doesn't seem necessary (And can break digging. Conditions need verification)
  • Face of the block is currently not validated and neither is where the player looks

To dig a block

Optional arm animation 0x12, entity_id, 1
Start digging 0x0e, 0, x, y, z, face
Dig (send multiple) 0x0e, 1, x, y, z, face
Break block 0x0e, 3, x, y, z, face
Stop digging 0x0e, 2, 0, 0, 0, 0

How many times do I have to dig?

Dig counts varies with block type and equipped tool. The wrong tool will take damage while the right tool will lower the amount of digs required. Digging too many times doesn't seem to have a negative effect.

NOTE: Needs verification. Empty handed dig requires many more than this.

NOTE: Holding a random block seems to require same amount of digs as empty hands.

Block Empty hand Wood Shovel
Sand 76 38
Dirt
Stone
Wood

Left to test

  • Can you dig behind blocks?
  • Digging further than 6 blocks away
  • Is tools enforced to drop blocks like stone?
  • What happens to your tool if you dig too many times?
  • Figure out tool modifiers to number of digs and verify dig counts on different blocks

Placing/using stuff

TODO: can you place blocks from behind walls?

Crafting

TODO

Attacking mobs/players

TODO

Chat

Main article: Chat

Health and Respawning

The server sends 0x08 health updates whenever your health changes. If the server sends a health value less than 1, you are dead. Your avatar will disappear a second or two after dying. The "fall over" animation is not automatic.

You can safely send a 0x09 Respawn immediately after you get a death notice. You'll be given a 0x0D and possibly other messages after you do.