package com.dairymoose.modernlife.core;

import com.dairymoose.entity.DummyEntity;
import com.dairymoose.entity.RenderableBlock;
import com.dairymoose.modernlife.blocks.gui.HBlockPos;
import com.dairymoose.modernlife.network.play.client.ClientboundWinchDummyPacket;
import com.dairymoose.modernlife.tileentities.WinchBlockEntity;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.function.BiFunction;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.PacketDistributor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/core/BlockVolume.class */
public class BlockVolume {
    public int minX;
    public int minY;
    public int minZ;
    public int maxX;
    public int maxY;
    public int maxZ;
    public boolean valid;
    private static final Logger LOGGER = LogManager.getLogger();
    public static boolean shouldFloat = false;
    public static DummyEntity dummy = null;

    public BlockVolume(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) {
        this.valid = false;
        this.minX = minX;
        this.minY = minY;
        this.minZ = minZ;
        this.maxX = maxX;
        this.maxY = maxY;
        this.maxZ = maxZ;
    }

    public BlockVolume(Level level, BlockPos root, Block anchorBlock) {
        this.valid = false;
        BlockState state = level.getBlockState(root);
        if (state.is(anchorBlock)) {
            Queue<HBlockPos> toVisit = new LinkedList<>();
            Set<HBlockPos> visited = new HashSet<>();
            this.valid = true;
            this.minX = root.getX();
            this.minY = root.getY();
            this.minZ = root.getZ();
            this.maxX = root.getX();
            this.maxY = root.getY();
            this.maxZ = root.getZ();
            toVisit.add(new HBlockPos(root));
            while (!toVisit.isEmpty()) {
                HBlockPos currentPos = toVisit.remove();
                int x = currentPos.getX();
                int y = currentPos.getY();
                int z = currentPos.getZ();
                if (x < this.minX) {
                    this.minX = x;
                }
                if (y < this.minY) {
                    this.minY = y;
                }
                if (z < this.minZ) {
                    this.minZ = z;
                }
                if (x > this.maxX) {
                    this.maxX = x;
                }
                if (y > this.maxY) {
                    this.maxY = y;
                }
                if (z > this.maxZ) {
                    this.maxZ = z;
                }
                HBlockPos northPos = new HBlockPos(currentPos.north());
                HBlockPos eastPos = new HBlockPos(currentPos.east());
                HBlockPos southPos = new HBlockPos(currentPos.south());
                HBlockPos westPos = new HBlockPos(currentPos.west());
                HBlockPos upPos = new HBlockPos(currentPos.above());
                HBlockPos downPos = new HBlockPos(currentPos.below());
                BlockState northState = level.getBlockState(northPos);
                BlockState eastState = level.getBlockState(eastPos);
                BlockState southState = level.getBlockState(southPos);
                BlockState westState = level.getBlockState(westPos);
                BlockState upState = level.getBlockState(upPos);
                BlockState downState = level.getBlockState(downPos);
                if (!visited.contains(northPos) && northState.is(anchorBlock)) {
                    visited.add(northPos);
                    toVisit.add(northPos);
                }
                if (!visited.contains(eastPos) && eastState.is(anchorBlock)) {
                    visited.add(eastPos);
                    toVisit.add(eastPos);
                }
                if (!visited.contains(southPos) && southState.is(anchorBlock)) {
                    visited.add(southPos);
                    toVisit.add(southPos);
                }
                if (!visited.contains(westPos) && westState.is(anchorBlock)) {
                    visited.add(westPos);
                    toVisit.add(westPos);
                }
                if (!visited.contains(upPos) && upState.is(anchorBlock)) {
                    visited.add(upPos);
                    toVisit.add(upPos);
                }
                if (!visited.contains(downPos) && downState.is(anchorBlock)) {
                    visited.add(downPos);
                    toVisit.add(downPos);
                }
            }
        }
    }

    private boolean isWithinVolume(BlockPos pos) {
        if (pos.getX() >= this.minX && pos.getX() <= this.maxX && pos.getY() >= this.minY && pos.getY() <= this.maxY && pos.getZ() >= this.minZ && pos.getZ() <= this.maxZ) {
            return true;
        }
        return false;
    }

    private boolean isValidSourceBlock(BlockState sourceState) {
        return (sourceState.is(CustomBlocks.BLOCK_PHOTON.get()) || !sourceState.m_155947_()) && sourceState.getFluidState().isEmpty();
    }

    private boolean isValidDestBlock(BlockState destinationState, boolean destroyChain, boolean destIsChain) {
        return (destinationState.is(CustomBlocks.BLOCK_PHOTON.get()) || !destinationState.m_155947_()) && (destinationState.isAir() || (destroyChain && destIsChain)) && destinationState.getFluidState().isEmpty();
    }

    private boolean canMoveLayer(BlockPos cornerPos, boolean increaseXYZ, Level level, BiFunction<BlockState, BlockPos, BlockPos> posShifter, BlockState shifterState, boolean destroyChain, WinchBlockEntity winchBlockEntity) {
        BlockPos currentPos;
        int xIterations = (this.maxX - this.minX) + 1;
        int zIterations = (this.maxZ - this.minZ) + 1;
        for (int x = 0; x < xIterations; x++) {
            for (int z = 0; z < zIterations; z++) {
                if (increaseXYZ) {
                    currentPos = new BlockPos(cornerPos.getX() + x, cornerPos.getY(), cornerPos.getZ() + z);
                } else {
                    currentPos = new BlockPos(cornerPos.getX() - x, cornerPos.getY(), cornerPos.getZ() - z);
                }
                BlockPos destination = posShifter.apply(shifterState, currentPos);
                BlockState sourceState = level.getBlockState(currentPos);
                BlockState destinationState = level.getBlockState(destination);
                boolean destIsChain = destinationState.is(Blocks.CHAIN);
                if (isValidDestBlock(destinationState, destroyChain, destIsChain) && isValidSourceBlock(sourceState)) {
                    if (destroyChain && destIsChain) {
                        int chainSlot = winchBlockEntity.getOpenChainSlot();
                        if (chainSlot == -1) {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    public void moveEntities(Level level, BlockPos destination, BlockState shifterState, BiFunction<BlockState, BlockPos, BlockPos> posShifter) {
        BlockPos entityTargetPosition = posShifter.apply(shifterState, destination);
        Vec3 entityTargetPositionVec = new Vec3(entityTargetPosition.getX(), entityTargetPosition.getY(), entityTargetPosition.getZ());
        Vec3 destinationVec = new Vec3(destination.getX() + 0.5d, destination.getY(), destination.getZ() + 0.5d);
        entityTargetPositionVec.subtract(destinationVec);
        List<Entity> destEntities = level.getEntities((Entity) null, new AABB(destination.getX(), destination.getY(), destination.getZ(), destination.getX() + 0.5d, destination.getY() + 0.5d, destination.getZ() + 0.5d));
        for (Entity e : destEntities) {
            e.getDeltaMovement();
            ((Player) level.m_6907_().get(0)).displayClientMessage(new TextComponent("destinationVec=" + destinationVec), false);
            e.moveTo(destinationVec);
        }
    }

    public void moveEntitiesAboveSource(Set<Integer> movedEntities, Level level, BlockPos src, BlockPos dest, BlockState shifterState, BiFunction<BlockState, BlockPos, BlockPos> posShifter, boolean doShift) {
        BlockPos destination = dest;
        if (doShift) {
            destination = posShifter.apply(shifterState, dest);
        }
        new Vec3(destination.getX() + 0.5d, destination.getY(), destination.getZ() + 0.5d);
        BlockPos abovePos = dest.above();
        dest.below();
        List<Entity> destEntities = level.m_45976_(Entity.class, new AABB(abovePos.getX(), abovePos.getY() - 0.5d, abovePos.getZ(), abovePos.getX() + 1.0d, abovePos.getY() + 1.8d, abovePos.getZ() + 1.0d));
        if (!level.getBlockState(abovePos).isAir() || !level.getBlockState(src).isAir()) {
            return;
        }
        List<Entity> otherEntities = level.m_6443_(Entity.class, new AABB(abovePos.getX(), abovePos.getY() - 0.9d, abovePos.getZ(), abovePos.getX() + 1.0d, abovePos.getY() + 1.8d, abovePos.getZ() + 1.0d), x -> {
            return ((x instanceof Player) || (x instanceof Boat) || x.position().with(Direction.Axis.Y, 0.0d).distanceToSqr((double) abovePos.getX(), 0.0d, (double) abovePos.getZ()) > 2.25d) ? false : true;
        });
        List<Boat> boatEntities = level.m_6443_(Boat.class, new AABB(abovePos.getX() + 0.3d, abovePos.getY() - 1.8d, abovePos.getZ() + 0.3d, abovePos.getX() + 0.7d, abovePos.getY() + 1.8d, abovePos.getZ() + 0.7d), x2 -> {
            return x2.m_20182_().with(Direction.Axis.Y, 0.0d).distanceToSqr((double) abovePos.getX(), 0.0d, (double) abovePos.getZ()) <= 2.25d;
        });
        destEntities.addAll(otherEntities);
        destEntities.addAll(boatEntities);
        for (Entity e : destEntities) {
            if (!movedEntities.contains(Integer.valueOf(e.getId()))) {
                movedEntities.add(Integer.valueOf(e.getId()));
                if (e.getVehicle() == null) {
                    double destX = e.getX();
                    BlockPos eInitialPos = e.blockPosition();
                    BlockPos eFinalPos = posShifter.apply(shifterState, eInitialPos);
                    int yDiffShifted = eFinalPos.getY() - eInitialPos.getY();
                    double destY = e.getY() + yDiffShifted;
                    double destZ = e.getZ();
                    e.absMoveTo(destX, destY, destZ);
                    e.setPacketCoordinates(destX, destY, destZ);
                }
            }
        }
    }

    public boolean shiftBlocks(Level level, BiFunction<BlockState, BlockPos, BlockPos> posShifter, BlockState shifterState, boolean destroyChain, WinchBlockEntity winchBlockEntity) {
        BlockPos startingPos;
        boolean increaseXYZ;
        BlockPos blockPos;
        BlockPos currentPos;
        if (posShifter == null || shifterState == null) {
            return false;
        }
        BlockPos minPos = new BlockPos(this.minX, this.minY, this.minZ);
        BlockPos maxPos = new BlockPos(this.maxX, this.maxY, this.maxZ);
        BlockPos shiftedMinPos = posShifter.apply(shifterState, minPos);
        BlockPos shiftedMaxPos = posShifter.apply(shifterState, maxPos);
        if (isWithinVolume(shiftedMinPos) && isWithinVolume(shiftedMaxPos)) {
            return false;
        }
        if (isWithinVolume(shiftedMinPos)) {
            startingPos = maxPos;
            increaseXYZ = false;
        } else {
            startingPos = minPos;
            increaseXYZ = true;
        }
        Set<Integer> movedEntities = new HashSet<>();
        BlockPos cornerPos = startingPos;
        int yIterations = (this.maxY - this.minY) + 1;
        for (int y = 0; y < yIterations; y++) {
            int xIterations = (this.maxX - this.minX) + 1;
            int zIterations = (this.maxZ - this.minZ) + 1;
            if (!canMoveLayer(cornerPos, increaseXYZ, level, posShifter, shifterState, destroyChain, winchBlockEntity)) {
                return false;
            }
            for (int x = 0; x < xIterations; x++) {
                for (int z = 0; z < zIterations; z++) {
                    if (increaseXYZ) {
                        currentPos = new BlockPos(cornerPos.getX() + x, cornerPos.getY(), cornerPos.getZ() + z);
                    } else {
                        currentPos = new BlockPos(cornerPos.getX() - x, cornerPos.getY(), cornerPos.getZ() - z);
                    }
                    BlockPos destination = posShifter.apply(shifterState, currentPos);
                    if (increaseXYZ) {
                        moveEntitiesAboveSource(movedEntities, level, currentPos, destination, shifterState, posShifter, !increaseXYZ);
                    }
                    BlockState sourceState = level.getBlockState(currentPos);
                    BlockState destinationState = level.getBlockState(destination);
                    boolean destIsChain = destinationState.is(Blocks.CHAIN);
                    if (isValidDestBlock(destinationState, destroyChain, destIsChain) && isValidSourceBlock(sourceState)) {
                        if (destroyChain && destIsChain) {
                            int chainSlot = winchBlockEntity.getOpenChainSlot();
                            if (chainSlot == -1) {
                                return false;
                            }
                            ItemStack itemStack = winchBlockEntity.getItem(chainSlot);
                            if (itemStack.is(Items.CHAIN)) {
                                itemStack.grow(1);
                            } else {
                                ItemStack newChain = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.core.BlockVolume.1
                                    C01121() {
                                    }

                                    public Item asItem() {
                                        return Items.CHAIN;
                                    }
                                });
                                winchBlockEntity.setItem(chainSlot, newChain);
                            }
                        }
                        level.setBlock(destination, sourceState, 2);
                        level.setBlock(currentPos, Blocks.AIR.defaultBlockState(), 2);
                        level.updateNeighborsAt(destination, level.getBlockState(destination).getBlock());
                        level.updateNeighborsAt(currentPos, Blocks.AIR);
                        if (!increaseXYZ) {
                            moveEntitiesAboveSource(movedEntities, level, currentPos, destination, shifterState, posShifter, !increaseXYZ);
                        }
                    } else {
                        return false;
                    }
                }
            }
            if (increaseXYZ) {
                blockPos = new BlockPos(cornerPos.getX(), cornerPos.getY() + 1, cornerPos.getZ());
            } else {
                blockPos = new BlockPos(cornerPos.getX(), cornerPos.getY() - 1, cornerPos.getZ());
            }
            cornerPos = blockPos;
        }
        if (!destroyChain) {
            int chainSlot2 = winchBlockEntity.getNextChainSlot();
            if (chainSlot2 == -1) {
                return false;
            }
            ItemStack itemStack2 = winchBlockEntity.getItem(chainSlot2);
            if (itemStack2.is(Items.CHAIN)) {
                itemStack2.shrink(1);
                return true;
            }
            return true;
        }
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.dairymoose.modernlife.core.BlockVolume$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/core/BlockVolume$1.class */
    public class C01121 implements ItemLike {
        C01121() {
        }

        public Item asItem() {
            return Items.CHAIN;
        }
    }

    public boolean shiftBlocksWithDummyEntity(Level level, BiFunction<BlockState, BlockPos, BlockPos> posShifter, BlockState shifterState, boolean destroyChain, WinchBlockEntity winchBlockEntity) {
        BlockPos startingPos;
        boolean increaseXYZ;
        BlockPos blockPos;
        BlockPos currentPos;
        int chainSlot;
        if (posShifter == null || shifterState == null) {
            return false;
        }
        BlockPos minPos = new BlockPos(this.minX, this.minY, this.minZ);
        BlockPos maxPos = new BlockPos(this.maxX, this.maxY, this.maxZ);
        BlockPos shiftedMinPos = posShifter.apply(shifterState, minPos);
        BlockPos shiftedMaxPos = posShifter.apply(shifterState, maxPos);
        if (isWithinVolume(shiftedMinPos) && isWithinVolume(shiftedMaxPos)) {
            return false;
        }
        if (isWithinVolume(shiftedMinPos)) {
            startingPos = maxPos;
            increaseXYZ = false;
        } else {
            startingPos = minPos;
            increaseXYZ = true;
        }
        Set<Integer> movedEntities = new HashSet<>();
        BlockPos cornerPos = startingPos;
        int yIterations = (this.maxY - this.minY) + 1;
        if (dummy == null && !level.isClientSide) {
            dummy = DummyEntity.getEntity(0.5f, level, startingPos, false);
            ModernLifeNetwork.INSTANCE.send(PacketDistributor.ALL.noArg(), new ClientboundWinchDummyPacket(dummy.getId(), winchBlockEntity.getBlockPos()));
        }
        if (dummy != null && dummy.renderables.isEmpty()) {
            int xIterations = (this.maxX - this.minX) + 1;
            int zIterations = (this.maxZ - this.minZ) + 1;
            for (int y = 0; y < yIterations; y++) {
                if (!canMoveLayer(cornerPos, increaseXYZ, level, posShifter, shifterState, destroyChain, winchBlockEntity)) {
                }
                for (int x = 0; x < xIterations; x++) {
                    for (int z = 0; z < zIterations; z++) {
                        if (increaseXYZ) {
                            currentPos = new BlockPos(cornerPos.getX() + x, cornerPos.getY(), cornerPos.getZ() + z);
                        } else {
                            currentPos = new BlockPos(cornerPos.getX() - x, cornerPos.getY(), cornerPos.getZ() - z);
                        }
                        BlockPos destination = posShifter.apply(shifterState, currentPos);
                        moveEntitiesAboveSource(movedEntities, level, currentPos, destination, shifterState, posShifter, !increaseXYZ);
                        BlockState sourceState = level.getBlockState(currentPos);
                        BlockState destinationState = level.getBlockState(destination);
                        boolean destIsChain = destinationState.is(Blocks.CHAIN);
                        if (destroyChain && destIsChain && (chainSlot = winchBlockEntity.getOpenChainSlot()) != -1) {
                            ItemStack itemStack = winchBlockEntity.getItem(chainSlot);
                            if (itemStack.is(Items.CHAIN)) {
                                itemStack.grow(1);
                            } else {
                                ItemStack newChain = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.core.BlockVolume.2
                                    C01132() {
                                    }

                                    public Item asItem() {
                                        return Items.CHAIN;
                                    }
                                });
                                winchBlockEntity.setItem(chainSlot, newChain);
                            }
                        }
                        if (level.isClientSide && dummy != null) {
                            dummy.renderables.add(new RenderableBlock(dummy, currentPos, sourceState));
                        }
                    }
                }
                if (increaseXYZ) {
                    blockPos = new BlockPos(cornerPos.getX(), cornerPos.getY() + 1, cornerPos.getZ());
                } else {
                    blockPos = new BlockPos(cornerPos.getX(), cornerPos.getY() - 1, cornerPos.getZ());
                }
                cornerPos = blockPos;
            }
            shouldFloat = false;
            Minecraft.getInstance().player.setDeltaMovement(0.0d, 0.125d, 0.0d);
            dummy.setDeltaMovement(0.0d, 0.125d, 0.0d);
            return true;
        }
        if (level.isClientSide && dummy != null) {
            BlockPos dummyPos = dummy.blockPosition();
            if (!canMoveLayer(dummyPos, increaseXYZ, level, posShifter, shifterState, destroyChain, winchBlockEntity)) {
            }
            for (RenderableBlock renderableBlock : dummy.renderables) {
            }
            return true;
        }
        return true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.dairymoose.modernlife.core.BlockVolume$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/core/BlockVolume$2.class */
    public class C01132 implements ItemLike {
        C01132() {
        }

        public Item asItem() {
            return Items.CHAIN;
        }
    }

    public String toString() {
        return "BlockVolume [minX=" + this.minX + ", minY=" + this.minY + ", minZ=" + this.minZ + ", maxX=" + this.maxX + ", maxY=" + this.maxY + ", maxZ=" + this.maxZ + "]";
    }
}
