• 1

    posted a message on Java Edition: Convert offline mode server to an online mode server

    I have a private server with my friends and when we started the server some of my friends didn't own the game. So I started the server in offline mode (very naughty I know) however, everyone has now purchased the game! So I had to convert my server to an online mode server as no one had any skins and it was super annoying.


    So I wrote a python script that will take all of the player.dat files and advancement.json files from the world folder and convert them into the appropriate online UUIDs, I thought I would share this code incase someone in the future finds it useful.


    For this code to work: the player.dat files need to be in a folder called `input/playerdata` and the advancements must be in `input/advancements`. You must also create an `output/playerdata` and an `output/advancements` folder for the new files to go to. If a user cannot be converted to an online UUID, they will not be converted at all and their playerdata will not appear in the output.


    The code is quite basic, it loops through all the player.dat files, extracts the `bukkit.lastKnownName` attributes, and then takes that name and goes to here

    https://api.mojang.com/users/profiles/minecraft/PLAYER_NAME_HERE

    This will return the players UUID which I then use to build an array of old UUIDs -> new UUIDs, then to finish off I loop through that array I built and just copy the files over changing the name of the file to the new UUID as I do it. You can then copy these files in `output/playerdata` and `output/advancements` to the playerdata and advancement folders inside your world folder.


    DO NOT RUN RANDOM CODE YOU FIND ONLINE UNLESS YOU UNDERSTAND WHAT IT DOES

    AND. ALWAYS. MAKE. BACKUPS!


    Here is my Python code


    import re
    import nbt
    import glob
    import requests
    
    from shutil import copyfile
    from json.decoder import JSONDecodeError
    
    
    def get_online_uuid(name):
        url = 'https://api.mojang.com/users/profiles/minecraft/' + name
    
        response = requests.get(url=url)
    
        try:
            data = response.json()
    
            data_uuid = data['id']
    
            if len(data_uuid) != 32:
                return data_uuid
    
            matches = re.match('(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})', data_uuid)
    
            output = "{}-{}-{}-{}-{}".format(matches.group(1), matches.group(2), matches.group(3), matches.group(4),
                                        matches.group(5))
    
            print('Name: ' + name + ' Found: ' + output)
    
            return output
        except JSONDecodeError:
            print('Name: ' + name + ' Found: ')
    
            return ""
    
    
    def get_old_uuids():
        player_dot_dats = glob.glob("input/playerdata/*.dat")
    
        uuids = []
    
        for filename in player_dot_dats:
            search = re.match('input/playerdata/(.*)\\.dat', filename)
    
            uuids.append(search.group(1))
    
        return uuids
    
    
    def get_new_uuids(uuids):
        convert = []
    
        for uuid in uuids:
            nbtfile = nbt.nbt.NBTFile('input/playerdata/' + uuid + '.dat', 'rb')
    
            convert.append([uuid, get_online_uuid(str(nbtfile['bukkit']['lastKnownName']))])
    
        return convert
    
    
    def convert_player_dat(original, new):
        if not new:
            return
    
        copyfile('input/playerdata/' + original + '.dat', 'output/playerdata/' + new + '.dat')
    
    
    def convert_advancements(original, new):
        if not new:
            return
    
        copyfile('input/advancements/' + original + '.json', 'output/advancements/' + new + '.json')
    
    
    def convert_files(conversions):
        for conversion in conversions:
            convert_player_dat(conversion[0], conversion[1])
            convert_advancements(conversion[0], conversion[1])
    
    
    convert_files(get_new_uuids(get_old_uuids()))


    Copyright License: Opensource MIT


    Copyright 2020 Jack Price-Burns


    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.


    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Posted in: Minecraft Tools
  • To post a comment, please or register a new account.