Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to send changes to a lever in Minecraft to a MQTT broker - and listen for changes in an Arduino
var mqtt = require('sc-mqtt');
var client = mqtt.client(); // local host is default. Otherwise use host, user/pwd
//Subscribe to changes in the state of the Arduino
var player; // To remember who pulled the switch
// Here we tell Minecraft to give us control after certain events
events.on('player.PlayerInteractEvent', function (listener, event) {
var block = event.getClickedBlock(); // Which block?
var type = block.getType(); // What type is it?
player = event.player; // Note who shall be alerted
if(type==org.bukkit.Material.LEVER) {
var loc = block.location; //We need to find the location
// Compose a MQTT Topic which contains the world name and location of the block
var mqttTopic='minecraft/''/lever/'+loc.x+','+loc.y+','+loc.z+'/status';
// Is the lever up or down?
var state='';
if ({
// Now publish it
client.publish(mqttTopic, // which topic
state, // the status
1, // QoS 1 ( deliver at least once )
true); // broker should retain message
//Give the player a notification if a message arrives
client.onMessageArrived(function(topic, message){
var bytes = message.payload;
var javaString = new java.lang.String(bytes); // Using the Java libraries, we can convert from binary
player.sendMessage(javaString.slice(0, - 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment