Tuesday, 26 April 2016

SAM Labs and Minecraft - Summon a Pig at the Push of a Button

With SAM Labs wireless electronics and a bit of Minecraft know-how you too will be able to summon a pig at the push of a physical button. Yes, really. It's all done in vanilla Minecraft too - no mods needed.

The SAM Labs software now has 'apps' for generating virtual keypresses and mouse events. So of course our first thought was to control Minecraft using SAM Labs wireless components. You can find out how we achieved the basics in our Control Minecraft with SAM Labs electronics post.

Check out our SAM Labs review first if you're not familiar with them.

It's fun to be able to mine, place blocks and look around using SAM Labs. But what we really wanted to be able to do was summon a pig at the push of a button. Well not really, but it's a fun project which demonstrates the basic approach we've used and can be built on.

Here's the project in action:

You'll Need:

You just need a SAM Labs button (and Bluetooth dongle) for this particular project.

Minecraft fans are likely to want to combine this project with controlling other Minecraft features with other SAM modules. We like the Make Kit which also has other inputs that we have used for controlling Minecraft.

You'll also need a Minecraft desktop license and Minecraft installed.

Solution Outline

But Minecraft doesn't allow you to detect key presses

You can do lots of clever things with Minecraft Command Blocks but unfortunately detecting key presses isn't yet one of them. Ideally we wanted a solution in vanilla Minecraft (without mods) so we looked at what we can do. 

You can tell which hotbar slot the player has currently selected. We used this to detect when the player selects slot 3 followed by slot 4. Just detecting slot 3 would mean that our block would keep triggering repeatedly. 

Okay, now we can detect when the player selects slot 3 and then slot 4. We can use this trigger a pig summoning command block. 

But SAM Labs doesn't general virtual number presses

SAM Labs doesn't currently support generating virtual presses of the number keys. That's easy to work around though. You can remap your hotbar keys in Minecraft so that letters instead of numbers are used. We used the bottom row of letters plus the L key for slot 8. 

We need to generate two key presses

We want one button press to summon a pig, this involves two key presses, v then b. 

With the SAM labs software it was easy to trigger two key presses. We added a one second delay before the b to make sure that we got v and then b.

Step by Step 

Set up Minecraft:

  1. Create a new Minecraft profile called SAM Labs. You're going to change the keyboard controls so you probably don't want to do that on your main profile.
  2. Create a new Creative Mode world called SAM Labs Pigs. You probably want to set it to Peaceful initially to avoid distractions. 
  3. Find or create a nice flat area in your world. 
  4. Give yourself command blocks with:
    /give @p minecraft:command_block
  5. Create the following layout of command blocks and redstone comparators:

    Configure the command blocks as follows:
    Command Block 1: Repeating; Unconditional; Always Active
    Command Block 2: Repeating; Unconditional; Needs Redstone
    Command Block 3: Impulse; Unconditional; Needs Redstone
  6. Set the commands for the command blocks as follows:

    Command Block 1:
    testfor @a {SelectedItemSlot: 3}
    Command Block 2: 
    testfor @a {SelectedItemSlot: 4}
    Command Block 3:
    summon Pig ~ ~1 ~

    Note that when using commands the slots are numbered from 0 whereas when setting up keys they are numbered from 1!
  7. Test your command blocks by pressing 3 and then 4.
  8. Did you get a pig? Great. 
  9. Now save your world.  Go to Options then Controls and set the hotbar control keys as follows:
    z = slot 1 (0)
    v = slot 4 (3)
    b = slot 5 (4)
    l = slot 9 (8)

Set up SAM Labs

You'll need a SAM button and bluetooth dongle and the SAM labs software. 

You'll need to create this project:
  1. Create a new SAM Labs project and call it Minecraft Pigs
  2. Turn on your SAM button and drag it on to the workspace. 
  3. Add a 'To Key' app, set it to 'V' and wire the SAM button to it. 
  4. Add a second 'To Key' app and set it to 'B'. The time add a 'Delay' app from Time and place it in-between the button and the B 'To Key', configure the delay to one second. 

Test it

Now switch back to Minecraft and go into your world. 

Press the button and you should get a pig.

This is what happened when my 7 year old got hold of the button:

Obviously this is just an example of what you can do with SAM Labs and Minecraft. Try summoning other mobs or maybe a LightningBolt!

When my 9 year old got hold of the button, he changed the summon command to summon wolves instead:

More from Tech Age Kids:


Post a Comment