package net.minecraft.core;

import java.util.Spliterator;
import java.util.stream.StreamSupport;
import java.util.function.Consumer;
import java.util.Spliterators;
import java.util.stream.Stream;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ChunkPos;

public class SectionPos extends Vec3i {
    private SectionPos(final int integer1, final int integer2, final int integer3) {
        super(integer1, integer2, integer3);
    }
    
    public static SectionPos of(final int integer1, final int integer2, final int integer3) {
        return new SectionPos(integer1, integer2, integer3);
    }
    
    public static SectionPos of(final BlockPos fk) {
        return new SectionPos(blockToSectionCoord(fk.getX()), blockToSectionCoord(fk.getY()), blockToSectionCoord(fk.getZ()));
    }
    
    public static SectionPos of(final ChunkPos bjh, final int integer) {
        return new SectionPos(bjh.x, integer, bjh.z);
    }
    
    public static SectionPos of(final Entity akq) {
        return new SectionPos(blockToSectionCoord(Mth.floor(akq.getX())), blockToSectionCoord(Mth.floor(akq.getY())), blockToSectionCoord(Mth.floor(akq.getZ())));
    }
    
    public static SectionPos of(final long long1) {
        return new SectionPos(x(long1), y(long1), z(long1));
    }
    
    public static long offset(final long long1, final Direction fp) {
        return offset(long1, fp.getStepX(), fp.getStepY(), fp.getStepZ());
    }
    
    public static long offset(final long long1, final int integer2, final int integer3, final int integer4) {
        return asLong(x(long1) + integer2, y(long1) + integer3, z(long1) + integer4);
    }
    
    public static int blockToSectionCoord(final int integer) {
        return integer >> 4;
    }
    
    public static int sectionRelative(final int integer) {
        return integer & 0xF;
    }
    
    public static short sectionRelativePos(final BlockPos fk) {
        final int integer2 = sectionRelative(fk.getX());
        final int integer3 = sectionRelative(fk.getY());
        final int integer4 = sectionRelative(fk.getZ());
        return (short)(integer2 << 8 | integer4 << 4 | integer3);
    }
    
    public static int sectionToBlockCoord(final int integer) {
        return integer << 4;
    }
    
    public static int x(final long long1) {
        return (int)(long1 << 0 >> 42);
    }
    
    public static int y(final long long1) {
        return (int)(long1 << 44 >> 44);
    }
    
    public static int z(final long long1) {
        return (int)(long1 << 22 >> 42);
    }
    
    public int x() {
        return this.getX();
    }
    
    public int y() {
        return this.getY();
    }
    
    public int z() {
        return this.getZ();
    }
    
    public int minBlockX() {
        return this.x() << 4;
    }
    
    public int minBlockY() {
        return this.y() << 4;
    }
    
    public int minBlockZ() {
        return this.z() << 4;
    }
    
    public int maxBlockX() {
        return (this.x() << 4) + 15;
    }
    
    public int maxBlockY() {
        return (this.y() << 4) + 15;
    }
    
    public int maxBlockZ() {
        return (this.z() << 4) + 15;
    }
    
    public static long blockToSection(final long long1) {
        return asLong(blockToSectionCoord(BlockPos.getX(long1)), blockToSectionCoord(BlockPos.getY(long1)), blockToSectionCoord(BlockPos.getZ(long1)));
    }
    
    public static long getZeroNode(final long long1) {
        return long1 & 0xFFFFFFFFFFF00000L;
    }
    
    public BlockPos origin() {
        return new BlockPos(sectionToBlockCoord(this.x()), sectionToBlockCoord(this.y()), sectionToBlockCoord(this.z()));
    }
    
    public BlockPos center() {
        final int integer2 = 8;
        return this.origin().offset(8, 8, 8);
    }
    
    public ChunkPos chunk() {
        return new ChunkPos(this.x(), this.z());
    }
    
    public static long asLong(final int integer1, final int integer2, final int integer3) {
        long long4 = 0L;
        long4 |= ((long)integer1 & 0x3FFFFFL) << 42;
        long4 |= ((long)integer2 & 0xFFFFFL) << 0;
        long4 |= ((long)integer3 & 0x3FFFFFL) << 20;
        return long4;
    }
    
    public long asLong() {
        return asLong(this.x(), this.y(), this.z());
    }
    
    public Stream<BlockPos> blocksInside() {
        return BlockPos.betweenClosedStream(this.minBlockX(), this.minBlockY(), this.minBlockZ(), this.maxBlockX(), this.maxBlockY(), this.maxBlockZ());
    }
    
    public static Stream<SectionPos> cube(final SectionPos gd, final int integer) {
        final int integer2 = gd.x();
        final int integer3 = gd.y();
        final int integer4 = gd.z();
        return betweenClosedStream(integer2 - integer, integer3 - integer, integer4 - integer, integer2 + integer, integer3 + integer, integer4 + integer);
    }
    
    public static Stream<SectionPos> aroundChunk(final ChunkPos bjh, final int integer) {
        final int integer2 = bjh.x;
        final int integer3 = bjh.z;
        return betweenClosedStream(integer2 - integer, 0, integer3 - integer, integer2 + integer, 15, integer3 + integer);
    }
    
    public static Stream<SectionPos> betweenClosedStream(final int integer1, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6) {
        return StreamSupport.<SectionPos>stream((Spliterator<SectionPos>)new Spliterators.AbstractSpliterator<SectionPos>((long)((integer4 - integer1 + 1) * (integer5 - integer2 + 1) * (integer6 - integer3 + 1)), 64) {
            final Cursor3D cursor = new Cursor3D(integer1, integer2, integer3, integer4, integer5, integer6);
            
            @Override
            public boolean tryAdvance(final Consumer<? super SectionPos> consumer) {
                if (this.cursor.advance()) {
                    consumer.accept(new SectionPos(this.cursor.nextX(), this.cursor.nextY(), this.cursor.nextZ(), null));
                    return true;
                }
                return false;
            }
        }, false);
    }
}
