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

import net.minecraft.server.level.ServerLevel;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import java.util.Map;
import java.util.function.Predicate;
import net.minecraft.world.entity.LivingEntity;

public class RunIf<E extends LivingEntity> extends Behavior<E> {
    private final Predicate<E> predicate;
    private final Behavior<? super E> wrappedBehavior;
    private final boolean checkWhileRunningAlso;
    
    public RunIf(final Map<MemoryModuleType<?>, MemoryStatus> map, final Predicate<E> predicate, final Behavior<? super E> aqh, final boolean boolean4) {
        super(mergeMaps(map, aqh.entryCondition));
        this.predicate = predicate;
        this.wrappedBehavior = aqh;
        this.checkWhileRunningAlso = boolean4;
    }
    
    private static Map<MemoryModuleType<?>, MemoryStatus> mergeMaps(final Map<MemoryModuleType<?>, MemoryStatus> map1, final Map<MemoryModuleType<?>, MemoryStatus> map2) {
        final Map<MemoryModuleType<?>, MemoryStatus> map3 = Maps.newHashMap();
        map3.putAll(map1);
        map3.putAll(map2);
        return map3;
    }
    
    public RunIf(final Predicate<E> predicate, final Behavior<? super E> aqh) {
        this((Map)ImmutableMap.of(), predicate, aqh, false);
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final E aoy) {
        return this.predicate.test(aoy) && this.wrappedBehavior.checkExtraStartConditions(zd, aoy);
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel zd, final E aoy, final long long3) {
        return this.checkWhileRunningAlso && this.predicate.test(aoy) && this.wrappedBehavior.canStillUse(zd, aoy, long3);
    }
    
    @Override
    protected boolean timedOut(final long long1) {
        return false;
    }
    
    @Override
    protected void start(final ServerLevel zd, final E aoy, final long long3) {
        this.wrappedBehavior.start(zd, aoy, long3);
    }
    
    @Override
    protected void tick(final ServerLevel zd, final E aoy, final long long3) {
        this.wrappedBehavior.tick(zd, aoy, long3);
    }
    
    @Override
    protected void stop(final ServerLevel zd, final E aoy, final long long3) {
        this.wrappedBehavior.stop(zd, aoy, long3);
    }
    
    @Override
    public String toString() {
        return "RunIf: " + this.wrappedBehavior;
    }
}
