Untitled

 avatar
unknown
plain_text
a year ago
1.9 kB
11
Indexable
import os
import re

def replace_display_name(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            lines = file.readlines()

        with open(file_path, 'w', encoding='utf-8') as file:
            for line in lines:
                # Capture existing values dynamically
                match = re.search(
                    r'display-name: \'{"extra":\[\{"bold":false,"italic":false,"underlined":false,"strikethrough":false,"obfuscated":false,"color":"([^"]*)","text":"([^"]*)"\}\],"text":""}\'',
                    line
                )

                if match:
                    old_color = match.group(1)
                    old_text = match.group(2)
                    print(f"Found match in line: {line.strip()}")

                    # Replace with dynamic values
                    updated_line = (
                        'display-name: {{"text":"","extra":[{{"text":"{}","obfuscated":false,"italic":false,"underlined":false,"strikethrough":false,"color":"{}","bold":false}}]}}'
                        .format(old_text, old_color)
                    )
                    print(f"Replacing with: {updated_line.strip()}")
                    print("--")
                    file.write(updated_line + '\n')
                else:
                    file.write(line)

    except Exception as e:
        print(f"Error processing file {file_path}: {e}")

# Get the directory of the script
script_directory = os.path.dirname(os.path.abspath(__file__))

# Example usage
for subdir, _, files in os.walk(script_directory):
    for file in files:
        file_path = os.path.join(subdir, file)
        if file_path.endswith(".txt") or file_path.endswith(".json") or file_path.endswith(".sfi"):
            print(f"Processing file: {file_path}")
            replace_display_name(file_path)
Editor is loading...
Leave a Comment