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

package com.hypixel.hytale.server.core.universe.world.meta.state;

import com.hypixel.hytale.server.core.entity.entities.player.data.PlayerWorldData;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.common.util.ArrayUtil;
import com.hypixel.hytale.server.core.entity.entities.player.data.PlayerRespawnPointData;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.entity.entities.Player;
import java.util.logging.Level;
import com.hypixel.hytale.server.core.universe.Universe;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.query.Query;
import javax.annotation.Nonnull;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.component.system.RefSystem;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.modules.block.BlockModule;
import com.hypixel.hytale.component.ComponentType;
import java.util.UUID;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.component.Component;

public class RespawnBlock implements Component<ChunkStore>
{
    public static final BuilderCodec<RespawnBlock> CODEC;
    private UUID ownerUUID;
    
    public static ComponentType<ChunkStore, RespawnBlock> getComponentType() {
        return BlockModule.get().getRespawnBlockComponentType();
    }
    
    public RespawnBlock() {
    }
    
    public RespawnBlock(final UUID ownerUUID) {
        this.ownerUUID = ownerUUID;
    }
    
    public UUID getOwnerUUID() {
        return this.ownerUUID;
    }
    
    public void setOwnerUUID(final UUID ownerUUID) {
        this.ownerUUID = ownerUUID;
    }
    
    @Nullable
    @Override
    public Component<ChunkStore> clone() {
        return new RespawnBlock(this.ownerUUID);
    }
    
    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             "OwnerUUID"
        //    16: getstatic       com/hypixel/hytale/codec/Codec.UUID_BINARY:Lcom/hypixel/hytale/codec/codecs/UUIDBinaryCodec;
        //    19: invokespecial   com/hypixel/hytale/codec/KeyedCodec.<init>:(Ljava/lang/String;Lcom/hypixel/hytale/codec/Codec;)V
        //    22: invokedynamic   BootstrapMethod #1, accept:()Ljava/util/function/BiConsumer;
        //    27: invokedynamic   BootstrapMethod #2, apply:()Ljava/util/function/Function;
        //    32: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.append:(Lcom/hypixel/hytale/codec/KeyedCodec;Ljava/util/function/BiConsumer;Ljava/util/function/Function;)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/universe/world/meta/state/RespawnBlock.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.");
    }
    
    public static class OnRemove extends RefSystem<ChunkStore>
    {
        @Nonnull
        private static final HytaleLogger LOGGER;
        public static final ComponentType<ChunkStore, RespawnBlock> COMPONENT_TYPE_RESPAWN_BLOCK;
        public static final ComponentType<ChunkStore, BlockModule.BlockStateInfo> COMPONENT_TYPE_BLOCK_STATE_INFO;
        @Nonnull
        public static final Query<ChunkStore> QUERY;
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<ChunkStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<ChunkStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<ChunkStore> store, @Nonnull final CommandBuffer<ChunkStore> commandBuffer) {
            if (reason == RemoveReason.UNLOAD) {
                return;
            }
            final RespawnBlock respawnState = commandBuffer.getComponent(ref, OnRemove.COMPONENT_TYPE_RESPAWN_BLOCK);
            assert respawnState != null;
            if (respawnState.ownerUUID == null) {
                return;
            }
            final BlockModule.BlockStateInfo blockStateInfoComponent = commandBuffer.getComponent(ref, OnRemove.COMPONENT_TYPE_BLOCK_STATE_INFO);
            assert blockStateInfoComponent != null;
            final PlayerRef playerRef = Universe.get().getPlayer(respawnState.ownerUUID);
            if (playerRef == null) {
                OnRemove.LOGGER.at(Level.WARNING).log("Failed to fetch player ref during removal of respawn block entity.");
                return;
            }
            final Player playerComponent = playerRef.getComponent(Player.getComponentType());
            if (playerComponent == null) {
                OnRemove.LOGGER.at(Level.WARNING).log("Failed to fetch player component during removal of respawn block entity.");
                return;
            }
            final Ref<ChunkStore> chunkRef = blockStateInfoComponent.getChunkRef();
            if (!chunkRef.isValid()) {
                return;
            }
            final World world = commandBuffer.getExternalData().getWorld();
            final PlayerWorldData playerWorldData = playerComponent.getPlayerConfigData().getPerWorldData(world.getName());
            final PlayerRespawnPointData[] respawnPoints = playerWorldData.getRespawnPoints();
            if (respawnPoints == null) {
                OnRemove.LOGGER.at(Level.WARNING).log("Failed to find valid respawn points for player " + String.valueOf(respawnState.ownerUUID) + " during removal of respawn block entity.");
                return;
            }
            final WorldChunk worldChunkComponent = commandBuffer.getComponent(chunkRef, WorldChunk.getComponentType());
            assert worldChunkComponent != null;
            final Vector3i blockPosition = new Vector3i(ChunkUtil.worldCoordFromLocalCoord(worldChunkComponent.getX(), ChunkUtil.xFromBlockInColumn(blockStateInfoComponent.getIndex())), ChunkUtil.yFromBlockInColumn(blockStateInfoComponent.getIndex()), ChunkUtil.worldCoordFromLocalCoord(worldChunkComponent.getZ(), ChunkUtil.zFromBlockInColumn(blockStateInfoComponent.getIndex())));
            for (int i = 0; i < respawnPoints.length; ++i) {
                final PlayerRespawnPointData respawnPoint = respawnPoints[i];
                if (respawnPoint.getBlockPosition().equals(blockPosition)) {
                    OnRemove.LOGGER.at(Level.INFO).log("Removing respawn point for player " + String.valueOf(respawnState.ownerUUID) + " at position " + String.valueOf(blockPosition) + " due to respawn block removal.");
                    playerWorldData.setRespawnPoints(ArrayUtil.remove(respawnPoints, i));
                    return;
                }
            }
        }
        
        @Nullable
        @Override
        public Query<ChunkStore> getQuery() {
            return OnRemove.QUERY;
        }
        
        static {
            LOGGER = HytaleLogger.forEnclosingClass();
            COMPONENT_TYPE_RESPAWN_BLOCK = RespawnBlock.getComponentType();
            COMPONENT_TYPE_BLOCK_STATE_INFO = BlockModule.BlockStateInfo.getComponentType();
            QUERY = Query.and(OnRemove.COMPONENT_TYPE_RESPAWN_BLOCK, OnRemove.COMPONENT_TYPE_BLOCK_STATE_INFO);
        }
    }
}
