// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.spawning.local;

import com.hypixel.hytale.logger.HytaleLogger;
import java.util.logging.Level;
import com.hypixel.hytale.server.spawning.SpawningPlugin;
import com.hypixel.hytale.math.random.RandomExtra;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.system.tick.ArchetypeTickingSystem;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.ResourceType;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.component.Archetype;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;

public class LocalSpawnForceTriggerSystem extends EntityTickingSystem<EntityStore>
{
    private static final double[] RERUN_TIME_RANGE;
    private final Archetype<EntityStore> archetype;
    private final ComponentType<EntityStore, PlayerRef> playerRefComponentType;
    private final ComponentType<EntityStore, LocalSpawnController> spawnControllerComponentType;
    private final ResourceType<EntityStore, LocalSpawnState> localSpawnStateResourceType;
    
    public LocalSpawnForceTriggerSystem(final ComponentType<EntityStore, LocalSpawnController> spawnControllerComponentType, final ResourceType<EntityStore, LocalSpawnState> localSpawnStateResourceType) {
        this.playerRefComponentType = PlayerRef.getComponentType();
        this.spawnControllerComponentType = spawnControllerComponentType;
        this.localSpawnStateResourceType = localSpawnStateResourceType;
        this.archetype = Archetype.of(this.playerRefComponentType, spawnControllerComponentType);
    }
    
    @Override
    public Query<EntityStore> getQuery() {
        return this.archetype;
    }
    
    @Override
    public void tick(final float dt, final int systemIndex, @Nonnull final Store<EntityStore> store) {
        final LocalSpawnState state = store.getResource(this.localSpawnStateResourceType);
        if (state.pollForceTriggerControllers()) {
            store.tick(this, dt, systemIndex);
        }
    }
    
    @Override
    public void tick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final PlayerRef playerRefComponent = archetypeChunk.getComponent(index, this.playerRefComponentType);
        assert playerRefComponent != null;
        final LocalSpawnController controller = archetypeChunk.getComponent(index, this.spawnControllerComponentType);
        final double seconds = RandomExtra.randomRange(LocalSpawnForceTriggerSystem.RERUN_TIME_RANGE);
        controller.setTimeToNextRunSeconds(seconds);
        final HytaleLogger.Api context = SpawningPlugin.get().getLogger().at(Level.FINE);
        if (context.isEnabled()) {
            context.log("Force running local spawn controller for %s in %f seconds", playerRefComponent.getUsername(), seconds);
        }
    }
    
    static {
        RERUN_TIME_RANGE = new double[] { 0.0, 5.0 };
    }
}
