• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

OpenTibia Monster item namer Python script

Sizaro

Advanced OT User
Joined
Aug 20, 2007
Messages
5,151
Solutions
5
Reaction score
210
Location
Sweden
GitHub
coldensjo
This will name all your loot inside a monster file.
So that
Code:
<item id="2430" chance="2500" />
Becomes
Code:
<item id="2430" chance="2500" /> <!-- knight axe -->

Make sure your folder structure looks something like this.

Code:
*\Desktop\pythontest\itemnames.py
*\Desktop\pythontest\monsters\
*\Desktop\pythontest\items.xml
  • BACKUP YOUR MONSTERS
  • Any monsters (wherever they are) inside /monsters/ folders will have ALL their items commented.
    If they already have a comment, it will skip those.
  • Place your items.xml inside the same folder as the pythonscript.
  • If you haven't already, download Python at Download Python (https://www.python.org/downloads/)
  • Run by using Powershell or CMD.
Code:
python itemnames.py

Here is the script.

Python:
import os
import xml.etree.ElementTree as ET
import re


# Regular expression pattern to find item IDs
pattern = re.compile(r'<item id="(\d+)"')


def get_item_dict(items_file_path):
    tree = ET.parse(items_file_path)
    root = tree.getroot()
    item_dict = {}
    for item in root.findall('item'):
        item_id = item.get('id')
        item_name = item.get('name', 'Unknown Item')
        if item_id:
            item_dict[item_id] = item_name
    return item_dict


def process_file(file_path, item_dict):
    with open(file_path, 'r') as file:
        lines = file.readlines()


    new_lines = []


    for line in lines:
        # Check if the line has a comment at the beginning
        comment_match = re.match(r'\s*<!-- (.*?) -->\s*<item id="(\d+)"', line)
        if comment_match:
            comment, item_id = comment_match.groups()
            # Rewrite the line to move the comment to the end
            line = re.sub(r'<!-- .*? -->', '', line).strip() + f' <!-- {comment} -->\n'
        else:
            match = pattern.search(line)
            if match:
                item_id = match.group(1)
                item_name = item_dict.get(item_id, 'Unknown Item')
                # Check if the line already contains the comment to avoid duplicates
                if item_name not in line:
                    line = f'{line.strip()} <!-- {item_name} -->\n'
        new_lines.append(line)


    # Write the modified lines back to the file
    with open(file_path, 'w') as file:
        file.writelines(new_lines)


def process_monster_directory(directory, item_dict):
    for root_dir, _, files in os.walk(directory):
        for filename in files:
            if filename.endswith(".xml"):
                process_file(os.path.join(root_dir, filename), item_dict)


if __name__ == '__main__':
    # Adjust this path if your items.xml is in a different location.
    item_dict = get_item_dict('items.xml')
    process_monster_directory('monsters', item_dict)
 
Back
Top