Skip to content

Commit

Permalink
add config system
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed May 29, 2024
1 parent 6a8f551 commit 056a46c
Show file tree
Hide file tree
Showing 18 changed files with 338 additions and 31 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ on:
pull_request:
workflow_dispatch:

concurrency:
group: ${{ github.head_ref || format('{0}-{1}', github.ref, github.run_number) }}
cancel-in-progress: true

jobs:
build:
name: Build
Expand All @@ -18,8 +14,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up JDK 21
uses: actions/setup-java@v4
Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ repositories {
maven("https://repo.spongepowered.org/maven/")
maven("https://api.modrinth.com/maven")
maven("https://maven.neoforged.net/releases/")
maven("https://maven.terraformersmc.com/releases/")
mavenCentral()
mavenLocal()
}
Expand Down Expand Up @@ -127,6 +128,7 @@ dependencies {
modImplementation("net.fabricmc:fabric-language-kotlin:${libs.versions.fabric.language.kotlin.get()}")
modCompileOnly("gg.essential:elementa-${elementaPlatform ?: platform}:${libs.versions.elementa.get()}")
modImplementation("include"("gg.essential:universalcraft-${universalPlatform ?: platform}:${universalVersion}")!!)
modCompileOnly(libs.modMenu)
} else if (platform.isForgeLike) {
if (platform.isLegacyForge) {
shade(libs.bundles.kotlin) { isTransitive = false }
Expand Down Expand Up @@ -214,7 +216,7 @@ tasks {
val map: Map<String, String> =
gson.fromJson(jsonFile.reader(), Map::class.java) as Map<String, String>
val fileName = jsonFile.nameWithoutExtension.split("_").let {
"${it[0]}_${it[1].uppercase()}.lang"
"${it[0]}_${if (platform.mcVersion == 10809) it[1].uppercase() else it[1]}.lang"
}
val langFile = File(generatedDir, fileName)
langFile.printWriter().use { out ->
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Convert backend to an abstract service api, allowing for easier support for more services
- Fix background breaking when using essential (or another mod with universalcraft) in mc 1.20.5+
- Fix crash in case of malformed html in markdown
- Fix localizations not working on 1.12.2 forge
- Added small bisect hosting ad to the top of the browse page, this is to help support the project and can be disabled in the new settings page

----------------------------------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ mixin = "0.7.11-SNAPSHOT"
minotaur = "2.8.0"
cursegradle = "1.4.0"

# mods
modMenu = "7.2.1"

# Misc
shadow = "8.1.1"
blossom = "1.3.2"
Expand All @@ -39,6 +42,9 @@ tagsoup = { module = "org.ccil.cowan.tagsoup:tagsoup", version.ref = "tagsoup" }
# Mixin
mixin = { module = "org.spongepowered:mixin", version.ref = "mixin" }

# Mods
modMenu = { module = "com.terraformersmc:modmenu", version.ref = "modMenu" }

[plugins]
# Kotlin
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
46 changes: 44 additions & 2 deletions src/main/kotlin/dev/dediamondpro/resourcify/Resourcify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,53 @@ package dev.dediamondpro.resourcify

//#if MODERN == 0
import net.minecraftforge.fml.common.Mod
//#else
//#elseif FORGE == 1
//$$ import dev.dediamondpro.resourcify.platform.EventHandler

//$$ import net.minecraft.client.Minecraft
//$$ import net.minecraftforge.fml.ModLoadingContext
//$$ import dev.dediamondpro.resourcify.config.SettingsPage
//$$ //#if MC < 11800
//$$ import net.minecraftforge.fml.ExtensionPoint
//$$ import net.minecraft.client.gui.screen.Screen
//$$ import java.util.function.BiFunction
//$$ //#elseif MC < 11900
//$$ //$$ import net.minecraft.client.gui.screens.Screen
//$$ //$$ import net.minecraftforge.client.ConfigGuiHandler.ConfigGuiFactory
//$$ //#else
//$$ //$$ import net.minecraft.client.gui.screens.Screen
//$$ //$$ import net.minecraftforge.client.ConfigScreenHandler.ConfigScreenFactory;
//$$ //#endif
//#endif

//#if NEOFORGE == 1 && MODERN == 1
//$$ import thedarkcolour.kotlinforforge.neoforge.forge.FORGE_BUS
//$$ import thedarkcolour.kotlinforforge.neoforge.forge.MOD_BUS
//$$ import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent
//$$ import net.neoforged.fml.common.Mod
//$$ import net.minecraft.client.gui.screens.Screen
//$$ import dev.dediamondpro.resourcify.platform.EventHandler
//$$ import net.minecraft.client.Minecraft
//$$ import net.neoforged.fml.ModLoadingContext
//$$ import dev.dediamondpro.resourcify.config.SettingsPage
//#elseif FORGE == 1 && MODERN == 1
//$$ import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent
//$$ import net.minecraftforge.fml.common.Mod
//$$ import thedarkcolour.kotlinforforge.forge.FORGE_BUS
//$$ import thedarkcolour.kotlinforforge.forge.MOD_BUS
//#endif

//#if MC >= 12006 && NEOFORGE == 1
//$$ import net.neoforged.neoforge.client.gui.IConfigScreenFactory
//#elseif NEOFORGE == 1
//$$ import net.neoforged.neoforge.client.ConfigScreenHandler.ConfigScreenFactory
//#endif

//#if MODERN == 0
@Mod(
name = ModInfo.NAME,
modid = ModInfo.ID,
version = ModInfo.VERSION,
guiFactory = "dev.dediamondpro.resourcify.config.ForgeGuiFactory",
modLanguageAdapter = "dev.dediamondpro.resourcify.platform.KotlinLanguageAdapter"
)
//#else
Expand All @@ -52,6 +77,23 @@ object Resourcify {
//#if MC > 11202
//$$ init {
//$$ MOD_BUS.addListener(this::onClientInit)
//$$ //#if MC < 11800
//$$ ModLoadingContext.get().registerExtensionPoint(ExtensionPoint.CONFIGGUIFACTORY) {
//$$ BiFunction { _: Minecraft?, _: Screen? -> SettingsPage() }
//$$ }
//$$ //#elseif MC < 11900
//$$ //$$ ModLoadingContext.get().registerExtensionPoint(ConfigGuiFactory::class.java) {
//$$ //$$ ConfigGuiFactory { _: Minecraft?, _: Screen? -> SettingsPage() }
//$$ //$$ }
//$$ //#elseif MC < 12006
//$$ //$$ ModLoadingContext.get().registerExtensionPoint(ConfigScreenFactory::class.java) {
//$$ //$$ ConfigScreenFactory { _: Minecraft?, _: Screen? -> SettingsPage() }
//$$ //$$ }
//$$ //#else
//$$ //$$ ModLoadingContext.get().registerExtensionPoint(IConfigScreenFactory::class.java) {
//$$ //$$ IConfigScreenFactory { _: Minecraft?, _: Screen? -> SettingsPage() }
//$$ //$$ }
//$$ //#endif
//$$ }
//$$
//$$ private fun onClientInit(event: FMLClientSetupEvent) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/kotlin/dev/dediamondpro/resourcify/config/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.dediamondpro.resourcify.config

import dev.dediamondpro.resourcify.services.modrinth.ModrinthService
import dev.dediamondpro.resourcify.util.fromJson
import dev.dediamondpro.resourcify.util.toJson
import java.io.File

class Config {
var defaultService: String = ModrinthService.getName()
var adsEnabled: Boolean = true

companion object {
private val configFile = File("./config/resourcify.json")
val instance: Config = load()

private fun load(): Config {
return try {
configFile.readText().fromJson()
} catch (e: Exception) {
Config()
}
}

fun save() {
configFile.outputStream().bufferedWriter().use {
it.write(instance.toJson())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.dediamondpro.resourcify.config

//#if FORGE == 1 && MC <= 11202

import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiScreen
import net.minecraftforge.fml.client.IModGuiFactory

class ForgeGuiFactory : IModGuiFactory {
override fun initialize(minecraft: Minecraft?) {
}

//#if MC==10809
override fun mainConfigGuiClass(): Class<out GuiScreen> {
return SettingsPage::class.java
}

override fun getHandlerFor(runtimeOptionCategoryElement: IModGuiFactory.RuntimeOptionCategoryElement?): IModGuiFactory.RuntimeOptionGuiHandler? {
return null
}
//#else
//$$ override fun createConfigGui(guiScreen: GuiScreen?): GuiScreen {
//$$ return SettingsPage()
//$$ }
//$$
//$$ override fun hasConfigGui(): Boolean = true
//#endif

override fun runtimeGuiCategories(): MutableSet<IModGuiFactory.RuntimeOptionCategoryElement>? {
return null
}
}

//#endif
127 changes: 127 additions & 0 deletions src/main/kotlin/dev/dediamondpro/resourcify/config/SettingsPage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package dev.dediamondpro.resourcify.config

import dev.dediamondpro.resourcify.constraints.ChildLocationSizeConstraint
import dev.dediamondpro.resourcify.elements.DropDown
import dev.dediamondpro.resourcify.gui.PaginatedScreen
import dev.dediamondpro.resourcify.services.ServiceRegistry
import dev.dediamondpro.resourcify.util.localize
import gg.essential.elementa.components.UIBlock
import gg.essential.elementa.components.UIContainer
import gg.essential.elementa.components.UIText
import gg.essential.elementa.components.UIWrappedText
import gg.essential.elementa.constraints.CenterConstraint
import gg.essential.elementa.constraints.ChildBasedMaxSizeConstraint
import gg.essential.elementa.constraints.SiblingConstraint
import gg.essential.elementa.constraints.animation.Animations
import gg.essential.elementa.dsl.*
import gg.essential.elementa.effects.OutlineEffect
import net.minecraft.client.gui.GuiScreen
import java.awt.Color

class SettingsPage() : PaginatedScreen(adaptScale = false) {

//#if FORGE && MC <= 11202
constructor(lastPage: GuiScreen) : this()
//#endif

init {
val mainBox = UIContainer().constrain {
x = CenterConstraint()
width = min(692.pixels(), basicWidthConstraint { window.getWidth() - 8 })
height = 100.percent()
} childOf window
UIText("resourcify.config.title".localize()).constrain {
x = CenterConstraint()
y = 8.pixels()
textScale = 2.pixels()
} childOf mainBox

// Source
val sourceBox = UIBlock(Color(0, 0, 0, 100)).constrain {
x = 0.pixels()
y = SiblingConstraint(padding = 4f)
width = 100.percent()
height = ChildBasedMaxSizeConstraint() + 4.pixels()
} childOf mainBox
val sourceDescriptionBox = UIContainer().constrain {
x = 4.pixels()
y = 4.pixels()
width = 100.percent() - 168.pixels()
height = ChildLocationSizeConstraint()
} childOf sourceBox
UIWrappedText("resourcify.config.source.title".localize()).constrain {
width = 100.percent()
} childOf sourceDescriptionBox
UIWrappedText("resourcify.config.source.description".localize()).constrain {
y = SiblingConstraint(padding = 4f)
width = 100.percent()
color = Color.LIGHT_GRAY.toConstraint()
} childOf sourceDescriptionBox
DropDown(
ServiceRegistry.getServices().map { it.getName() },
true, mutableListOf(Config.instance.defaultService)
).constrain {
x = 4.pixels(true)
y = CenterConstraint()
width = 160.pixels()
}.onSelectionUpdate {
Config.instance.defaultService = it.first()
Config.save()
} childOf sourceBox

// Ads
val adsBox = UIBlock(Color(0, 0, 0, 100)).constrain {
x = 0.pixels()
y = SiblingConstraint(padding = 4f)
width = 100.percent()
height = ChildBasedMaxSizeConstraint() + 4.pixels()
} childOf mainBox
val adsDescriptionBox = UIContainer().constrain {
x = 4.pixels()
y = 4.pixels()
width = 100.percent() - 168.pixels()
height = ChildLocationSizeConstraint()
} childOf adsBox
UIWrappedText("resourcify.config.ads.title".localize()).constrain {
width = 100.percent()
} childOf adsDescriptionBox
UIWrappedText("resourcify.config.ads.description".localize()).constrain {
y = SiblingConstraint(padding = 4f)
width = 100.percent()
color = Color.LIGHT_GRAY.toConstraint()
} childOf adsDescriptionBox
val checkBox = UIContainer().constrain {
x = 4.pixels(alignOpposite = true)
y = CenterConstraint()
width = 14.pixels()
height = 14.pixels()
} childOf adsBox effect OutlineEffect(Color.LIGHT_GRAY, 1f)
val check = UIBlock(Color(192, 192, 192, if (Config.instance.adsEnabled) 255 else 0)).constrain {
x = 2.pixels()
y = 2.pixels()
width = 10.pixels()
height = 10.pixels()
}.animateBeforeHide {
setColorAnimation(
Animations.IN_OUT_QUAD,
0.15f,
Color(192, 192, 192, 0).toConstraint(),
0f
)
}.animateAfterUnhide {
setColorAnimation(
Animations.IN_OUT_QUAD,
0.15f,
Color(192, 192, 192, 255).toConstraint(),
0f
)
} childOf checkBox
if (!Config.instance.adsEnabled) check.hide(true)
checkBox.onMouseClick {
val adsEnabled = !Config.instance.adsEnabled
Config.instance.adsEnabled = adsEnabled
Config.save()
if (adsEnabled) check.unhide() else check.hide()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import kotlin.math.floor
//$$ import net.minecraft.client.gui.DrawContext
//#endif

abstract class PaginatedScreen : WindowScreen(
abstract class PaginatedScreen(private val adaptScale: Boolean = true) : WindowScreen(
version = ElementaVersion.V5,
//#if MC>=12005
//$$ drawDefaultBackground = true
Expand Down Expand Up @@ -64,6 +64,7 @@ abstract class PaginatedScreen : WindowScreen(
}

override fun onTick() {
if (!adaptScale) return
if (defaultScale == -1) defaultScale = UMinecraft.guiScale
val updatedScale = getGuiScale(defaultScale)
if (updatedScale != UResolution.scaleFactor.toInt()) {
Expand Down
Loading

0 comments on commit 056a46c

Please sign in to comment.