Begin port to 26.3: movement
This commit is contained in:
+49
-6
@@ -3,11 +3,14 @@
|
||||
Run with SERVER=... PLAYER_NAME=mosfet1 GAME_PASSWORD=... python -m mosfet.connect
|
||||
"""
|
||||
import os
|
||||
import threading
|
||||
from minecraft.networking.connection import Connection
|
||||
from minecraft.networking.modern import (
|
||||
CancelPostjoinDialog, ChatCommand, PlayShowDialog,
|
||||
PlayLogin, SubmitPostjoinLoginDialog, SystemChat, TeleportPosition,
|
||||
)
|
||||
from mosfet.world_26_3 import WorldState
|
||||
from mosfet.vector import Point3D
|
||||
|
||||
|
||||
def component_text(value):
|
||||
@@ -39,14 +42,17 @@ def component_text(value):
|
||||
|
||||
|
||||
def main(global_state=None):
|
||||
server = os.environ.get('SERVER', 'games.protospace.ca')
|
||||
server = os.environ.get('SERVER', '127.0.0.1')
|
||||
port = int(os.environ.get('PORT', '25565'))
|
||||
username = os.environ.get('PLAYER_NAME', 'mosfet1')
|
||||
password = os.environ.get('GAME_PASSWORD', 'mosfetbot')
|
||||
|
||||
connection = Connection(server, port, username=username,
|
||||
allowed_versions=['26.3'])
|
||||
allowed_versions=['26.3'],
|
||||
handle_exception=False)
|
||||
connection.authme_password = password
|
||||
connection.world_state = WorldState()
|
||||
connection.dimension_types_by_id = []
|
||||
sent_auth_commands = set()
|
||||
if global_state is not None:
|
||||
global_state.connection = connection
|
||||
@@ -54,12 +60,32 @@ def main(global_state=None):
|
||||
global_state.connected = False
|
||||
global_state.connection_status = 'connecting'
|
||||
global_state.player_position = None
|
||||
global_state.world_state = connection.world_state
|
||||
global_state.chunks = connection.world_state
|
||||
from mosfet.protocol.managers import DataManager
|
||||
global_state.mcdata = DataManager()
|
||||
|
||||
@connection.listener(PlayLogin)
|
||||
def on_play_login(packet):
|
||||
if global_state is not None:
|
||||
global_state.connected = True
|
||||
global_state.connection_status = 'in_game'
|
||||
global_state.player_id = packet.player_id
|
||||
global_state.dimension = packet.dimension.removeprefix('minecraft:')
|
||||
packet.render_distance = packet.chunk_radius
|
||||
global_state.info = packet
|
||||
global_state.eid = packet.player_id
|
||||
global_state.command_lock = False
|
||||
from mosfet.world import World
|
||||
from mosfet.commands import Commands
|
||||
from mosfet.protocol.modern import ModernChatManager
|
||||
from mosfet.protocol.entities_26_3 import ModernEntityManager
|
||||
from mosfet.automation_26_3 import start as start_modern_movement
|
||||
global_state.world = World(global_state)
|
||||
global_state.chat = ModernChatManager(global_state)
|
||||
global_state.entities = ModernEntityManager(global_state)
|
||||
global_state.commands = Commands(global_state)
|
||||
start_modern_movement(global_state)
|
||||
|
||||
|
||||
def send_auth_command(action):
|
||||
@@ -75,7 +101,15 @@ def main(global_state=None):
|
||||
@connection.listener(TeleportPosition)
|
||||
def on_spawn(packet):
|
||||
if global_state is not None:
|
||||
global_state.player_position = (packet.x, packet.y, packet.z)
|
||||
position = getattr(connection, 'player_position', None)
|
||||
if position is None:
|
||||
position = (packet.x, packet.y, packet.z,
|
||||
packet.yaw, packet.pitch)
|
||||
x, y, z, yaw, pitch = position
|
||||
global_state.player_position = (x, y, z)
|
||||
global_state.pos = Point3D(global_state.player_position)
|
||||
global_state.yaw = yaw
|
||||
global_state.pitch = pitch
|
||||
if getattr(connection, 'authme_mode', 'register') == 'register':
|
||||
send_auth_command('register')
|
||||
|
||||
@@ -96,6 +130,9 @@ def main(global_state=None):
|
||||
message = component_text(packet.content)
|
||||
if global_state is not None:
|
||||
global_state.last_chat = message
|
||||
chat = global_state.get('chat')
|
||||
if chat is not None:
|
||||
chat.handle_system_chat(packet, message)
|
||||
print('[server] ' + message, flush=True)
|
||||
lowered = message.lower()
|
||||
if 'already registered' in lowered:
|
||||
@@ -111,26 +148,32 @@ def main(global_state=None):
|
||||
connection.connect()
|
||||
print('Connected as {} to {}:{} using offline protocol 777.'.format(
|
||||
username, server, port), flush=True)
|
||||
shutdown_requested = False
|
||||
try:
|
||||
while connection.connected:
|
||||
if global_state is not None and not global_state.get('running', True):
|
||||
shutdown_requested = True
|
||||
break
|
||||
thread = connection.networking_thread
|
||||
if thread is None:
|
||||
break
|
||||
thread.join(timeout=0.5)
|
||||
if connection.connected:
|
||||
connection.disconnect()
|
||||
if connection.exception:
|
||||
connection.disconnect(immediate=True)
|
||||
if connection.exception and not shutdown_requested:
|
||||
raise connection.exception
|
||||
print('Connection ended.', flush=True)
|
||||
except KeyboardInterrupt:
|
||||
shutdown_requested = True
|
||||
print('Disconnecting...', flush=True)
|
||||
connection.disconnect()
|
||||
finally:
|
||||
if connection.connected:
|
||||
connection.disconnect()
|
||||
connection.disconnect(immediate=True)
|
||||
if global_state is not None:
|
||||
worker = global_state.get('movement_thread')
|
||||
if worker and worker is not threading.current_thread():
|
||||
worker.join(timeout=1.0)
|
||||
global_state.connection = False
|
||||
global_state.connected = False
|
||||
global_state.connection_status = 'disconnected'
|
||||
|
||||
Reference in New Issue
Block a user