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

package com.hypixel.hytale.server.flock;

import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.modules.entity.damage.Damage;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.modules.entity.AllLegacyLivingEntityTypesQuery;
import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent;
import com.hypixel.hytale.component.Ref;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathSystems;

public class FlockDeathSystems
{
    public static class PlayerDeath extends DeathSystems.OnDeathSystem
    {
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return Player.getComponentType();
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            commandBuffer.tryRemoveComponent(ref, FlockMembership.getComponentType());
        }
    }
    
    public static class EntityDeath extends DeathSystems.OnDeathSystem
    {
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityDeath() {
            this.query = (Query<EntityStore>)Query.and(AllLegacyLivingEntityTypesQuery.INSTANCE, Query.not((Query<Object>)Player.getComponentType()));
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final NPCEntity npcComponent = commandBuffer.getComponent(ref, NPCEntity.getComponentType());
            if (npcComponent != null) {
                final Role role = npcComponent.getRole();
                if (role != null && !role.isCorpseStaysInFlock()) {
                    commandBuffer.tryRemoveComponent(ref, FlockMembership.getComponentType());
                }
            }
            final Damage damageInfo = component.getDeathInfo();
            Ref<EntityStore> sourceRef = null;
            if (damageInfo != null) {
                final Damage.Source source = damageInfo.getSource();
                if (source instanceof final Damage.EntitySource entitySource) {
                    sourceRef = entitySource.getRef();
                }
            }
            if (sourceRef == null) {
                return;
            }
            final Flock attackerFlock = FlockPlugin.getFlock(commandBuffer, sourceRef);
            if (attackerFlock != null) {
                attackerFlock.onTargetKilled(commandBuffer, ref);
            }
        }
    }
}
