<commit_before>import configparser
import os.path
import sys

from quarry.net.client import ClientProtocol, ClientFactory
from quarry.mojang.profile import Profile

CONFIG_FILE = 'config.cfg'


class DevotedBotClientProtocol(ClientProtocol):
    pass


class DevotedBotClientFactory(ClientFactory):
    protocol = DevotedBotClientProtocol


def generate_config_file(name, config):
    config['auth'] = {'username': 'USERNAME',
                      'password': 'PASSWORD',
                      'auth': 'true'}
    config['server'] = {'ip': 'play.devotedmc.com',
                        'port': '25565'}
    with open(name, 'w') as configfile:
        configfile.write('# Config automatically generated. Delete this file to reset.\n\n')
        config.write(configfile)
    print('Config file generated. Edit the username and password fields to set up an account.')
    sys.exit()


def main():
    cfg = configparser.ConfigParser()
    if os.path.isfile(CONFIG_FILE):
       cfg.read(CONFIG_FILE)
    else:
        generate_config_file(CONFIG_FILE, cfg)

    profile = Profile()
    factory = DevotedBotClientFactory()
    if not cfg['auth'].getboolean('auth'):
        profile.login_offline(cfg['auth']['username'])
        factory.profile = profile
    else:
        def login_ok(data):
            factory.connect(cfg['server']['ip'], int(cfg['server']['port']))

        def login_failed(err):
            print('login failed:', err.value)
            factory.stop()
            sys.exit(1)

        factory.profile = profile
        deferred = profile.login(cfg['auth']['username'], cfg['auth']['password'])
        deferred.addCallbacks(login_ok, login_failed)
        factory.run()


if __name__ == '__main__':
    main()<commit_msg>Responds to keepalive packets and respawns on death<commit_after>import configparser
import os.path
import sys

from quarry.net.client import ClientProtocol, ClientFactory
from quarry.mojang.profile import Profile

CONFIG_FILE = 'config.cfg'


class DevotedBotClientProtocol(ClientProtocol):
    def packet_keep_alive(self, buff):
        identifier = buff.unpack_varint(signed=True)
        self.send_packet('keep_alive', self.buff_type.pack_varint(identifier, signed=True))

    def packet_update_health(self, buff):
        health = buff.unpack('f')
        food = buff.unpack_varint()
        saturation = buff.unpack('f')

        if health <= 0:
            self.send_packet('client_status', self.buff_type.pack_varint(0))
            print('Died, respawning...')

    def packet_disconnect(self, buff):
        message = buff.unpack_chat()
        print('Disconnected for reason: ', message)
        sys.exit(1)


class DevotedBotClientFactory(ClientFactory):
    protocol = DevotedBotClientProtocol


def generate_config_file(name, config):
    config['auth'] = {'username': 'USERNAME',
                      'password': 'PASSWORD',
                      'auth': 'true'}
    config['server'] = {'ip': 'play.devotedmc.com',
                        'port': '25565'}
    with open(name, 'w') as configfile:
        configfile.write('# Config automatically generated. Delete this file to reset.\n\n')
        config.write(configfile)
    print('Config file generated. Edit the username and password fields to set up an account.')
    sys.exit()


def main():
    cfg = configparser.ConfigParser()
    if os.path.isfile(CONFIG_FILE):
        cfg.read(CONFIG_FILE)
    else:
        generate_config_file(CONFIG_FILE, cfg)

    profile = Profile()
    factory = DevotedBotClientFactory()
    if not cfg['auth'].getboolean('auth'):
        profile.login_offline(cfg['auth']['username'])
        factory.profile = profile
    else:
        def login_ok(data):
            factory.connect(cfg['server']['ip'], int(cfg['server']['port']))

        def login_failed(err):
            print('login failed:', err.value)
            factory.stop()
            sys.exit(1)

        factory.profile = profile
        deferred = profile.login(cfg['auth']['username'], cfg['auth']['password'])
        deferred.addCallbacks(login_ok, login_failed)
        factory.run()


if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.12684223242329065, 'fraction_numerical': 0.00362406378352259, 'mean_word_length': 2.9316239316239314, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '29639', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1161, 'n_words': 265}