    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[10];
    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(356, 246));
    034            this.setMinimumSize(new Dimension(356, 246));
    035            this.setMaximumSize(new Dimension(356, 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 x = 0;
    057                for (Integer id : DimensionManager.getIDs())
    058                {
    059                    this.displayStrings[5 + x] = "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 + x] = this.displayStrings[5 + x] + ", " + world.theChunkProviderServer.makeString();
    065                    }
    066                    x++;
    067                }
    068            }
    069
    070            this.memoryUse[this.updateCounter++ & 255] = (int)(this.func_79015_a(this.field_79017_e.sentPacketSizeArray) * 100.0D / 12500.0D);
    071            this.repaint();
    072        }
    073
    074        private double func_79015_a(long[] par1ArrayOfLong)
    075        {
    076            long var2 = 0L;
    077            long[] var4 = par1ArrayOfLong;
    078            int var5 = par1ArrayOfLong.length;
    079
    080            for (int var6 = 0; var6 < var5; ++var6)
    081            {
    082                long var7 = var4[var6];
    083                var2 += var7;
    084            }
    085
    086            return (double)var2 / (double)par1ArrayOfLong.length;
    087        }
    088
    089        public void paint(Graphics par1Graphics)
    090        {
    091            par1Graphics.setColor(new Color(16777215));
    092            par1Graphics.fillRect(0, 0, 356, 246);
    093            int var2;
    094
    095            for (var2 = 0; var2 < 256; ++var2)
    096            {
    097                int var3 = this.memoryUse[var2 + this.updateCounter & 255];
    098                par1Graphics.setColor(new Color(var3 + 28 << 16));
    099                par1Graphics.fillRect(var2, 100 - var3, 1, var3);
    100            }
    101
    102            par1Graphics.setColor(Color.BLACK);
    103
    104            for (var2 = 0; var2 < this.displayStrings.length; ++var2)
    105            {
    106                String var4 = this.displayStrings[var2];
    107
    108                if (var4 != null)
    109                {
    110                    par1Graphics.drawString(var4, 32, 116 + var2 * 16);
    111                }
    112            }
    113        }
    114
    115        /**
    116         * Public static accessor to call updateStats.
    117         */
    118        static void update(GuiStatsComponent par0GuiStatsComponent)
    119        {
    120            par0GuiStatsComponent.updateStats();
    121        }
    122    }