package com.zivilon.cinder_loe.world.event;

import com.zivilon.cinder_loe.items.specials.Ulukai;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;

public class UlukaiCurseHandler {
    private int tickCounter = 20000;
    public static final int INTERVAL_TICKS = 92000; // 1 hour ~~10 minutes at 20 TPS~~ + 20000 Ticks, to offset warband running

    @SubscribeEvent
    public void onServerTick(TickEvent.ServerTickEvent event) {
        if (event.phase == TickEvent.Phase.END) {
            tickCounter++;
            if (tickCounter >= INTERVAL_TICKS) {
                tickCounter = 0;
                runUlukaiCleanup();
            }
        }
    }

    private void runUlukaiCleanup() {
        for (World world : DimensionManager.getWorlds()) {
            if (world == null || world.isRemote) continue;

            for (Object obj : world.loadedEntityList) {
                if (obj instanceof EntityLivingBase entity &&
                        entity.getEntityData().hasKey("UlukaiModifiers")) {

                    Ulukai.removeExpiredUlukaiModifiers(entity, world.getTotalWorldTime());
                }
            }
        }
    }
}