// ==================================================
// ERROR HANDLER PATCH FOR RPG MAKER MZ
// Place this in js/plugins and enable it
// ==================================================

/*:
 * @target MZ
 * @plugindesc Shows detailed error messages for script calls instead of crashing
 * @author Debug Helper
 *
 * @help This plugin catches script execution errors and displays them
 * in the console with the actual code that failed.
 */

(() => {
    const _Game_Interpreter_command355 = Game_Interpreter.prototype.command355;
    
    Game_Interpreter.prototype.command355 = function() {
        const script = this.currentCommand().parameters[0];
        try {
            return _Game_Interpreter_command355.call(this);
        } catch (e) {
            console.error('==========================================');
            console.error('SCRIPT EXECUTION ERROR CAUGHT:');
            console.error('==========================================');
            console.error('Error:', e.message);
            console.error('Script that failed:');
            console.error(script);
            console.error('==========================================');
            
            // Show error in battle log if available
            if (BattleManager._logWindow) {
                BattleManager._logWindow.addText('\\c[18][SCRIPT ERROR: ' + e.message + ']');
            }
            
            // Continue execution instead of crashing
            return true;
        }
    };
})();
