For those who don’t know me I have to say that I’m a big fan of Minecraft (1 and 2). And I’ve always wanted to connect the things that happen in that virtual world with the things in our real world (and other people have done great things; House light sync, controlling a Christmas tree. Now that I have one of the pieces of the puzzle I am available to do it, so this morning I started writing some code and in a couple of hours I accomplished my objective.

We can think of connecting the two worlds as to different steps:

  • Extracting data from Minecraft to a real computer.
  • Make the real computer do something in the real world.

Note: obviously you can also do it the other way around. Change things in your Minecraft world in response of things that happen in the real world.

Update: Check this other post to know how I’ve sync the real weather of my city info Minecraft.

To extract information from Minecraft I’ve used ComputerCraft, a mod that adds computers and turtles to the game. I’ve used a computer with a simple program that detects the changes on a Redstone signal and writes the state into a file. That file is the bridge between Minecraft and your real computer.

local SIDE = "back"

function fillFile(file)
  local signal = redstone.getInput(SIDE)
  file.write(signal)
end

while true do
  f = fs.open('bridge.out','w')
  fillFile(f)
  f.close()

  sleep(1)
end

Then I’ve wrote a script that reacts to changes in that file. Every time that the computer in Minecraft changes the file, the script on the real computer has the chance to do something.

require 'listen'

tolisten = "" // computer craft folder

previous = false

listener = Listen.to(tolisten, only: /\.out$/) do |modified, added, removed|
  if modified.count > 0 then
    file = File.open(modified[1])
    contents = file.read
    file.close()
    puts "-" + contents + "-"
    if contents.include? "true" then
      if !previous then
        puts "on"
        system "wemo switch sol on"
      end
      previous = true
    else
      if previous then
        puts "off"
        system "wemo switch sol off"
      end
      previous = false
    end
    puts "done"
  end
end
listener.start # not blocking
sleep

This is the last piece of the puzzle that I was talking about, a WeMo Switch. You need something that you can control with a computer, and now that Home Automation (domotique) is so accessible is a good moment to try things like this. As you can see I use ouimeaux to send commands to the WeMo Switch. It’s the most reliable API that I’ve found to do it.

So that’s it! Combining Minecraft, Computercraft, LUA, Ruby, a Python program and a WeMo Switch I can connect Minecraft with the Real World™. A funny Saturday morning! :D