package net.minecraft.world.entity.ai.behavior;

import net.minecraft.world.level.pathfinder.Node;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Iterator;
import net.minecraft.world.level.Level;
import net.minecraft.core.Position;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.tags.BlockTags;
import java.util.function.Predicate;
import java.util.function.Function;
import java.util.Set;
import net.minecraft.world.entity.ai.Brain;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import java.util.List;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import net.minecraft.world.level.pathfinder.Path;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.LivingEntity;

public class InteractWithDoor extends Behavior<LivingEntity> {
    public InteractWithDoor() {
        super(ImmutableMap.<MemoryModuleType<Path>, MemoryStatus>of(MemoryModuleType.PATH, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<Path>)MemoryModuleType.INTERACTABLE_DOORS, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<Path>)MemoryModuleType.OPENED_DOORS, MemoryStatus.REGISTERED));
    }
    
    @Override
    protected void start(final ServerLevel zd, final LivingEntity aoy, final long long3) {
        final Brain<?> apr6 = aoy.getBrain();
        final Path czf7 = apr6.<Path>getMemory(MemoryModuleType.PATH).get();
        final List<GlobalPos> list8 = apr6.<List<GlobalPos>>getMemory(MemoryModuleType.INTERACTABLE_DOORS).get();
        final List<BlockPos> list9 = czf7.getNodes().stream().map(czd -> new BlockPos(czd.x, czd.y, czd.z)).collect(Collectors.toList());
        final Set<BlockPos> set10 = this.getDoorsThatAreOnMyPath(zd, list8, list9);
        final int integer11 = czf7.getIndex() - 1;
        this.openOrCloseDoors(zd, list9, set10, integer11, aoy, apr6);
    }
    
    private Set<BlockPos> getDoorsThatAreOnMyPath(final ServerLevel zd, final List<GlobalPos> list2, final List<BlockPos> list3) {
        return list2.stream().filter(gc -> gc.dimension() == zd.dimension()).map(GlobalPos::pos).filter(list3::contains).collect(Collectors.toSet());
    }
    
    private void openOrCloseDoors(final ServerLevel zd, final List<BlockPos> list, final Set<BlockPos> set, final int integer, final LivingEntity aoy, final Brain<?> apr) {
        // 
        // This method could not be decompiled.
        // 
        // Original Bytecode:
        // 
        //     1: aload_2         /* list */
        //     2: aload_1         /* zd */
        //     3: iload           integer
        //     5: aload           apr
        //     7: invokedynamic   BootstrapMethod #4, accept:(Ljava/util/List;Lnet/minecraft/server/level/ServerLevel;ILnet/minecraft/world/entity/ai/Brain;)Ljava/util/function/Consumer;
        //    12: invokeinterface java/util/Set.forEach:(Ljava/util/function/Consumer;)V
        //    17: aload_1         /* zd */
        //    18: aload_2         /* list */
        //    19: iload           integer
        //    21: aload           aoy
        //    23: aload           apr
        //    25: invokestatic    net/minecraft/world/entity/ai/behavior/InteractWithDoor.closeAllOpenedDoors:(Lnet/minecraft/server/level/ServerLevel;Ljava/util/List;ILnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/entity/ai/Brain;)V
        //    28: return         
        //    Signature:
        //  (Lnet/minecraft/server/level/ServerLevel;Ljava/util/List<Lnet/minecraft/core/BlockPos;>;Ljava/util/Set<Lnet/minecraft/core/BlockPos;>;ILnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/entity/ai/Brain<*>;)V
        //    MethodParameters:
        //  Name     Flags  
        //  -------  -----
        //  zd       
        //  list     
        //  set      
        //  integer  
        //  aoy      
        //  apr      
        // 
        // The error that occurred was:
        // 
        // java.lang.NullPointerException
        //     at com.strobel.decompiler.languages.java.ast.NameVariables.generateNameForVariable(NameVariables.java:264)
        //     at com.strobel.decompiler.languages.java.ast.NameVariables.assignNamesToVariables(NameVariables.java:198)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:276)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:99)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethodBody(AstBuilder.java:782)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethod(AstBuilder.java:675)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addTypeMembers(AstBuilder.java:552)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeCore(AstBuilder.java:519)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeNoCache(AstBuilder.java:161)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createType(AstBuilder.java:150)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addType(AstBuilder.java:125)
        //     at cuchaz.enigma.SourceProvider.getSources(SourceProvider.java:85)
        //     at cuchaz.enigma.EnigmaProject$JarExport.decompileClass(EnigmaProject.java:241)
        //     at cuchaz.enigma.EnigmaProject$JarExport.lambda$decompile$1(EnigmaProject.java:230)
        //     at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        //     at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
        //     at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        //     at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        //     at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:952)
        //     at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:926)
        //     at java.base/java.util.stream.AbstractTask.compute(AbstractTask.java:327)
        //     at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
        //     at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        //     at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
        //     at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
        //     at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
        //     at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
        // 
        throw new IllegalStateException("An error occurred while decompiling this method.");
    }
    
    public static void closeAllOpenedDoors(final ServerLevel zd, final List<BlockPos> list, final int integer, final LivingEntity aoy, final Brain<?> apr) {
        final Iterator<GlobalPos> iterator6;
        GlobalPos gc7;
        BlockPos fu8;
        int integer2;
        BlockState cfj10;
        Block bvr11;
        apr.<Set<GlobalPos>>getMemory(MemoryModuleType.OPENED_DOORS).ifPresent(set -> {
            iterator6 = set.iterator();
            while (iterator6.hasNext()) {
                gc7 = iterator6.next();
                fu8 = gc7.pos();
                integer2 = list.indexOf(fu8);
                if (zd.dimension() != gc7.dimension()) {
                    iterator6.remove();
                }
                else {
                    cfj10 = zd.getBlockState(fu8);
                    bvr11 = cfj10.getBlock();
                    if (BlockTags.WOODEN_DOORS.contains(bvr11) && bvr11 instanceof DoorBlock && integer2 < integer && fu8.closerThan(aoy.position(), 4.0)) {
                        ((DoorBlock)bvr11).setOpen(zd, fu8, false);
                        iterator6.remove();
                    }
                    else {
                        continue;
                    }
                }
            }
        });
    }
}
