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

package com.hypixel.hytale.server.core.modules.entity.damage;

import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.entity.InteractionManager;
import com.hypixel.hytale.server.core.modules.interaction.InteractionModule;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.entity.effect.EffectControllerComponent;
import com.hypixel.hytale.component.Archetype;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatValue;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.Ref;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.RefChangeSystem;

public class RespawnSystems
{
    public abstract static class OnRespawnSystem extends RefChangeSystem<EntityStore, DeathComponent>
    {
        @Nonnull
        @Override
        public ComponentType<EntityStore, DeathComponent> componentType() {
            return DeathComponent.getComponentType();
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentSet(@Nonnull final Ref<EntityStore> ref, final DeathComponent oldComponent, @Nonnull final DeathComponent newComponent, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
    }
    
    public static class ResetStatsRespawnSystem extends OnRespawnSystem
    {
        @Nonnull
        private static final Query<EntityStore> QUERY;
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return ResetStatsRespawnSystem.QUERY;
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final EntityStatMap entityStatMapComponent = store.getComponent(ref, EntityStatMap.getComponentType());
            assert entityStatMapComponent != null;
            for (int index = 0; index < entityStatMapComponent.size(); ++index) {
                final EntityStatValue value = entityStatMapComponent.get(index);
                if (value != null) {
                    entityStatMapComponent.resetStatValue(index);
                }
            }
        }
        
        static {
            QUERY = Archetype.of(Player.getComponentType(), EntityStatMap.getComponentType());
        }
    }
    
    public static class ResetPlayerRespawnSystem extends OnRespawnSystem
    {
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return Player.getComponentType();
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final Player playerComponent = store.getComponent(ref, Player.getComponentType());
            assert playerComponent != null;
            playerComponent.setLastSpawnTimeNanos(java.lang.System.nanoTime());
        }
    }
    
    public static class ClearEntityEffectsRespawnSystem extends OnRespawnSystem
    {
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return EffectControllerComponent.getComponentType();
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final EffectControllerComponent effectControllerComponent = commandBuffer.getComponent(ref, EffectControllerComponent.getComponentType());
            assert effectControllerComponent != null;
            effectControllerComponent.clearEffects(ref, commandBuffer);
        }
    }
    
    public static class ClearInteractionsRespawnSystem extends OnRespawnSystem
    {
        @Override
        public Query<EntityStore> getQuery() {
            return InteractionModule.get().getInteractionManagerComponent();
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final InteractionManager interactionManagerComponent = store.getComponent(ref, InteractionModule.get().getInteractionManagerComponent());
            interactionManagerComponent.clear();
        }
    }
    
    public static class CheckBrokenItemsRespawnSystem extends OnRespawnSystem
    {
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return Player.getComponentType();
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final Player playerComponent = commandBuffer.getComponent(ref, Player.getComponentType());
            assert playerComponent != null;
            if (playerComponent.getInventory().containsBrokenItem()) {
                playerComponent.sendMessage(Message.translation("server.general.repair.itemBrokenOnRespawn").color("#ff5555"));
            }
        }
    }
}
