File size: 2,533 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package net.minecraft.server.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import javax.swing.JComponent;
import javax.swing.Timer;
import net.minecraft.Util;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.TimeUtil;

public class StatsComponent extends JComponent {
    private static final DecimalFormat DECIMAL_FORMAT = Util.make(
        new DecimalFormat("########0.000"), p_139968_ -> p_139968_.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ROOT))
    );
    private final int[] values = new int[256];
    private int vp;
    private final String[] msgs = new String[11];
    private final MinecraftServer server;
    private final Timer timer;

    public StatsComponent(MinecraftServer p_139963_) {
        this.server = p_139963_;
        this.setPreferredSize(new Dimension(456, 246));
        this.setMinimumSize(new Dimension(456, 246));
        this.setMaximumSize(new Dimension(456, 246));
        this.timer = new Timer(500, p_139966_ -> this.tick());
        this.timer.start();
        this.setBackground(Color.BLACK);
    }

    private void tick() {
        long i = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        this.msgs[0] = "Memory use: "
            + i / 1024L / 1024L
            + " mb ("
            + Runtime.getRuntime().freeMemory() * 100L / Runtime.getRuntime().maxMemory()
            + "% free)";
        this.msgs[1] = "Avg tick: " + DECIMAL_FORMAT.format((double)this.server.getAverageTickTimeNanos() / (double)TimeUtil.NANOSECONDS_PER_MILLISECOND) + " ms";
        this.values[this.vp++ & 0xFF] = (int)(i * 100L / Runtime.getRuntime().maxMemory());
        this.repaint();
    }

    @Override
    public void paint(Graphics p_139973_) {
        p_139973_.setColor(new Color(16777215));
        p_139973_.fillRect(0, 0, 456, 246);

        for (int i = 0; i < 256; i++) {
            int j = this.values[i + this.vp & 0xFF];
            p_139973_.setColor(new Color(j + 28 << 16));
            p_139973_.fillRect(i, 100 - j, 1, j);
        }

        p_139973_.setColor(Color.BLACK);

        for (int k = 0; k < this.msgs.length; k++) {
            String s = this.msgs[k];
            if (s != null) {
                p_139973_.drawString(s, 32, 116 + k * 16);
            }
        }
    }

    public void close() {
        this.timer.stop();
    }
}