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