-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c890068
commit 6b96504
Showing
1 changed file
with
68 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,107 @@ | ||
from pieraknet import Server as RakNetServer | ||
from pieraknet.packets.game_packet import GamePacket | ||
from pieraknet.connection import Connection as RakNetConnection | ||
from piemc.handlers.logger import create_logger | ||
from pieraknet.packets.frame_set import Frame as RakNetFrame | ||
from piemc.meta.protocol_info import ProtocolInfo | ||
from piemc.handlers.lang import LangHandler | ||
|
||
import logging | ||
import os | ||
import time | ||
import random | ||
import os | ||
import random | ||
import threading | ||
import time | ||
import piemc.config | ||
|
||
class BedrockServer: | ||
|
||
def __init__(self, hostname="0.0.0.0", port=19132, logger=logging.getLogger("PieBedrock")): | ||
self.logger = logger | ||
self.hostname = hostname | ||
self.port = port | ||
self.port_v6 = 19131 | ||
self.logger.debug("Initializing...") | ||
def __init__(self): | ||
self.threads = [] | ||
self.lang = LangHandler.initialize_language() | ||
self.logger = create_logger('PieBedrock') | ||
self.server_status = None | ||
self.hostname = piemc.config.HOST | ||
self.edition = "MCPE" | ||
self.protocol_version = 594 | ||
self.version_name = "1.20.12" | ||
self.motd1 = "PieBedrock" | ||
self.motd2 = "Server" | ||
self.players_online = 0 | ||
self.max_players = 20 | ||
self.players = [] | ||
self._gamemode_map = { | ||
self.motd = piemc.config.MOTD | ||
self.level = "Powered by PieMC" | ||
self.players_online = 2 # 2 players online XD. Update (By andiri): YES :sunglasses: | ||
self.max_players = piemc.config.MAX_PLAYERS | ||
self.gamemode_map = { | ||
"survival": ("Survival", 1), | ||
"creative": ("Creative", 2), | ||
"adventure": ("Adventure", 3) | ||
} | ||
try: | ||
self.set_gamemode("survival") | ||
except KeyError: | ||
self.gamemode = ("Survival", 1) | ||
self.gamemode = self.gamemode_map.get(piemc.config.GAMEMODE.lower(), ("Survival", 0)) | ||
self.logger.info(self.lang['NOT_EXISTING_GAMEMODE']) if self.gamemode[1] == 0 else None | ||
self.port = piemc.config.BEDROCK_PORT | ||
self.port_v6 = 19133 | ||
self.guid = random.randint(1, 99999999) | ||
self.uid = self.guid = random.randint(1, 99999999) | ||
self.timeout = 20 | ||
with open('pieuid.dat', 'r') as f: | ||
pieuid = f.read().strip() | ||
self.uid = pieuid | ||
self.raknet_version = 11 | ||
self.raknet_server = RakNetServer(self.hostname, self.port, logging.getLogger('PieRakNet')) | ||
self.timeout = 20 | ||
self.raknet_server = RakNetServer(self.hostname, self.port, create_logger('PieRakNet')) | ||
self.raknet_server.interface = self | ||
self.update_server_status() | ||
self.raknet_server.protocol_version = self.raknet_version | ||
self.raknet_server.timeout = self.timeout | ||
# self.raknet_server.magic = '' | ||
self.raknet_thread = threading.Thread(target=self.raknet_server.start) | ||
self.raknet_thread.daemon = True | ||
self.threads.append(self.raknet_thread) | ||
self.running = False | ||
self.logger.info(self.lang['SERVER_INITIALIZED']) | ||
self.start_time = int(time.time()) | ||
|
||
def set_gamemode(self, gamemode): | ||
gm = gamemode.lower() | ||
if (gm in self._gamemode_map.keys()): | ||
self.gamemode = self._gamemode_map[gm] | ||
else: | ||
raise KeyError(f"Gamemode {str(gamemode)} not exists") | ||
self.start() | ||
|
||
def get_time_ms(self): | ||
return round(time.time() - self.start_time, 4) | ||
|
||
def update_server_status(self): | ||
self.server_status = ";".join([ | ||
self.edition, | ||
self.motd1, | ||
str(self.protocol_version), | ||
self.motd, | ||
f"{self.protocol_version}", | ||
self.version_name, | ||
str(self.players_online), | ||
str(self.max_players), | ||
str(self.uid), | ||
self.motd2, | ||
f"{self.players_online}", | ||
f"{self.max_players}", | ||
f"{self.uid}", | ||
self.level, | ||
self.gamemode[0], | ||
str(self.gamemode[1]), | ||
str(self.port), | ||
str(self.port_v6), | ||
]) | ||
f"{self.gamemode[1]}", | ||
f"{self.port}", | ||
f"{self.port_v6}" | ||
]) + ";" | ||
self.raknet_server.name = self.server_status | ||
|
||
def on_game_packet(self, packet: GamePacket, connection: RakNetConnection): | ||
packet.decode() | ||
if packet.body[0] == ProtocolInfo.LOGIN: | ||
self.logger.info(f"New Login Packet: {str(packet.body)}") | ||
|
||
def on_new_incoming_connection(self, connection: RakNetConnection): | ||
self.logger.info(f"New Incoming Connection: {str(connection.address)}") | ||
|
||
def on_disconnect(self, connection: RakNetConnection): | ||
self.logger.info(f"Disconnected: {str(connection.address)}") | ||
self.logger.info(f"{str(connection.address)} disconnected") | ||
|
||
def on_unknown_packet(self, packet: RakNetFrame, connection: RakNetConnection): | ||
self.logger.info(f"New Unknown Packet: {str(packet.body)}") | ||
|
||
def start(self): | ||
self.running = True | ||
self.raknet_server.start() | ||
|
||
if __name__ == "__main__": | ||
server = BedrockServer() | ||
server.start() | ||
self.raknet_thread.start() | ||
self.logger.info(f"{self.lang['RUNNING']} ({self.get_time_ms()}s.)") | ||
self.logger.info(f"{self.lang['PORT']}: {self.port}") | ||
|
||
def stop(self): | ||
self.logger.info(self.lang['STOPPING_WAIT']) | ||
self.running = False | ||
self.raknet_server.stop() | ||
self.raknet_thread.join() | ||
self.logger.info(self.lang['STOP']) |