Skip to content

Commit

Permalink
Minecraft 1.20.0 backport
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Jun 30, 2024
1 parent f4319fd commit 58f084d
Show file tree
Hide file tree
Showing 32 changed files with 452 additions and 39 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ subprojects {
withSourcesJar()

toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(17)
}
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"

javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(17)
}
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ maven_group = juliand665
archives_base_name = dynamic-fps

# Game Properties
minecraft_version_min = 1.21.0
enabled_platforms=fabric,neoforge
minecraft_version_min = 1.20.0
enabled_platforms=fabric,forge,neoforge,quilt
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[versions]
minecraft = "1.21"
minecraft = "1.20.4"

# Platform libraries

fabric_loader = "0.15.10"
fabric_api = "0.100.1+1.21"
fabric_api = "0.96.1+1.20.4"

forge = "1.20.4-49.0.30"

neoforge = "21.0.0-beta"
neoforge = "20.4.237"

quilt_loader = "0.25.0"
quilt_standard_libraries = "8.0.0-alpha.12+1.20.4"
Expand All @@ -19,8 +19,8 @@ battery = "1.0.1"

# Modding libraries

modmenu = "11.0.0-beta.1"
cloth_config = "15.0.127"
modmenu = "9.0.0"
cloth_config = "13.0.121"

mixinextras = "0.3.5"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import dynamic_fps.impl.util.FallbackConfigScreen;
import dynamic_fps.impl.util.Logging;
import dynamic_fps.impl.feature.state.OptionHolder;
import dynamic_fps.impl.util.ModCompatHelper;
import dynamic_fps.impl.util.ResourceLocations;
import dynamic_fps.impl.util.Version;
import dynamic_fps.impl.feature.volume.SmoothVolumeHandler;
import dynamic_fps.impl.util.duck.DuckLoadingOverlay;
import dynamic_fps.impl.feature.state.WindowObserver;
import dynamic_fps.impl.service.Platform;
import dynamic_fps.impl.util.duck.DuckScreen;
import net.lostluma.battery.api.State;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -174,7 +176,7 @@ public static boolean shouldShowToasts() {
}

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

public static void onBatteryChargeChanged(int before, int after) {
Expand All @@ -196,11 +198,17 @@ public static void onBatteryStatusChanged(State before, State after) {
private static void doInit() {
// NOTE: Init battery tracker first here
// Since the idle handler queries it for info
ModCompatHelper.init();

BatteryTracker.init();
IdleHandler.init();
SmoothVolumeHandler.init();
}

private static boolean isLevelCoveredByScreen() {
return minecraft.screen != null && ((DuckScreen) minecraft.screen).dynamic_fps$rendersBackground();
}

private static void showNotification(String titleTranslationKey, String iconPath) {
if (!DynamicFPSConfig.INSTANCE.batteryTracker().notifications()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void init() {
}
} else {
manager = temp; // Keep around to allow updating batteries
Thread.ofVirtual().name("refresh-battery").start(BatteryTracker::updateBatteries);
new Thread(BatteryTracker::updateBatteries, "refresh-battery").start();
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ private static void updateBatteries() {
updateState();

try {
Thread.sleep(updateInterval);
Thread.sleep(updateInterval.toMillis());
} catch (InterruptedException e) {
active = false;
Thread.currentThread().interrupt();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dynamic_fps.impl.mixin;

import dynamic_fps.impl.service.ModCompat;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import dynamic_fps.impl.util.duck.DuckScreen;
import net.minecraft.client.gui.screens.Screen;

@Mixin(Screen.class)
public class ScreenMixin implements DuckScreen {
@Unique
private boolean dynamic_fps$canOptimize = false;

@Unique
private boolean dynamic_fps$hasOptedOut = false;

@Override
public boolean dynamic_fps$rendersBackground() {
return dynamic_fps$canOptimize;
}

@Override
public void dynamic_fps$setRendersBackground() {
this.dynamic_fps$canOptimize = true;
}

@Inject(method = "init", at = @At("HEAD"))
private void onInit(CallbackInfo callbackInfo) {
String name = this.getClass().getName();

this.dynamic_fps$hasOptedOut = ModCompat.getInstance().isScreenOptedOut(name);

// Allow other mods to opt out on behalf of vanilla screens
// That Dynamic FPS forced to opt in via its own mod metadata.
if (!this.dynamic_fps$hasOptedOut) {
this.dynamic_fps$canOptimize = ModCompat.getInstance().isScreenOptedIn(name);
}
}

@Inject(method = "renderDirtBackground", at = @At("HEAD"))
private void onRenderDirtBackground(CallbackInfo callbackInfo) {
if (!this.dynamic_fps$hasOptedOut) {
this.dynamic_fps$canOptimize = true; // Signal to apply optimizations on next frame
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dynamic_fps.impl.mixin;

import dynamic_fps.impl.util.duck.DuckScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.gui.screens.achievement.StatsScreen;

@Mixin(StatsScreen.class)
public class StatsScreenMixin {
@Inject(method = "onStatsUpdated", at = @At("HEAD"))
private void onStatsUpdated(CallbackInfo callbackInfo) {
((DuckScreen) this).dynamic_fps$setRendersBackground();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package dynamic_fps.impl.service;

import java.util.Set;

public interface ModCompat {
boolean isDisabled();

boolean disableOverlayOptimization();

Set<String> getOptedInScreens();
Set<String> getOptedOutScreens();

default boolean isScreenOptedIn(String className) {
return getOptedInScreens().contains(className);
}

default boolean isScreenOptedOut(String className) {
return getOptedOutScreens().contains(className);
}

static ModCompat getInstance() {
return Services.MOD_COMPAT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected void init() {

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
this.renderDirtBackground(guiGraphics);
super.render(guiGraphics, mouseX, mouseY, partialTicks);

int width = guiGraphics.guiWidth() / 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dynamic_fps.impl.util;

import dynamic_fps.impl.service.ModCompat;
import dynamic_fps.impl.service.Platform;

import java.util.Optional;

public class ModCompatHelper {
public static void init() {
fixFastloadSoftLock();
}

/**
* Fix softlock in combination with Fastload <=3.4.0 due to our screen / loading overlay optimization.
*
* See the <a href="https://github.com/juliand665/Dynamic-FPS/issues/129">issue report</a> for more info.
*/
private static void fixFastloadSoftLock() {
Optional<Version> optional = Platform.getInstance().getModVersion("fastload");

if (optional.isEmpty()) {
return;
}

Version other;

try {
other = Version.of("3.4.0");
} catch (Version.VersionParseException e) {
throw new RuntimeException(e);
}

// If a version below 3.4.0 is present opt their custom world loading screen out of our optimization
if (optional.get().compareTo(other) <= 0) {
ModCompat.getInstance().getOptedOutScreens().add(
"io.github.bumblesoftware.fastload.client.BuildingTerrainScreen"
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public class ResourceLocations {
public static ResourceLocation of(String namespace, String path) {
return ResourceLocation.fromNamespaceAndPath(namespace, path);
return new ResourceLocation(namespace, path);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dynamic_fps.impl.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand All @@ -13,9 +14,11 @@
public class VariableStepTransformer {
private boolean unsorted;
private final List<Step> steps;
private final List<Step> reversedSteps;

public VariableStepTransformer() {
this.steps = new ArrayList<>();
this.reversedSteps = new ArrayList<>();
}

/**
Expand All @@ -27,6 +30,7 @@ public VariableStepTransformer() {
public void addStep(int change, int max) {
this.unsorted = true;
this.steps.add(new Step(change, max));
this.reversedSteps.add(new Step(change, max));
}

/**
Expand All @@ -42,7 +46,7 @@ public int toStep(int value) {
int currentChange = 0;
int currentValue = value;

for (Step pair : this.steps.reversed()) {
for (Step pair : this.reversedSteps) {
if (currentValue > pair.max && currentChange != 0) {
step += Math.floorDiv(currentValue - pair.max, currentChange);
currentValue = pair.max;
Expand Down Expand Up @@ -86,6 +90,14 @@ public int compare(Step self, Step other) {
return Integer.compare(self.max, other.max);
}
});

this.reversedSteps.sort(new Comparator<Step>() {
@Override
public int compare(Step self, Step other) {
return Integer.compare(self.max, other.max);
}
});
Collections.reverse(this.reversedSteps);
}

private record Step(int change, int max) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dynamic_fps.impl.util.duck;

public interface DuckScreen {
default boolean dynamic_fps$rendersBackground() {
throw new RuntimeException("No implementation for dynamic_fps$rendersBackground was found.");
}

default void dynamic_fps$setRendersBackground() {
throw new RuntimeException("No implementation for dynamic_fps$rendersBackground was found.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"GuiMixin",
"LoadingOverlayMixin",
"MinecraftMixin",
"ScreenMixin",
"SoundEngineMixin",
"StatsScreenMixin",
"ToastComponentMixin",
"WindowMixin",
"bugfix.BlockableEventLoopMixin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ public class GuiMixin {
/**
* Render info on whether Dynamic FPS is disabled or always reducing the user's FPS.
*/
@Inject(method = "renderSavingIndicator", at = @At("HEAD"))
@Inject(
method = "render",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/Gui;renderSavingIndicator(Lnet/minecraft/client/gui/GuiGraphics;)V"
)
)
private void renderSavingIndicator(CallbackInfo callbackInfo, @Local(argsOnly = true) GuiGraphics guiGraphics) {
HudInfoRenderer.renderInfo(guiGraphics);
}
Expand Down
Loading

0 comments on commit 58f084d

Please sign in to comment.