diff --git a/mcpi/connection.py b/mcpi/connection.py index fc30594..8c22d7e 100644 --- a/mcpi/connection.py +++ b/mcpi/connection.py @@ -12,10 +12,11 @@ class Connection: """Connection to a Minecraft Pi game""" RequestFailed = "Fail" - def __init__(self, address, port): + def __init__(self, address, port, encoding): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((address, port)) self.lastSent = "" + self.encoding = encoding def drain(self): """Drains the socket of incoming data""" @@ -51,7 +52,7 @@ def _send(self, s): def receive(self): """Receives data. Note that the trailing newline '\n' is trimmed""" - s = self.socket.makefile("r").readline().rstrip("\n") + s = self.socket.makefile("r", encoding=self.encoding).readline().rstrip("\n") if s == Connection.RequestFailed: raise RequestError("%s failed"%self.lastSent.strip()) return s diff --git a/mcpi/minecraft.py b/mcpi/minecraft.py index 5d87f70..9114e4f 100644 --- a/mcpi/minecraft.py +++ b/mcpi/minecraft.py @@ -372,8 +372,8 @@ def removeEntities(self, typeId=-1): return int(self.conn.sendReceive(b"world.removeEntities", typeId)) @staticmethod - def create(address = "localhost", port = 4711): - return Minecraft(Connection(address, port)) + def create(address = "localhost", port = 4711, encoding = "utf-8"): + return Minecraft(Connection(address, port, encoding)) if __name__ == "__main__":