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

package com.hypixel.hytale.server.core.entity.entities;

import com.hypixel.hytale.server.core.modules.entity.BlockMigrationExtraInfo;
import com.hypixel.hytale.codec.ExtraInfo;
import javax.annotation.Nullable;
import com.hypixel.hytale.assetstore.map.BlockTypeAssetMap;
import com.hypixel.hytale.server.core.asset.type.blockhitbox.BlockBoundingBoxes;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.asset.type.projectile.config.Projectile;
import com.hypixel.hytale.server.core.modules.entity.component.BoundingBox;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.modules.physics.component.Velocity;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.modules.entity.DespawnComponent;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.modules.time.TimeResource;
import com.hypixel.hytale.server.core.modules.entity.EntityModule;
import com.hypixel.hytale.component.ComponentType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.modules.physics.SimplePhysicsProvider;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Component;

public class BlockEntity implements Component<EntityStore>
{
    public static final BuilderCodec<BlockEntity> CODEC;
    public static final int DEFAULT_DESPAWN_SECONDS = 120;
    @Nonnull
    private transient SimplePhysicsProvider simplePhysicsProvider;
    protected String blockTypeKey;
    private boolean isBlockIdNetworkOutdated;
    
    public static ComponentType<EntityStore, BlockEntity> getComponentType() {
        return EntityModule.get().getBlockEntityComponentType();
    }
    
    protected BlockEntity() {
        this.simplePhysicsProvider = new SimplePhysicsProvider();
    }
    
    public BlockEntity(final String blockTypeKey) {
        this.simplePhysicsProvider = new SimplePhysicsProvider();
        this.blockTypeKey = blockTypeKey;
    }
    
    @Nonnull
    public static Holder<EntityStore> assembleDefaultBlockEntity(@Nonnull final TimeResource time, final String blockTypeKey, @Nonnull final Vector3d position) {
        final Holder<EntityStore> holder = EntityStore.REGISTRY.newHolder();
        holder.addComponent(getComponentType(), new BlockEntity(blockTypeKey));
        holder.addComponent(DespawnComponent.getComponentType(), DespawnComponent.despawnInSeconds(time, 120));
        holder.addComponent(TransformComponent.getComponentType(), new TransformComponent(position.clone(), Vector3f.FORWARD));
        holder.ensureComponent(Velocity.getComponentType());
        holder.ensureComponent(UUIDComponent.getComponentType());
        return holder;
    }
    
    @Nonnull
    public SimplePhysicsProvider initPhysics(@Nonnull final BoundingBox boundingBox) {
        this.simplePhysicsProvider.initialize(Projectile.getAssetMap().getAsset("Projectile"), boundingBox);
        this.simplePhysicsProvider.setProvideCharacterCollisions(false);
        this.simplePhysicsProvider.setMoveOutOfSolid(true);
        return this.simplePhysicsProvider;
    }
    
    @Nonnull
    public BoundingBox updateHitbox(@Nonnull final Ref<EntityStore> ref, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final BoundingBox boundingBoxComponent = this.createBoundingBoxComponent();
        commandBuffer.putComponent(ref, BoundingBox.getComponentType(), boundingBoxComponent);
        return boundingBoxComponent;
    }
    
    @Nullable
    public BoundingBox createBoundingBoxComponent() {
        if (this.blockTypeKey == null) {
            return null;
        }
        final BlockTypeAssetMap<String, BlockType> assetMap = BlockType.getAssetMap();
        if (assetMap == null) {
            return null;
        }
        final BlockType blockType = assetMap.getAsset(this.blockTypeKey);
        if (blockType == null) {
            return null;
        }
        return new BoundingBox(BlockBoundingBoxes.getAssetMap().getAsset(blockType.getHitboxTypeIndex()).get(0).getBoundingBox());
    }
    
    public void setBlockTypeKey(final String blockTypeKey, @Nonnull final Ref<EntityStore> ref, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        this.blockTypeKey = blockTypeKey;
        this.isBlockIdNetworkOutdated = true;
        this.updateHitbox(ref, commandBuffer);
    }
    
    @Nonnull
    public SimplePhysicsProvider getSimplePhysicsProvider() {
        return this.simplePhysicsProvider;
    }
    
    public String getBlockTypeKey() {
        return this.blockTypeKey;
    }
    
    public void addForce(final float x, final float y, final float z) {
        this.simplePhysicsProvider.addVelocity(x, y, z);
    }
    
    public void addForce(@Nonnull final Vector3d force) {
        this.simplePhysicsProvider.addVelocity((float)force.x, (float)force.y, (float)force.z);
    }
    
    public boolean consumeBlockIdNetworkOutdated() {
        final boolean temp = this.isBlockIdNetworkOutdated;
        this.isBlockIdNetworkOutdated = false;
        return temp;
    }
    
    @Nonnull
    @Override
    public Component<EntityStore> clone() {
        return new BlockEntity(this.blockTypeKey);
    }
    
    static {
        // 
        // This method could not be decompiled.
        // 
        // Original Bytecode:
        // 
        //     2: invokedynamic   BootstrapMethod #0, get:()Ljava/util/function/Supplier;
        //     7: invokestatic    com/hypixel/hytale/codec/builder/BuilderCodec.builder:(Ljava/lang/Class;Ljava/util/function/Supplier;)Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //    10: new             Lcom/hypixel/hytale/codec/KeyedCodec;
        //    13: dup            
        //    14: ldc             "BlockTypeKey"
        //    16: getstatic       com/hypixel/hytale/codec/Codec.STRING:Lcom/hypixel/hytale/codec/codecs/simple/StringCodec;
        //    19: invokespecial   com/hypixel/hytale/codec/KeyedCodec.<init>:(Ljava/lang/String;Lcom/hypixel/hytale/codec/Codec;)V
        //    22: invokedynamic   BootstrapMethod #1, accept:()Lcom/hypixel/hytale/function/consumer/TriConsumer;
        //    27: invokedynamic   BootstrapMethod #2, apply:()Ljava/util/function/BiFunction;
        //    32: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.append:(Lcom/hypixel/hytale/codec/KeyedCodec;Lcom/hypixel/hytale/function/consumer/TriConsumer;Ljava/util/function/BiFunction;)Lcom/hypixel/hytale/codec/builder/BuilderField$FieldBuilder;
        //    35: invokevirtual   com/hypixel/hytale/codec/builder/BuilderField$FieldBuilder.add:()Lcom/hypixel/hytale/codec/builder/BuilderCodec$BuilderBase;
        //    38: checkcast       Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //    41: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.build:()Lcom/hypixel/hytale/codec/builder/BuilderCodec;
        //    44: putstatic       com/hypixel/hytale/server/core/entity/entities/BlockEntity.CODEC:Lcom/hypixel/hytale/codec/builder/BuilderCodec;
        //    47: return         
        // 
        // The error that occurred was:
        // 
        // java.lang.UnsupportedOperationException: The requested operation is not supported.
        //     at com.strobel.util.ContractUtils.unsupported(ContractUtils.java:27)
        //     at com.strobel.assembler.metadata.TypeReference.getRawType(TypeReference.java:284)
        //     at com.strobel.assembler.metadata.TypeReference.getRawType(TypeReference.java:279)
        //     at com.strobel.assembler.metadata.TypeReference.makeGenericType(TypeReference.java:154)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:225)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:25)
        //     at com.strobel.assembler.metadata.ParameterizedType.accept(ParameterizedType.java:103)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visit(TypeSubstitutionVisitor.java:40)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:211)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:25)
        //     at com.strobel.assembler.metadata.ParameterizedType.accept(ParameterizedType.java:103)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visit(TypeSubstitutionVisitor.java:40)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitMethod(TypeSubstitutionVisitor.java:314)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferCall(TypeAnalysis.java:2611)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1040)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:782)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:778)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferCall(TypeAnalysis.java:2483)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1040)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:782)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:778)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1510)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:782)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:778)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferCall(TypeAnalysis.java:2483)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1040)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.runInference(TypeAnalysis.java:684)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypesForVariables(TypeAnalysis.java:593)
        //     at com.strobel.decompiler.ast.TypeAnalysis.runInference(TypeAnalysis.java:405)
        //     at com.strobel.decompiler.ast.TypeAnalysis.run(TypeAnalysis.java:95)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:109)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:42)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:206)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:93)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethodBody(AstBuilder.java:868)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethod(AstBuilder.java:761)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addTypeMembers(AstBuilder.java:638)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeCore(AstBuilder.java:605)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeNoCache(AstBuilder.java:195)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createType(AstBuilder.java:162)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addType(AstBuilder.java:137)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.buildAst(JavaLanguage.java:71)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.decompileType(JavaLanguage.java:59)
        //     at com.strobel.decompiler.DecompilerDriver.decompileType(DecompilerDriver.java:333)
        //     at com.strobel.decompiler.DecompilerDriver.decompileJar(DecompilerDriver.java:254)
        //     at com.strobel.decompiler.DecompilerDriver.main(DecompilerDriver.java:129)
        // 
        throw new IllegalStateException("An error occurred while decompiling this method.");
    }
}
