-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (100 loc) · 3.74 KB
/
main.py
File metadata and controls
132 lines (100 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import logging
import threading
import socket
from basalt.commands import console_handle_commands
from basalt.log import MinecraftColourFormatter, zip_server_log
from basalt.net.attributes import ConnectionAttributes, ServerAttributes
from basalt.net.packet.outbound import DespawnPlayer, MessageOutbound
from basalt.net.packet_manager import PacketManager, send
# Define your server address and port
server_attr = ServerAttributes("127.0.0.1", 25565)
logger = logging.getLogger(__name__)
def handle_client(client_socket, client_address):
# Create a PacketManager instance for this client
pacman = PacketManager()
attr = ConnectionAttributes(server_attr)
attr.address = client_address
while True:
try:
data = pacman.receive_packet(client_socket)
# Check if the client disconnected
if not data:
logger.info(f"&cClient disconnected: {attr.address}")
break
# Process the received packet using the PacketManager
received, response = pacman.handle(data, attr)
# Send the response packet back to the client
for packet_type, broadcast in response:
send(client_socket, attr, packet_type, broadcast)
except Exception as e:
logger.error("Error handling client:", e)
break
send(
client_socket,
attr,
MessageOutbound(f"&e{attr.player.username} left the game"),
True,
)
# Send a DespawnPlayer packet to all clients
send(client_socket, attr, DespawnPlayer(attr.player.id), True)
attr.destroy_player()
logger.debug("Closing client socket")
# Close the client socket when done
client_socket.close()
del attr
def start_server():
logger.info("&9Starting Basalt server...")
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the server address and port
server_socket.bind(server_attr.addr)
# Enable the server to accept connections
server_socket.listen(5)
logger.info(
"&9Server listening on &1{}:{}".format(server_attr.addr[0], server_attr.addr[1])
)
console_attr = ConnectionAttributes(server_attr)
console_attr.create_player("Console", True)
command_thread = threading.Thread(
target=console_handle_commands, args=(server_socket, console_attr)
)
command_thread.start()
while True:
try:
# Accept a client connection
client_socket, client_address = server_socket.accept()
logger.info(f"&6Accepted connection from: {client_address}")
# Create a new thread to handle the client
client_thread = threading.Thread(
target=handle_client, args=(client_socket, client_address)
)
client_thread.start()
except Exception as e:
# logger.error("Error accepting client connection:", e)
break
# Close the server socket when done
server_socket.close()
logger.info("&cServer closed.")
log_file = "logs/server.log"
if __name__ == "__main__":
# Zip the server log if it exists
zip_server_log(log_file)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler(),
],
)
# Get the console handler and set the formatter
console = logging.getLogger().handlers[1]
console.setFormatter(
MinecraftColourFormatter(
fmt=console.formatter._fmt, datefmt=console.formatter.datefmt
)
)
# Start the server
start_server()