Initial Python3 support
Libraries and modules changed to support Python 3. TODO: Refactoring of connection.py submodule to fix the monkey patching of methods for different version of Python (or, at least, to highlight the mixin more clearlymaster
parent
9b8aeb9f39
commit
c81ccce0e4
|
@ -1 +1 @@
|
|||
from main import *
|
||||
from . main import *
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import settings
|
||||
from . import settings
|
||||
|
||||
class Block:
|
||||
"""Minecraft PI block description. Can be sent to Minecraft.setBlock/s"""
|
||||
|
|
|
@ -7,7 +7,7 @@ import os
|
|||
import platform
|
||||
import base64
|
||||
from hashlib import md5
|
||||
from util import flatten_parameters_to_string
|
||||
from . util import flatten_parameters_to_string
|
||||
|
||||
""" @author: Aron Nieminen, Mojang AB"""
|
||||
|
||||
|
@ -37,6 +37,7 @@ class Connection:
|
|||
self.socket.connect((address, port))
|
||||
self.readFile = self.socket.makefile("r")
|
||||
self.lastSent = ""
|
||||
self.encoding = "cp437" # "utf-8"
|
||||
if self.windows:
|
||||
atexit.register(self.close)
|
||||
|
||||
|
@ -46,7 +47,7 @@ class Connection:
|
|||
try:
|
||||
atexit.unregister(self.close)
|
||||
except:
|
||||
pass
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
|
@ -59,11 +60,11 @@ class Connection:
|
|||
self.socket.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
@staticmethod
|
||||
def tohex(data):
|
||||
return "".join((hex(b) for b in data))
|
||||
|
||||
|
||||
def authenticate(self, username, password):
|
||||
challenge = self.sendReceive("world.getBlock",0,0,0)
|
||||
if challenge.startswith("security.challenge "):
|
||||
|
@ -84,44 +85,40 @@ class Connection:
|
|||
e = "Drained Data: <%s>\n"%data.strip()
|
||||
e += "Last Message: <%s>\n"%self.lastSent.strip()
|
||||
sys.stderr.write(e)
|
||||
|
||||
|
||||
def send(self, f, *data):
|
||||
"""Sends data. Note that a trailing newline '\n' is added here"""
|
||||
s = "%s(%s)\n"%(f, flatten_parameters_to_string(data))
|
||||
#print "f,data:",f,data
|
||||
self.drain()
|
||||
self.lastSent = s
|
||||
self.socket.sendall(s)
|
||||
|
||||
def send_python3(self, f, *data):
|
||||
"""Sends data. Note that a trailing newline '\n' is added here"""
|
||||
s = "%s(%s)\n"%(f, flatten_parameters_to_string(data))
|
||||
#print "f,data:",f,data
|
||||
s = "%s(%s)\n" % (f, flatten_parameters_to_string(data))
|
||||
self.drain()
|
||||
self.lastSent = s
|
||||
self.socket.sendall(s.encode("utf-8"))
|
||||
self.socket.sendall(s.encode(self.encoding))
|
||||
|
||||
def send_flat(self, f, data):
|
||||
"""Sends data. Note that a trailing newline '\n' is added here"""
|
||||
# print "f,data:",f,list(data)
|
||||
s = "%s(%s)\n"%(f, ",".join(data))
|
||||
s = "%s(%s)\n" % (f, ",".join(data))
|
||||
self.drain()
|
||||
self.lastSent = s
|
||||
self.socket.sendall(s)
|
||||
|
||||
def send_flat_python3(self, f, data):
|
||||
"""Sends data. Note that a trailing newline '\n' is added here"""
|
||||
# print "f,data:",f,list(data)
|
||||
s = "%s(%s)\n"%(f, ",".join(data))
|
||||
s = "%s(%s)\n" % (f, ",".join(data))
|
||||
self.drain()
|
||||
self.lastSent = s
|
||||
self.socket.sendall(s.encode("utf-8"))
|
||||
self.socket.sendall(s.encode(self.encoding))
|
||||
|
||||
def receive(self):
|
||||
"""Receives data. Note that the trailing newline '\n' is trimmed"""
|
||||
s = self.readFile.readline().rstrip("\n")
|
||||
if s == Connection.RequestFailed:
|
||||
raise RequestError("%s failed"%self.lastSent.strip())
|
||||
raise RequestError("%s failed" % self.lastSent.strip())
|
||||
return s
|
||||
|
||||
def sendReceive(self, *data):
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from vec3 import Vec3
|
||||
from . vec3 import Vec3
|
||||
|
||||
class BlockEvent:
|
||||
"""An Event related to blocks (e.g. placed, removed, hit)"""
|
||||
|
|
|
@ -1,39 +1,77 @@
|
|||
import time, random, math, os
|
||||
import connection
|
||||
import blocklist as bl
|
||||
from util import *
|
||||
from event import *
|
||||
from . import connection
|
||||
from . import blocklist as bl
|
||||
from . util import *
|
||||
from . event import *
|
||||
|
||||
LIBRARY_VERSION = 0.6
|
||||
|
||||
# Wait for connection
|
||||
wait_for_conn = True
|
||||
while wait_for_conn:
|
||||
try:
|
||||
conn = connection.Connection("localhost", 4711)
|
||||
except:
|
||||
print("Waiting for connection...")
|
||||
time.sleep(1)
|
||||
else:
|
||||
wait_for_conn = False
|
||||
conn = None
|
||||
player = None
|
||||
|
||||
# Find the player
|
||||
# players = mc.getPlayerEntityIds()
|
||||
|
||||
# Wait for at least one player
|
||||
def connect_server(host="localhost", port=4711):
|
||||
""" This function connect to a server on a specific port and wait until at
|
||||
least a client is connected.
|
||||
|
||||
wait_for_player = True
|
||||
while wait_for_player:
|
||||
try:
|
||||
ids = conn.sendReceive("world.getPlayerIds")
|
||||
except:
|
||||
print("Waiting for a player to connect...")
|
||||
time.sleep(1)
|
||||
else:
|
||||
wait_for_player = False
|
||||
Parameters:
|
||||
|
||||
players = map(int, ids.split("|"))
|
||||
player = players[0]
|
||||
- host (string) ip or domain name of the server
|
||||
- port (int) port on which the server is waiting for
|
||||
connection
|
||||
|
||||
Returns:
|
||||
|
||||
A tuple composed of the connection handle and the player id
|
||||
|
||||
Examples:
|
||||
|
||||
> (conn, player) = connect_server()
|
||||
|
||||
connect to localhost:4711 (default server address:port configuration)
|
||||
|
||||
> (conn, player) = connect_server("localhost", 4711)
|
||||
|
||||
Same effect of the pervious example but with explicit parameters
|
||||
specification
|
||||
|
||||
> (conn, player) = connect_server(port=4712)
|
||||
|
||||
We are connecting on localhost on an alternate port readnumber
|
||||
|
||||
"""
|
||||
# Wait for connection
|
||||
wait_for_conn = True
|
||||
while wait_for_conn:
|
||||
try:
|
||||
conn = connection.Connection(host, port)
|
||||
except:
|
||||
print("Waiting for connection...")
|
||||
time.sleep(1)
|
||||
else:
|
||||
wait_for_conn = False
|
||||
|
||||
# Find the player
|
||||
# players = mc.getPlayerEntityIds()
|
||||
|
||||
# Wait for at least one player
|
||||
|
||||
wait_for_player = True
|
||||
while wait_for_player:
|
||||
try:
|
||||
ids = conn.sendReceive("world.getPlayerIds")
|
||||
except:
|
||||
print("Waiting for a player to connect...")
|
||||
time.sleep(1)
|
||||
else:
|
||||
wait_for_player = False
|
||||
|
||||
players = list(map(int, ids.split("|")))
|
||||
player = players[0]
|
||||
return (conn, player)
|
||||
|
||||
|
||||
conn, player = connect_server()
|
||||
|
||||
# BLOCKS
|
||||
air = bl.AIR.id
|
||||
|
@ -1085,3 +1123,4 @@ class Turtle:
|
|||
|
||||
def round_vec3(self, position):
|
||||
return Vec3(int(position.x), int(position.y), int(position.z))
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import collections
|
||||
import math
|
||||
import copy
|
||||
from vec3 import Vec3
|
||||
from . vec3 import Vec3
|
||||
|
||||
def intFloor(*args):
|
||||
return [int(math.floor(x)) for x in flatten(args)]
|
||||
|
||||
def flatten(l):
|
||||
for e in l:
|
||||
if isinstance(e, collections.Iterable) and not isinstance(e, basestring):
|
||||
if isinstance(e, collections.Iterable) and not isinstance(e, str):
|
||||
for ee in flatten(e): yield ee
|
||||
else: yield e
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from pycraft_minetest import *
|
||||
import datetime
|
||||
|
||||
ov = 0
|
||||
mv = 0
|
||||
sv = 0
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from pycraft_minetest import *
|
||||
import time
|
||||
|
||||
# Forever
|
||||
while True:
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from pycraft_minetest import *
|
||||
import time
|
||||
|
||||
(conn, player) = connect_server()
|
||||
|
||||
while True:
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
from pycraft_minetest import *
|
||||
|
||||
connect_server()
|
||||
|
||||
chat("Hello Minecraft!")
|
||||
block(stone, 192, 63, 73, absolute=True)
|
||||
|
||||
|
|
Loading…
Reference in New Issue