Ejemplo n.º 1
0
    def open_hook(self, player, container, block):
        """
        The ``player`` is a Player's protocol
        The ``container`` is a 0x64 message
        The ``block`` is a block we trying to open
        :returns: None or window object
        """
        if block != blocks["chest"].slot:
            returnValue(None)

        bigx, smallx, bigz, smallz = split_coords(container.x, container.z)
        chunk = yield self.factory.world.request_chunk(bigx, bigz)

        chests_around = chestsAround(self.factory,
                                     (container.x, container.y, container.z))
        chests_around_num = len(chests_around)

        if chests_around_num == 0:  # small chest
            chest = self.get_chest_tile(chunk, (smallx, container.y, smallz))
            if chest is None:
                returnValue(None)
            coords = bigx, smallx, bigz, smallz, container.y
            window = ChestWindow(player.wid, player.player.inventory,
                                 chest.inventory, coords)
        elif chests_around_num == 1:  # large chest
            # process second chest coordinates
            x2, y2, z2 = chests_around[0]
            bigx2, smallx2, bigz2, smallz2 = split_coords(x2, z2)
            if bigx == bigx2 and bigz == bigz2:
                # both chest blocks are in same chunk
                chunk2 = chunk
            else:
                chunk2 = yield self.factory.world.request_chunk(bigx2, bigz2)

            chest1 = self.get_chest_tile(chunk, (smallx, container.y, smallz))
            chest2 = self.get_chest_tile(chunk2,
                                         (smallx2, container.y, smallz2))
            if chest1 is None or chest2 is None:
                returnValue(None)
            c1 = bigx, smallx, bigz, smallz, container.y
            c2 = bigx2, smallx2, bigz2, smallz2, container.y
            # We shall properly order chest inventories
            if c1 < c2:
                window = LargeChestWindow(player.wid, player.player.inventory,
                                          chest1.inventory, chest2.inventory,
                                          c1)
            else:
                window = LargeChestWindow(player.wid, player.player.inventory,
                                          chest2.inventory, chest1.inventory,
                                          c2)
        else:
            log.msg("Chest at (%d, %d, %d) have three chests connected" %
                    (container.x, container.y, container.z))
            returnValue(None)

        player.windows.append(window)
        returnValue(window)
Ejemplo n.º 2
0
 def setUp(self):
     self.i = ChestWindow(1, Inventory(), ChestStorage(), 0)
Ejemplo n.º 3
0
class TestChestIntegration(unittest.TestCase):
    def setUp(self):
        self.i = ChestWindow(1, Inventory(), ChestStorage(), 0)

    def test_internals(self):
        self.assertEqual(self.i.metalist, [[None] * 27, [None] * 27, [None] * 9])

    def test_parameters(self):
        self.i.slots.title = "MyChest"
        self.assertEqual( self.i.slots_num, 27 )
        self.assertEqual( self.i.identifier, "chest" )
        self.assertEqual( self.i.title, "MyChest" )

    def test_dirty_slots_move(self):
        self.i.slots.storage[0] = Slot(2, 0, 1)
        self.i.slots.storage[2] = Slot(1, 0, 4)
        # simple move
        self.i.select(0)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {0 : None, 1 : (2, 0, 1)})

    def test_dirty_slots_split_and_stack(self):
        self.i.slots.storage[0] = Slot(2, 0, 1)
        self.i.slots.storage[2] = Slot(1, 0, 4)
        # split
        self.i.select(2, True)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {1 : (1, 0, 2), 2 : (1, 0, 2)})
        # stack
        self.i.select(2)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {1 : (1, 0, 4), 2 : None})

    def test_dirty_slots_move_stack(self):
        self.i.slots.storage[0] = Slot(2, 0, 1)
        self.i.select(0, False, True)
        self.assertEqual(self.i.dirty_slots, {0 : None})

    def test_dirty_slots_packaging(self):
        self.i.slots.storage[0] = Slot(1, 0, 1)
        self.i.select(0)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {0 : None, 1 : (1, 0, 1)})
        packets = self.i.packets_for_dirty(self.i.dirty_slots)
        self.assertEqual(packets, '\x67\x01\x00\x00\xff\xff' +\
                                  '\x67\x01\x00\x01\x00\x01\x01\x00\x00')
Ejemplo n.º 4
0
 def setUp(self):
     self.i = ChestWindow(1, Inventory(), ChestStorage(), 0)
Ejemplo n.º 5
0
class TestChestIntegration(unittest.TestCase):
    def setUp(self):
        self.i = ChestWindow(1, Inventory(), ChestStorage(), 0)

    def test_internals(self):
        self.assertEqual(self.i.metalist,
                         [[None] * 27, [None] * 27, [None] * 9])

    def test_parameters(self):
        self.i.slots.title = "MyChest"
        self.assertEqual(self.i.slots_num, 27)
        self.assertEqual(self.i.identifier, "chest")
        self.assertEqual(self.i.title, "MyChest")

    def test_dirty_slots_move(self):
        self.i.slots.storage[0] = Slot(2, 0, 1)
        self.i.slots.storage[2] = Slot(1, 0, 4)
        # simple move
        self.i.select(0)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {0: None, 1: (2, 0, 1)})

    def test_dirty_slots_split_and_stack(self):
        self.i.slots.storage[0] = Slot(2, 0, 1)
        self.i.slots.storage[2] = Slot(1, 0, 4)
        # split
        self.i.select(2, True)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {1: (1, 0, 2), 2: (1, 0, 2)})
        # stack
        self.i.select(2)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {1: (1, 0, 4), 2: None})

    def test_dirty_slots_move_stack(self):
        self.i.slots.storage[0] = Slot(2, 0, 1)
        self.i.select(0, False, True)
        self.assertEqual(self.i.dirty_slots, {0: None})

    def test_dirty_slots_packaging(self):
        self.i.slots.storage[0] = Slot(1, 0, 1)
        self.i.select(0)
        self.i.select(1)
        self.assertEqual(self.i.dirty_slots, {0: None, 1: (1, 0, 1)})
        packets = self.i.packets_for_dirty(self.i.dirty_slots)
        self.assertEqual(packets, '\x67\x01\x00\x00\xff\xff' +\
                                  '\x67\x01\x00\x01\x00\x01\x01\x00\x00')