Skip to content

Commit

Permalink
Update Config to use the new DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
camnwalter committed Jul 22, 2024
1 parent 6a92996 commit e252ebd
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 153 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
kotlin("jvm") version "2.0.0"
kotlin("plugin.serialization") version "2.0.0"
id("fabric-loom") version "1.7-SNAPSHOT"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.gui.tooltip.TooltipPositioner;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -22,7 +20,7 @@ public class DrawContextMixin {
cancellable = true
)
private void screenshots$cancelHover(TextRenderer textRenderer, List<TooltipComponent> components, int x, int y, TooltipPositioner positioner, CallbackInfo ci) {
if (Screenshots.INSTANCE.getDisplayScreenshotHud()) {
if (Screenshots.getDisplayScreenshotHud()) {
ci.cancel();
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/squagward/screenshots/mixin/KeyboardMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.squagward.screenshots.Config;
import com.squagward.screenshots.Screenshots;
import com.squagward.screenshots.config.ScreenshotsConfig;
import com.squagward.screenshots.hud.ScreenshotHud;
import com.squagward.screenshots.screen.ScreenshotScreen;
import net.minecraft.client.Keyboard;
Expand Down Expand Up @@ -34,27 +34,27 @@ public class KeyboardMixin {
)
)
private void screenshots$openhud(File gameDirectory, Framebuffer framebuffer, Consumer<Text> messageReceiver, Operation<Void> original) {
ScreenshotsConfig config = ScreenshotsConfig.CONFIG.instance();
Config config = Screenshots.getConfig();
if (!config.getEnabled() || !config.getCropImage()) {
original.call(gameDirectory, framebuffer, messageReceiver);
return;
}

ScreenshotHud.INSTANCE.updateBackgroundImage(framebuffer);
Screenshots.INSTANCE.setDisplayScreenshotHud(true);
ScreenshotHud.INSTANCE.reset();
ScreenshotHud.updateBackgroundImage(framebuffer);
Screenshots.setDisplayScreenshotHud(true);
ScreenshotHud.reset();

if (client.currentScreen == null) {
client.send(() -> {
client.setScreen(new ScreenshotScreen());
Screenshots.INSTANCE.setDisplayScreenshotScreen(true);
Screenshots.setDisplayScreenshotScreen(true);
});
}
}

@Inject(method = "onChar", at = @At("HEAD"), cancellable = true)
private void screenshots$cancelCharType(long window, int codePoint, int modifiers, CallbackInfo ci) {
if (Screenshots.INSTANCE.getDisplayScreenshotHud()) {
if (Screenshots.getDisplayScreenshotHud()) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.squagward.screenshots.mixin;

import com.squagward.screenshots.Config;
import com.squagward.screenshots.Screenshots;
import com.squagward.screenshots.config.ScreenshotsConfig;
import com.squagward.screenshots.hud.ScreenshotHud;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.util.ScreenshotRecorder;
Expand All @@ -19,7 +19,7 @@
public class ScreenshotRecorderMixin {
@ModifyVariable(method = "saveScreenshotInner", at = @At("STORE"))
private static NativeImage screenshots$cropImage(NativeImage original) {
ScreenshotsConfig config = ScreenshotsConfig.CONFIG.instance();
Config config = Screenshots.getConfig();
if (!config.getEnabled()) {
return original;
}
Expand All @@ -39,7 +39,7 @@ public class ScreenshotRecorderMixin {

@Inject(method = "method_1661", at = @At("HEAD"), cancellable = true)
private static void screenshots$shouldWriteToFile(NativeImage nativeImage, File file, Consumer<Text> consumer, CallbackInfo ci) {
ScreenshotsConfig config = ScreenshotsConfig.CONFIG.instance();
Config config = Screenshots.getConfig();
if (config.getEnabled() && !config.getSaveScreenshotFile()) {
nativeImage.close();
ci.cancel();
Expand Down
115 changes: 111 additions & 4 deletions src/main/kotlin/com/squagward/screenshots/Screenshots.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package com.squagward.screenshots

import com.mojang.brigadier.Command
import com.squagward.screenshots.compat.MacOSCompat
import com.squagward.screenshots.config.ScreenshotsConfig
import com.squagward.screenshots.hud.ScreenshotHud
import dev.isxander.yacl3.api.OptionDescription
import dev.isxander.yacl3.dsl.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.screen.Screen
import net.minecraft.client.texture.NativeImage
import net.minecraft.text.Text
import net.minecraft.util.Formatting
Expand All @@ -20,26 +26,127 @@ import java.awt.datatransfer.Transferable
import java.awt.datatransfer.UnsupportedFlavorException
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.File
import javax.imageio.ImageIO

@Serializable
data class Config(
var enabled: Boolean,
var cropImage: Boolean,
var pauseGameWhileCropping: Boolean,
var saveScreenshotFile: Boolean,
var copyToClipboard: Boolean
) {
companion object {
val DEFAULT = Config(
enabled = true,
cropImage = true,
pauseGameWhileCropping = true,
saveScreenshotFile = true,
copyToClipboard = true
)
}
}

object Screenshots : ClientModInitializer {
private val LOGGER: Logger = LogManager.getLogger("Screenshots")
private val json = Json { prettyPrint = true }
private val configLocation: File = FabricLoader.getInstance().configDir.resolve("screenshots.json").toFile()

@JvmStatic
var displayScreenshotHud = false

@JvmStatic
var displayScreenshotScreen = false
private val LOGGER: Logger = LogManager.getLogger("Screenshots")

@JvmStatic
lateinit var config: Config
private set

private fun load() {
if (!configLocation.exists()) {
save(Config.DEFAULT)
}
config = json.decodeFromString<Config>(configLocation.readText())
}

private fun save(config: Config) {
configLocation.writeText(json.encodeToString(config))
}

fun createScreen(parent: Screen?): Screen = YetAnotherConfigLib("screenshots") {
save { save(config) }

val general by categories.registering {
val enabled by rootOptions.registering {
description(OptionDescription.of(Text.translatable("screenshots.setting.enable.description")))

controller = tickBox()
binding(config::enabled, Config.DEFAULT.enabled)

listener { _, value: Boolean ->
val cropImage = rootOptions.futureRef<Boolean>("cropImage")
val pauseWhileCropping = rootOptions.futureRef<Boolean>("pauseWhileCropping")
val saveScreenshot = rootOptions.futureRef<Boolean>("saveScreenshot")
val copyToClipboard = rootOptions.futureRef<Boolean>("copyToClipboard")

cropImage.onReady { crop ->
pauseWhileCropping.onReady { it.setAvailable(crop.available()) }
crop.setAvailable(value)
}
saveScreenshot.onReady { it.setAvailable(value) }
copyToClipboard.onReady { it.setAvailable(value) }
}
}

val cropImage by rootOptions.registering {
description(OptionDescription.of(Text.translatable("screenshots.setting.crop.description")))

controller = tickBox()
binding(config::cropImage, Config.DEFAULT.cropImage)

listener { opt, value: Boolean ->
val pauseWhileCropping = rootOptions.futureRef<Boolean>("pauseWhileCropping")
pauseWhileCropping.onReady { it.setAvailable(opt.available() && value) }
}
}

val pauseWhileCropping by rootOptions.registering {
description(OptionDescription.of(Text.translatable("screenshots.setting.pause_crop.description")))

controller = tickBox()
binding(config::pauseGameWhileCropping, Config.DEFAULT.pauseGameWhileCropping)
}

val saveScreenshot by rootOptions.registering {
description(OptionDescription.of(Text.translatable("screenshots.setting.save_file.description")))

controller = tickBox()
binding(config::saveScreenshotFile, Config.DEFAULT.saveScreenshotFile)
}

val copyToClipboard by rootOptions.registering {
description(OptionDescription.of(Text.translatable("screenshots.setting.copy.description")))

controller = tickBox()
binding(config::copyToClipboard, Config.DEFAULT.copyToClipboard)
}
}
}.generateScreen(parent)

override fun onInitializeClient() {
LOGGER.info("Initialized Screenshots!")

ScreenshotHud.init()
ScreenshotsConfig.CONFIG.load()
load()

ClientCommandRegistrationCallback.EVENT.register { dispatcher, _ ->
dispatcher.register(
literal("screenshots").executes {
val client: MinecraftClient = MinecraftClient.getInstance()

client.send {
client.setScreen(ScreenshotsConfig.createScreen(null))
client.setScreen(createScreen(null))
}

Command.SINGLE_SUCCESS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.squagward.screenshots.config

import com.squagward.screenshots.Screenshots
import com.terraformersmc.modmenu.api.ConfigScreenFactory
import com.terraformersmc.modmenu.api.ModMenuApi

class ModMenuEntry : ModMenuApi {
override fun getModConfigScreenFactory(): ConfigScreenFactory<*> {
return ConfigScreenFactory(ScreenshotsConfig::createScreen)
return ConfigScreenFactory(Screenshots::createScreen)
}
}
111 changes: 0 additions & 111 deletions src/main/kotlin/com/squagward/screenshots/config/ScreenshotsConfig.kt

This file was deleted.

Loading

0 comments on commit e252ebd

Please sign in to comment.