<commit_before>package net.minecraftforge.client.model.pipeline;

import javax.vecmath.Vector3f;

import com.google.common.base.Objects;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;

public class VertexLighterFlat extends QuadGatheringTransformer
{
    protected final BlockInfo blockInfo = new BlockInfo();
    private int tint = -1;

    protected int posIndex = -1;
    protected int normalIndex = -1;
    protected int colorIndex = -1;
    protected int lightmapIndex = -1;

    @Override
    public void setParent(IVertexConsumer parent)
    {
        super.setParent(parent);
        if(Objects.equal(getVertexFormat(), parent.getVertexFormat())) return;
        setVertexFormat(getVertexFormat(parent));
        for(int i = 0; i < getVertexFormat().getElementCount(); i++)
        {
            switch(getVertexFormat().getElement(i).getUsage())
            {
                case POSITION:
                    posIndex = i;
                    break;
                case NORMAL:
                    normalIndex = i;
                    break;
                case COLOR:
                    colorIndex = i;
                    break;
                case UV:
                    if(getVertexFormat().getElement(i).getIndex() == 1)
                    {
                        lightmapIndex = i;
                    }
                    break;
                default:
            }
        }
        if(posIndex == -1)
        {
            throw new IllegalArgumentException("vertex lighter needs format with position");
        }
        if(lightmapIndex == -1)
        {
            throw new IllegalArgumentException("vertex lighter needs format with lightmap");
        }
        if(colorIndex == -1)
        {
            throw new IllegalArgumentException("vertex lighter needs format with color");
        }
    }

    private static VertexFormat getVertexFormat(IVertexConsumer parent)
    {
        VertexFormat format = parent.getVertexFormat();
        if(format.hasNormal()) return format;
        format = new VertexFormat(format);
        format.setElement(new VertexFormatElement(0, VertexFormatElement.EnumType.FLOAT, VertexFormatElement.EnumUsage.NORMAL, 4));
        return format;
    }

    @Override
    protected void processQuad()
    {
        float[][] position = quadData[posIndex];
        float[][] normal = null;
        float[][] lightmap = quadData[lightmapIndex];
        float[][] color = quadData[colorIndex];

        if(normalIndex != -1 && (
            quadData[normalIndex][0][0] != -1 ||
            quadData[normalIndex][0][1] != -1 ||
            quadData[normalIndex][0][2] != -1))
        {
            normal = quadData[normalIndex];
        }
        else
        {
            normal = new float[4][4];
            Vector3f v1 = new Vector3f(position[3]);
            Vector3f t = new Vector3f(position[1]);
            Vector3f v2 = new Vector3f(position[2]);
            v1.sub(t);
            t.set(position[0]);
            v2.sub(t);
            v1.cross(v2, v1);
            v1.normalize();
            for(int v = 0; v < 4; v++)
            {
                normal[v][0] = v1.x;
                normal[v][1] = v1.y;
                normal[v][2] = v1.z;
                normal[v][3] = 0;
            }
        }

        int multiplier = -1;
        if(tint != -1)
        {
            multiplier = blockInfo.getColorMultiplier(tint);
        }

        for(int v = 0; v < 4; v++)
        {
            position[v][0] += blockInfo.getShx();
            position[v][1] += blockInfo.getShy();
            position[v][2] += blockInfo.getShz();

            float x = position[v][0] - .5f;
            float y = position[v][1] - .5f;
            float z = position[v][2] - .5f;

            //if(blockInfo.getBlock().isFullCube())
            {
                x += normal[v][0] * .5f;
                y += normal[v][1] * .5f;
                z += normal[v][2] * .5f;
            }

            updateLightmap(normal[v], lightmap[v], x, y, z);
            updateColor(normal[v], color[v], x, y, z, tint, multiplier);

            // no need for remapping cause all we could've done is add 1 element to the end
            for(int e = 0; e < parent.getVertexFormat().getElementCount(); e++)
            {
                switch(parent.getVertexFormat().getElement(e).getUsage())
                {
                    case POSITION:
                        float[] pos = new float[4];
                        System.arraycopy(position[v], 0, pos, 0, position[v].length);
                        pos[0] += (blockInfo.getBlockPos().getX() & 0xF);
                        pos[1] += (blockInfo.getBlockPos().getY() & 0xF);
                        pos[2] += (blockInfo.getBlockPos().getZ() & 0xF);
                        parent.put(e, pos);
                        break;
                    case NORMAL: if(normalIndex != -1)
                    {
                        parent.put(e, normal[v]);
                        break;
                    }
                    case COLOR:
                        parent.put(e, color[v]);
                        break;
                    case UV: if(getVertexFormat().getElement(e).getIndex() == 1)
                    {
                        parent.put(e, lightmap[v]);
                        break;
                    }
                    default:
                        parent.put(e, quadData[e][v]);
                }
            }
        }
        tint = -1;
    }

    protected void updateLightmap(float[] normal, float[] lightmap, float x, float y, float z)
    {
        float e1 = .5f - 1e4f;
        float e2 = 1 - 1e4f;
        BlockPos pos = blockInfo.getBlockPos();

        if(y < -e1 && normal[1] < -e2) pos = pos.down();
        if(y >  e1 && normal[1] >  e2) pos = pos.up();
        if(z < -e1 && normal[2] < -e2) pos = pos.north();<commit_msg>Small fix for reworked classic lighting<commit_after>package net.minecraftforge.client.model.pipeline;

import javax.vecmath.Vector3f;

import com.google.common.base.Objects;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;

public class VertexLighterFlat extends QuadGatheringTransformer
{
    protected final BlockInfo blockInfo = new BlockInfo();
    private int tint = -1;

    protected int posIndex = -1;
    protected int normalIndex = -1;
    protected int colorIndex = -1;
    protected int lightmapIndex = -1;

    @Override
    public void setParent(IVertexConsumer parent)
    {
        super.setParent(parent);
        if(Objects.equal(getVertexFormat(), parent.getVertexFormat())) return;
        setVertexFormat(getVertexFormat(parent));
        for(int i = 0; i < getVertexFormat().getElementCount(); i++)
        {
            switch(getVertexFormat().getElement(i).getUsage())
            {
                case POSITION:
                    posIndex = i;
                    break;
                case NORMAL:
                    normalIndex = i;
                    break;
                case COLOR:
                    colorIndex = i;
                    break;
                case UV:
                    if(getVertexFormat().getElement(i).getIndex() == 1)
                    {
                        lightmapIndex = i;
                    }
                    break;
                default:
            }
        }
        if(posIndex == -1)
        {
            throw new IllegalArgumentException("vertex lighter needs format with position");
        }
        if(lightmapIndex == -1)
        {
            throw new IllegalArgumentException("vertex lighter needs format with lightmap");
        }
        if(colorIndex == -1)
        {
            throw new IllegalArgumentException("vertex lighter needs format with color");
        }
    }

    private static VertexFormat getVertexFormat(IVertexConsumer parent)
    {
        VertexFormat format = parent.getVertexFormat();
        if(format.hasNormal()) return format;
        format = new VertexFormat(format);
        format.setElement(new VertexFormatElement(0, VertexFormatElement.EnumType.FLOAT, VertexFormatElement.EnumUsage.NORMAL, 4));
        return format;
    }

    @Override
    protected void processQuad()
    {
        float[][] position = quadData[posIndex];
        float[][] normal = null;
        float[][] lightmap = quadData[lightmapIndex];
        float[][] color = quadData[colorIndex];

        if(normalIndex != -1 && (
            quadData[normalIndex][0][0] != -1 ||
            quadData[normalIndex][0][1] != -1 ||
            quadData[normalIndex][0][2] != -1))
        {
            normal = quadData[normalIndex];
        }
        else
        {
            normal = new float[4][4];
            Vector3f v1 = new Vector3f(position[3]);
            Vector3f t = new Vector3f(position[1]);
            Vector3f v2 = new Vector3f(position[2]);
            v1.sub(t);
            t.set(position[0]);
            v2.sub(t);
            v1.cross(v2, v1);
            v1.normalize();
            for(int v = 0; v < 4; v++)
            {
                normal[v][0] = v1.x;
                normal[v][1] = v1.y;
                normal[v][2] = v1.z;
                normal[v][3] = 0;
            }
        }

        int multiplier = -1;
        if(tint != -1)
        {
            multiplier = blockInfo.getColorMultiplier(tint);
        }

        for(int v = 0; v < 4; v++)
        {
            position[v][0] += blockInfo.getShx();
            position[v][1] += blockInfo.getShy();
            position[v][2] += blockInfo.getShz();

            float x = position[v][0] - .5f;
            float y = position[v][1] - .5f;
            float z = position[v][2] - .5f;

            //if(blockInfo.getBlock().isFullCube())
            {
                x += normal[v][0] * .5f;
                y += normal[v][1] * .5f;
                z += normal[v][2] * .5f;
            }

            updateLightmap(normal[v], lightmap[v], x, y, z);
            updateColor(normal[v], color[v], x, y, z, tint, multiplier);

            // no need for remapping cause all we could've done is add 1 element to the end
            for(int e = 0; e < parent.getVertexFormat().getElementCount(); e++)
            {
                switch(parent.getVertexFormat().getElement(e).getUsage())
                {
                    case POSITION:
                        float[] pos = new float[4];
                        System.arraycopy(position[v], 0, pos, 0, position[v].length);
                        pos[0] += (blockInfo.getBlockPos().getX() & 0xF);
                        pos[1] += (blockInfo.getBlockPos().getY() & 0xF);
                        pos[2] += (blockInfo.getBlockPos().getZ() & 0xF);
                        parent.put(e, pos);
                        break;
                    case NORMAL: if(normalIndex != -1)
                    {
                        parent.put(e, normal[v]);
                        break;
                    }
                    case COLOR:
                        parent.put(e, color[v]);
                        break;
                    case UV: if(getVertexFormat().getElement(e).getIndex() == 1)
                    {
                        parent.put(e, lightmap[v]);
                        break;
                    }
                    default:
                        parent.put(e, quadData[e][v]);
                }
            }
        }
        tint = -1;
    }

    protected void updateLightmap(float[] normal, float[] lightmap, float x, float y, float z)
    {
        float e1 = .5f - 1e-2f;
        float e2 = 1 - 1e-2f;
        BlockPos pos = blockInfo.getBlockPos();

        if(y < -e1 && normal[1] < -e2) pos = pos.down();
        if(y >  e1 && normal[1] >  e2) pos = pos.up();
        if(z < -e1 && normal[2] < -e2) pos = pos.north();
{'fraction_non_alphanumeric': 0.12332351979064442, 'fraction_numerical': 0.016846581615963363, 'mean_word_length': 1.4418929712460065, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4335748', 'n_tokens_mistral': 3536, 'n_tokens_neox': 3386, 'n_words': 736}