Skip to content

Commit

Permalink
Minecraft 1.18.2 backport
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Apr 21, 2024
1 parent 6996839 commit cbcf10e
Show file tree
Hide file tree
Showing 36 changed files with 485 additions and 103 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,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)
}
}
}
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ mod_version = 3.4.3
maven_group = juliand665
archives_base_name = dynamic-fps

enabled_platforms=fabric
# enabled_platforms=fabric,forge,neoforge,quilt
enabled_platforms=fabric,forge,quilt
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[versions]
minecraft = "1.20.5-rc2"
minecraft = "1.18.2"

# Platform libraries

fabric_loader = "0.15.10"
fabric_api = "0.97.4+1.20.5"
fabric_api = "0.77.0+1.18.2"

forge = "1.20.4-49.0.30"
forge = "1.18.2-40.2.17"

neoforge = "20.4.160-beta"

quilt_loader = "0.25.0"
quilt_standard_libraries = "8.0.0-alpha.12+1.20.4"
quilt_standard_libraries = "1.1.0-beta.26+1.18.2"

# Third-party libraries

modmenu = "10.0.0-alpha.3"
cloth_config = "14.0.125"
modmenu = "3.2.5"
cloth_config = "6.2.57"

mixinextras = "0.3.5"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import dynamic_fps.impl.config.DynamicFPSConfig;
import dynamic_fps.impl.service.ModCompat;
import dynamic_fps.impl.util.Logging;
import dynamic_fps.impl.util.ModCompatHelper;
import dynamic_fps.impl.util.OptionsHolder;
import dynamic_fps.impl.util.duck.DuckScreen;
import dynamic_fps.impl.util.duck.DuckLoadingOverlay;
import dynamic_fps.impl.util.duck.DuckSoundEngine;
import dynamic_fps.impl.util.event.InputObserver;
Expand Down Expand Up @@ -51,6 +53,8 @@ public class DynamicFPSMod {
// Internal "API" for Dynamic FPS itself

public static void init() {
ModCompatHelper.init();

Platform platform = Platform.getInstance();
String version = platform.getModVersion(Constants.MOD_ID).orElseThrow();

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

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

// Internal logic

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

private static boolean isIdle() {
long idleTime = modConfig.idleTime();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
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.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.sounds.SoundSource;

import java.util.Locale;
Expand Down Expand Up @@ -43,7 +44,7 @@ public static Screen genConfigScreen(Screen parent) {
);

general.addEntry(
entryBuilder.startTextDescription(CommonComponents.SPACE).build()
entryBuilder.startTextDescription(new TextComponent(" ")).build()
);

general.addEntry(
Expand Down Expand Up @@ -101,7 +102,7 @@ public static Screen genConfigScreen(Screen parent) {

volumes.add(
entryBuilder.startIntSlider(
Component.translatable("soundCategory." + name),
new TranslatableComponent("soundCategory." + name),
(int) (config.rawVolumeMultiplier(source) * 100),
0, 100
)
Expand All @@ -123,7 +124,7 @@ public static Screen genConfigScreen(Screen parent) {
.setDefaultValue(standard.graphicsState())
.setSaveConsumer(config::setGraphicsState)
.setEnumNameProvider(ClothConfig::graphicsStateMessage)
.setTooltipSupplier(ClothConfig::graphicsStateTooltip)
.setTooltipSupplier((selected) -> ClothConfig.graphicsStateTooltip((GraphicsState) selected)) // what
.build()
);

Expand Down Expand Up @@ -173,14 +174,14 @@ private static int fromConfigFpsTarget(int value) {

private static Component fpsTargetMessage(int value) {
if (toConfigFpsTarget(value) != -1) {
return Component.translatable("options.framerate", value);
return new TranslatableComponent("options.framerate", value);
} else {
return Component.translatable("options.framerateLimit.max");
return new TranslatableComponent("options.framerateLimit.max");
}
}

private static Component volumeMultiplierMessage(int value) {
return Component.literal(Integer.toString(value) + "%");
return new TranslatableComponent(Integer.toString(value) + "%");
}

private static Component graphicsStateMessage(Enum<GraphicsState> graphicsState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void getFramerateLimit(CallbackInfoReturnable<Integer> callbackInfo) {
callbackInfo.setReturnValue(limit);
}
} else if (DynamicFPSMod.uncapMenuFrameRate()) {
if (this.options.enableVsync().get()) {
if (this.options.enableVsync) {
// VSync will regulate to a non-infinite value
callbackInfo.setReturnValue(NO_FRAME_RATE_LIMIT);
} else {
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,19 +1,23 @@
package dynamic_fps.impl.mixin;

import java.util.Deque;

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.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.Redirect;

import dynamic_fps.impl.DynamicFPSMod;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.components.toasts.ToastComponent;

@Mixin(ToastComponent.class)
public class ToastComponentMixin {
@Inject(method = "freeSlots", at = @At("HEAD"), cancellable = true)
private void hasFreeSlots(CallbackInfoReturnable<Integer> callbackInfo) {
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Ljava/util/Deque;isEmpty()Z"))
private boolean onQueueIsEmpty(Deque<Toast> queued) {
if (!DynamicFPSMod.shouldShowToasts()) {
callbackInfo.setReturnValue(0);
return true;
} else {
return queued.isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ public interface ModCompat {

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 @@ -2,44 +2,37 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.Font.DisplayMode;
import net.minecraft.network.chat.Component;

import static dynamic_fps.impl.util.Localization.localized;

import org.joml.Matrix4f;
import com.mojang.blaze3d.vertex.PoseStack;

import dynamic_fps.impl.DynamicFPSMod;

public final class HudInfoRenderer {
private static final Minecraft minecraft = Minecraft.getInstance();

public static void renderInfo(GuiGraphics guiGraphics) {
public static void renderInfo(PoseStack poseStack) {
if (DynamicFPSMod.disabledByUser()) {
drawCenteredText(guiGraphics, localized("gui", "hud.disabled"), 32);
drawCenteredText(poseStack, localized("gui", "hud.disabled"), 32);
} else if (DynamicFPSMod.isForcingLowFPS()) {
drawCenteredText(guiGraphics, localized("gui", "hud.reducing"), 32);
drawCenteredText(poseStack, localized("gui", "hud.reducing"), 32);
}
}

private static void drawCenteredText(GuiGraphics guiGraphics, Component component, float y) {
private static void drawCenteredText(PoseStack poseStack, Component component, float y) {
Font fontRenderer = minecraft.gui.getFont();

int stringWidth = fontRenderer.width(component);
int windowWidth = minecraft.getWindow().getGuiScaledWidth();

fontRenderer.drawInBatch(
fontRenderer.drawShadow(
poseStack,
component,
(windowWidth - stringWidth) / 2f,
y,
0xFFFFFFFF,
true,
new Matrix4f(),
guiGraphics.bufferSource(),
DisplayMode.NORMAL,
0,
255
0xFFFFFFFF
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dynamic_fps.impl.util;

import dynamic_fps.impl.Constants;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TranslatableComponent;

public final class Localization {
/** e.g. keyString("title", "config") -> "title.dynamic_fps.config") */
Expand All @@ -11,7 +11,7 @@ public static String translationKey(String domain, String path) {
}

public static MutableComponent localized(String domain, String path, Object... args) {
return Component.translatable(translationKey(domain, path), args);
return new TranslatableComponent(translationKey(domain, path), args);
}

private Localization() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dynamic_fps.impl.util;

import dynamic_fps.impl.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Logging {
private static final Logger logger = LoggerFactory.getLogger(Constants.MOD_ID);
private static final Logger logger = LogManager.getLogger(Constants.MOD_ID);

public static Logger getLogger() {
return logger;
Expand Down
Loading

0 comments on commit cbcf10e

Please sign in to comment.