- Inscription
Langue : [automatic], [fr], [en], … | Allez on remonte !
« Retourner à la documentation

[Minecraft] ComputerCraft: Code in Python!

TL;DR Control ComputerCraft remotely (aka a backdoor) with, for instance and as shown here, Python.



I really like MC and especially modded MC such as the FTB modpacks.

One of the mods often included in those packs is ComputerCraft.
A very neat "little" (not really) mod that allows you to control computers or turtles ("computers" that can move around and interact with the world). You can skip directly to the code.

But it comes at one cost (at least IMHO), you gotta control it using the programming language named Lua, which I'm very not fond of.

I really prefer Python as a programming language.

The problem?

Well, for one, the dev(s) of CC don't want to integrate (or switch over) Python in their mod.

Implementing the possibility of executing Python via CC would require to know how to mod the mod itself and thus coding in Java. meh…

(Note: Some ppl did it but I find it a bit edgy…)

My (a lil' hacky) solution?

Well, what would require the least knowledge to setup?

A "bridge"! Proxy, backdoor, … or whichever way you want to name it actually.

Yup, 20 lines of Lua and +20 lines of Python later, there you have it!

A way to control ComputerCraft turtles remotely from outside MC.

As a fact, "anything" could be used to control it but, you guessed it, I chose using Python personally. :)

Lua code:

local url = 'http://127.0.0.1:4343/'
local args = ''
 
server_on = http.get(url..'hello/')
 
if server_on == nil then
print('PyServer is off')
return
else
print('PyServer ready')
end
 
while true do
src = http.get(url..args)
if src then
loadstring(src.readAll())()
end
args = ''
end
 


A Python HTTP Server:

I chose flask for its simplicity. To install it, simply type "pip install flask" in your terminal.

# coding: utf-8
 
import socket
 
from flask import Flask
 
s = socket.socket()
s.connect(('127.0.0.1', 4344))
 
app = Flask(__name__)
 
@app.route('/hello/')
def hello():
return 'Hello World!'
 
@app.route('/')
def req():
return s.recv(4096).decode('utf-8')
 
app.run(None, 4343)


As the server is "blocking" input, I used a quick dirty workaround to be able to send requests to the server, which then send it back to the turtle who executes it.

# coding: utf-8
 
# This is the main code that will need to be changed.
 
import socket
 
s = socket.socket()
 
s.bind(('127.0.0.1', 4344))
s.listen(-1)
 
c = s.accept()
c = c[0]
 
print('Server is ready, please type your commands:')
 
while True:
c.send(input().encode('utf-8'))



The final step, not featured here, will be to teach my Python program to be able "to translate" Python commands in Lua ones.
Dernière modification le 01/09/2015 à 0h08
» Commenter cet article / 0 commentaire(s)