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

package com.hypixel.hytale.server.core.modules.item.commands;

import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.entity.ItemUtils;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.server.core.modules.entity.item.ItemComponent;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import java.util.concurrent.ThreadLocalRandom;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.permissions.PermissionHolder;
import com.hypixel.hytale.server.core.command.system.CommandUtil;
import com.hypixel.hytale.server.core.permissions.HytalePermissions;
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 com.hypixel.hytale.server.core.command.system.arguments.system.DefaultArg;
import com.hypixel.hytale.server.core.asset.type.item.config.Item;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class SpawnItemCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
    @Nonnull
    private final RequiredArg<Item> itemArg;
    @Nonnull
    private final DefaultArg<Integer> quantityArg;
    @Nonnull
    private final OptionalArg<Integer> countArg;
    @Nonnull
    private final DefaultArg<Float> forceArg;
    
    public SpawnItemCommand() {
        super("spawnitem", "server.commands.spawnitem.desc");
        this.itemArg = this.withRequiredArg("item", "server.commands.spawnitem.item.desc", ArgTypes.ITEM_ASSET);
        this.quantityArg = this.withDefaultArg("qty", "server.commands.spawnitem.quantity.desc", ArgTypes.INTEGER, 1, "1");
        this.countArg = this.withOptionalArg("count", "server.commands.spawnitem.count.desc", ArgTypes.INTEGER).addAliases("n");
        this.forceArg = this.withDefaultArg("force", "server.commands.spawnitem.force.desc", ArgTypes.FLOAT, 1.0f, "1.0").addAliases("x");
    }
    
    @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) {
        CommandUtil.requirePermission(context.sender(), HytalePermissions.fromCommand("spawnitem"));
        final Item item = this.itemArg.get(context);
        final String itemId = item.getId();
        final int quantity = this.quantityArg.get(context);
        final float force = this.forceArg.get(context);
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        final ModelComponent modelComponent = store.getComponent(ref, ModelComponent.getComponentType());
        if (transformComponent == null || modelComponent == null) {
            context.sendMessage(SpawnItemCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
            return;
        }
        final Vector3d playerPosition = transformComponent.getPosition();
        final Model playerModel = modelComponent.getModel();
        final float throwSpeed = 6.0f * force;
        if (this.countArg.provided(context)) {
            final int count = this.countArg.get(context);
            final Vector3d throwPosition = playerPosition.clone();
            throwPosition.add(0.0, playerModel.getEyeHeight(ref, store), 0.0);
            final ThreadLocalRandom random = ThreadLocalRandom.current();
            for (int i = 0; i < count; ++i) {
                final Holder<EntityStore> itemEntityHolder = ItemComponent.generateItemDrop(store, new ItemStack(itemId, quantity), throwPosition, Vector3f.ZERO, (float)random.nextGaussian() * throwSpeed, 0.5f, (float)random.nextGaussian() * throwSpeed);
                final ItemComponent itemEntityComponent = itemEntityHolder.getComponent(ItemComponent.getComponentType());
                if (itemEntityComponent != null) {
                    itemEntityComponent.setPickupDelay(1.5f);
                }
                store.addEntity(itemEntityHolder, AddReason.SPAWN);
            }
            final int totalQuantity = count * quantity;
            context.sendMessage(Message.translation("server.commands.spawnitem.spawnedMultiple").param("count", count).param("quantity", quantity).param("total", totalQuantity).param("item", itemId));
        }
        else {
            ItemUtils.throwItem(ref, new ItemStack(itemId, quantity), throwSpeed, store);
            context.sendMessage(Message.translation("server.commands.spawnitem.spawned").param("quantity", quantity).param("item", itemId));
        }
    }
    
    static {
        MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
    }
}
