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

package com.hypixel.hytale.server.core.command.commands.utility.sound;

import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.SoundUtil;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.RelativeVector3i;
import com.hypixel.hytale.protocol.SoundCategory;
import com.hypixel.hytale.server.core.command.system.arguments.system.DefaultArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.type.soundevent.config.SoundEvent;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractTargetPlayerCommand;

public class SoundPlay3DCommand extends AbstractTargetPlayerCommand
{
    @Nonnull
    private final RequiredArg<SoundEvent> soundEventArg;
    @Nonnull
    private final DefaultArg<SoundCategory> categoryArg;
    @Nonnull
    private final RequiredArg<RelativeVector3i> positionArg;
    @Nonnull
    private final FlagArg allFlag;
    
    public SoundPlay3DCommand() {
        super("3d", "server.commands.sound.3d.desc");
        this.soundEventArg = this.withRequiredArg("sound", "server.commands.sound.play3d.sound.desc", ArgTypes.SOUND_EVENT_ASSET);
        this.categoryArg = this.withDefaultArg("category", "server.commands.sound.category.desc", ArgTypes.SOUND_CATEGORY, SoundCategory.SFX, "server.commands.sound.category.default");
        this.positionArg = this.withRequiredArg("position", "server.commands.sound.play3d.position.desc", ArgTypes.RELATIVE_VECTOR3I);
        this.allFlag = this.withFlagArg("all", "server.commands.sound.all.desc");
        this.addAliases("play3d");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nullable final Ref<EntityStore> sourceRef, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final SoundEvent soundEvent = this.soundEventArg.get(context);
        final SoundCategory soundCategory = this.categoryArg.get(context);
        final RelativeVector3i relativePosition = this.positionArg.get(context);
        final int soundEventIndex = SoundEvent.getAssetMap().getIndex(soundEvent.getId());
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final Vector3d basePosition = transformComponent.getPosition();
        final Vector3i blockPosition = relativePosition.resolve(MathUtil.floor(basePosition.x), MathUtil.floor(basePosition.y), MathUtil.floor(basePosition.z));
        if (this.allFlag.provided(context)) {
            SoundUtil.playSoundEvent3d(soundEventIndex, soundCategory, blockPosition.x, blockPosition.y, blockPosition.z, world.getEntityStore().getStore());
        }
        else {
            SoundUtil.playSoundEvent3dToPlayer(ref, soundEventIndex, soundCategory, blockPosition.x, blockPosition.y, blockPosition.z, store);
        }
    }
}
