Skip to content

Instantly share code, notes, and snippets.

@badryan
Created December 21, 2015 17:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save badryan/f6f9036b492d3280eac8 to your computer and use it in GitHub Desktop.
Save badryan/f6f9036b492d3280eac8 to your computer and use it in GitHub Desktop.
Block inventory in Minecraft

This is a short flow to demonstrate how to communicate from Node-RED with Minecraft. It uses a TCP request to push "player.getPos()\n" into Steve's world and will receive the coordinate tuple in a node.js buffer format that is converted. It goes on to extract the coordinates and populates an array with "word.getBlock()" statements for a cube around the player's position. The results of the following TCP requests are collected, and if no more information arrives, printed to the debug panel. Note that you have to run Minecraft and enter a world before triggering this flow.

[{"id":"533edf4.facc12","type":"function","z":"e13b1b02.1ec4e8","name":"getPos()","func":"msg.payload = \"player.getPos()\\n\";\nreturn msg;","outputs":1,"noerr":0,"x":207,"y":151,"wires":[["71429f10.8ebd6"]]},{"id":"dd58ffe0.22a7","type":"debug","z":"e13b1b02.1ec4e8","name":"","active":true,"console":"false","complete":"payload","x":642,"y":278,"wires":[]},{"id":"cc9ed7e5.336128","type":"inject","z":"e13b1b02.1ec4e8","name":"inject","topic":"","payload":"trigger","payloadType":"string","repeat":"","crontab":"","once":false,"x":98,"y":53,"wires":[["533edf4.facc12"]]},{"id":"71429f10.8ebd6","type":"tcp request","z":"e13b1b02.1ec4e8","server":"127.0.0.1","port":"4711","out":"char","splitc":"\\n","name":"","x":329,"y":74,"wires":[["a9afe2c4.56502"]]},{"id":"a9afe2c4.56502","type":"function","z":"e13b1b02.1ec4e8","name":"getBlock()","func":"var arr = msg.payload.toString().replace(/(\\n|\\r)+$/, '').split(\",\");\n\nvar x = parseInt(arr[0]);\nvar z = parseInt(arr[1]);\nvar y = parseInt(arr[2])\nvar cube_size = 3;\n\nvar coord_set = []\n for (var h = x-cube_size; h < x+cube_size+1; h++) {\n for (var k = z-cube_size; k < z+cube_size+1; k++) {\n for (var l = y-cube_size; l < y+cube_size+1; l++) {\n coord_set.push({payload: \"world.getBlock(\"+h+\",\"+k+\",\"+l+\")\\n\"});\n }\n }\n }\nreturn [coord_set];","outputs":1,"noerr":0,"x":463,"y":140,"wires":[["9713fdf5.68ec"]]},{"id":"9713fdf5.68ec","type":"tcp request","z":"e13b1b02.1ec4e8","server":"127.0.0.1","port":"4711","out":"char","splitc":"\\n","name":"","x":203,"y":277,"wires":[["4958e94e.b6a718","e8e39686.171c68"]]},{"id":"4958e94e.b6a718","type":"function","z":"e13b1b02.1ec4e8","name":"tabulate","func":"if (msg.payload === \"trigger\") {\n msg.payload = context.table;\n context.table = undefined;\n return msg;\n} else {\n var ID = msg.payload.toString().replace(/(\\n|\\r)+$/, '');\n \n context.table = context.table || {};\n context.table[ID] = context.table[ID] || 0;\n context.table[ID] = context.table[ID]+1;\n}","outputs":1,"noerr":0,"x":470,"y":278,"wires":[["dd58ffe0.22a7"]]},{"id":"e8e39686.171c68","type":"trigger","z":"e13b1b02.1ec4e8","op1":"1","op2":"trigger","op1type":"nul","op2type":"val","duration":"250","extend":true,"units":"ms","name":"","x":362,"y":354,"wires":[["4958e94e.b6a718"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment