Skip to content

Commit ad4e57c

Browse files
committed
improve Block constructor and equality
1 parent f558561 commit ad4e57c

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

mcpipy/board2d.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def setBlock(self, *args):
8181
if 0 <= a[0] < self.width and 0 <= a[1] < self.height:
8282
self.board[a[0]][a[1]] = a[2:]
8383

84+
def getBlock(self, x, y):
85+
return Block(self.board[x][y])
86+
8487
def _to3d(self, x, y):
8588
if self.horizontal:
8689
return (self.left+x, self.plane, self.bottom-y)

mcpipy/mcpi/block.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,41 @@ class Block:
1515
MAX_MATERIAL = MATERIAL_ROUGH
1616

1717
def __init__(self, id, data=0, nbt=None):
18-
self.id = id
19-
self.data = data
20-
if nbt is not None and len(nbt)==0:
21-
self.nbt = None
22-
else:
23-
self.nbt = nbt
18+
try:
19+
if len(id) >= 1:
20+
self.id = id[0]
21+
if len(id) >= 2:
22+
self.data = id[1]
23+
if len(id) >= 3:
24+
self.nbt = nbt
25+
else:
26+
self.nbt = None
27+
except TypeError:
28+
self.id = id
29+
self.data = data
30+
if nbt is not None and len(nbt)==0:
31+
self.nbt = None
32+
else:
33+
self.nbt = nbt
34+
35+
def __getitem__(self, index):
36+
if index < 0:
37+
index += 3
38+
if index == 0:
39+
return self.id
40+
elif index == 1:
41+
return self.data
42+
elif index == 2:
43+
return self.nbt
44+
45+
def __len__(self):
46+
return 3
2447

2548
def __eq__(self, rhs):
2649
try:
2750
return self.id == rhs.id and self.data == rhs.data and self.nbt == rhs.nbt
2851
except:
29-
return self.data == 0 and self.nbt is None and self.id == rhs
52+
return self == Block(rhs)
3053

3154
def __ne__(self, rhs):
3255
return not self.__eq__(rhs)

0 commit comments

Comments
 (0)