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.a = ChestStorage()
     self.b = ChestStorage()
     self.i = LargeChestWindow(1, Inventory(), self.a, self.b, 0)
Ejemplo n.º 3
0
class TestLargeChestIntegration(unittest.TestCase):
    def setUp(self):
        self.a = ChestStorage()
        self.b = ChestStorage()
        self.i = LargeChestWindow(1, Inventory(), self.a, self.b, 0)

    def test_internals(self):
        slot = self.i.slot_for_container(self.i.slots.storage, 0)
        self.assertEqual(slot, 0)
        slot = self.i.slot_for_container(self.i.slots.storage, 27)
        self.assertEqual(slot, 27)
        slot = self.i.slot_for_container(self.i.inventory.storage, 0)
        self.assertEqual(slot, 54)
        slot = self.i.slot_for_container(self.i.inventory.holdables, 0)
        self.assertEqual(slot, 81)

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

    def test_combining(self):
        self.a.storage[0] = Slot(1, 0, 1)
        self.b.storage[0] = Slot(2, 0, 1)
        self.assertEqual(self.i.slots.storage[0], (1, 0, 1))
        self.assertEqual(self.i.slots.storage[27], (2, 0, 1))

    def test_dirty_slots_move(self):
        self.a.storage[0] = Slot(1, 0, 1)
        # simple move
        self.i.select(0)
        self.i.select(53)
        self.assertEqual(self.a.storage[0], None)
        self.assertEqual(self.b.storage[26], (1, 0, 1))
        self.assertEqual(self.i.dirty_slots, {0 : None, 53 : (1, 0, 1)})

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

    def test_dirty_slots_move_stack(self):
        self.b.storage[3] = Slot(1, 0, 1)
        self.i.select(30, False, True)
        self.assertEqual(self.b.storage[3], None)
        self.assertEqual(self.i.dirty_slots, {30 : None})
        self.i.inventory.holdables[0] = Slot(2, 0, 1)
        self.i.select(81, False, True)
        self.assertEqual(self.i.inventory.holdables[0], None)
        self.assertEqual(self.a.storage[0], (2, 0, 1))

    def test_dirty_slots_packaging(self):
        self.a.storage[0] = Slot(1, 0, 1)
        self.i.select(0)
        self.i.select(53)
        self.assertEqual(self.i.dirty_slots, {0 : None, 53 : (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\x35\x00\x01\x01\x00\x00')
Ejemplo n.º 4
0
 def setUp(self):
     self.a = ChestStorage()
     self.b = ChestStorage()
     self.i = LargeChestWindow(1, Inventory(), self.a, self.b, 0)
Ejemplo n.º 5
0
class TestLargeChestIntegration(unittest.TestCase):
    def setUp(self):
        self.a = ChestStorage()
        self.b = ChestStorage()
        self.i = LargeChestWindow(1, Inventory(), self.a, self.b, 0)

    def test_internals(self):
        slot = self.i.slot_for_container(self.i.slots.storage, 0)
        self.assertEqual(slot, 0)
        slot = self.i.slot_for_container(self.i.slots.storage, 27)
        self.assertEqual(slot, 27)
        slot = self.i.slot_for_container(self.i.inventory.storage, 0)
        self.assertEqual(slot, 54)
        slot = self.i.slot_for_container(self.i.inventory.holdables, 0)
        self.assertEqual(slot, 81)

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

    def test_combining(self):
        self.a.storage[0] = Slot(1, 0, 1)
        self.b.storage[0] = Slot(2, 0, 1)
        self.assertEqual(self.i.slots.storage[0], (1, 0, 1))
        self.assertEqual(self.i.slots.storage[27], (2, 0, 1))

    def test_dirty_slots_move(self):
        self.a.storage[0] = Slot(1, 0, 1)
        # simple move
        self.i.select(0)
        self.i.select(53)
        self.assertEqual(self.a.storage[0], None)
        self.assertEqual(self.b.storage[26], (1, 0, 1))
        self.assertEqual(self.i.dirty_slots, {0: None, 53: (1, 0, 1)})

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

    def test_dirty_slots_move_stack(self):
        self.b.storage[3] = Slot(1, 0, 1)
        self.i.select(30, False, True)
        self.assertEqual(self.b.storage[3], None)
        self.assertEqual(self.i.dirty_slots, {30: None})
        self.i.inventory.holdables[0] = Slot(2, 0, 1)
        self.i.select(81, False, True)
        self.assertEqual(self.i.inventory.holdables[0], None)
        self.assertEqual(self.a.storage[0], (2, 0, 1))

    def test_dirty_slots_packaging(self):
        self.a.storage[0] = Slot(1, 0, 1)
        self.i.select(0)
        self.i.select(53)
        self.assertEqual(self.i.dirty_slots, {0: None, 53: (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\x35\x00\x01\x01\x00\x00')