def test_world_say(): with mock.patch('picraft.world.Connection') as c: World().say('Hello world!') c().send.assert_called_once_with('chat.post(Hello world!)') c().send.reset_mock() World().say('Hello\nworld!') c().send.assert_any_call('chat.post(Hello)') c().send.assert_any_call('chat.post(world!)')
def test_checkpoint_restore(): with mock.patch('picraft.world.Connection') as c: c().server_version = 'minecraft-pi' World().checkpoint.restore() c().send.assert_called_once_with('world.checkpoint.restore()') c().server_version = 'raspberry-juice' with pytest.raises(NotSupported): World().checkpoint.restore()
def test_world_height_get(): with mock.patch('picraft.world.Connection') as c: World().height[Vector(1, 2, 3)] c().transact.assert_called_once_with('world.getHeight(1,3)') c().transact.reset_mock() v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) World().height[v_from:v_to] for v in vector_range(v_from, v_to): c().transact.assert_any_call('world.getHeight(%d,%d)' % (v.x, v.z))
def test_checkpoint_context(): with mock.patch('picraft.world.Connection') as c: c().server_version = 'minecraft-pi' with World().checkpoint: pass c().send.assert_called_with('world.checkpoint.save()') try: with World().checkpoint: c().send.assert_called_with('world.checkpoint.save()') raise ValueError() except ValueError: c().send.assert_called_with('world.checkpoint.restore()')
def randommove_player(mc, x, y, z, ip): w = World("192.168.7.%s" % ip, 4711) #rotation = mc.player.getRotation() mc.postToChat("You are going to be moved in:") mc.postToChat("5") sleep(1) mc.postToChat("4") sleep(1) mc.postToChat("3") sleep(1) mc.postToChat("2") sleep(1) mc.postToChat("1") sleep(1) for i in range(0, 10): newx = x + (random.randint(1, 20)) newy = y + (random.randint(1, 20)) newz = z + (random.randint(1, 20)) mc.player.setPos(newx, newy, newz) mc.postToChat("x = %s, y = %s, z = %s" % (str(newx), str(newy), str(newz))) #mc.camera.setNormal(random.randint(0, 2)) w.camera.third_person(w.player) sleep(0.5) w.camera.first_person(w.player)
def main(): #API setup world = World() while True: #get recent sword hits hits = world.events.poll() for hit in hits: currenthit = hit.pos operation(world, currenthit)
def __init__(self, origin=V(), size=(12, 8)): self.camera = picamera.PiCamera() self.camera.resolution = (64, int(64 / size[0] * size[1])) self.camera.framerate = 2 self.world = World(ignore_errors=True) self.origin = origin self.size = V(0, size[1], size[0]) self.button_vec = None self.screen = MinecraftTVScreen(self.world, origin + V(0, 1, 1), (size[0] - 2, size[1] - 2))
def test_world_nametags(): with mock.patch('picraft.world.Connection') as c: w = World() with pytest.raises(AttributeError): w.nametags_visible c().server_version = 'minecraft-pi' w.nametags_visible = False c().send.assert_called_once_with('world.setting(nametags_visible,0)') c().server_version = 'raspberry-juice' with pytest.raises(NotSupported): w.nametags_visible = False
def test_world_objects(): with mock.patch('picraft.world.Connection') as c: w = World() assert w.connection is c() assert isinstance(w.players, picraft.player.Players) assert isinstance(w.player, picraft.player.HostPlayer) assert isinstance(w.blocks, picraft.block.Blocks) assert isinstance(w.height, picraft.world.WorldHeight) assert isinstance(w.camera, picraft.world.Camera) assert isinstance(w.checkpoint, picraft.world.Checkpoint) assert isinstance(w.events, picraft.events.Events)
def test_camera_3rd_person(): with mock.patch('picraft.world.Connection') as c: w = World() c().transact.return_value = '1|2|3' c().server_version = 'minecraft-pi' w.camera.third_person(w.player) c().send.assert_called_with('camera.mode.setFollow()') w.camera.third_person(w.players[3]) c().send.assert_called_with('camera.mode.setFollow(3)') c().server_version = 'raspberry-juice' with pytest.raises(NotSupported): w.camera.third_person(w.player)
def test_camera_pos(): with mock.patch('picraft.world.Connection') as c: w = World() with pytest.raises(AttributeError): w.camera.pos c().server_version = 'minecraft-pi' w.camera.pos = Vector(1, 2, 3) assert c().send.mock_calls == [ mock.call('camera.mode.setFixed()'), mock.call('camera.setPos(1,2,3)'), ] c().server_version = 'raspberry-juice' with pytest.raises(NotSupported): w.camera.pos = Vector(1, 2, 3)
def main(): #API setup world = World() while True: #get recent sword hits hits = world.events.poll() for hit in hits: #get position and orientation for stairs position = hit.pos data = face_to_dataval(hit.face) #this function needs to be made #call the building function if data != -1: operation(world, position, data)
def main(): #API setup world = World() while True: #get recent sword hits hits = world.events.poll() for hit in hits: #get rotation position = hit.pos data = face_to_dataval(hit.face) #call the building function if data != -1: operation(world, position, data)
from __future__ import unicode_literals import time from picraft import World, Vector, Block from collections import deque world = World(ignore_errors=True) world.say('Auto-bridge active') try: bridge = deque() last_pos = None while True: this_pos = world.player.pos if last_pos is not None: # Has the player moved more than 0.1 units in a horizontal direction? movement = (this_pos - last_pos).replace(y=0.0) if movement.magnitude > 0.1: # Find the next tile they're going to step on next_pos = (this_pos + movement.unit).floor() - Vector(y=1) if world.blocks[next_pos] == Block('air'): with world.connection.batch_start(): bridge.append(next_pos) world.blocks[next_pos] = Block('diamond_block') while len(bridge) > 10: world.blocks[bridge.popleft()] = Block('air') last_pos = this_pos time.sleep(0.01) except KeyboardInterrupt: world.say('Auto-bridge deactivated') with world.connection.batch_start(): while bridge:
#Load the picraft API from picraft import World, Block, Vector #Connect API to the world world = World() ############ Write your program below ############# while True: #Get the player position position = world.player.tile_pos #lower the position below the player position -= Vector(y=1) #place a block there world.blocks[position] = Block('gold_block')
def test_world_context(): with mock.patch('picraft.world.Connection') as c: with World(): pass c().close.assert_called_once_with()
def main(): #API setup world = World() while True:
def test_world_init(): with mock.patch('picraft.world.Connection') as c: World() c.assert_called_once_with('localhost', 4711, 0.3, False)
o = self.origin self.world.blocks[o:o + self.size + 1] = Block('#ffffff') self.world.blocks[o + V(0, 1, 1):o + self.size - V(0, 2, 2) + 1] = Block('#000000') self.button_pos = o + V(z=3) self.quit_pos = o + V(z=1) self.world.blocks[self.button_pos] = Block('#0080ff') self.world.blocks[self.quit_pos] = Block('#800000') self.world.say('Behold the Minecraft TV!') def destroy_tv(self): o = self.origin self.world.blocks[o:o + self.size + 1] = Block('air') def switch_on(self): self.world.say('Switching TV on') self.camera.start_recording(self.screen, format='mjpeg') def switch_off(self): self.world.say('Switching TV off') self.camera.stop_recording() o = self.origin self.world.blocks[o + V(0, 1, 1):o + self.size - V(0, 2, 2) + 1] = Block('#000000') with World() as world: p = world.player.tile_pos tv = MinecraftTV(world, origin=p + 8 * X + 2 * Y, size=(20, 14)) tv.main_loop()
from picraft import World, Model, Block print('Loading model airboat.obj') m = Model('airboat.obj') print('Model has the following materials:') print('\n'.join(s or '<None>' for s in m.materials)) materials_map = { None: Block('stone'), 'bluteal': Block('diamond_block'), 'bronze': Block('gold_block'), 'dkdkgrey': Block('#404040'), 'dkteal': Block('#000080'), 'red': Block('#ff0000'), 'silver': Block('#ffffff'), 'black': Block('#000000'), } with World() as w: with w.connection.batch_start(): for v, b in m.render(materials=materials_map).items(): w.blocks[v] = b
from picraft import World, Vector, Block, O, X, Y, Z, lines def polygon(sides, center=O, radius=5): angle = 2 * math.pi / sides for side in range(sides): yield Vector(center.x + radius * math.cos(side * angle), center.y + radius * math.sin(side * angle), center.z).round() def shapes(center=O): for sides in range(3, 9): yield lines(polygon(sides, center=center)) w = World() for shape in shapes(w.player.tile_pos + 15 * Y + 10 * Z): # Copy the generator into a list so we can re-use # the coordinates shape = list(shape) # Draw the shape with w.connection.batch_start(): for p in shape: w.blocks[p] = Block('gold_block') sleep(0.5) # Wipe the shape with w.connection.batch_start(): for p in shape: w.blocks[p] = Block('air')