From ad4e57cb7b8f902b581c6d5a720c2801611f392b Mon Sep 17 00:00:00 2001 From: arpruss Date: Wed, 31 May 2017 13:36:07 -0500 Subject: [PATCH] improve Block constructor and equality --- mcpipy/board2d.py | 3 +++ mcpipy/mcpi/block.py | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/mcpipy/board2d.py b/mcpipy/board2d.py index 335a1c1..f9395d9 100755 --- a/mcpipy/board2d.py +++ b/mcpipy/board2d.py @@ -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) diff --git a/mcpipy/mcpi/block.py b/mcpipy/mcpi/block.py index c7ae7f5..090d2e8 100644 --- a/mcpipy/mcpi/block.py +++ b/mcpipy/mcpi/block.py @@ -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)