def run_client(name: str, lifespan: float, acts: Tuple[Act, ...]): start_time = time.time() try: with connect('127.0.0.1', player_name=name, client_factory=_Client, timeout=30) as client: try: while client.is_active and time.time() - start_time < lifespan: act = select(acts) act.callable(client) finally: remove_all_mobs(client) except KeyboardInterrupt: pass
def test_tcp_perform_action(self): with ServerProcess(self._handler, tcp_server(self._handler)) as server: with connect('127.0.0.1', player_name='Taro') as client: # type: Client self._test_perform_action(client) server.stop()
def test_tcp_command_request(self): with ServerProcess(self._handler, tcp_server(self._handler)) as server: with connect('127.0.0.1', player_name='Taro') as client: # type: Client self._test_command_request(client) server.stop()
entity_runtime_id: EntityRuntimeID, data: int = 0) -> None: self.client.execute_command('/entity_event {} {} {}'.format( event_type.name.lower(), entity_runtime_id, data)) class GameEventCommandClient(MCPEClient, GameEventCommandMixin): @property def client(self) -> MCPEClient: return self if __name__ == '__main__': import time with connect('127.0.0.1', client_factory=GameEventCommandClient) as _client: _entity = _client.get_entity() for e in _client.entities: if e.entity_runtime_id != _client.entity_runtime_id: _entity = e break else: print('Login by other player.') _client.sound_event(SoundType.HURT, _entity.position) time.sleep(1) _client.space_event(SpaceEventType.SOUND_GHAST, _entity.position) time.sleep(1)
Vector3(east, height, south), pitch, yaw, head_yaw, on_ground) def remove_mob(self, mob_eid: EntityRuntimeID): self.perform_action(ActionType.REMOVE_MOB, mob_eid) class ActionCommandClient(MCPEClient, ActionCommandMixin): @property def client(self) -> MCPEClient: return self if __name__ == '__main__': with connect('127.0.0.1', player_name='Taro', client_factory=ActionCommandClient) as _client: print('Number of entities', len(_client.entities)) _client.move_player(10.0, 0.0, 10.0, is_teleport=True) _client.wait_response(timeout=1) print('Player', _client.get_entity()) _position = _client.get_entity().position + (1, 0, -1) _client.move_player(*_position) _client.wait_response(timeout=1) print('Player', _client.get_entity()) _height = _client.get_height(*_position.to_2d()) print('Height', _height, 'at', _position.to_2d())
def run(): with connect('127.0.0.1', player_name='', client_factory=_Client, timeout=30) as client: player = client.get_entity() mobs = [] def mob_added(event: EntityEvent, info: EntityInfo): if event is EntityEvent.ADDED and info.owner_runtime_id == player.entity_runtime_id: mobs.append(info.entity_runtime_id) client.set_entity_event_listener(mob_added) base_position = player.position + (0, 0, 2) client.spawn_mob(EntityType.CHICKEN, base_position.x, base_position.y, base_position.z) client.spawn_mob(EntityType.CHICKEN, base_position.x, base_position.y, base_position.z) client.spawn_mob(EntityType.CHICKEN, base_position.x, base_position.y, base_position.z) while len(mobs) != 3: if not client.wait_response(10): raise AssertionError('timeout') def move(index: int, x: float = 0.0, y: float = 0.0, z: float = 0.0, pitch: float = 0.0, yaw: float = 0.0, head_yaw: float = 0.0, on_ground: bool = True): entity = client.get_entity(mobs[index]) client.move_mob(mobs[index], *entity.position + (x, y, z), pitch=entity.pitch + pitch, yaw=entity.yaw + yaw, head_yaw=entity.head_yaw + head_yaw, on_ground=on_ground) def jump_up(y: float): move(0, y=y, on_ground=False) time.sleep(0.1) move(1, y=y, on_ground=False) time.sleep(0.1) move(2, y=y, on_ground=False) def jump_down(y: float): move(0, y=-y) time.sleep(0.1) move(1, y=-y) time.sleep(0.1) move(2, y=-y) def rotate(yaw: float): move(0, yaw=yaw) time.sleep(0.1) move(1, yaw=yaw) time.sleep(0.1) move(2, yaw=yaw) def circle(index: int, radius: float, step: int, start: Face): start_position = client.get_entity(mobs[index]).position o = start_position + start.inverse.direction * radius for i in range(step): degree = (i + 1) * (360 / step) + start.yaw x = radius * math.sin(math.radians(degree)) z = radius * math.cos(math.radians(degree)) client.move_mob(mobs[index], o.x - x, o.y, round(o.z + z, 1)) time.sleep(0.1) while client.get_entity(mobs[index]).position != start_position: client.wait_response(10) time.sleep(1) move(0, x=-2) move(2, x=+2) time.sleep(1) for _ in range(3): jump_up(2) time.sleep(0.1) jump_down(2) for _ in range(4): rotate(90) for _ in range(4): rotate(-90) circle(0, 1, 12, Face.WEST) circle(2, 1, 12, Face.EAST) circle(1, 1, 12, Face.SOUTH) rotate(180) time.sleep(1) jump_up(10) time.sleep(3) for mob_id in mobs: client.remove_mob(mob_id)