Detect disconnection and exit the script

This commit is contained in:
Tanner Collin 2021-03-04 21:39:51 +00:00
parent e4b51aa2d6
commit f35c7a4e51
3 changed files with 20 additions and 1 deletions

View File

@ -21,7 +21,7 @@ from protocol.packets import (
ClientWindowConfirmationPacket, EntityMetadataPacket,
SpawnLivingEntityPacket, EntityPositionRotationPacket, DestroyEntitiesPacket,
EntityActionPacket, EntityTeleport, InteractEntityPacket, TradeListPacket,
SelectTradePacket,
SelectTradePacket, DisconnectPacket,
)
from protocol.types import Slot
@ -374,6 +374,7 @@ class Game:
register(self.handle_update_health, clientbound.play.UpdateHealthPacket)
#register(self.handle_entity_velocity, clientbound.play.EntityVelocityPacket)
register(self.handle_trade_list, TradeListPacket)
register(self.handle_disconnect, DisconnectPacket)
#register(self.handle_packet, Packet, early=True)
@ -1230,6 +1231,12 @@ class Game:
packet.selected_slot = num
self.g.connection.write_packet(packet)
def handle_disconnect(self, packet):
print(packet)
print('Client disconnected!')
import os
os._exit(1)
def tick(self):
if self.g.breaking:
self.animate()

View File

@ -19,6 +19,7 @@ def get_packets(old_get_packets):
mc_packets.add(packets.DestroyEntitiesPacket)
mc_packets.add(packets.EntityTeleport)
mc_packets.add(packets.TradeListPacket)
mc_packets.add(packets.DisconnectPacket)
return mc_packets

View File

@ -447,3 +447,14 @@ class SelectTradePacket(Packet):
definition = [
{'selected_slot': VarInt},
]
class DisconnectPacket(Packet):
# Sent by the server before it disconnects a client
# https://wiki.vg/Protocol#Disconnect_.28play.29
id = 0x19
packet_name = 'disconnect'
definition = [
{'reason': String},
]