ServerEvents.tick(event => {
    if (event.server.tickCount % 100 !== 0) return;

    let level = event.server.getLevel('minecraft:overworld');
    let day = Math.floor(level.dayTime() / 24000);
    let pData = event.server.persistentData;

    // -------------------------------
    // FIXED EARLY WAVES
    // -------------------------------

    if (day >= 41 && day < 55 && !pData.getBoolean('zombies40_done')) {
        event.server.runCommandSilent('hordeManager startJsonHorde zombies40 @p');
        pData.putBoolean('zombies40_done', true);
    }

    if (day >= 71 && day < 81 && !pData.getBoolean('zombies70_done')) {
        event.server.runCommandSilent('hordeManager startJsonHorde zombies70 @p');
        pData.putBoolean('zombies70_done', true);
    }

    if (day >= 91 && day < 101 && !pData.getBoolean('zombies90_done')) {
        event.server.runCommandSilent('hordeManager startJsonHorde zombies90 @p');
        pData.putBoolean('zombies90_done', true);
    }

    if (day >= 111 && day < 121 && !pData.getBoolean('zombies110_done')) {
        event.server.runCommandSilent('hordeManager startJsonHorde zombies110 @p');
        pData.putBoolean('zombies110_done', true);
    }

    // -------------------------------
    // REPEATING HORDE SYSTEM
    // STARTS AFTER DAY 130
    // GUARANTEED EVERY 18 DAYS
    // -------------------------------

    let startDay = 130;

        let hordes = [
        'zombies90',
        'zombies110'
        
    ];

    // initialize tracker
    if (!pData.contains('lastRepeatingHordeDay')) {
        pData.putInt('lastRepeatingHordeDay', startDay);
    }

    let lastHordeDay = pData.getInt('lastRepeatingHordeDay');

    // trigger repeating horde every 18 days
    if (day >= startDay && (day - lastHordeDay) >= 18) {

        let pick = hordes[Math.floor(Math.random() * hordes.length)];

        event.server.runCommandSilent(`hordeManager startJsonHorde ${pick} @p`);

        pData.putInt('lastRepeatingHordeDay', day);
    }
});