| 1 | package net.minecraft.server.gui;
|
| 2 |
|
| 3 | import java.awt.Color;
|
| 4 | import java.awt.Dimension;
|
| 5 | import java.awt.Graphics;
|
| 6 | import java.text.DecimalFormat;
|
| 7 | import java.text.DecimalFormatSymbols;
|
| 8 | import java.util.Locale;
|
| 9 | import javax.swing.JComponent;
|
| 10 | import javax.swing.Timer;
|
| 11 | import net.minecraft.server.MinecraftServer;
|
| 12 | import net.minecraft.util.TimeUtil;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class StatsComponent extends JComponent {
|
| 16 | private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("########0.000", DecimalFormatSymbols.getInstance(Locale.ROOT));
|
| 17 | private final int[] values = new int[256];
|
| 18 | private int vp;
|
| 19 | private final @Nullable String[] msgs = new String[11];
|
| 20 | private final MinecraftServer server;
|
| 21 | private final Timer timer;
|
| 22 |
|
| 23 | public StatsComponent(final MinecraftServer server) {
|
| 24 | this.server = server;
|
| 25 | this.setPreferredSize(new Dimension(456, 246));
|
| 26 | this.setMinimumSize(new Dimension(456, 246));
|
| 27 | this.setMaximumSize(new Dimension(456, 246));
|
| 28 | this.timer = new Timer(500, event -> this.tick());
|
| 29 | this.timer.start();
|
| 30 | this.setBackground(Color.BLACK);
|
| 31 | }
|
| 32 |
|
| 33 | private void tick() {
|
| 34 | long usedRam = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
|
| 35 | this.msgs[0] = "Memory use: " + usedRam / 1024L / 1024L + " mb (" + Runtime.getRuntime().freeMemory() * 100L / Runtime.getRuntime().maxMemory() + "% free)";
|
| 36 | this.msgs[1] = "Avg tick: " + DECIMAL_FORMAT.format((double)this.server.getAverageTickTimeNanos() / TimeUtil.NANOSECONDS_PER_MILLISECOND) + " ms";
|
| 37 | this.values[this.vp++ & 0xFF] = (int)(usedRam * 100L / Runtime.getRuntime().maxMemory());
|
| 38 | this.repaint();
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public void paint(final Graphics g) {
|
| 43 | g.setColor(new Color(16777215));
|
| 44 | g.fillRect(0, 0, 456, 246);
|
| 45 |
|
| 46 | for (int x = 0; x < 256; x++) {
|
| 47 | int v = this.values[x + this.vp & 0xFF];
|
| 48 | g.setColor(new Color(v + 28 << 16));
|
| 49 | g.fillRect(x, 100 - v, 1, v);
|
| 50 | }
|
| 51 |
|
| 52 | g.setColor(Color.BLACK);
|
| 53 |
|
| 54 | for (int i = 0; i < this.msgs.length; i++) {
|
| 55 | String msg = this.msgs[i];
|
| 56 | if (msg != null) {
|
| 57 | g.drawString(msg, 32, 116 + i * 16);
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public void close() {
|
| 63 | this.timer.stop();
|
| 64 | }
|
| 65 | }
|
| 66 |
|