package net.minecraft.client.renderer;

import org.apache.logging.log4j.LogManager;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.HitResult;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.server.packs.resources.SimpleResource;
import net.minecraft.client.Screenshot;
import net.minecraft.CrashReportCategory;
import com.mojang.blaze3d.platform.Window;
import java.util.Locale;
import net.minecraft.ReportedException;
import net.minecraft.CrashReport;
import com.mojang.blaze3d.platform.Lighting;
import net.minecraft.world.effect.MobEffects;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.math.Matrix4f;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.level.GameType;
import net.minecraft.world.entity.player.Player;
import com.mojang.math.Vector3f;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.util.Mth;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.projectile.ProjectileUtil;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import net.minecraft.world.entity.monster.EnderMan;
import net.minecraft.world.entity.monster.Spider;
import net.minecraft.world.entity.monster.Creeper;
import net.minecraft.world.entity.Entity;
import net.minecraft.Util;
import net.minecraft.client.Camera;
import net.minecraft.resources.ResourceLocation;
import javax.annotation.Nullable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.gui.MapRenderer;
import java.util.Random;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.client.Minecraft;
import org.apache.logging.log4j.Logger;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;

public class GameRenderer implements AutoCloseable, ResourceManagerReloadListener {
    private static final Logger LOGGER;
    private final Minecraft minecraft;
    private final ResourceManager resourceManager;
    private final Random random;
    private float renderDistance;
    public final ItemInHandRenderer itemInHandRenderer;
    private final MapRenderer mapRenderer;
    private final RenderBuffers renderBuffers;
    private int tick;
    private float fov;
    private float oldFov;
    private float darkenWorldAmount;
    private float darkenWorldAmountO;
    private boolean renderHand;
    private boolean renderBlockOutline;
    private long lastScreenshotAttempt;
    private long lastActiveTime;
    private final LightTexture lightTexture;
    private final OverlayTexture overlayTexture;
    private boolean panoramicMode;
    private float zoom;
    private float zoomX;
    private float zoomY;
    @Nullable
    private ItemStack itemActivationItem;
    private int itemActivationTicks;
    private float itemActivationOffX;
    private float itemActivationOffY;
    @Nullable
    private PostChain postEffect;
    private static final ResourceLocation[] EFFECTS;
    public static final int EFFECT_NONE;
    private int effectIndex;
    private boolean effectActive;
    private final Camera mainCamera;
    
    public GameRenderer(final Minecraft dbn, final ResourceManager zb, final RenderBuffers dqz) {
        this.random = new Random();
        this.renderHand = true;
        this.renderBlockOutline = true;
        this.lastActiveTime = Util.getMillis();
        this.overlayTexture = new OverlayTexture();
        this.zoom = 1.0f;
        this.effectIndex = GameRenderer.EFFECT_NONE;
        this.mainCamera = new Camera();
        this.minecraft = dbn;
        this.resourceManager = zb;
        this.itemInHandRenderer = dbn.getItemInHandRenderer();
        this.mapRenderer = new MapRenderer(dbn.getTextureManager());
        this.lightTexture = new LightTexture(this, dbn);
        this.renderBuffers = dqz;
        this.postEffect = null;
    }
    
    @Override
    public void close() {
        this.lightTexture.close();
        this.mapRenderer.close();
        this.overlayTexture.close();
        this.shutdownEffect();
    }
    
    public void shutdownEffect() {
        if (this.postEffect != null) {
            this.postEffect.close();
        }
        this.postEffect = null;
        this.effectIndex = GameRenderer.EFFECT_NONE;
    }
    
    public void togglePostEffect() {
        this.effectActive = !this.effectActive;
    }
    
    public void checkEntityPostEffect(@Nullable final Entity akq) {
        if (this.postEffect != null) {
            this.postEffect.close();
        }
        this.postEffect = null;
        if (akq instanceof Creeper) {
            this.loadEffect(new ResourceLocation("shaders/post/creeper.json"));
        }
        else if (akq instanceof Spider) {
            this.loadEffect(new ResourceLocation("shaders/post/spider.json"));
        }
        else if (akq instanceof EnderMan) {
            this.loadEffect(new ResourceLocation("shaders/post/invert.json"));
        }
    }
    
    private void loadEffect(final ResourceLocation sm) {
        if (this.postEffect != null) {
            this.postEffect.close();
        }
        try {
            (this.postEffect = new PostChain(this.minecraft.getTextureManager(), this.resourceManager, this.minecraft.getMainRenderTarget(), sm)).resize(this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight());
            this.effectActive = true;
        }
        catch (IOException iOException3) {
            GameRenderer.LOGGER.warn("Failed to load shader: {}", sm, iOException3);
            this.effectIndex = GameRenderer.EFFECT_NONE;
            this.effectActive = false;
        }
        catch (JsonSyntaxException jsonSyntaxException3) {
            GameRenderer.LOGGER.warn("Failed to load shader: {}", sm, jsonSyntaxException3);
            this.effectIndex = GameRenderer.EFFECT_NONE;
            this.effectActive = false;
        }
    }
    
    @Override
    public void onResourceManagerReload(final ResourceManager zb) {
        if (this.postEffect != null) {
            this.postEffect.close();
        }
        this.postEffect = null;
        if (this.effectIndex == GameRenderer.EFFECT_NONE) {
            this.checkEntityPostEffect(this.minecraft.getCameraEntity());
        }
        else {
            this.loadEffect(GameRenderer.EFFECTS[this.effectIndex]);
        }
    }
    
    public void tick() {
        this.tickFov();
        this.lightTexture.tick();
        if (this.minecraft.getCameraEntity() == null) {
            this.minecraft.setCameraEntity(this.minecraft.player);
        }
        this.mainCamera.tick();
        ++this.tick;
        this.itemInHandRenderer.tick();
        this.minecraft.levelRenderer.tickRain(this.mainCamera);
        this.darkenWorldAmountO = this.darkenWorldAmount;
        if (this.minecraft.gui.getBossOverlay().shouldDarkenScreen()) {
            this.darkenWorldAmount += 0.05f;
            if (this.darkenWorldAmount > 1.0f) {
                this.darkenWorldAmount = 1.0f;
            }
        }
        else if (this.darkenWorldAmount > 0.0f) {
            this.darkenWorldAmount -= 0.0125f;
        }
        if (this.itemActivationTicks > 0) {
            --this.itemActivationTicks;
            if (this.itemActivationTicks == 0) {
                this.itemActivationItem = null;
            }
        }
    }
    
    @Nullable
    public PostChain currentEffect() {
        return this.postEffect;
    }
    
    public void resize(final int integer1, final int integer2) {
        if (this.postEffect != null) {
            this.postEffect.resize(integer1, integer2);
        }
        this.minecraft.levelRenderer.resize(integer1, integer2);
    }
    
    public void pick(final float float1) {
        final Entity akq2 = this.minecraft.getCameraEntity();
        if (akq2 == null) {
            return;
        }
        if (this.minecraft.level == null) {
            return;
        }
        this.minecraft.getProfiler().push("pick");
        this.minecraft.crosshairPickEntity = null;
        double double4 = this.minecraft.gameMode.getPickRange();
        this.minecraft.hitResult = akq2.pick(double4, float1, false);
        final Vec3 cvl6 = akq2.getEyePosition(float1);
        boolean boolean7 = false;
        final int integer8 = 3;
        double double5 = double4;
        if (this.minecraft.gameMode.hasFarPickRange()) {
            double5 = (double4 = 6.0);
        }
        else {
            if (double5 > 3.0) {
                boolean7 = true;
            }
            double4 = double5;
        }
        double5 *= double5;
        if (this.minecraft.hitResult != null) {
            double5 = this.minecraft.hitResult.getLocation().distanceToSqr(cvl6);
        }
        final Vec3 cvl7 = akq2.getViewVector(1.0f);
        final Vec3 cvl8 = cvl6.add(cvl7.x * double4, cvl7.y * double4, cvl7.z * double4);
        final float float2 = 1.0f;
        final AABB cvf14 = akq2.getBoundingBox().expandTowards(cvl7.scale(double4)).inflate(1.0, 1.0, 1.0);
        final EntityHitResult cvh15 = ProjectileUtil.getEntityHitResult(akq2, cvl6, cvl8, cvf14, akq -> !akq.isSpectator() && akq.isPickable(), double5);
        if (cvh15 != null) {
            final Entity akq3 = cvh15.getEntity();
            final Vec3 cvl9 = cvh15.getLocation();
            final double double6 = cvl6.distanceToSqr(cvl9);
            if (boolean7 && double6 > 9.0) {
                this.minecraft.hitResult = BlockHitResult.miss(cvl9, Direction.getNearest(cvl7.x, cvl7.y, cvl7.z), new BlockPos(cvl9));
            }
            else if (double6 < double5 || this.minecraft.hitResult == null) {
                this.minecraft.hitResult = cvh15;
                if (akq3 instanceof LivingEntity || akq3 instanceof ItemFrame) {
                    this.minecraft.crosshairPickEntity = akq3;
                }
            }
        }
        this.minecraft.getProfiler().pop();
    }
    
    private void tickFov() {
        float float2 = 1.0f;
        if (this.minecraft.getCameraEntity() instanceof AbstractClientPlayer) {
            final AbstractClientPlayer dpy3 = (AbstractClientPlayer)this.minecraft.getCameraEntity();
            float2 = dpy3.getFieldOfViewModifier();
        }
        this.oldFov = this.fov;
        this.fov += (float2 - this.fov) * 0.5f;
        if (this.fov > 1.5f) {
            this.fov = 1.5f;
        }
        if (this.fov < 0.1f) {
            this.fov = 0.1f;
        }
    }
    
    private double getFov(final Camera dbb, final float float2, final boolean boolean3) {
        if (this.panoramicMode) {
            return 90.0;
        }
        double double5 = 70.0;
        if (boolean3) {
            double5 = this.minecraft.options.fov;
            double5 *= Mth.lerp(float2, this.oldFov, this.fov);
        }
        if (dbb.getEntity() instanceof LivingEntity && ((LivingEntity)dbb.getEntity()).getHealth() <= 0.0f) {
            final float float3 = Math.min(((LivingEntity)dbb.getEntity()).deathTime + float2, 20.0f);
            double5 /= (1.0f - 500.0f / (float3 + 500.0f)) * 2.0f + 1.0f;
        }
        final FluidState coj7 = dbb.getFluidInCamera();
        if (!coj7.isEmpty()) {
            double5 = double5 * 60.0 / 70.0;
        }
        return double5;
    }
    
    private void bobHurt(final PoseStack cyl, final float float2) {
        if (this.minecraft.getCameraEntity() instanceof LivingEntity) {
            final LivingEntity akz4 = (LivingEntity)this.minecraft.getCameraEntity();
            float float3 = akz4.hurtTime - float2;
            if (akz4.getHealth() <= 0.0f) {
                final float float4 = Math.min(akz4.deathTime + float2, 20.0f);
                cyl.mulPose(Vector3f.ZP.rotationDegrees(40.0f - 8000.0f / (float4 + 200.0f)));
            }
            if (float3 < 0.0f) {
                return;
            }
            float3 /= akz4.hurtDuration;
            float3 = Mth.sin(float3 * float3 * float3 * float3 * 3.1415927f);
            final float float4 = akz4.hurtDir;
            cyl.mulPose(Vector3f.YP.rotationDegrees(-float4));
            cyl.mulPose(Vector3f.ZP.rotationDegrees(-float3 * 14.0f));
            cyl.mulPose(Vector3f.YP.rotationDegrees(float4));
        }
    }
    
    private void bobView(final PoseStack cyl, final float float2) {
        if (!(this.minecraft.getCameraEntity() instanceof Player)) {
            return;
        }
        final Player ayj4 = (Player)this.minecraft.getCameraEntity();
        final float float3 = ayj4.walkDist - ayj4.walkDistO;
        final float float4 = -(ayj4.walkDist + float3 * float2);
        final float float5 = Mth.lerp(float2, ayj4.oBob, ayj4.bob);
        cyl.translate(Mth.sin(float4 * 3.1415927f) * float5 * 0.5f, -Math.abs(Mth.cos(float4 * 3.1415927f) * float5), 0.0);
        cyl.mulPose(Vector3f.ZP.rotationDegrees(Mth.sin(float4 * 3.1415927f) * float5 * 3.0f));
        cyl.mulPose(Vector3f.XP.rotationDegrees(Math.abs(Mth.cos(float4 * 3.1415927f - 0.2f) * float5) * 5.0f));
    }
    
    private void renderItemInHand(final PoseStack cyl, final Camera dbb, final float float3) {
        if (this.panoramicMode) {
            return;
        }
        this.resetProjectionMatrix(this.getProjectionMatrix(dbb, float3, false));
        final PoseStack.Pose a5 = cyl.last();
        a5.pose().setIdentity();
        a5.normal().setIdentity();
        cyl.pushPose();
        this.bobHurt(cyl, float3);
        if (this.minecraft.options.bobView) {
            this.bobView(cyl, float3);
        }
        final boolean boolean6 = this.minecraft.getCameraEntity() instanceof LivingEntity && ((LivingEntity)this.minecraft.getCameraEntity()).isSleeping();
        if (this.minecraft.options.thirdPersonView == 0 && !boolean6 && !this.minecraft.options.hideGui && this.minecraft.gameMode.getPlayerMode() != GameType.SPECTATOR) {
            this.lightTexture.turnOnLightLayer();
            this.itemInHandRenderer.renderHandsWithItems(float3, cyl, this.renderBuffers.bufferSource(), this.minecraft.player, this.minecraft.getEntityRenderDispatcher().<LocalPlayer>getPackedLightCoords(this.minecraft.player, float3));
            this.lightTexture.turnOffLightLayer();
        }
        cyl.popPose();
        if (this.minecraft.options.thirdPersonView == 0 && !boolean6) {
            ScreenEffectRenderer.renderScreenEffect(this.minecraft, cyl);
            this.bobHurt(cyl, float3);
        }
        if (this.minecraft.options.bobView) {
            this.bobView(cyl, float3);
        }
    }
    
    public void resetProjectionMatrix(final Matrix4f b) {
        RenderSystem.matrixMode(5889);
        RenderSystem.loadIdentity();
        RenderSystem.multMatrix(b);
        RenderSystem.matrixMode(5888);
    }
    
    public Matrix4f getProjectionMatrix(final Camera dbb, final float float2, final boolean boolean3) {
        final PoseStack cyl5 = new PoseStack();
        cyl5.last().pose().setIdentity();
        if (this.zoom != 1.0f) {
            cyl5.translate(this.zoomX, -this.zoomY, 0.0);
            cyl5.scale(this.zoom, this.zoom, 1.0f);
        }
        cyl5.last().pose().multiply(Matrix4f.perspective(this.getFov(dbb, float2, boolean3), this.minecraft.getWindow().getWidth() / (float)this.minecraft.getWindow().getHeight(), 0.05f, this.renderDistance * 4.0f));
        return cyl5.last().pose();
    }
    
    public static float getNightVisionScale(final LivingEntity akz, final float float2) {
        final int integer3 = akz.getEffect(MobEffects.NIGHT_VISION).getDuration();
        if (integer3 > 200) {
            return 1.0f;
        }
        return 0.7f + Mth.sin((integer3 - float2) * 3.1415927f * 0.2f) * 0.3f;
    }
    
    public void render(final float float1, final long long2, final boolean boolean3) {
        if (this.minecraft.isWindowActive() || !this.minecraft.options.pauseOnLostFocus || (this.minecraft.options.touchscreen && this.minecraft.mouseHandler.isRightPressed())) {
            this.lastActiveTime = Util.getMillis();
        }
        else if (Util.getMillis() - this.lastActiveTime > 500L) {
            this.minecraft.pauseGame(false);
        }
        if (this.minecraft.noRender) {
            return;
        }
        final int integer6 = (int)(this.minecraft.mouseHandler.xpos() * this.minecraft.getWindow().getGuiScaledWidth() / this.minecraft.getWindow().getScreenWidth());
        final int integer7 = (int)(this.minecraft.mouseHandler.ypos() * this.minecraft.getWindow().getGuiScaledHeight() / this.minecraft.getWindow().getScreenHeight());
        final PoseStack cyl8 = new PoseStack();
        RenderSystem.viewport(0, 0, this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight());
        if (boolean3 && this.minecraft.level != null) {
            this.minecraft.getProfiler().push("level");
            this.renderLevel(float1, long2, cyl8);
            if (this.minecraft.hasSingleplayerServer() && this.lastScreenshotAttempt < Util.getMillis() - 1000L) {
                this.lastScreenshotAttempt = Util.getMillis();
                if (!this.minecraft.getSingleplayerServer().hasWorldScreenshot()) {
                    this.takeAutoScreenshot();
                }
            }
            this.minecraft.levelRenderer.doEntityOutline();
            if (this.postEffect != null && this.effectActive) {
                RenderSystem.disableBlend();
                RenderSystem.disableDepthTest();
                RenderSystem.disableAlphaTest();
                RenderSystem.enableTexture();
                RenderSystem.matrixMode(5890);
                RenderSystem.pushMatrix();
                RenderSystem.loadIdentity();
                this.postEffect.process(float1);
                RenderSystem.popMatrix();
            }
            this.minecraft.getMainRenderTarget().bindWrite(true);
        }
        final Window cxx9 = this.minecraft.getWindow();
        RenderSystem.clear(256, Minecraft.ON_OSX);
        RenderSystem.matrixMode(5889);
        RenderSystem.loadIdentity();
        RenderSystem.ortho(0.0, cxx9.getWidth() / cxx9.getGuiScale(), cxx9.getHeight() / cxx9.getGuiScale(), 0.0, 1000.0, 3000.0);
        RenderSystem.matrixMode(5888);
        RenderSystem.loadIdentity();
        RenderSystem.translatef(0.0f, 0.0f, -2000.0f);
        Lighting.setupFor3DItems();
        if (boolean3 && this.minecraft.level != null) {
            this.minecraft.getProfiler().popPush("gui");
            if (!this.minecraft.options.hideGui || this.minecraft.screen != null) {
                RenderSystem.defaultAlphaFunc();
                this.renderItemActivationAnimation(this.minecraft.getWindow().getGuiScaledWidth(), this.minecraft.getWindow().getGuiScaledHeight(), float1);
                this.minecraft.gui.render(float1);
                RenderSystem.clear(256, Minecraft.ON_OSX);
            }
            this.minecraft.getProfiler().pop();
        }
        if (this.minecraft.overlay != null) {
            try {
                this.minecraft.overlay.render(integer6, integer7, this.minecraft.getDeltaFrameTime());
                return;
            }
            catch (Throwable throwable10) {
                final CrashReport h11 = CrashReport.forThrowable(throwable10, "Rendering overlay");
                final CrashReportCategory i12 = h11.addCategory("Overlay render details");
                i12.setDetail("Overlay name", () -> this.minecraft.overlay.getClass().getCanonicalName());
                throw new ReportedException(h11);
            }
        }
        if (this.minecraft.screen != null) {
            try {
                this.minecraft.screen.render(integer6, integer7, this.minecraft.getDeltaFrameTime());
            }
            catch (Throwable throwable10) {
                final CrashReport h11 = CrashReport.forThrowable(throwable10, "Rendering screen");
                final CrashReportCategory i12 = h11.addCategory("Screen render details");
                i12.setDetail("Screen name", () -> this.minecraft.screen.getClass().getCanonicalName());
                i12.setDetail("Mouse location", () -> String.format(Locale.ROOT, "Scaled: (%d, %d). Absolute: (%f, %f)", integer6, integer7, this.minecraft.mouseHandler.xpos(), this.minecraft.mouseHandler.ypos()));
                i12.setDetail("Screen size", () -> String.format(Locale.ROOT, "Scaled: (%d, %d). Absolute: (%d, %d). Scale factor of %f", this.minecraft.getWindow().getGuiScaledWidth(), this.minecraft.getWindow().getGuiScaledHeight(), this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight(), this.minecraft.getWindow().getGuiScale()));
                throw new ReportedException(h11);
            }
        }
    }
    
    private void takeAutoScreenshot() {
        if (this.minecraft.levelRenderer.countRenderedChunks() > 10 && this.minecraft.levelRenderer.hasRenderedAllChunks() && !this.minecraft.getSingleplayerServer().hasWorldScreenshot()) {
            final NativeImage cxr2 = Screenshot.takeScreenshot(this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight(), this.minecraft.getMainRenderTarget());
            final NativeImage nativeImage;
            int integer3;
            int integer4;
            int integer5;
            int integer6;
            NativeImage cxr3;
            final Throwable t2;
            SimpleResource.IO_EXECUTOR.execute(() -> {
                integer3 = nativeImage.getWidth();
                integer4 = nativeImage.getHeight();
                integer5 = 0;
                integer6 = 0;
                if (integer3 > integer4) {
                    integer5 = (integer3 - integer4) / 2;
                    integer3 = integer4;
                }
                else {
                    integer6 = (integer4 - integer3) / 2;
                    integer4 = integer3;
                }
                try {
                    cxr3 = new NativeImage(64, 64, false);
                    try {
                        nativeImage.resizeSubRectTo(integer5, integer6, integer3, integer4, cxr3);
                        cxr3.writeToFile(this.minecraft.getSingleplayerServer().getWorldScreenshotFile());
                    }
                    catch (Throwable t) {
                        throw t;
                    }
                    finally {
                        if (cxr3 != null) {
                            if (t2 != null) {
                                try {
                                    cxr3.close();
                                }
                                catch (Throwable exception) {
                                    t2.addSuppressed(exception);
                                }
                            }
                            else {
                                cxr3.close();
                            }
                        }
                    }
                }
                catch (IOException iOException7) {
                    GameRenderer.LOGGER.warn("Couldn't save auto screenshot", (Throwable)iOException7);
                }
                finally {
                    nativeImage.close();
                }
            });
        }
    }
    
    private boolean shouldRenderBlockOutline() {
        if (!this.renderBlockOutline) {
            return false;
        }
        final Entity akq2 = this.minecraft.getCameraEntity();
        boolean boolean3 = akq2 instanceof Player && !this.minecraft.options.hideGui;
        if (boolean3 && !((Player)akq2).abilities.mayBuild) {
            final ItemStack ben4 = ((LivingEntity)akq2).getMainHandItem();
            final HitResult cvi5 = this.minecraft.hitResult;
            if (cvi5 != null && cvi5.getType() == HitResult.Type.BLOCK) {
                final BlockPos fk6 = ((BlockHitResult)cvi5).getBlockPos();
                final BlockState byj7 = this.minecraft.level.getBlockState(fk6);
                if (this.minecraft.gameMode.getPlayerMode() == GameType.SPECTATOR) {
                    boolean3 = (byj7.getMenuProvider(this.minecraft.level, fk6) != null);
                }
                else {
                    final BlockInWorld byn8 = new BlockInWorld(this.minecraft.level, fk6, false);
                    boolean3 = (!ben4.isEmpty() && (ben4.hasAdventureModeBreakTagForBlock(this.minecraft.level.getTagManager(), byn8) || ben4.hasAdventureModePlaceTagForBlock(this.minecraft.level.getTagManager(), byn8)));
                }
            }
        }
        return boolean3;
    }
    
    public void renderLevel(final float float1, final long long2, final PoseStack cyl) {
        this.lightTexture.updateLightTexture(float1);
        if (this.minecraft.getCameraEntity() == null) {
            this.minecraft.setCameraEntity(this.minecraft.player);
        }
        this.pick(float1);
        this.minecraft.getProfiler().push("center");
        final boolean boolean6 = this.shouldRenderBlockOutline();
        this.minecraft.getProfiler().popPush("camera");
        final Camera dbb7 = this.mainCamera;
        this.renderDistance = (float)(this.minecraft.options.renderDistance * 16);
        final PoseStack cyl2 = new PoseStack();
        cyl2.last().pose().multiply(this.getProjectionMatrix(dbb7, float1, true));
        this.bobHurt(cyl2, float1);
        if (this.minecraft.options.bobView) {
            this.bobView(cyl2, float1);
        }
        final float float2 = Mth.lerp(float1, this.minecraft.player.oPortalTime, this.minecraft.player.portalTime);
        if (float2 > 0.0f) {
            int integer10 = 20;
            if (this.minecraft.player.hasEffect(MobEffects.CONFUSION)) {
                integer10 = 7;
            }
            float float3 = 5.0f / (float2 * float2 + 5.0f) - float2 * 0.04f;
            float3 *= float3;
            final Vector3f e12 = new Vector3f(0.0f, Mth.SQRT_OF_TWO / 2.0f, Mth.SQRT_OF_TWO / 2.0f);
            cyl2.mulPose(e12.rotationDegrees((this.tick + float1) * integer10));
            cyl2.scale(1.0f / float3, 1.0f, 1.0f);
            final float float4 = -(this.tick + float1) * integer10;
            cyl2.mulPose(e12.rotationDegrees(float4));
        }
        final Matrix4f b10 = cyl2.last().pose();
        this.resetProjectionMatrix(b10);
        dbb7.setup(this.minecraft.level, (this.minecraft.getCameraEntity() == null) ? this.minecraft.player : this.minecraft.getCameraEntity(), this.minecraft.options.thirdPersonView > 0, this.minecraft.options.thirdPersonView == 2, float1);
        cyl.mulPose(Vector3f.XP.rotationDegrees(dbb7.getXRot()));
        cyl.mulPose(Vector3f.YP.rotationDegrees(dbb7.getYRot() + 180.0f));
        this.minecraft.levelRenderer.renderLevel(cyl, float1, long2, boolean6, dbb7, this, this.lightTexture, b10);
        this.minecraft.getProfiler().popPush("hand");
        if (this.renderHand) {
            RenderSystem.clear(256, Minecraft.ON_OSX);
            this.renderItemInHand(cyl, dbb7, float1);
        }
        this.minecraft.getProfiler().pop();
    }
    
    public void resetData() {
        this.itemActivationItem = null;
        this.mapRenderer.resetData();
        this.mainCamera.reset();
    }
    
    public MapRenderer getMapRenderer() {
        return this.mapRenderer;
    }
    
    public void displayItemActivation(final ItemStack ben) {
        this.itemActivationItem = ben;
        this.itemActivationTicks = 40;
        this.itemActivationOffX = this.random.nextFloat() * 2.0f - 1.0f;
        this.itemActivationOffY = this.random.nextFloat() * 2.0f - 1.0f;
    }
    
    private void renderItemActivationAnimation(final int integer1, final int integer2, final float float3) {
        if (this.itemActivationItem == null || this.itemActivationTicks <= 0) {
            return;
        }
        final int integer3 = 40 - this.itemActivationTicks;
        final float float4 = (integer3 + float3) / 40.0f;
        final float float5 = float4 * float4;
        final float float6 = float4 * float5;
        final float float7 = 10.25f * float6 * float5 - 24.95f * float5 * float5 + 25.5f * float6 - 13.8f * float5 + 4.0f * float4;
        final float float8 = float7 * 3.1415927f;
        final float float9 = this.itemActivationOffX * (integer1 / 4);
        final float float10 = this.itemActivationOffY * (integer2 / 4);
        RenderSystem.enableAlphaTest();
        RenderSystem.pushMatrix();
        RenderSystem.pushLightingAttributes();
        RenderSystem.enableDepthTest();
        RenderSystem.disableCull();
        final PoseStack cyl13 = new PoseStack();
        cyl13.pushPose();
        cyl13.translate(integer1 / 2 + float9 * Mth.abs(Mth.sin(float8 * 2.0f)), integer2 / 2 + float10 * Mth.abs(Mth.sin(float8 * 2.0f)), -50.0);
        final float float11 = 50.0f + 175.0f * Mth.sin(float8);
        cyl13.scale(float11, -float11, float11);
        cyl13.mulPose(Vector3f.YP.rotationDegrees(900.0f * Mth.abs(Mth.sin(float8))));
        cyl13.mulPose(Vector3f.XP.rotationDegrees(6.0f * Mth.cos(float4 * 8.0f)));
        cyl13.mulPose(Vector3f.ZP.rotationDegrees(6.0f * Mth.cos(float4 * 8.0f)));
        final MultiBufferSource.BufferSource a15 = this.renderBuffers.bufferSource();
        this.minecraft.getItemRenderer().renderStatic(this.itemActivationItem, ItemTransforms.TransformType.FIXED, 15728880, OverlayTexture.NO_OVERLAY, cyl13, a15);
        cyl13.popPose();
        a15.endBatch();
        RenderSystem.popAttributes();
        RenderSystem.popMatrix();
        RenderSystem.enableCull();
        RenderSystem.disableDepthTest();
    }
    
    public float getDarkenWorldAmount(final float float1) {
        return Mth.lerp(float1, this.darkenWorldAmountO, this.darkenWorldAmount);
    }
    
    public float getRenderDistance() {
        return this.renderDistance;
    }
    
    public Camera getMainCamera() {
        return this.mainCamera;
    }
    
    public LightTexture lightTexture() {
        return this.lightTexture;
    }
    
    public OverlayTexture overlayTexture() {
        return this.overlayTexture;
    }
    
    static {
        LOGGER = LogManager.getLogger();
        EFFECTS = new ResourceLocation[] { new ResourceLocation("shaders/post/notch.json"), new ResourceLocation("shaders/post/fxaa.json"), new ResourceLocation("shaders/post/art.json"), new ResourceLocation("shaders/post/bumpy.json"), new ResourceLocation("shaders/post/blobs2.json"), new ResourceLocation("shaders/post/pencil.json"), new ResourceLocation("shaders/post/color_convolve.json"), new ResourceLocation("shaders/post/deconverge.json"), new ResourceLocation("shaders/post/flip.json"), new ResourceLocation("shaders/post/invert.json"), new ResourceLocation("shaders/post/ntsc.json"), new ResourceLocation("shaders/post/outline.json"), new ResourceLocation("shaders/post/phosphor.json"), new ResourceLocation("shaders/post/scan_pincushion.json"), new ResourceLocation("shaders/post/sobel.json"), new ResourceLocation("shaders/post/bits.json"), new ResourceLocation("shaders/post/desaturate.json"), new ResourceLocation("shaders/post/green.json"), new ResourceLocation("shaders/post/blur.json"), new ResourceLocation("shaders/post/wobble.json"), new ResourceLocation("shaders/post/blobs.json"), new ResourceLocation("shaders/post/antialias.json"), new ResourceLocation("shaders/post/creeper.json"), new ResourceLocation("shaders/post/spider.json") };
        EFFECT_NONE = GameRenderer.EFFECTS.length;
    }
}
