/FileExtraction/Java_unseen/ionar2_summit/src/main/java/com/salhack/summit/module/exploit/NewChunks.java
package com.salhack.summit.module.exploit;

import com.salhack.summit.events.MinecraftEvent;
import com.salhack.summit.events.bus.EventHandler;
import com.salhack.summit.events.bus.Listener;
import com.salhack.summit.events.network.EventServerPacket;
import com.salhack.summit.events.render.RenderEvent;
import com.salhack.summit.module.Module;
import com.salhack.summit.module.Value;
import com.salhack.summit.util.Pair;
import com.salhack.summit.util.render.RenderUtil;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.network.play.server.SPacketChunkData;
import net.minecraft.util.math.AxisAlignedBB;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.lwjgl.opengl.GL11;

public class NewChunks extends Module
{
    public Value<Boolean> Render = new Value<Boolean>("Render", new String[]
    { "R" }, "Renders the new chunks in the world", true);

    private List<Pair<Integer, Integer>> chunkDataList = new CopyOnWriteArrayList<>();

    public NewChunks()
    {
        super("NewChunks", new String[]
        { "ChunkGen" }, "Highlights newly generated chunks", "NONE", 0x7FE047, ModuleType.EXPLOIT);
    }

    @EventHandler
    private Listener<EventServerPacket> onServerPacket = new Listener<>(event ->
    {
        if (event.getStage() != MinecraftEvent.Stage.Pre)
            return;
        
        if (event.getPacket() instanceof SPacketChunkData)
        {
            final SPacketChunkData packet = (SPacketChunkData) event.getPacket();
            if (!packet.isFullChunk())
            {
                chunkDataList.removeIf(chunkData ->
                {
                    return chunkData.getFirst() == packet.getChunkX() && chunkData.getSecond() == packet.getChunkZ();
                });
                
                final Pair<Integer, Integer> chunk = new Pair<>(packet.getChunkX(), packet.getChunkZ());
                if (!this.chunkDataList.contains(chunk))
                {
                    this.chunkDataList.add(chunk);
                }
            }
        }
    });

    @EventHandler
    private Listener<RenderEvent> OnRenderEvent = new Listener<>(p_Event ->
    {
        if (!Render.getValue())
            return;

        if (mc.getRenderManager() == null)
            return;
        
        chunkDataList.forEach(chunkData ->
        {
            float x = chunkData.getFirst() * 16;
            float z = chunkData.getSecond() * 16;
            
            RenderUtil.camera.setPosition(mc.getRenderViewEntity().posX, mc.getRenderViewEntity().posY, mc.getRenderViewEntity().posZ);

            final AxisAlignedBB bb = new AxisAlignedBB(
                    x - mc.getRenderManager().viewerPosX,
                    0 - mc.getRenderManager().viewerPosY,
                    z - mc.getRenderManager().viewerPosZ,
                    x + 16 - mc.getRenderManager().viewerPosX,
                    1 - mc.getRenderManager().viewerPosY,
                    z + 16 - mc.getRenderManager().viewerPosZ);
            
            final AxisAlignedBB t = new AxisAlignedBB(x, 0, z, x + 16, 1, z + 16);
            if (RenderUtil.camera.isBoundingBoxInFrustum(t))
            {
                GlStateManager.pushMatrix();
                GlStateManager.enableBlend();
                GlStateManager.disableDepth();
                GlStateManager.tryBlendFuncSeparate(770, 771, 0, 1);
                GlStateManager.disableTexture2D();
                GlStateManager.depthMask(false);
                GL11.glEnable(GL11.GL_LINE_SMOOTH);
                GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST);
                GL11.glLineWidth(1.5f);
                
                RenderGlobal.renderFilledBox(bb.minX, bb.minY, bb.minZ, bb.maxX, bb.minY, bb.maxZ, 0.6f, 0f, 0.9333333f, 0.5f);
                
                GL11.glDisable(GL11.GL_LINE_SMOOTH);
                GlStateManager.depthMask(true);
                GlStateManager.enableDepth();
                GlStateManager.enableTexture2D();
                GlStateManager.disableBlend();
                GlStateManager.popMatrix();
            }
        });
    });
}
