Skip to content

Commit

Permalink
Merge pull request #166 from LostLuma/add-global-enable-toggle
Browse files Browse the repository at this point in the history
Add config toggle to enable and disable the mod, debug info
  • Loading branch information
LostLuma authored Feb 17, 2024
2 parents 6c11f4e + 4b9cdf8 commit 9ca0379
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 16 deletions.
39 changes: 30 additions & 9 deletions platforms/common/src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class DynamicFPSMod {
private static Config config = Config.ACTIVE;
private static PowerState state = PowerState.FOCUSED;

public static DynamicFPSConfig modConfig = DynamicFPSConfig.load();

private static boolean isForcingLowFPS = false;
private static boolean isDisabled = false;
private static boolean isKeybindDisabled = false;

private static final Minecraft minecraft = Minecraft.getInstance();

Expand All @@ -53,12 +56,34 @@ public static void init() {
Logging.getLogger().info("Dynamic FPS {} active on {}!", platform.modVersion(), platform.getName());
}

public static boolean disabledByUser() {
return isKeybindDisabled;
}

public static boolean isDisabled() {
return isDisabled;
return isKeybindDisabled || !modConfig.enabled() || ModCompat.getInstance().isDisabled();
}

public static String whyIsTheModNotWorking() {
List<String> results = new ArrayList<>();

if (isKeybindDisabled) {
results.add("keybinding");
}

if (!modConfig.enabled()) {
results.add("mod config");
}

if (ModCompat.getInstance().isDisabled()) {
results.add("another mod");
}

return String.join(", ", results);
}

public static void toggleDisabled() {
isDisabled = !isDisabled;
isKeybindDisabled = !isKeybindDisabled;
onStatusChanged(true);
}

Expand Down Expand Up @@ -125,15 +150,11 @@ public static boolean shouldShowToasts() {
}

public static boolean shouldShowLevels() {
return isDisabledInternal() || !(isLevelCoveredByScreen() || isLevelCoveredByOverlay());
return isDisabled() || !(isLevelCoveredByScreen() || isLevelCoveredByOverlay());
}

// Internal logic

private static boolean isDisabledInternal() {
return isDisabled || ModCompat.getInstance().isDisabled();
}

private static boolean isLevelCoveredByScreen() {
return minecraft.screen != null && ((DuckScreen) minecraft.screen).dynamic_fps$rendersBackground();
}
Expand Down Expand Up @@ -233,7 +254,7 @@ private static void checkForStateChanges() {
private static void checkForStateChanges0() {
PowerState current;

if (isDisabledInternal()) {
if (isDisabled()) {
current = PowerState.FOCUSED;
} else if (isForcingLowFPS) {
current = PowerState.UNFOCUSED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundSource;

Expand All @@ -31,6 +32,20 @@ public static Screen genConfigScreen(Screen parent) {
localized("config", "category.general")
);

general.addEntry(
entryBuilder.startBooleanToggle(
localized("config", "enabled"),
DynamicFPSMod.modConfig.enabled()
)
.setDefaultValue(true)
.setSaveConsumer(DynamicFPSMod.modConfig::setEnabled)
.build()
);

general.addEntry(
entryBuilder.startTextDescription(CommonComponents.SPACE).build()
);

general.addEntry(
entryBuilder.startIntSlider(
localized("config", "idle_time"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import dynamic_fps.impl.PowerState;

public final class DynamicFPSConfig {
private boolean enabled;
private int idleTime; // Seconds
private boolean uncapMenuFrameRate;

@SerializedName("states")
private final Map<PowerState, Config> configs;

DynamicFPSConfig(int abandonTime, boolean uncapMenuFrameRate, Map<PowerState, Config> configs) {
DynamicFPSConfig(boolean enabled, int abandonTime, boolean uncapMenuFrameRate, Map<PowerState, Config> configs) {
this.enabled = enabled;
this.idleTime = abandonTime;
this.uncapMenuFrameRate = uncapMenuFrameRate;

Expand All @@ -34,6 +36,14 @@ public Config get(PowerState state) {
}
}

public boolean enabled() {
return this.enabled;
}

public void setEnabled(boolean value) {
this.enabled = value;
}

public int idleTime() {
return this.idleTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ public static DynamicFPSConfig load() {
try {
data = Files.readAllBytes(config);
} catch (NoSuchFileException e) {
DynamicFPSConfig instance = new DynamicFPSConfig(0, false, new EnumMap<>(PowerState.class));
DynamicFPSConfig instance = new DynamicFPSConfig(
true,
0,
false,
new EnumMap<>(PowerState.class)
);
instance.save();
return instance;
} catch (IOException e) {
Expand All @@ -87,6 +92,7 @@ private static void upgradeConfig(JsonObject root) {
upgradeVolumeMultiplier(root);
addAbandonedConfig(root);
addUncapMenuFrameRate(root);
addEnabled(root);
}

private static void addIdleTime(JsonObject root) {
Expand Down Expand Up @@ -154,6 +160,13 @@ private static void addUncapMenuFrameRate(JsonObject root) {
}
}

private static void addEnabled(JsonObject root) {
// Add enabled field if it's missing
if (!root.has("enabled")) {
root.addProperty("enabled", true);
}
}

private static @Nullable JsonObject getStatesAsObject(JsonObject root) {
if (!root.has("states")) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ public class DebugScreenOverlayMixin {
*/
@ModifyReturnValue(method = "getGameInformation", at = @At("RETURN"))
private List<String> getGameInformation(List<String> result) {
PowerState status = DynamicFPSMod.powerState();
if (DynamicFPSMod.isDisabled()) {
String reason = DynamicFPSMod.whyIsTheModNotWorking();
result.add(2, this.format("§c[Dynamic FPS] Inactive! Reason: %s§r", reason));
} else {
PowerState status = DynamicFPSMod.powerState();

if (status != PowerState.FOCUSED) {
int target = DynamicFPSMod.targetFrameRate();
result.add(2, String.format(Locale.ROOT, "§c[Dynamic FPS] FPS: %s P: %s§r", target, status.toString().toLowerCase()));
if (status != PowerState.FOCUSED) {
int target = DynamicFPSMod.targetFrameRate();
result.add(
2,
this.format("§c[Dynamic FPS] FPS: %s P: %s§r", target, status.toString().toLowerCase())
);
}
}

return result;
}

private String format(String template, Object... args) {
return String.format(Locale.ROOT, template, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class HudInfoRenderer {
private static final Minecraft minecraft = Minecraft.getInstance();

public static void renderInfo(GuiGraphics guiGraphics) {
if (DynamicFPSMod.isDisabled()) {
if (DynamicFPSMod.disabledByUser()) {
drawCenteredText(guiGraphics, localized("gui", "hud.disabled"), 32);
} else if (DynamicFPSMod.isForcingLowFPS()) {
drawCenteredText(guiGraphics, localized("gui", "hud.reducing"), 32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"config.dynamic_fps.category.abandoned": "Idle",
"config.dynamic_fps.category.invisible": "Invisible",

"config.dynamic_fps.enabled": "Enabled",

"config.dynamic_fps.disabled": "Disabled",
"config.dynamic_fps.minutes": "%d Minute(s)",

Expand Down

0 comments on commit 9ca0379

Please sign in to comment.