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 xd, final LivingEntity akz, final long long3) {
        final Brain<?> alp6 = akz.getBrain();
        final Path cqq7 = alp6.<Path>getMemory(MemoryModuleType.PATH).get();
        final List<GlobalPos> list8 = alp6.<List<GlobalPos>>getMemory(MemoryModuleType.INTERACTABLE_DOORS).get();
        final List<BlockPos> list9 = cqq7.getNodes().stream().map(cqo -> new BlockPos(cqo.x, cqo.y, cqo.z)).collect(Collectors.toList());
        final Set<BlockPos> set10 = this.getDoorsThatAreOnMyPath(xd, list8, list9);
        final int integer11 = cqq7.getIndex() - 1;
        this.openOrCloseDoors(xd, list9, set10, integer11, akz, alp6);
    }
    
    private Set<BlockPos> getDoorsThatAreOnMyPath(final ServerLevel xd, final List<GlobalPos> list2, final List<BlockPos> list3) {
        return list2.stream().filter(fr -> fr.dimension() == xd.getDimension().getType()).map(GlobalPos::pos).filter(list3::contains).collect(Collectors.toSet());
    }
    
    private void openOrCloseDoors(final ServerLevel xd, final List<BlockPos> list, final Set<BlockPos> set, final int integer, final LivingEntity akz, final Brain<?> alp) {
        // 
        // This method could not be decompiled.
        // 
        // Original Bytecode:
        // 
        //     1: aload_2         /* list */
        //     2: aload_1         /* xd */
        //     3: iload           integer
        //     5: aload           alp
        //     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         /* xd */
        //    18: aload_2         /* list */
        //    19: iload           integer
        //    21: aload           akz
        //    23: aload           alp
        //    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  
        //  -------  -----
        //  xd       
        //  list     
        //  set      
        //  integer  
        //  akz      
        //  alp      
        // 
        // 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 xd, final List<BlockPos> list, final int integer, final LivingEntity akz, final Brain<?> alp) {
        final Iterator<GlobalPos> iterator6;
        GlobalPos fr7;
        BlockPos fk8;
        int integer2;
        BlockState byj10;
        Block bph11;
        alp.<Set<GlobalPos>>getMemory(MemoryModuleType.OPENED_DOORS).ifPresent(set -> {
            iterator6 = set.iterator();
            while (iterator6.hasNext()) {
                fr7 = iterator6.next();
                fk8 = fr7.pos();
                integer2 = list.indexOf(fk8);
                if (xd.getDimension().getType() != fr7.dimension()) {
                    iterator6.remove();
                }
                else {
                    byj10 = xd.getBlockState(fk8);
                    bph11 = byj10.getBlock();
                    if (BlockTags.WOODEN_DOORS.contains(bph11) && bph11 instanceof DoorBlock && integer2 < integer && fk8.closerThan(akz.position(), 4.0)) {
                        ((DoorBlock)bph11).setOpen(xd, fk8, false);
                        iterator6.remove();
                    }
                    else {
                        continue;
                    }
                }
            }
        });
    }
}
