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

package com.hypixel.hytale.builtin.adventure.objectives.interactions;

import java.util.UUID;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.universe.world.meta.state.RespawnBlock;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockComponentChunk;
import com.hypixel.hytale.protocol.InteractionState;
import com.hypixel.hytale.math.util.ChunkUtil;
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.protocol.WaitForDataFrom;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.server.core.modules.interaction.interaction.config.client.SimpleBlockInteraction;

public class CanBreakRespawnPointInteraction extends SimpleBlockInteraction
{
    public static final BuilderCodec<CanBreakRespawnPointInteraction> CODEC;
    
    @Nonnull
    @Override
    public WaitForDataFrom getWaitForDataFrom() {
        return WaitForDataFrom.Server;
    }
    
    @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 ChunkStore chunkStore = world.getChunkStore();
        final Ref<ChunkStore> chunk = chunkStore.getChunkReference(ChunkUtil.indexChunkFromBlock(targetBlock.x, targetBlock.z));
        if (chunk == null) {
            context.getState().state = InteractionState.Failed;
            return;
        }
        final BlockComponentChunk blockComp = chunkStore.getStore().getComponent(chunk, BlockComponentChunk.getComponentType());
        if (blockComp == null) {
            context.getState().state = InteractionState.Failed;
            return;
        }
        final Ref<ChunkStore> blockEntity = blockComp.getEntityReference(ChunkUtil.indexBlockInColumn(targetBlock.x, targetBlock.y, targetBlock.z));
        if (blockEntity == null) {
            context.getState().state = InteractionState.Finished;
            return;
        }
        final RespawnBlock respawnState = chunkStore.getStore().getComponent(blockEntity, RespawnBlock.getComponentType());
        if (respawnState == null) {
            context.getState().state = InteractionState.Finished;
            return;
        }
        final UUIDComponent uuidComponent = commandBuffer.getComponent(context.getOwningEntity(), UUIDComponent.getComponentType());
        if (uuidComponent == null) {
            context.getState().state = InteractionState.Failed;
            return;
        }
        final UUID ownerUUID = respawnState.getOwnerUUID();
        if (ownerUUID == null || uuidComponent.getUuid().equals(ownerUUID)) {
            context.getState().state = InteractionState.Finished;
            return;
        }
        context.getState().state = InteractionState.Failed;
    }
    
    @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(CanBreakRespawnPointInteraction.class, CanBreakRespawnPointInteraction::new, SimpleBlockInteraction.CODEC).build();
    }
}
