    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(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 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                        this.displayStrings[5 + x] = this.displayStrings[5 + x] + ", Vec3: " + world.func_82732_R().func_82590_d() + " / " + world.func_82732_R().func_82591_c();
    066                    }
    067                    x++;
    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            long[] var4 = par1ArrayOfLong;
    079            int var5 = par1ArrayOfLong.length;
    080
    081            for (int var6 = 0; var6 < var5; ++var6)
    082            {
    083                long var7 = var4[var6];
    084                var2 += var7;
    085            }
    086
    087            return (double)var2 / (double)par1ArrayOfLong.length;
    088        }
    089
    090        public void paint(Graphics par1Graphics)
    091        {
    092            par1Graphics.setColor(new Color(16777215));
    093            par1Graphics.fillRect(0, 0, 456, 246);
    094            int var2;
    095
    096            for (var2 = 0; var2 < 256; ++var2)
    097            {
    098                int var3 = this.memoryUse[var2 + this.updateCounter & 255];
    099                par1Graphics.setColor(new Color(var3 + 28 << 16));
    100                par1Graphics.fillRect(var2, 100 - var3, 1, var3);
    101            }
    102
    103            par1Graphics.setColor(Color.BLACK);
    104
    105            for (var2 = 0; var2 < this.displayStrings.length; ++var2)
    106            {
    107                String var4 = this.displayStrings[var2];
    108
    109                if (var4 != null)
    110                {
    111                    par1Graphics.drawString(var4, 32, 116 + var2 * 16);
    112                }
    113            }
    114        }
    115
    116        /**
    117         * Public static accessor to call updateStats.
    118         */
    119        static void update(GuiStatsComponent par0GuiStatsComponent)
    120        {
    121            par0GuiStatsComponent.updateStats();
    122        }
    123    }