Added out-of-focus fps lock option
@@ -2,13 +2,13 @@ package com.matt.forgehax.mods;
import static com.matt.forgehax.Helper.getWorld;
-import com.matt.forgehax.events.LocalPlayerUpdateEvent;
import com.matt.forgehax.util.command.Setting;
import com.matt.forgehax.util.mod.Category;
import com.matt.forgehax.util.mod.ToggleMod;
import com.matt.forgehax.util.mod.loader.RegisterMod;
-import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
+import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
+import org.lwjgl.opengl.Display;
@RegisterMod
public class FPSLock extends ToggleMod {
@@ -17,20 +17,30 @@ public class FPSLock extends ToggleMod {
.builders()
.<Integer>newSettingBuilder()
.name("fps")
- .description("FPS to use when the world is loaded")
+ .description("FPS to use when the world is loaded. Set to 0 to disable.")
.min(0)
- .defaultTo(60)
+ .defaultTo(0)
.build();
private final Setting<Integer> menu_fps =
getCommandStub()
.builders()
.<Integer>newSettingBuilder()
.name("menu-fps")
- .description("FPS when the GUI is opened")
+ .description("FPS when the GUI is opened. Set to 0 to disable.")
.min(0)
.defaultTo(60)
.build();
+ private final Setting<Integer> no_focus_fps =
+ getCommandStub()
+ .builders()
+ .<Integer>newSettingBuilder()
+ .name("no-focus-fps")
+ .description("FPS when the game window doesn't have focus. Set to 0 to disable.")
+ .min(0)
+ .defaultTo(1)
+ .build();
+
public FPSLock() {
super(
Category.MISC,
@@ -39,19 +49,26 @@ public class FPSLock extends ToggleMod {
"Lock the fps to a lower-than-allowed value, and restore when disabled");
}
- @SubscribeEvent
- void onTick(LocalPlayerUpdateEvent event) {
- if (getWorld() != null) MC.gameSettings.limitFramerate = fps.get();
- else MC.gameSettings.limitFramerate = menu_fps.get();
- }
-
- @SubscribeEvent
- void onWorldUnload(WorldEvent.Unload event) {
- MC.gameSettings.limitFramerate = menu_fps.get();
+ private int getFps() {
+ if (no_focus_fps.get() > 0 && !Display.isActive()) return no_focus_fps.get();
+ else if (getWorld() != null) return fps.get() > 0 ? fps.get() : MC.gameSettings.limitFramerate;
+ else return menu_fps.get() > 0 ? menu_fps.get() : MC.gameSettings.limitFramerate;
}
@Override
protected void onDisabled() {
- MC.gameSettings.limitFramerate = menu_fps.get();
+ MC.gameSettings.limitFramerate = 60;
+ }
+
+ @SubscribeEvent
+ void onTick(ClientTickEvent event) {
+ switch (event.phase) {
+ case START:
+ MC.gameSettings.limitFramerate = getFps();
+ break;
+ case END:
+ default:
+ break;
+ }
}
}
