Skip to content

Commit

Permalink
improve Block constructor and equality
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed May 31, 2017
1 parent f558561 commit ad4e57c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions mcpipy/board2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def setBlock(self, *args):
if 0 <= a[0] < self.width and 0 <= a[1] < self.height:
self.board[a[0]][a[1]] = a[2:]

def getBlock(self, x, y):
return Block(self.board[x][y])

def _to3d(self, x, y):
if self.horizontal:
return (self.left+x, self.plane, self.bottom-y)
Expand Down
37 changes: 30 additions & 7 deletions mcpipy/mcpi/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,41 @@ class Block:
MAX_MATERIAL = MATERIAL_ROUGH

def __init__(self, id, data=0, nbt=None):
self.id = id
self.data = data
if nbt is not None and len(nbt)==0:
self.nbt = None
else:
self.nbt = nbt
try:
if len(id) >= 1:
self.id = id[0]
if len(id) >= 2:
self.data = id[1]
if len(id) >= 3:
self.nbt = nbt
else:
self.nbt = None
except TypeError:
self.id = id
self.data = data
if nbt is not None and len(nbt)==0:
self.nbt = None
else:
self.nbt = nbt

def __getitem__(self, index):
if index < 0:
index += 3
if index == 0:
return self.id
elif index == 1:
return self.data
elif index == 2:
return self.nbt

def __len__(self):
return 3

def __eq__(self, rhs):
try:
return self.id == rhs.id and self.data == rhs.data and self.nbt == rhs.nbt
except:
return self.data == 0 and self.nbt is None and self.id == rhs
return self == Block(rhs)

def __ne__(self, rhs):
return not self.__eq__(rhs)
Expand Down

0 comments on commit ad4e57c

Please sign in to comment.