def create_data_store() -> DataStore: world_name = get_value(ConfigKey.WORLD_NAME) name = world_name.replace(' ', '_') seed = get_value(ConfigKey.SEED) suffix = ('p' if seed >= 0 else 'n') + str(seed) db_name = '{}-{}'.format(name, suffix) return _DataBase(db_name)
def __init__(self, handler: GameDataHandler) -> None: super().__init__(handler) self._sessions = {} # TODO session timeout self.server_id = 'MCPE;PyMineHub;160;1.2.7;0;20;{};{};{};'.format( self.guid, get_value(ConfigKey.WORLD_NAME), get_value(ConfigKey.GAME_MODE).title() )
async def _next_moment(self) -> None: start_time = time.time() self._world_extension.update(self._perform_action) if get_value(ConfigKey.SPAWN_MOB): self._update_mob() run_time = time.time() - start_time tick_time = get_value(ConfigKey.WORLD_TICK_TIME) if run_time < tick_time: await asyncio.sleep(tick_time - run_time) else: _logger.warning('Either plugin is too heavy. (time=%f)', round(run_time, 3))
def _process_move_mob(self, action: Action) -> None: mob = self._entity.get_mob(action.entity_runtime_id) if not mob.has_move_action: asyncio.get_event_loop().call_later( get_value(ConfigKey.WORLD_TICK_TIME), self._move_mob, action.entity_runtime_id) mob.push_move_action(action)
def _process_move_player(self, action: Action) -> None: player = self._entity.get_player(action.entity_runtime_id) if not player.has_move_action: asyncio.get_event_loop().call_later( get_value(ConfigKey.WORLD_TICK_TIME), self._move_player, action.entity_runtime_id) player.push_move_action(action)
def connect_raknet(client: Client, server_host: str, port: Optional[int] = None, timeout: float = 0) -> ClientConnection[Client]: server_address = (server_host, get_value(ConfigKey.RAKNET_SERVER_PORT) if port is None else port) return _RakNetClientConnection(client, server_address, timeout)
def run(store: DataStore, plugin: PluginLoader) -> WorldProxy: from pyminehub.mcpe.world.generator import BatchSpaceGenerator, OnDemandSpaceGenerator space_size = get_value(ConfigKey.INIT_SPACE) generator = OnDemandSpaceGenerator(plugin.generator, store) if space_size is None \ else BatchSpaceGenerator(plugin.generator, store, *space_size) world = _WorldProxyImpl(generator, store, plugin.world_extension, plugin.mob_processor, plugin.player_config) return world
def __init__(self, notify_time: Callable[[int], None]) -> None: self._notify_time = notify_time clock_time = get_value(ConfigKey.CLOCK_TIME) if clock_time is None: self._logic = _DynamicClock() else: if clock_time < 0: clock_time *= -1 self._notify_time = None self._logic = _StaticClock(clock_time)
def __init__(self, spec: EntitySpec, entity_unique_id: EntityUniqueID, entity_runtime_id: EntityRuntimeID) -> None: self._spec = spec self._entity_unique_id = entity_unique_id self._entity_runtime_id = entity_runtime_id self._spawn_position = Vector3(*get_value(ConfigKey.PLAYER_SPAWN_POSITION)) self._position = None self._pitch = 0.0 self._yaw = 0.0 self._head_yaw = 0.0 self._on_ground = True self._move_actions = [] # type: List[Action]
async def run_loop(self, interrupted: Callable[[], None]) -> None: try: if self._notify_time is None: await asyncio.Event().wait() # wait cancel return while True: start_time = time.time() self._logic.update() self._notify_time(self._logic.time) run_time = time.time() - start_time tick_time = get_value(ConfigKey.CLOCK_TICK_TIME) if run_time < tick_time: await asyncio.sleep(tick_time - run_time) except asyncio.CancelledError: pass except KeyboardInterrupt: interrupted()
def raknet_server(handler: GameDataHandler) -> Server: server_address = (get_unspecified_address(), get_value(ConfigKey.RAKNET_SERVER_PORT)) return _RakNetServer(handler, server_address)
def get_seed(self) -> int: return get_value(ConfigKey.SEED)
def get_game_mode(self) -> GameMode: return GameMode[get_value(ConfigKey.GAME_MODE)]
from collections import defaultdict from typing import Dict from pyminehub.config import ConfigKey, get_value from pyminehub.mcpe.network.packet import GamePacketType from pyminehub.network.handler import Reliability __all__ = ['UNRELIABLE', 'RELIABLE', 'DEFAULT_CHANEL', 'RELIABILITY_DICT'] UNRELIABLE = Reliability(False, None) RELIABLE = Reliability(True, None) DEFAULT_CHANEL = Reliability(True, 0) def _init_reliability( reliabilities: Dict[GamePacketType, Reliability] ) -> Dict[GamePacketType, Reliability]: d = defaultdict(lambda: DEFAULT_CHANEL) d.update(reliabilities) return d RELIABILITY_DICT = _init_reliability({ GamePacketType.MOVE_PLAYER: UNRELIABLE, GamePacketType.MOVE_ENTITY: UNRELIABLE, } if get_value(ConfigKey.ROUGH_MOVE) else {})
def _does_compress(payload: bytes) -> bool: return len(payload) >= get_value(ConfigKey.BATCH_COMPRESS_THRESHOLD)
def _get_resend_time_in_future() -> int: return time.time() + get_value(ConfigKey.RESEND_TIME)
def guid(self) -> int: return get_value(ConfigKey.SERVER_GUID)
def get_difficulty(self) -> Difficulty: return Difficulty[get_value(ConfigKey.DIFFICULTY)]
def __str__(self) -> str: max_length = get_value(ConfigKey.MAX_LOG_LENGTH) return str(self._value) if max_length is None else str( self._value)[:max_length]
def tcp_server(handler: GameDataHandler) -> Server: server_address = (get_unspecified_address(), get_value(ConfigKey.TCP_SERVER_PORT)) return _TcpServer(handler, server_address)
def get_world_name(self) -> str: return get_value(ConfigKey.WORLD_NAME)
def get_lightning_level(self) -> float: return get_value(ConfigKey.LIGHTNING_LEVEL)
def get_rain_level(self) -> float: return get_value(ConfigKey.RAIN_LEVEL)