Thursday, 4 June 2015

Programming Minecraft with Dart - mcpi-dart v0.1

The Minecraft phenomena has been an amazing success with game players and coders. A version was released for the Raspberry Pi computer that allowed programming via a Python wrapper. The same programming API was added to regular Minecraft servers via Raspberry Juice.

Of course, it would be better if you could program Minecraft in Dart... and now you can! I have put up a first version of mcpi-dart on GitHub. An early version so please be kind, try it out and provide feedback. Make sure you Dart is up to date as it requires async.

This entire project was inspired by the awesome book 'Adventures in Minecraft' by Martyn O'Hanlon.

Let's take a run through the main.dart demo sample.

Connecting to the server

Make sure the IP is pointing to your Minecraft server running Bukkit & Raspberry Juice. This was tested on a slightly older Minecraft 1.6.4 on Bukkit Server with Raspberry Juice.

  mci = new Minecraft();
  await mci.connect("192.168.0.110", 4711);

Chat Messages

Talkie talkie! Easy one this.

mci.chat("Dart is awesome!");

Placing Blocks

This is what it is all about. Let's get building!

blockDemo() {
  print("Block Demo Start");
  for (int i = 0; i < 99; i++) {
    mci
      ..setBlock(1, i, 0, Block.GOLD_BLOCK)
      ..setBlock(3, i, 0, Block.DIAMOND_BLOCK)
      ..setBlock(5, i, 0, Block.WOOL);
  }
  print("Block Demo End");
}

Getting the Player's Position

This method is a little trickier as we have to await a response.

  var s = (await mci.getPos());
  print(s);

Building a Rainbow

Just for fun :-)

  print("Starting rainbow...");
  var colors = [
    Block.BRICK_BLOCK,
    Block.GOLD_BLOCK,
    Block.MELON,
    Block.DIAMOND_BLOCK,
    Block.SANDSTONE,
    Block.GLOWSTONE_BLOCK,
    Block.LAPIS_LAZULI_BLOCK
  ];

  var height = 80;

  for (int x = 0; x < 128; x++) {
    for (int ci = 0; ci < colors.length; ci++) {
      double y = math.sin((x / 128.0) * math.PI) * height + ci;
      mci.setBlock(x - 64, y.round(), 0, colors[(colors.length - 1) - ci]);
    }
  }
  print("Built rainbow.");

Next steps

  • Some blocks require data rather than just an ID.
  • Get package onto Pub.
  • More Demos!