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

package com.hypixel.hytale.server.core.entity.knockback;

import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.modules.entity.damage.DamageModule;
import com.hypixel.hytale.component.SystemGroup;
import com.hypixel.hytale.server.core.modules.splitvelocity.VelocityConfig;
import com.hypixel.hytale.protocol.ChangeVelocityType;
import com.hypixel.hytale.server.core.modules.entity.player.KnockbackSimulation;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.entity.AllLegacyLivingEntityTypesQuery;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.modules.physics.component.Velocity;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.physics.systems.IVelocityModifyingSystem;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;

public class KnockbackSystems
{
    public static class ApplyKnockback extends EntityTickingSystem<EntityStore> implements IVelocityModifyingSystem
    {
        @Nonnull
        private static final Query<EntityStore> QUERY;
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return ApplyKnockback.QUERY;
        }
        
        @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 KnockbackComponent knockbackComponent = archetypeChunk.getComponent(index, KnockbackComponent.getComponentType());
            assert knockbackComponent != null;
            knockbackComponent.applyModifiers();
            final Velocity velocityComponent = archetypeChunk.getComponent(index, Velocity.getComponentType());
            assert velocityComponent != null;
            velocityComponent.addInstruction(knockbackComponent.getVelocity(), knockbackComponent.getVelocityConfig(), knockbackComponent.getVelocityType());
            if (knockbackComponent.getDuration() > 0.0f) {
                knockbackComponent.incrementTimer(dt);
            }
            if (knockbackComponent.getDuration() == 0.0f || knockbackComponent.getTimer() > knockbackComponent.getDuration()) {
                final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
                commandBuffer.tryRemoveComponent(ref, KnockbackComponent.getComponentType());
            }
        }
        
        static {
            QUERY = Query.and(AllLegacyLivingEntityTypesQuery.INSTANCE, KnockbackComponent.getComponentType(), Velocity.getComponentType(), Query.not((Query<Object>)Player.getComponentType()));
        }
    }
    
    public static class ApplyPlayerKnockback extends EntityTickingSystem<EntityStore> implements IVelocityModifyingSystem
    {
        public static boolean DO_SERVER_PREDICTION;
        @Nonnull
        private static final Query<EntityStore> QUERY;
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return ApplyPlayerKnockback.QUERY;
        }
        
        @Override
        public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
            return EntityTickingSystem.maybeUseParallel(archetypeChunkSize, taskCount);
        }
        
        @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 KnockbackComponent knockbackComponent = archetypeChunk.getComponent(index, KnockbackComponent.getComponentType());
            assert knockbackComponent != null;
            final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
            KnockbackSimulation knockbackSimulationComponent = archetypeChunk.getComponent(index, KnockbackSimulation.getComponentType());
            if (knockbackSimulationComponent == null && ApplyPlayerKnockback.DO_SERVER_PREDICTION) {
                commandBuffer.addComponent(ref, KnockbackSimulation.getComponentType(), knockbackSimulationComponent = new KnockbackSimulation());
            }
            knockbackComponent.applyModifiers();
            switch (knockbackComponent.getVelocityType()) {
                case Add: {
                    if (ApplyPlayerKnockback.DO_SERVER_PREDICTION) {
                        if (knockbackSimulationComponent != null) {
                            knockbackSimulationComponent.addRequestedVelocity(knockbackComponent.getVelocity());
                            break;
                        }
                        break;
                    }
                    else {
                        final Velocity velocityComponent = archetypeChunk.getComponent(index, Velocity.getComponentType());
                        assert velocityComponent != null;
                        velocityComponent.addInstruction(knockbackComponent.getVelocity(), knockbackComponent.getVelocityConfig(), ChangeVelocityType.Add);
                        if (knockbackComponent.getDuration() > 0.0f) {
                            knockbackComponent.incrementTimer(dt);
                        }
                        if (knockbackComponent.getDuration() == 0.0f || knockbackComponent.getTimer() > knockbackComponent.getDuration()) {
                            commandBuffer.tryRemoveComponent(ref, KnockbackComponent.getComponentType());
                        }
                        break;
                    }
                    break;
                }
                case Set: {
                    if (ApplyPlayerKnockback.DO_SERVER_PREDICTION) {
                        if (knockbackSimulationComponent != null) {
                            knockbackSimulationComponent.setRequestedVelocity(knockbackComponent.getVelocity());
                            break;
                        }
                        break;
                    }
                    else {
                        final Velocity velocityComponent = archetypeChunk.getComponent(index, Velocity.getComponentType());
                        assert velocityComponent != null;
                        velocityComponent.addInstruction(knockbackComponent.getVelocity(), null, ChangeVelocityType.Set);
                        if (knockbackComponent.getDuration() > 0.0f) {
                            knockbackComponent.incrementTimer(dt);
                        }
                        if (knockbackComponent.getDuration() == 0.0f || knockbackComponent.getTimer() > knockbackComponent.getDuration()) {
                            commandBuffer.tryRemoveComponent(ref, KnockbackComponent.getComponentType());
                            break;
                        }
                        break;
                    }
                    break;
                }
            }
            if (ApplyPlayerKnockback.DO_SERVER_PREDICTION && knockbackSimulationComponent != null) {
                knockbackSimulationComponent.reset();
            }
        }
        
        @Nullable
        @Override
        public SystemGroup<EntityStore> getGroup() {
            return DamageModule.get().getInspectDamageGroup();
        }
        
        static {
            ApplyPlayerKnockback.DO_SERVER_PREDICTION = false;
            QUERY = Query.and(KnockbackComponent.getComponentType(), Player.getComponentType(), Velocity.getComponentType());
        }
    }
}
