Skip to content

Commit

Permalink
1.16.5
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Oct 12, 2023
1 parent 7707890 commit 59c0b14
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 84 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = project.maven_group
version = "${generateVersion()}+minecraft-${libs.versions.minecraft.get()}"
version = "${generateVersion()}+minecraft-${libs.versions.minecraft.get()}-to-1.18.2"

base {
archivesName = project.archives_base_name
Expand Down Expand Up @@ -37,12 +37,12 @@ processResources {
java {
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

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

it.options.compilerArgs += ["-Xlint:deprecation"]
Expand Down
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[versions]
minecraft = "1.20"
minecraft = "1.16.5"
fabric_loader = "0.14.22"

modmenu = "7.0.1"
fabric_api = "0.83.0+1.20"
modmenu = "1.15.0"
fabric_api = "0.29.4+1.16"

cloth_config = "11.0.99"
cloth_config = "4.14.54"

[libraries]
minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" }
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/dynamic_fps/impl/DynamicFPSMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dynamic_fps.impl.compat.FREX;
import dynamic_fps.impl.config.Config;
import dynamic_fps.impl.config.DynamicFPSConfig;
import dynamic_fps.impl.util.DynamicFPSSplashOverlay;
import dynamic_fps.impl.util.HudInfoRenderer;
import dynamic_fps.impl.util.KeyMappingHandler;
import dynamic_fps.impl.util.Logging;
Expand All @@ -16,6 +17,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.LoadingOverlay;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.sounds.SoundManager;
import net.minecraft.sounds.SoundSource;

import static dynamic_fps.impl.util.Localization.translationKey;
Expand Down Expand Up @@ -136,7 +138,7 @@ private static boolean isLevelCoveredByScreen() {
}

private static boolean isLevelCoveredByOverlay() {
return OVERLAY_OPTIMIZATION_ACTIVE && minecraft.getOverlay() instanceof LoadingOverlay loadingOverlay && loadingOverlay.dynamic_fps$isReloadComplete();
return OVERLAY_OPTIMIZATION_ACTIVE && minecraft.getOverlay() instanceof LoadingOverlay && ((DynamicFPSSplashOverlay)minecraft.getOverlay()).dynamic_fps$isReloadComplete();
}

@SuppressWarnings("squid:S1215") // Garbage collector call
Expand All @@ -145,11 +147,25 @@ public static void handleStateChange(PowerState previous, PowerState current) {
Logging.getLogger().info("State changed from {} to {}.", previous, current);
}

var before = config;
Config before = config;
config = modConfig.get(current);

hasRenderedLastFrame = false; // Render next frame w/o delay

// Fix LWJGL <3.2.x issue happening on some OSes
// Causing the cursor hover event to not execute
// If cursor is captured but window is unfocused
if (minecraft.screen == null && !minecraft.options.pauseOnLostFocus) {
if (current.configurable) {
minecraft.mouseHandler.releaseMouse();
} else {
// Grabbing the mouse only works when Minecraft
// Agrees that the window is focused. The next client
// Tick is late enough for this, but still unnoticable.
minecraft.tell(minecraft.mouseHandler::grabMouse);
}
}

if (config.runGarbageCollector()) {
System.gc();
}
Expand Down Expand Up @@ -197,7 +213,7 @@ private static void checkForStateChanges() {
}

if (state != current) {
var previous = state;
PowerState previous = state;
state = current;

handleStateChange(previous, current);
Expand All @@ -213,7 +229,7 @@ private static void setVolumeMultiplier(float multiplier) {
// Makes for a rather jarring experience when music
// Is stopped. Also fixes now-playing compatibility

var manager = minecraft.getSoundManager();
SoundManager manager = minecraft.getSoundManager();

if (multiplier == 0) {
manager.pause();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/dynamic_fps/impl/GraphicsState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dynamic_fps.impl;

import java.util.Locale;
import java.util.Optional;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
Expand Down Expand Up @@ -34,10 +35,10 @@ public <T> T write(DynamicOps<T> ops, GraphicsState value) {

@Override
public <T> DataResult<GraphicsState> read(DynamicOps<T> ops, T input) {
var value = ops.getStringValue(input).get().left();
Optional<String> value = ops.getStringValue(input).get().left();

if (value.isEmpty()) {
return DataResult.error(() -> "Graphics state must not be empty!");
if (!value.isPresent()) {
return DataResult.error("Graphics state must not be empty!");
} else {
return DataResult.success(GraphicsState.valueOf(value.get().toUpperCase(Locale.ROOT)));
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/dynamic_fps/impl/PowerState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dynamic_fps.impl;

import java.util.Locale;
import java.util.Optional;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
Expand Down Expand Up @@ -48,10 +49,10 @@ public <T> T write(DynamicOps<T> ops, PowerState value) {

@Override
public <T> DataResult<PowerState> read(DynamicOps<T> ops, T input) {
var value = ops.getStringValue(input).get().left();
Optional<String> value = ops.getStringValue(input).get().left();

if (value.isEmpty()) {
return DataResult.error(() -> "Power state must not be empty!");
if (!value.isPresent()) {
return DataResult.error("Power state must not be empty!");
} else {
return DataResult.success(PowerState.valueOf(value.get().toUpperCase(Locale.ROOT)));
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/dynamic_fps/impl/compat/ClothConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dynamic_fps.impl.DynamicFPSMod;
import dynamic_fps.impl.GraphicsState;
import dynamic_fps.impl.PowerState;
import dynamic_fps.impl.config.Config;
import dynamic_fps.impl.config.DynamicFPSConfig;

public final class ClothConfig {
Expand All @@ -20,16 +21,16 @@ public static Screen genConfigScreen(Screen parent) {

ConfigEntryBuilder entryBuilder = builder.entryBuilder();

for (var state : PowerState.values()) {
for (PowerState state : PowerState.values()) {
if (!state.configurable) {
continue;
}

var config = DynamicFPSMod.modConfig.get(state);
var standard = DynamicFPSConfig.getDefaultConfig(state);
Config config = DynamicFPSMod.modConfig.get(state);
Config standard = DynamicFPSConfig.getDefaultConfig(state);

builder.getOrCreateCategory(
localized("config", "category." + state.toString().toLowerCase()))
localized("config", "category.".concat(state.toString().toLowerCase())))
.addEntry(
entryBuilder
.startTextDescription(
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/dynamic_fps/impl/config/DynamicFPSConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import java.util.EnumMap;
import java.util.Map;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;

Expand All @@ -21,7 +23,7 @@
public final class DynamicFPSConfig {
private Map<PowerState, Config> configs;

private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(DynamicFPSMod.MOD_ID + ".json");
private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(DynamicFPSMod.MOD_ID.concat(".json"));
private static final Codec<Map<PowerState, Config>> STATES_CODEC = Codec.unboundedMap(PowerState.CODEC, Config.CODEC);

private static final Codec<DynamicFPSConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Expand All @@ -31,7 +33,7 @@ public final class DynamicFPSConfig {
private DynamicFPSConfig(Map<PowerState, Config> configs) {
this.configs = new EnumMap<>(configs);

for (var state : PowerState.values()) {
for (PowerState state : PowerState.values()) {
if (state.configurable) {
this.configs.computeIfAbsent(state, DynamicFPSConfig::getDefaultConfig);
}
Expand Down Expand Up @@ -60,25 +62,25 @@ public static DynamicFPSConfig load() {
String data;

try {
data = Files.readString(PATH);
data = String.join("", Files.readAllLines(PATH, StandardCharsets.UTF_8));
} catch (NoSuchFileException e) {
return new DynamicFPSConfig(new EnumMap<>(PowerState.class));
} catch (IOException e) {
throw new RuntimeException("Failed to load Dynamic FPS config.", e);
}

var root = JsonParser.parseString(data);
var parsed = CODEC.parse(JsonOps.INSTANCE, root);
JsonElement root = new JsonParser().parse(data);
DataResult<DynamicFPSConfig> parsed = CODEC.parse(JsonOps.INSTANCE, root);

return parsed.getOrThrow(false, RuntimeException::new);
}

public void save() {
var data = CODEC.encodeStart(JsonOps.INSTANCE, this);
var root = data.getOrThrow(false, RuntimeException::new);
DataResult<JsonElement> data = CODEC.encodeStart(JsonOps.INSTANCE, this);
JsonElement root = data.getOrThrow(false, RuntimeException::new);

try {
Files.writeString(PATH, root.toString(), StandardCharsets.UTF_8);
Files.write(PATH, root.toString().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
// Cloth Config's automatic saving does not support catching exceptions
throw new RuntimeException("Failed to save Dynamic FPS config.", e);
Expand All @@ -97,7 +99,7 @@ public static Config getDefaultConfig(PowerState state) {
return new Config(0, 0.0f, GraphicsState.DEFAULT, false, false);
}
default: {
throw new RuntimeException("Getting default configuration for unhandled power state " + state.toString());
throw new RuntimeException("Getting default configuration for unhandled power state ".concat(state.toString()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class DebugScreenOverlayMixin {
*/
@Inject(method = "getGameInformation", at = @At("RETURN"))
private void onGetGameInformation(CallbackInfoReturnable<List<String>> callbackInfo) {
var status = DynamicFPSMod.powerState();
PowerState status = DynamicFPSMod.powerState();

if (status != PowerState.FOCUSED) {
var result = callbackInfo.getReturnValue();
List<String> result = callbackInfo.getReturnValue();
int target = DynamicFPSMod.targetFrameRate();

result.add(2, String.format(Locale.ROOT, "§c[Dynamic FPS] FPS: %s P: %s§r", target, status.toString().toLowerCase()));
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/dynamic_fps/impl/mixin/ToastComponentMixin.java
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();
}
}
}
23 changes: 8 additions & 15 deletions src/main/java/dynamic_fps/impl/util/HudInfoRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,37 @@
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
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 implements HudRenderCallback {
@Override
public void onHudRender(GuiGraphics drawContext, float tickDelta) {
public void onHudRender(PoseStack poseStack, float tickDelta) {
if (DynamicFPSMod.isDisabled()) {
drawCenteredText(drawContext, localized("gui", "hud.disabled"), 32);
drawCenteredText(poseStack, localized("gui", "hud.disabled"), 32);
} else if (DynamicFPSMod.isForcingLowFPS()) {
drawCenteredText(drawContext, localized("gui", "hud.reducing"), 32);
drawCenteredText(poseStack, localized("gui", "hud.reducing"), 32);
}
}

private void drawCenteredText(GuiGraphics drawContext, Component component, float y) {
private void drawCenteredText(PoseStack poseStack, Component component, float y) {
Minecraft client = Minecraft.getInstance();
Font fontRenderer = client.gui.getFont();

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

fontRenderer.drawInBatch(
fontRenderer.drawShadow(
poseStack,
component,
(windowWidth - stringWidth) / 2f,
y,
0xFFFFFFFF,
true,
new Matrix4f(),
drawContext.bufferSource(),
DisplayMode.NORMAL,
0,
255
0xFFFFFFFF
);
}
}
5 changes: 3 additions & 2 deletions src/main/java/dynamic_fps/impl/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import dynamic_fps.impl.DynamicFPSMod;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;

public final class Localization {
/** e.g. keyString("title", "config") -> "title.dynamic_fps.config") */
public static String translationKey(String domain, String path) {
return domain + "." + DynamicFPSMod.MOD_ID + "." + path;
return String.join(".", domain, DynamicFPSMod.MOD_ID, path);
}

public static Component 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
6 changes: 3 additions & 3 deletions src/main/java/dynamic_fps/impl/util/Logging.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dynamic_fps.impl.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import dynamic_fps.impl.DynamicFPSMod;

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

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

0 comments on commit 59c0b14

Please sign in to comment.