Skip to content

Instantly share code, notes, and snippets.

@sisomm
Last active January 4, 2016 09:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sisomm/8603395 to your computer and use it in GitHub Desktop.
Save sisomm/8603395 to your computer and use it in GitHub Desktop.
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
client.connect();
//Subscribe to changes in the state of the Arduino
client.subscribe('/arduino/1/status');
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/'+loc.world.name+'/lever/'+loc.x+','+loc.y+','+loc.z+'/status';
// Is the lever up or down?
var state='';
if (block.data==3){
state='DOWN'
}
else
{
state='UP'
}
// 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