Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem of unsupported gbk encoding #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions mcpi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mcpi/minecraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down