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

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

import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.universe.world.meta.BlockState;
import com.hypixel.hytale.server.core.universe.world.chunk.section.BlockSection;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.server.core.util.FillerBlockUtil;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.server.core.universe.world.meta.state.ItemContainerState;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
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.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class StashCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_STASH_DROP_LIST_SET;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_STASH_NO_DROP_LIST;
    @Nonnull
    private static final Message MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE;
    private static final int DISTANCE_MAX = 10;
    @Nonnull
    private final OptionalArg<String> setArg;
    
    public StashCommand() {
        super("stash", "server.commands.stash.getDroplist.desc");
        this.setArg = this.withOptionalArg("set", "server.commands.stash.setDroplist.desc", ArgTypes.STRING);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
        final ItemContainerState itemContainerState = this.getItemContainerState(ref, world, context, store);
        if (itemContainerState == null) {
            return;
        }
        if (this.setArg.provided(context)) {
            final String dropList = this.setArg.get(context);
            itemContainerState.setDroplist(dropList);
            context.sendMessage(StashCommand.MESSAGE_COMMANDS_STASH_DROP_LIST_SET);
        }
        else {
            final String droplist = itemContainerState.getDroplist();
            if (droplist != null) {
                context.sendMessage(Message.translation("server.commands.stash.currentDroplist").param("droplist", droplist));
            }
            else {
                context.sendMessage(StashCommand.MESSAGE_COMMANDS_STASH_NO_DROP_LIST);
            }
        }
    }
    
    @Nullable
    private ItemContainerState getItemContainerState(@Nonnull final Ref<EntityStore> ref, @Nonnull final World world, @Nonnull final CommandContext context, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final Vector3i block = TargetUtil.getTargetBlock(ref, 10.0, componentAccessor);
        if (block == null) {
            context.sendMessage(StashCommand.MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE);
            return null;
        }
        final ChunkStore chunkStore = world.getChunkStore();
        final long chunkIndex = ChunkUtil.indexChunkFromBlock(block.x, block.z);
        final Ref<ChunkStore> chunkRef = chunkStore.getChunkReference(chunkIndex);
        if (chunkRef == null || !chunkRef.isValid()) {
            final int chunkX = ChunkUtil.chunkCoordinate(block.x);
            final int chunkZ = ChunkUtil.chunkCoordinate(block.z);
            context.sendMessage(Message.translation("server.commands.errors.chunkNotLoaded").param("chunkX", chunkX).param("chunkZ", chunkZ).param("world", world.getName()));
            return null;
        }
        final Store<ChunkStore> chunkStoreStore = chunkStore.getStore();
        final BlockChunk blockChunkComponent = chunkStoreStore.getComponent(chunkRef, BlockChunk.getComponentType());
        assert blockChunkComponent != null;
        final WorldChunk worldChunkComponent = chunkStoreStore.getComponent(chunkRef, WorldChunk.getComponentType());
        assert worldChunkComponent != null;
        final BlockSection section = blockChunkComponent.getSectionAtBlockY(block.y);
        final int filler = section.getFiller(block.x, block.y, block.z);
        if (filler != 0) {
            final Vector3i vector3i = block;
            vector3i.x -= FillerBlockUtil.unpackX(filler);
            final Vector3i vector3i2 = block;
            vector3i2.y -= FillerBlockUtil.unpackY(filler);
            final Vector3i vector3i3 = block;
            vector3i3.z -= FillerBlockUtil.unpackZ(filler);
        }
        final BlockState state = worldChunkComponent.getState(block.x, block.y, block.z);
        if (!(state instanceof ItemContainerState)) {
            context.sendMessage(Message.translation("server.general.containerNotFound").param("block", block.toString()));
            return null;
        }
        return (ItemContainerState)state;
    }
    
    static {
        MESSAGE_COMMANDS_STASH_DROP_LIST_SET = Message.translation("server.commands.stash.droplistSet");
        MESSAGE_COMMANDS_STASH_NO_DROP_LIST = Message.translation("server.commands.stash.noDroplist");
        MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE = Message.translation("server.general.blockTargetNotInRange");
    }
}
