    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.awt.Color;
    006    import java.awt.Dimension;
    007    import java.awt.Graphics;
    008    import java.text.DecimalFormat;
    009    import javax.swing.JComponent;
    010    import net.minecraft.server.MinecraftServer;
    011    import net.minecraftforge.common.DimensionManager;
    012
    013    @SideOnly(Side.SERVER)
    014    public class GuiStatsComponent extends JComponent
    015    {
    016        private static final DecimalFormat field_79020_a = new DecimalFormat("########0.000");
    017
    018        /** An array containing the columns that make up the memory use graph. */
    019        private int[] memoryUse = new int[256];
    020
    021        /**
    022         * Counts the number of updates. Used as the index into the memoryUse array to display the latest value.
    023         */
    024        private int updateCounter = 0;
    025
    026        /** An array containing the strings displayed in this stats component. */
    027        private String[] displayStrings = new String[11];
    028        private final MinecraftServer field_79017_e;
    029
    030        public GuiStatsComponent(MinecraftServer par1MinecraftServer)
    031        {
    032            this.field_79017_e = par1MinecraftServer;
    033            this.setPreferredSize(new Dimension(456, 246));
    034            this.setMinimumSize(new Dimension(456, 246));
    035            this.setMaximumSize(new Dimension(456, 246));
    036            (new javax.swing.Timer(500, new GuiStatsListener(this))).start();
    037            this.setBackground(Color.BLACK);
    038        }
    039
    040        /**
    041         * Updates the stat values and calls paint to redraw the component.
    042         */
    043        private void updateStats()
    044        {
    045            this.displayStrings = new String[5 + DimensionManager.getIDs().length];
    046            long var1 = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    047            System.gc();
    048            this.displayStrings[0] = "Memory use: " + var1 / 1024L / 1024L + " mb (" + Runtime.getRuntime().freeMemory() * 100L / Runtime.getRuntime().maxMemory() + "% free)";
    049            this.displayStrings[1] = "Threads: " + TcpConnection.field_74471_a.get() + " + " + TcpConnection.field_74469_b.get();
    050            this.displayStrings[2] = "Avg tick: " + field_79020_a.format(this.func_79015_a(this.field_79017_e.tickTimeArray) * 1.0E-6D) + " ms";
    051            this.displayStrings[3] = "Avg sent: " + (int)this.func_79015_a(this.field_79017_e.sentPacketCountArray) + ", Avg size: " + (int)this.func_79015_a(this.field_79017_e.sentPacketSizeArray);
    052            this.displayStrings[4] = "Avg rec: " + (int)this.func_79015_a(this.field_79017_e.receivedPacketCountArray) + ", Avg size: " + (int)this.func_79015_a(this.field_79017_e.receivedPacketSizeArray);
    053
    054            if (this.field_79017_e.worldServers != null)
    055            {
    056                int var3 = 0;
    057                for (Integer id : DimensionManager.getIDs())
    058                {
    059                    this.displayStrings[5 + var3] = "Lvl " + id + " tick: " + field_79020_a.format(this.func_79015_a(this.field_79017_e.worldTickTimes.get(id)) * 1.0E-6D) + " ms";
    060
    061                    WorldServer world = DimensionManager.getWorld(id);
    062                    if (world != null && world.theChunkProviderServer != null)
    063                    {
    064                        this.displayStrings[5 + var3] = this.displayStrings[5 + var3] + ", " + world.theChunkProviderServer.makeString();
    065                        this.displayStrings[5 + var3] = this.displayStrings[5 + var3] + ", Vec3: " + world.getWorldVec3Pool().func_82590_d() + " / " + world.getWorldVec3Pool().func_82591_c();
    066                    }
    067                    var3++;
    068                }
    069            }
    070
    071            this.memoryUse[this.updateCounter++ & 255] = (int)(this.func_79015_a(this.field_79017_e.sentPacketSizeArray) * 100.0D / 12500.0D);
    072            this.repaint();
    073        }
    074
    075        private double func_79015_a(long[] par1ArrayOfLong)
    076        {
    077            long var2 = 0L;
    078
    079            for (int var4 = 0; var4 < par1ArrayOfLong.length; ++var4)
    080            {
    081                var2 += par1ArrayOfLong[var4];
    082            }
    083
    084            return (double)var2 / (double)par1ArrayOfLong.length;
    085        }
    086
    087        public void paint(Graphics par1Graphics)
    088        {
    089            par1Graphics.setColor(new Color(16777215));
    090            par1Graphics.fillRect(0, 0, 456, 246);
    091            int var2;
    092
    093            for (var2 = 0; var2 < 256; ++var2)
    094            {
    095                int var3 = this.memoryUse[var2 + this.updateCounter & 255];
    096                par1Graphics.setColor(new Color(var3 + 28 << 16));
    097                par1Graphics.fillRect(var2, 100 - var3, 1, var3);
    098            }
    099
    100            par1Graphics.setColor(Color.BLACK);
    101
    102            for (var2 = 0; var2 < this.displayStrings.length; ++var2)
    103            {
    104                String var4 = this.displayStrings[var2];
    105
    106                if (var4 != null)
    107                {
    108                    par1Graphics.drawString(var4, 32, 116 + var2 * 16);
    109                }
    110            }
    111        }
    112
    113        /**
    114         * Public static accessor to call updateStats.
    115         */
    116        static void update(GuiStatsComponent par0GuiStatsComponent)
    117        {
    118            par0GuiStatsComponent.updateStats();
    119        }
    120    }