Skip to content

Commit

Permalink
Minecraft 1.18.0 backport
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Oct 30, 2024
1 parent 9d83d17 commit 440137c
Show file tree
Hide file tree
Showing 50 changed files with 663 additions and 320 deletions.
4 changes: 2 additions & 2 deletions build-logic/src/main/kotlin/dynamic_fps.java.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ java {
withSourcesJar()

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

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"

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

# File naming version
minecraft_version = 1.21.2
minecraft_version = 1.18.0
# Version for publishing
minecraft_version_min = 1.21.2
minecraft_version_max = 1.21.2
minecraft_version_min = 1.18
minecraft_version_max = 1.18.2

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,14 +1,14 @@
[versions]
minecraft = "1.21.3"
minecraft = "1.18.2"

# Platform libraries

fabric_loader = "0.15.10"
fabric_api = "0.102.2+1.21.2"
fabric_api = "0.77.0+1.18.2"

forge = "1.21.3-53.0.7"
forge = "1.18.2-40.2.17"

neoforge = "21.3.0-beta"
neoforge = "20.4.237"

quilt_loader = "0.25.0"

Expand All @@ -18,8 +18,8 @@ battery = "1.3.0"

# Modding libraries

modmenu = "11.0.0-beta.1"
cloth_config = "15.0.127"
modmenu = "3.2.5"
cloth_config = "6.2.57"

mixinextras = "0.3.5"

Expand Down
19 changes: 11 additions & 8 deletions platforms/common/src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
import dynamic_fps.impl.feature.battery.BatteryToast;
import dynamic_fps.impl.feature.battery.BatteryTracker;
import dynamic_fps.impl.feature.state.IdleHandler;
import dynamic_fps.impl.util.BatteryUtil;
import dynamic_fps.impl.util.FallbackConfigScreen;
import dynamic_fps.impl.util.Logging;
import dynamic_fps.impl.util.*;
import dynamic_fps.impl.feature.state.OptionHolder;
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 @@ -178,7 +175,7 @@ public static GraphicsState graphicsState() {
}

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

public static void onBatteryChargeChanged(int before, int after) {
Expand All @@ -200,11 +197,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 Expand Up @@ -247,7 +250,7 @@ public static void handleStateChange(PowerState previous, PowerState current) {
}

// The FOCUSED config doesn't have the user's actual vsync preference sadly ...
boolean enableVsync = current != PowerState.FOCUSED ? config.enableVsync() : minecraft.options.enableVsync().get();
boolean enableVsync = current != PowerState.FOCUSED ? config.enableVsync() : minecraft.options.enableVsync;

if (enableVsync != before.enableVsync()) {
minecraft.getWindow().updateVsync(enableVsync);
Expand All @@ -263,7 +266,7 @@ private static void checkForStateChanges() {
checkForStateChanges0();
} else {
// Schedule check for the beginning of the next frame
minecraft.schedule(DynamicFPSMod::checkForStateChanges0);
minecraft.tell(DynamicFPSMod::checkForStateChanges0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,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 @@ -64,7 +65,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 @@ -94,7 +95,7 @@ public static Screen genConfigScreen(Screen parent) {
);

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

VariableStepTransformer volumeTransformer = getVolumeStepTransformer();
Expand Down Expand Up @@ -126,7 +127,7 @@ public static Screen genConfigScreen(Screen parent) {
);

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

BatteryTrackerConfig batteryTracker = config.batteryTracker();
Expand Down Expand Up @@ -220,7 +221,7 @@ public static Screen genConfigScreen(Screen parent) {

category.addEntry(
entryBuilder.startBooleanToggle(
Component.translatable("options.vsync"),
new TranslatableComponent("options.vsync"),
instance.enableVsync()
)
.setDefaultValue(standard.enableVsync())
Expand All @@ -240,7 +241,7 @@ public static Screen genConfigScreen(Screen parent) {

volumes.add(
entryBuilder.startIntSlider(
Component.translatable("soundCategory." + name),
new TranslatableComponent("soundCategory." + name),
(int) (instance.rawVolumeMultiplier(source) * 100),
0, 100
)
Expand Down Expand Up @@ -346,7 +347,7 @@ private static Component volumeTransitionMessage(int step) {
int value = getVolumeStepTransformer().toValue(step);

if (value <= 300) {
return Component.literal(value + "%");
return new TextComponent(value + "%");
} else {
return localized("config", "volume_transition_speed_instant");
}
Expand All @@ -368,14 +369,14 @@ private static Component fpsTargetMessage(int step) {
int fps = getFpsTransformer().toValue(step);

if (fps != Constants.NO_FRAME_RATE_LIMIT) {
return Component.translatable("options.framerate", fps);
return new TranslatableComponent("options.framerate", fps);
} 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 TextComponent(Integer.toString(value) + "%");
}

public static Component IdleConditionMessage(Enum<IdleCondition> state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void applyWorkaround() {
// Agrees that the window is focused. The mod is
// A little too fast for this, so we schedule it
// For the next client tick (before next frame).
minecraft.schedule(minecraft.mouseHandler::grabMouse);
minecraft.tell(minecraft.mouseHandler::grabMouse);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package dynamic_fps.impl.config.option;

import net.minecraft.client.gui.GuiGraphics;
import com.mojang.blaze3d.platform.Window;

/**
* Screen corner to render the battery indicator in.
*/
public enum BatteryIndicatorPlacement {
TOP_LEFT(graphics -> new int[] {4, 4}),
TOP_RIGHT(graphics -> new int[] {graphics.guiWidth() - 47, 4}),
BOTTOM_LEFT(graphics -> new int[] {4, graphics.guiHeight() - 20}),
BOTTOM_RIGHT(graphics -> new int[] {graphics.guiWidth() - 47, graphics.guiHeight() - 20});
TOP_LEFT(window -> new int[] {4, 4}),
TOP_RIGHT(window -> new int[] {window.getGuiScaledWidth() - 47, 4}),
BOTTOM_LEFT(window -> new int[] {4, window.getGuiScaledHeight() - 20}),
BOTTOM_RIGHT(window -> new int[] {window.getGuiScaledWidth() - 47, window.getGuiScaledHeight() - 20});

private final DynamicPlacement placement;

BatteryIndicatorPlacement(DynamicPlacement placement) {
this.placement = placement;
}

public int[] get(GuiGraphics graphics) {
return this.placement.get(graphics);
public int[] get(Window window) {
return this.placement.get(window);
}

@FunctionalInterface
private interface DynamicPlacement {
int[] get(GuiGraphics graphics);
int[] get(Window window);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public enum IdleCondition {
NONE,
VANILLA,
ON_BATTERY;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dynamic_fps.impl.feature.battery;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import dynamic_fps.impl.util.ResourceLocations;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.components.toasts.ToastManager;
import net.minecraft.client.gui.components.toasts.ToastComponent;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -14,12 +16,13 @@

public class BaseToast implements Toast {
private long firstRender;
private Visibility visibility;

protected Component title;
protected Component description;
protected @Nullable ResourceLocation icon;

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

private static final ResourceLocation MOD_ICON = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background_icon.png");
private static final ResourceLocation BACKGROUND_IMAGE = ResourceLocations.of("dynamic_fps", "textures/battery/toast/background.png");

Expand All @@ -28,47 +31,34 @@ protected BaseToast(Component title, Component description, @Nullable ResourceLo
this.description = description;

this.icon = icon;

this.visibility = Visibility.SHOW;
}

@Override
public @NotNull Visibility getWantedVisibility() {
return this.visibility;
}

@Override
public void update(ToastManager toastManager, long currentTime) {
if (this.firstRender == 0) {
return;
}

if (currentTime - this.firstRender >= 5000.0 * toastManager.getNotificationDisplayTimeMultiplier()) {
this.visibility = Visibility.HIDE;
}
}

@Override
public void render(GuiGraphics graphics, Font font, long currentTime) {
public @NotNull Visibility render(PoseStack poseStack, ToastComponent toastComponent, long currentTime) {
if (this.firstRender == 0) {
this.onFirstRender();
this.firstRender = currentTime;
}

// type, resource, x, y, ?, ?, width, height, width, height
graphics.blit(RenderType::guiTextured, BACKGROUND_IMAGE, 0, 0, 0.0f, 0, this.width(), this.height(), this.width(), this.height());
RenderSystem.setShaderTexture(0, BACKGROUND_IMAGE);
// resource, x, y, z, ?, ?, width, height, width, height
GuiComponent.blit(poseStack, 0, 0, 0.0f, 0.0f, this.width(), this.height(), this.width(), this.height());

int x = 8;

if (this.icon != null) {
x += 22;

graphics.blit(RenderType::guiTextured, MOD_ICON, 2, 2, 0.0f, 0, 8, 8, 8, 8);
graphics.blit(RenderType::guiTextured, this.icon, 8, 8, 0.0f, 0, 16, 16, 16, 16);
RenderSystem.setShaderTexture(0, MOD_ICON);
GuiComponent.blit(poseStack, 2, 2, 0, 0.0f, 0.0f, 8, 8, 8, 8);
RenderSystem.setShaderTexture(0, this.icon);
GuiComponent.blit(poseStack, 8, 8, 0, 0.0f, 0.0f, 16, 16, 16, 16);
}

graphics.drawString(Minecraft.getInstance().font, this.title, x, 7, 0x5f3315, false);
graphics.drawString(Minecraft.getInstance().font, this.description, x, 18, -16777216, false);
MINECRAFT.font.draw(poseStack, this.title, x, 7, 0x5f3315);
MINECRAFT.font.draw(poseStack, this.description, x, 18, -16777216);

return currentTime - this.firstRender >= 5000.0 ? Toast.Visibility.HIDE : Toast.Visibility.SHOW;
}

public void onFirstRender() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;

import static dynamic_fps.impl.util.Localization.localized;
Expand All @@ -10,7 +11,7 @@ public class BatteryToast extends BaseToast {
private static BatteryToast queuedToast;

private BatteryToast(Component title, ResourceLocation icon) {
super(title, Component.empty(), icon);
super(title, TextComponent.EMPTY, icon);
}

/**
Expand All @@ -23,7 +24,7 @@ public static void queueToast(Component title, ResourceLocation icon) {
queuedToast.icon = icon;
} else {
queuedToast = new BatteryToast(title, icon);
Minecraft.getInstance().getToastManager().addToast(queuedToast);
Minecraft.getInstance().getToasts().addToast(queuedToast);
}
}

Expand Down
Loading

0 comments on commit 440137c

Please sign in to comment.