package eu.mhsl.minenet.minigames.util;

import net.minestom.server.MinecraftServer;
import net.minestom.server.event.server.ServerTickMonitorEvent;
import net.minestom.server.monitoring.TickMonitor;

import java.util.concurrent.atomic.AtomicReference;

public class Monitoring {
    private static final Runtime runtime = Runtime.getRuntime();
    private static final AtomicReference<TickMonitor> lastTick = new AtomicReference<>();

    static {
        MinecraftServer.getGlobalEventHandler().addListener(ServerTickMonitorEvent .class, event -> lastTick.set(event.getTickMonitor()));
    }

    public static long getRamUsage() {
        return (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;
    }

    public static TickMonitor getTickMonitor() {
        return lastTick.get() != null ? lastTick.get() : new TickMonitor(0, 0);
    }


}
