net.lyonlancer5.mcmp.karasu.client.gui.GuiOverlayDebug.java Source code

Java tutorial

Introduction

Here is the source code for net.lyonlancer5.mcmp.karasu.client.gui.GuiOverlayDebug.java

Source

/***************************************************************************\
* Copyright 2016 [Lyonlancer5]                                              *
*                                                                           *
* Licensed under the Apache License, Version 2.0 (the "License");           *
* you may not use this file except in compliance with the License.          *
* You may obtain a copy of the License at                                   *
*                                                                           *
*     http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                           *
* Unless required by applicable law or agreed to in writing, software       *
* distributed under the License is distributed on an "AS IS" BASIS,         *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
* See the License for the specific language governing permissions and       *
* limitations under the License.                                            *
\***************************************************************************/
package net.lyonlancer5.mcmp.karasu.client.gui;

import java.util.List;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.lyonlancer5.mcmp.karasu.lib.BlockData;
import net.lyonlancer5.mcmp.karasu.lib.BlockPos;
import net.lyonlancer5.mcmp.karasu.lib.IBlockMetadata;
import net.lyonlancer5.mcmp.karasu.lib.WorldUtils;
import net.lyonlancer5.mcmp.karasu.util.Constants;
import net.minecraft.block.Block;
import net.minecraft.client.ClientBrandRetriever;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.StatCollector;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.MinecraftForge;

/**
 * Debug screen found on Minecraft 1.8+
 * 
 * A lot of work still need to be done with its current state
 */
@SideOnly(Side.CLIENT)
public class GuiOverlayDebug extends Gui {
    private static final FrameTimer frametimer = new FrameTimer();
    private final Minecraft mc;
    private final FontRenderer fontRenderer;

    public GuiOverlayDebug(Minecraft mc) {
        this.mc = mc;
        this.fontRenderer = mc.fontRenderer;
    }

    public void renderDebugInfo(ScaledResolution scaledResolutionIn) {
        this.mc.mcProfiler.startSection("debug");
        GL11.glPushMatrix();
        this.renderDebugInfoLeft();
        this.renderDebugInfoRight(scaledResolutionIn);

        if (doDisplayLagometer()) {
            this.renderLagometer(scaledResolutionIn);
        }

        GL11.glPopMatrix();
        this.mc.mcProfiler.endSection();
    }

    private boolean isReducedDebug() {
        //return this.mc.thePlayer.hasReducedDebug() || this.mc.gameSettings.reducedDebugInfo;
        return false;
    }

    private boolean doDisplayLagometer() {
        return false;
    }

    protected void renderDebugInfoLeft() {
        List<String> list = this.call();

        for (int i = 0; i < list.size(); ++i) {
            String s = (String) list.get(i);

            if (!Strings.isNullOrEmpty(s)) {
                int j = this.fontRenderer.FONT_HEIGHT;
                int k = this.fontRenderer.getStringWidth(s);
                int i1 = 2 + j * i;
                drawRect(1, i1 - 1, 2 + k + 1, i1 + j - 1, -1873784752);
                this.fontRenderer.drawString(s, 2, i1, 14737632);
            }
        }
    }

    protected void renderDebugInfoRight(ScaledResolution p_175239_1_) {
        List<String> list = this.getDebugInfoRight();

        for (int i = 0; i < list.size(); ++i) {
            String s = (String) list.get(i);

            if (!Strings.isNullOrEmpty(s)) {
                int j = this.fontRenderer.FONT_HEIGHT;
                int k = this.fontRenderer.getStringWidth(s);
                int l = p_175239_1_.getScaledWidth() - 2 - k;
                int i1 = 2 + j * i;
                drawRect(l - 1, i1 - 1, l + k + 1, i1 + j - 1, -1873784752);
                this.fontRenderer.drawString(s, l, i1, 14737632);
            }
        }
    }

    protected List<String> call() {
        //BlockPos blockpos = new BlockPos(this.mc.renderViewEntity.posX, this.mc.renderViewEntity.boundingBox.minY, this.mc.renderViewEntity.posZ);
        BlockPos blockpos = new BlockPos(this.mc.renderViewEntity.posX, this.mc.renderViewEntity.boundingBox.minY,
                this.mc.renderViewEntity.posZ);
        if (this.isReducedDebug()) {
            return Lists.newArrayList(new String[] {
                    "Minecraft 1.7.10 (" + Loader.MC_VERSION + "/" + ClientBrandRetriever.getClientModName() + ")",
                    this.mc.debug, this.mc.renderGlobal.getDebugInfoRenders(),
                    this.mc.renderGlobal.getDebugInfoEntities(),
                    "P: " + this.mc.effectRenderer.getStatistics() + ". T: "
                            + this.mc.theWorld.getDebugLoadedEntities(),
                    this.mc.theWorld.getProviderName(), "",
                    String.format("Chunk-relative: %d %d %d", new Object[] { Integer.valueOf(blockpos.getX() & 15),
                            Integer.valueOf(blockpos.getY() & 15), Integer.valueOf(blockpos.getZ() & 15) }) });
        } else {
            int i4 = MathHelper.floor_double((double) (this.mc.thePlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
            Entity entity = this.mc.renderViewEntity;
            //EnumFacing enumfacing = entity.getHorizontalFacing();
            String s = "Invalid";
            String[] directions = new String[] { "SOUTH", "WEST", "NORTH", "EAST" };
            switch (i4) {
            case 0:
                s = "Towards positive Z";
                break;
            case 1:
                s = "Towards negative X";
                break;
            case 2:
                s = "Towards negative Z";
                break;
            case 3:
                s = "Towards positive X";

            }

            List<String> list = Lists.newArrayList(new String[] {
                    "Minecraft 1.7.10 (" + Loader.MC_VERSION + "/" + ClientBrandRetriever.getClientModName() + ")",
                    this.mc.debug, this.mc.renderGlobal.getDebugInfoRenders(),
                    this.mc.renderGlobal.getDebugInfoEntities(), "P: " + this.mc.effectRenderer.getStatistics()
                            + ". T: " + this.mc.theWorld.getDebugLoadedEntities(),
                    this.mc.theWorld.getProviderName(), "",
                    String.format("XYZ: %.3f / %.5f / %.3f",
                            new Object[] { Double.valueOf(this.mc.renderViewEntity.posX),
                                    Double.valueOf(this.mc.renderViewEntity.boundingBox.minY),
                                    Double.valueOf(this.mc.renderViewEntity.posZ) }),
                    String.format("Block: %d %d %d",
                            new Object[] { Integer.valueOf(blockpos.getX()), Integer.valueOf(blockpos.getY()),
                                    Integer.valueOf(blockpos.getZ()) }),
                    String.format("Chunk: %d %d %d in %d %d %d",
                            new Object[] { Integer.valueOf(blockpos.getX() & 15),
                                    Integer.valueOf(blockpos.getY() & 15), Integer.valueOf(blockpos.getZ() & 15),
                                    Integer.valueOf(blockpos.getX() >> 4), Integer.valueOf(blockpos.getY() >> 4),
                                    Integer.valueOf(blockpos.getZ() >> 4) }),
                    String.format("Facing: %s (%s) (%.1f / %.1f)",
                            new Object[] { directions[i4], s,
                                    Float.valueOf(MathHelper.wrapAngleTo180_float(entity.rotationYaw)),
                                    Float.valueOf(MathHelper.wrapAngleTo180_float(entity.rotationPitch)) }) });

            if (this.mc.theWorld != null && WorldUtils.isBlockLoaded(this.mc.theWorld, blockpos)) {
                Chunk chunk = this.mc.theWorld.getChunkFromBlockCoords(blockpos.getX(), blockpos.getZ());
                list.add("Biome: " + chunk.getBiomeGenForWorldCoords(blockpos.getX() & 15, blockpos.getZ() & 15,
                        this.mc.theWorld.getWorldChunkManager()).biomeName);
                list.add("Light: "
                        + chunk.func_150808_b(blockpos.getX() & 15, blockpos.getY(), blockpos.getZ() & 15) + " ("
                        + chunk.getSavedLightValue(EnumSkyBlock.Sky, blockpos.getX() & 15, blockpos.getY(),
                                blockpos.getZ() & 15)
                        + " sky, " + chunk.getSavedLightValue(EnumSkyBlock.Block, blockpos.getX() & 15,
                                blockpos.getY(), blockpos.getZ() & 15)
                        + " block)");
                //DifficultyInstance difficultyinstance = this.mc.theWorld.getDifficultyForLocation(blockpos);
                EnumDifficulty diff = this.mc.theWorld.difficultySetting;

                if (this.mc.isIntegratedServerRunning() && this.mc.getIntegratedServer() != null) {
                    //EntityPlayerMP entityplayermp = this.mc.getIntegratedServer().getConfigurationManager().getPlayerByUUID(this.mc.thePlayer.getUniqueID());
                    EntityPlayerMP entityplayermp = this.mc.getIntegratedServer().getConfigurationManager()
                            .func_152612_a(this.mc.thePlayer.getCommandSenderName());

                    if (entityplayermp != null) {
                        //difficultyinstance = entityplayermp.worldObj.getDifficultyForLocation(new BlockPos(entityplayermp));
                        diff = entityplayermp.worldObj.difficultySetting;
                    }
                }

                //list.add(String.format("Local Difficulty: %.2f (Day %d)", new Object[] {Float.valueOf(diff.getDifficultyId()), Long.valueOf(this.mc.theWorld.getWorldTime() / 24000L)}));
                list.add(String.format("Local Difficulty: %s (Day %d)",
                        new Object[] { StatCollector.translateToLocal(diff.getDifficultyResourceKey()),
                                Long.valueOf(this.mc.theWorld.getWorldTime() / 24000L) }));
            }

            if (this.mc.entityRenderer != null && this.mc.entityRenderer.isShaderActive()) {
                list.add("Shader: " + this.mc.entityRenderer.getShaderGroup().getShaderGroupName());
            }

            //if (this.mc.objectMouseOver != null && this.mc.objectMouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK && this.mc.objectMouseOver.getBlockPos() != null)
            if (this.mc.objectMouseOver != null
                    && this.mc.objectMouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
                BlockPos blockpos1 = new BlockPos(this.mc.objectMouseOver);
                list.add(String.format("Looking at: %d %d %d", new Object[] { Integer.valueOf(blockpos1.getX()),
                        Integer.valueOf(blockpos1.getY()), Integer.valueOf(blockpos1.getZ()) }));
            }

            return list;
        }
    }

    protected List<String> getDebugInfoRight() {
        long i = Runtime.getRuntime().maxMemory();
        long j = Runtime.getRuntime().totalMemory();
        long k = Runtime.getRuntime().freeMemory();
        long l = j - k;
        List<String> list = Lists.newArrayList(new String[] {
                String.format("Java: %s %dbit",
                        new Object[] { System.getProperty("java.version"),
                                Integer.valueOf(this.mc.isJava64bit() ? 64 : 32) }),
                String.format("Mem: % 2d%% %03d/%03dMB",
                        new Object[] { Long.valueOf(l * 100L / i), Long.valueOf(bytesToMb(l)),
                                Long.valueOf(bytesToMb(i)) }),
                String.format("Allocated: % 2d%% %03dMB",
                        new Object[] { Long.valueOf(j * 100L / i), Long.valueOf(bytesToMb(j)) }),
                "", String.format("CPU: %s", new Object[] { getCpu() }), "",
                String.format("Display: %dx%d (%s)",
                        new Object[] { Integer.valueOf(Display.getWidth()), Integer.valueOf(Display.getHeight()),
                                GL11.glGetString(GL11.GL_VENDOR) }),
                GL11.glGetString(GL11.GL_RENDERER), GL11.glGetString(GL11.GL_VERSION) });

        list.add("");

        //Compute start
        List<String> lst = Lists.newArrayList(FMLCommonHandler.instance().getBrandings(false));
        List<String> lst2 = lst.subList(0, lst.size() - 1);
        lst2.add("Project Karasu v" + Constants.VERSION);
        List<String> lst3 = lst.subList(lst.size() - 1, lst.size());
        lst2.addAll(lst3);
        //Compute end

        list.addAll(lst2);

        if (this.isReducedDebug()) {
            return list;
        } else {
            if (this.mc.objectMouseOver != null
                    && this.mc.objectMouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
                BlockPos blockpos = new BlockPos(this.mc.objectMouseOver);
                BlockData state = WorldUtils.getBlockState(this.mc.theWorld, blockpos);

                list.add("");
                list.add(String.valueOf(Block.blockRegistry.getNameForObject(state.getBlock())));
                list.add("Metadata: " + String.valueOf(state.getMetadata()));

                if (state.getBlock() instanceof IBlockMetadata.Implementor) {
                    IBlockMetadata.Implementor type = (IBlockMetadata.Implementor) state.getBlock();
                    list.add("Variant: " + type.getMetaInfo(state.getMetadata()).getName());
                }
                //for (Entry<IProperty, Comparable> entry : iblockstate.getProperties().entrySet())
                //{
                //    String s = ((Comparable)entry.getValue()).toString();
                //    if (entry.getValue() == Boolean.TRUE)
                //    {
                //        s = EnumChatFormatting.GREEN + s;
                //    }
                //    else if (entry.getValue() == Boolean.FALSE)
                //    {
                //        s = EnumChatFormatting.RED + s;
                //    }
                //
                //    list.add(((IProperty)entry.getKey()).getName() + ": " + s);
                //}
            }

            return list;
        }
    }

    //Lagometer Render
    private void renderLagometer(ScaledResolution scaledresolution) {
        //GL11.glDisable(GL11.GL_DEPTH);
        int i = frametimer.func_181749_a();
        int j = frametimer.func_181750_b();
        long[] along = frametimer.func_181746_c();
        int k = i;
        int l = 0;
        drawRect(0, scaledresolution.getScaledHeight() - 60, 240, scaledresolution.getScaledHeight(), -1873784752);

        while (k != j) {
            int i1 = frametimer.func_181748_a(along[k], 30);
            int j1 = this.func_181552_c(MathHelper.clamp_int(i1, 0, 60), 0, 30, 60);
            this.drawVerticalLine(l, scaledresolution.getScaledHeight(), scaledresolution.getScaledHeight() - i1,
                    j1);
            ++l;
            k = frametimer.func_181751_b(k + 1);
        }

        drawRect(1, scaledresolution.getScaledHeight() - 30 + 1, 14, scaledresolution.getScaledHeight() - 30 + 10,
                -1873784752);
        this.fontRenderer.drawString("60", 2, scaledresolution.getScaledHeight() - 30 + 2, 14737632);
        this.drawHorizontalLine(0, 239, scaledresolution.getScaledHeight() - 30, -1);
        drawRect(1, scaledresolution.getScaledHeight() - 60 + 1, 14, scaledresolution.getScaledHeight() - 60 + 10,
                -1873784752);
        this.fontRenderer.drawString("30", 2, scaledresolution.getScaledHeight() - 60 + 2, 14737632);
        this.drawHorizontalLine(0, 239, scaledresolution.getScaledHeight() - 60, -1);
        this.drawHorizontalLine(0, 239, scaledresolution.getScaledHeight() - 1, -1);
        this.drawVerticalLine(0, scaledresolution.getScaledHeight() - 60, scaledresolution.getScaledHeight(), -1);
        this.drawVerticalLine(239, scaledresolution.getScaledHeight() - 60, scaledresolution.getScaledHeight(), -1);

        if (this.mc.gameSettings.limitFramerate <= 120) {
            this.drawHorizontalLine(0, 239,
                    scaledresolution.getScaledHeight() - 60 + this.mc.gameSettings.limitFramerate / 2, -16711681);
        }

        //GL11.glEnable(GL11.GL_DEPTH);
    }

    private int func_181552_c(int p_181552_1_, int p_181552_2_, int p_181552_3_, int p_181552_4_) {
        return p_181552_1_ < p_181552_3_
                ? this.func_181553_a(-16711936, -256, (float) p_181552_1_ / (float) p_181552_3_)
                : this.func_181553_a(-256, -65536,
                        (float) (p_181552_1_ - p_181552_3_) / (float) (p_181552_4_ - p_181552_3_));
    }

    private int func_181553_a(int p_181553_1_, int p_181553_2_, float p_181553_3_) {
        int i = p_181553_1_ >> 24 & 255;
        int j = p_181553_1_ >> 16 & 255;
        int k = p_181553_1_ >> 8 & 255;
        int l = p_181553_1_ & 255;
        int i1 = p_181553_2_ >> 24 & 255;
        int j1 = p_181553_2_ >> 16 & 255;
        int k1 = p_181553_2_ >> 8 & 255;
        int l1 = p_181553_2_ & 255;
        int i2 = MathHelper.clamp_int((int) ((float) i + (float) (i1 - i) * p_181553_3_), 0, 255);
        int j2 = MathHelper.clamp_int((int) ((float) j + (float) (j1 - j) * p_181553_3_), 0, 255);
        int k2 = MathHelper.clamp_int((int) ((float) k + (float) (k1 - k) * p_181553_3_), 0, 255);
        int l2 = MathHelper.clamp_int((int) ((float) l + (float) (l1 - l) * p_181553_3_), 0, 255);
        return i2 << 24 | j2 << 16 | k2 << 8 | l2;
    }
    //Lagometer Render end

    private static long bytesToMb(long bytes) {
        return bytes / 1024L / 1024L;
    }

    private static String getCpu() {
        return "<>";
    }

    public static class RenderTicker {

        private RenderTicker() {
            ;
        }

        @SubscribeEvent
        public void renderDebugOverlay(RenderGameOverlayEvent.Pre event) {
            if (event.type == RenderGameOverlayEvent.ElementType.DEBUG) {
                (new GuiOverlayDebug(Minecraft.getMinecraft())).renderDebugInfo(event.resolution);
                event.setCanceled(true);
            }
        }

        public static void register() {
            MinecraftForge.EVENT_BUS.register(new RenderTicker());
        }
    }

    public static class TickMonitor {

        private long lastTick = 0;

        private TickMonitor() {
            ;
        }

        @SubscribeEvent
        public void onTickAll(TickEvent event) {
            long k = System.nanoTime();
            GuiOverlayDebug.frametimer.func_181747_a(k - this.lastTick);
            this.lastTick = k;
        }

        public static void register() {
            FMLCommonHandler.instance().bus().register(new TickMonitor());
        }
    }

    public static class FrameTimer {

        private final long[] field_181752_a = new long[240];
        private int field_181753_b;
        private int field_181754_c;
        private int field_181755_d;

        public void func_181747_a(long p_181747_1_) {
            this.field_181752_a[this.field_181755_d] = p_181747_1_;
            ++this.field_181755_d;

            if (this.field_181755_d == 240) {
                this.field_181755_d = 0;
            }

            if (this.field_181754_c < 240) {
                this.field_181753_b = 0;
                ++this.field_181754_c;
            } else {
                this.field_181753_b = this.func_181751_b(this.field_181755_d + 1);
            }
        }

        public int func_181748_a(long p_181748_1_, int p_181748_3_) {
            double d0 = (double) p_181748_1_ / 1.6666666E7D;
            return (int) (d0 * (double) p_181748_3_);
        }

        public int func_181749_a() {
            return this.field_181753_b;
        }

        public int func_181750_b() {
            return this.field_181755_d;
        }

        public int func_181751_b(int p_181751_1_) {
            return p_181751_1_ % 240;
        }

        public long[] func_181746_c() {
            return this.field_181752_a;
        }

    }

}