Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ed6a196
Fix block.py and add test for file
hashbangstudio Oct 1, 2014
ff8a464
Added missing enum34 dependency
doismellburning Oct 1, 2014
4d18907
Rename test to indicate not py.test form
hashbangstudio Oct 1, 2014
d6db56f
Revert "Added missing enum34 dependency"
doismellburning Oct 1, 2014
9a1edc7
Remove runner code in test
hashbangstudio Oct 1, 2014
b69f607
Remove block ordering comparisons
hashbangstudio Oct 1, 2014
9182af3
Remove ordered comparison tests
hashbangstudio Oct 1, 2014
1c6ec04
Undo unintentional indent change
hashbangstudio Oct 1, 2014
810f99d
Update setup.py to inline with master
hashbangstudio Oct 1, 2014
728b943
Fix comparison and typo
hashbangstudio Oct 1, 2014
acf013c
Use PEP8 method names and clarify enum test
hashbangstudio Oct 1, 2014
bed58c6
Fix missing func def and class naming
hashbangstudio Oct 1, 2014
11bb748
Hopefully correct unintentional indent change
hashbangstudio Oct 1, 2014
c8dad24
Move to PEP8 compliant naming
hashbangstudio Oct 2, 2014
2d3500c
Move to py.test style
hashbangstudio Oct 2, 2014
3c1fd01
Rename for clarity and PEP8
hashbangstudio Oct 2, 2014
f42e1f3
Add eval repr test and use constant string
hashbangstudio Oct 2, 2014
ffb7ca3
Merge remote-tracking branch 'upstream/block-update' into block-update
hashbangstudio Oct 19, 2014
b677dc6
Merge remote-tracking branch 'upstream/master' into block-update
hashbangstudio Oct 19, 2014
a3e3a00
Fix conflict between files
hashbangstudio Oct 19, 2014
8cb35c0
Add new block elements
hashbangstudio Oct 19, 2014
23f43fe
Remove default type assignment
hashbangstudio Oct 19, 2014
2f58179
Also remove the comment line
hashbangstudio Oct 19, 2014
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
69 changes: 49 additions & 20 deletions minecraft/block.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,71 @@
import sys
if sys.version_info < (3, 4):
from flufl.enum import Enum
else:
from enum import Enum
from enum import IntEnum
from functools import total_ordering


@total_ordering
class Block:
"""
Minecraft PI block description. Can be sent to Minecraft.setBlock/s
block.type = the blockID of a block (It's meterial)
block.data = A unknown member that does something
The default type for blocks is dirt
"""
Minecraft PI block description. Can be sent to Minecraft.setBlock/s
block.type = the blockID of a block (It's material)
block.data = The variant of the type of block.
For example the colour of wool or the orientation of stairs
The default type for blocks is dirt
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the indentation change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is unnecessary, can you keep the indentation of docstrings to one level in line with PEP 257 please.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghickman Got the pep8/flake8 skillz to add this to tests so you don't need to keep hand-PEPing? ;P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unintentional will alter

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hashbangstudio – Thanks!


def __init__(self, type=3, data=0):
self.type = type
self.data = data

def __cmp__(self, rhs):
return hash(self) - hash(rhs)
def __eq__(self, rhs):
"""
Equality override
Two blocks are equal only if their hashes are equal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that's only true in this case where hash doesn't collide, and not generally true - would you mind making a proper __eq__ please?

"""
return hash(self) == hash(rhs)

def __lt__(self, rhs):
"""
Less than override
One block is less than another if hash is less
"""
return hash(self) < hash(rhs)

def __hash__(self):
return (self.id << 8) + self.data
"""
Override of hash generation
Returns a unique representation of contents of block
"""
return (self.type << 8) + self.data

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this changes a bit of the API we don't want to change, but we aren't testing thoroughly enough :s

# TODO: This looks wierd, does it have any use?
def __iter__(self):
"""Allows a Block to be sent whenever id [and data] is needed"""
return iter((self.id, self.data))
"""
Returns an Iterator of the contents of the Block class
Makes the Block an Iterable object
This means that Block can be treated like a list/tuple in places
list/tuple of type and data
For example when flattening lists or *args
Allows a Block to be sent whenever id [and data] is needed
"""
return iter((self.type, self.data))

def __repr__(self):
return 'Block({}, {:.2f})'.format(self.id, self.data)
""" Override string representation """
return 'Block({:d}, {:d})'.format(self.type, self.data)

# TODO: find out if this has any use
def withData(self, data):
return Block(self.id, data)
"""
Returns a new block with the same type as the invoking block but
with the passed in data value
"""
return Block(self.type, data)

@property
def id(self):
""" Here for backwards compatibility for previous attribute name """
return self.type


class blockType(Enum):
class blockType(IntEnum):
AIR = 0
STONE = 1
GRASS = 2
Expand Down
130 changes: 130 additions & 0 deletions tests/test_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import unittest
from minecraft.block import Block
from minecraft.block import blockType


class TestBlock(unittest.TestCase):

def testRepresentation(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not just testing that "the string format we hardcoded over there is the same we hardcoded over here"?

I'm not sure about the existence/state of QuickCheck for Python, but perhaps lets (attempt to) instead validate the repr/eval identity?

# Test repr
b = Block(2, 8)
expectedString = "Block({:d}, {:d})".format(b.type, b.data)
rep = repr(b)
self.assertEqual(rep, expectedString)

def testInstantiationAndWithDataFunction(self):
blck = Block(12)
self.assertEqual(blck.type, 12)
self.assertEqual(blck.data, 0)
blckWithData = Block(12, 4)
self.assertEqual(blckWithData.type, 12)
self.assertEqual(blckWithData.data, 4)
otherBlckWithData = blck.withData(8)
self.assertEqual(otherBlckWithData.type, 12)
self.assertEqual(otherBlckWithData.data, 8)

def testBackwardsCompatibility(self):
blck = Block(12)
self.assertEqual(blck.type, blck.id)

def testComparison(self):
b1 = Block(8, 3)
bSame = Block(8, 3)
bDiffId = Block(12, 3)
bDiffData = Block(8, 7)
bDiffIdAndData = Block(51, 7)

self.assertTrue(b1 == b1)
self.assertTrue(b1 == bSame)
self.assertTrue(b1 != bDiffId)
self.assertTrue(b1 != bDiffData)
self.assertTrue(b1 != bDiffIdAndData)

self.assertTrue(bDiffId > b1)
self.assertTrue(bDiffData > b1)
self.assertTrue(bDiffIdAndData > b1)

def testIteration(self):
idAndData = [35, 4]
b = Block(idAndData[0], idAndData[1])
for index, attr in enumerate(b):
self.assertEqual(attr, idAndData[index])

def testBlockConstants(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be alone here, but I'm unconvinced by the value of testing constants - persuade me...?

self.assertEqual(blockType.AIR, 0)
self.assertEqual(blockType.STONE, 1)
self.assertEqual(blockType.GRASS, 2)
self.assertEqual(blockType.DIRT, 3)
self.assertEqual(blockType.COBBLESTONE, 4)
self.assertEqual(blockType.WOOD_PLANKS, 5)
self.assertEqual(blockType.SAPLING, 6)
self.assertEqual(blockType.BEDROCK, 7)
self.assertEqual(blockType.WATER_FLOWING, 8)
self.assertEqual(blockType.WATER, blockType.WATER_FLOWING)
self.assertEqual(blockType.WATER_STATIONARY, 9)
self.assertEqual(blockType.LAVA_FLOWING, 10)
self.assertEqual(blockType.LAVA, blockType.LAVA_FLOWING)
self.assertEqual(blockType.LAVA_STATIONARY, 11)
self.assertEqual(blockType.SAND, 12)
self.assertEqual(blockType.GRAVEL, 13)
self.assertEqual(blockType.GOLD_ORE, 14)
self.assertEqual(blockType.IRON_ORE, 15)
self.assertEqual(blockType.COAL_ORE, 16)
self.assertEqual(blockType.WOOD, 17)
self.assertEqual(blockType.LEAVES, 18)
self.assertEqual(blockType.GLASS, 20)
self.assertEqual(blockType.LAPIS_LAZULI_ORE, 21)
self.assertEqual(blockType.LAPIS_LAZULI_BLOCK, 22)
self.assertEqual(blockType.SANDSTONE, 24)
self.assertEqual(blockType.BED, 26)
self.assertEqual(blockType.COBWEB, 30)
self.assertEqual(blockType.GRASS_TALL, 31)
self.assertEqual(blockType.WOOL, 35)
self.assertEqual(blockType.FLOWER_YELLOW, 37)
self.assertEqual(blockType.FLOWER_CYAN, 38)
self.assertEqual(blockType.MUSHROOM_BROWN, 39)
self.assertEqual(blockType.MUSHROOM_RED, 40)
self.assertEqual(blockType.GOLD_BLOCK, 41)
self.assertEqual(blockType.IRON_BLOCK, 42)
self.assertEqual(blockType.STONE_SLAB_DOUBLE, 43)
self.assertEqual(blockType.STONE_SLAB, 44)
self.assertEqual(blockType.BRICK_BLOCK, 45)
self.assertEqual(blockType.TNT, 46)
self.assertEqual(blockType.BOOKSHELF, 47)
self.assertEqual(blockType.MOSS_STONE, 48)
self.assertEqual(blockType.OBSIDIAN, 49)
self.assertEqual(blockType.TORCH, 50)
self.assertEqual(blockType.FIRE, 51)
self.assertEqual(blockType.STAIRS_WOOD, 53)
self.assertEqual(blockType.CHEST, 54)
self.assertEqual(blockType.DIAMOND_ORE, 56)
self.assertEqual(blockType.DIAMOND_BLOCK, 57)
self.assertEqual(blockType.CRAFTING_TABLE, 58)
self.assertEqual(blockType.FARMLAND, 60)
self.assertEqual(blockType.FURNACE_INACTIVE, 61)
self.assertEqual(blockType.FURNACE_ACTIVE, 62)
self.assertEqual(blockType.DOOR_WOOD, 64)
self.assertEqual(blockType.LADDER, 65)
self.assertEqual(blockType.STAIRS_COBBLESTONE, 67)
self.assertEqual(blockType.DOOR_IRON, 71)
self.assertEqual(blockType.REDSTONE_ORE, 73)
self.assertEqual(blockType.SNOW, 78)
self.assertEqual(blockType.ICE, 79)
self.assertEqual(blockType.SNOW_BLOCK, 80)
self.assertEqual(blockType.CACTUS, 81)
self.assertEqual(blockType.CLAY, 82)
self.assertEqual(blockType.SUGAR_CANE, 83)
self.assertEqual(blockType.FENCE, 85)
self.assertEqual(blockType.GLOWSTONE_BLOCK, 89)
self.assertEqual(blockType.BEDROCK_INVISIBLE, 95)
self.assertEqual(blockType.STONE_BRICK, 98)
self.assertEqual(blockType.GLASS_PANE, 102)
self.assertEqual(blockType.MELON, 103)
self.assertEqual(blockType.FENCE_GATE, 107)
self.assertEqual(blockType.GLOWING_OBSIDIAN, 246)
self.assertEqual(blockType.NETHER_REACTOR_CORE, 247)


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestBlock)
unittest.TextTestRunner(verbosity=2).run(suite)