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

package com.hypixel.hytale.server.npc.systems;

import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.npc.blackboard.view.event.entity.EntityEventType;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.npc.blackboard.view.event.entity.EntityEventView;
import com.hypixel.hytale.server.core.modules.entity.player.PlayerSettings;
import com.hypixel.hytale.protocol.GameMode;
import com.hypixel.hytale.server.npc.blackboard.Blackboard;
import com.hypixel.hytale.component.ResourceType;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.modules.entity.damage.Damage;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
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.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.modules.entity.AllLegacyLivingEntityTypesQuery;
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 NPCDeathSystems
{
    public static class NPCKillsEntitySystem extends DeathSystems.OnDeathSystem
    {
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return (Query<EntityStore>)Query.and(AllLegacyLivingEntityTypesQuery.INSTANCE, TransformComponent.getComponentType());
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final DeathComponent component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final ComponentType<EntityStore, NPCEntity> npcComponentType = NPCEntity.getComponentType();
            if (npcComponentType == null) {
                return;
            }
            final Damage deathInfo = component.getDeathInfo();
            if (deathInfo != null) {
                final Damage.Source source = deathInfo.getSource();
                if (source instanceof final Damage.EntitySource entitySource) {
                    final Ref<EntityStore> sourceRef = entitySource.getRef();
                    final NPCEntity sourceNpcComponent = store.getComponent(sourceRef, npcComponentType);
                    if (sourceNpcComponent == null) {
                        return;
                    }
                    final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
                    assert transformComponent != null;
                    sourceNpcComponent.getDamageData().onKill(ref, transformComponent.getPosition().clone());
                }
            }
        }
    }
    
    public static class EntityViewSystem extends DeathSystems.OnDeathSystem
    {
        @Nonnull
        private final ComponentType<EntityStore, Player> playerComponentType;
        @Nonnull
        private final ComponentType<EntityStore, TransformComponent> transformComponentType;
        @Nonnull
        private final ResourceType<EntityStore, Blackboard> blackboardResourceType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityViewSystem() {
            this.playerComponentType = Player.getComponentType();
            this.transformComponentType = TransformComponent.getComponentType();
            this.blackboardResourceType = Blackboard.getResourceType();
            this.query = (Query<EntityStore>)Query.and(Query.or(NPCEntity.getComponentType(), this.playerComponentType), this.transformComponentType);
        }
        
        @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 Damage deathInfo = component.getDeathInfo();
            if (deathInfo != null) {
                final Damage.Source source = deathInfo.getSource();
                if (source instanceof final Damage.EntitySource entitySource) {
                    final Ref<EntityStore> sourceRef = entitySource.getRef();
                    if (!sourceRef.isValid()) {
                        return;
                    }
                    final Player sourcePlayerComponent = commandBuffer.getComponent(sourceRef, Player.getComponentType());
                    if (sourcePlayerComponent != null && sourcePlayerComponent.getGameMode() == GameMode.Creative) {
                        final PlayerSettings playerSettingsComponent = commandBuffer.getComponent(sourceRef, PlayerSettings.getComponentType());
                        if (playerSettingsComponent == null || !playerSettingsComponent.creativeSettings().allowNPCDetection()) {
                            return;
                        }
                    }
                    final TransformComponent transformComponent = store.getComponent(ref, this.transformComponentType);
                    assert transformComponent != null;
                    final Vector3d position = transformComponent.getPosition();
                    final Blackboard blackboardResource = store.getResource(this.blackboardResourceType);
                    final EntityEventView entityEventView = blackboardResource.getView(EntityEventView.class, ChunkUtil.chunkCoordinate(position.x), ChunkUtil.chunkCoordinate(position.z));
                    entityEventView.processAttackedEvent(ref, sourceRef, commandBuffer, EntityEventType.DEATH);
                }
            }
        }
    }
}
