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

package com.hypixel.hytale.builtin.mounts.interactions;

import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.protocol.BlockSoundEvent;
import com.hypixel.hytale.server.core.asset.type.blocksound.config.BlockSoundSet;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.SoundUtil;
import com.hypixel.hytale.protocol.SoundCategory;
import com.hypixel.hytale.server.core.asset.type.soundevent.config.SoundEvent;
import com.hypixel.hytale.builtin.mounts.BlockMountAPI;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.protocol.BlockPosition;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.modules.interaction.interaction.CooldownHandler;
import com.hypixel.hytale.math.vector.Vector3i;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.entity.InteractionContext;
import com.hypixel.hytale.protocol.InteractionType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.server.core.universe.world.World;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.client.SimpleBlockInteraction;

public class SeatingInteraction extends SimpleBlockInteraction
{
    @Nonnull
    public static final BuilderCodec<SeatingInteraction> CODEC;
    
    @Override
    protected void interactWithBlock(@Nonnull final World world, @Nonnull final CommandBuffer<EntityStore> commandBuffer, @Nonnull final InteractionType type, @Nonnull final InteractionContext context, @Nullable final ItemStack itemInHand, @Nonnull final Vector3i targetBlock, @Nonnull final CooldownHandler cooldownHandler) {
        final Ref<EntityStore> ref = context.getEntity();
        final Player player = commandBuffer.getComponent(ref, Player.getComponentType());
        if (player == null) {
            return;
        }
        final BlockPosition rawTarget = context.getMetaStore().getMetaObject(SeatingInteraction.TARGET_BLOCK_RAW);
        final Vector3f whereWasHit = new Vector3f(rawTarget.x + 0.5f, rawTarget.y + 0.5f, rawTarget.z + 0.5f);
        final BlockMountAPI.BlockMountResult result = BlockMountAPI.mountOnBlock(ref, commandBuffer, targetBlock, whereWasHit);
        if (result == BlockMountAPI.DidNotMount.ALREADY_MOUNTED) {
            final int soundEventIndex = SoundEvent.getAssetMap().getIndex("SFX_Creative_Play_Add_Mask");
            SoundUtil.playSoundEvent2d(ref, soundEventIndex, SoundCategory.SFX, commandBuffer);
        }
        else if (result instanceof final BlockMountAPI.Mounted mounted) {
            final BlockSoundSet soundSet = BlockSoundSet.getAssetMap().getAsset(mounted.blockType().getBlockSoundSetIndex());
            final String seatSoundId = (soundSet == null) ? null : soundSet.getSoundEventIds().getOrDefault(BlockSoundEvent.Walk, null);
            if (seatSoundId != null) {
                final int soundEventIndex2 = SoundEvent.getAssetMap().getIndex(seatSoundId);
                SoundUtil.playSoundEvent3dToPlayer(ref, soundEventIndex2, SoundCategory.SFX, targetBlock.toVector3d(), commandBuffer);
            }
        }
        else {
            player.sendMessage(Message.translation("server.interactions.didNotMount").param("state", result.toString()));
        }
    }
    
    @Override
    protected void simulateInteractWithBlock(@Nonnull final InteractionType type, @Nonnull final InteractionContext context, @Nullable final ItemStack itemInHand, @Nonnull final World world, @Nonnull final Vector3i targetBlock) {
    }
    
    static {
        CODEC = BuilderCodec.builder(SeatingInteraction.class, SeatingInteraction::new, SimpleBlockInteraction.CODEC).documentation("Arranges perfect seating accommodations").build();
    }
}
