| 1 | package com.mojang.blaze3d.platform;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.InactivityFpsLimit;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.Options;
|
| 8 | import net.minecraft.util.Util;
|
| 9 |
|
| 10 | @Environment(EnvType.CLIENT)
|
| 11 | public class FramerateLimitTracker {
|
| 12 | private static final int OUT_OF_LEVEL_MENU_LIMIT = 60;
|
| 13 | private static final int ICONIFIED_WINDOW_LIMIT = 10;
|
| 14 | private static final int AFK_LIMIT = 30;
|
| 15 | private static final int LONG_AFK_LIMIT = 10;
|
| 16 | private static final long AFK_THRESHOLD_MS = 60000L;
|
| 17 | private static final long LONG_AFK_THRESHOLD_MS = 600000L;
|
| 18 | private final Options options;
|
| 19 | private final Minecraft minecraft;
|
| 20 | private int framerateLimit;
|
| 21 | private long latestInputTime;
|
| 22 |
|
| 23 | public FramerateLimitTracker(final Options options, final Minecraft minecraft) {
|
| 24 | this.options = options;
|
| 25 | this.minecraft = minecraft;
|
| 26 | this.framerateLimit = options.framerateLimit().get();
|
| 27 | }
|
| 28 |
|
| 29 | public int getFramerateLimit() {
|
| 30 | return switch (this.getThrottleReason()) {
|
| 31 | case NONE -> this.framerateLimit;
|
| 32 | case WINDOW_ICONIFIED -> 10;
|
| 33 | case LONG_AFK -> 10;
|
| 34 | case SHORT_AFK -> Math.min(this.framerateLimit, 30);
|
| 35 | case OUT_OF_LEVEL_MENU -> 60;
|
| 36 | };
|
| 37 | }
|
| 38 |
|
| 39 | public FramerateLimitTracker.FramerateThrottleReason getThrottleReason() {
|
| 40 | InactivityFpsLimit inactivityFpsLimit = this.options.inactivityFpsLimit().get();
|
| 41 | Window window = this.minecraft.getWindow();
|
| 42 | if (!window.isIconified() && (window.isFocused() || !window.isExclusiveFullscreen())) {
|
| 43 | if (inactivityFpsLimit == InactivityFpsLimit.AFK) {
|
| 44 | long afkTimeMillis = Util.getMillis() - this.latestInputTime;
|
| 45 | if (afkTimeMillis > 600000L) {
|
| 46 | return FramerateLimitTracker.FramerateThrottleReason.LONG_AFK;
|
| 47 | }
|
| 48 |
|
| 49 | if (afkTimeMillis > 60000L) {
|
| 50 | return FramerateLimitTracker.FramerateThrottleReason.SHORT_AFK;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | return this.minecraft.level != null || this.minecraft.gui.screen() == null && this.minecraft.gui.overlay() == null
|
| 55 | ? FramerateLimitTracker.FramerateThrottleReason.NONE
|
| 56 | : FramerateLimitTracker.FramerateThrottleReason.OUT_OF_LEVEL_MENU;
|
| 57 | } else {
|
| 58 | return FramerateLimitTracker.FramerateThrottleReason.WINDOW_ICONIFIED;
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public boolean isHeavilyThrottled() {
|
| 63 | FramerateLimitTracker.FramerateThrottleReason reason = this.getThrottleReason();
|
| 64 | return reason == FramerateLimitTracker.FramerateThrottleReason.WINDOW_ICONIFIED || reason == FramerateLimitTracker.FramerateThrottleReason.LONG_AFK;
|
| 65 | }
|
| 66 |
|
| 67 | public void setFramerateLimit(final int value) {
|
| 68 | this.framerateLimit = value;
|
| 69 | }
|
| 70 |
|
| 71 | public void onInputReceived() {
|
| 72 | this.latestInputTime = Util.getMillis();
|
| 73 | }
|
| 74 |
|
| 75 | @Environment(EnvType.CLIENT)
|
| 76 | public enum FramerateThrottleReason {
|
| 77 | NONE,
|
| 78 | WINDOW_ICONIFIED,
|
| 79 | LONG_AFK,
|
| 80 | SHORT_AFK,
|
| 81 | OUT_OF_LEVEL_MENU;
|
| 82 | }
|
| 83 | }
|
| 84 |
|