|
| 1 | +package org.polyfrost.polyplus.client.features |
| 2 | + |
| 3 | +import net.minecraft.client.Minecraft |
| 4 | +import org.apache.logging.log4j.LogManager |
| 5 | +import org.polyfrost.oneconfig.api.event.v1.eventHandler |
| 6 | +import org.polyfrost.oneconfig.api.event.v1.events.TickEvent |
| 7 | +import org.polyfrost.oneconfig.api.hud.v1.Font |
| 8 | +import org.polyfrost.oneconfig.api.hud.v1.Hud |
| 9 | +import org.polyfrost.oneconfig.api.hud.v1.HudManager |
| 10 | +import org.polyfrost.oneconfig.internal.ui.themes.MinecraftDark |
| 11 | +import org.polyfrost.oneconfig.internal.ui.themes.MinecraftLight |
| 12 | +import org.polyfrost.oneconfig.internal.ui.themes.PolyGlassDark |
| 13 | +import org.polyfrost.oneconfig.internal.ui.themes.PolyGlassLight |
| 14 | +import org.polyfrost.oneconfig.internal.ui.themes.ThemeRegistry |
| 15 | +import org.polyfrost.polyplus.client.PolyPlusConfig |
| 16 | + |
| 17 | +object OnboardingFeatures { |
| 18 | + private val logger = LogManager.getLogger("PolyPlus/Onboarding") |
| 19 | + |
| 20 | + fun initialize() { |
| 21 | + val polyBlurAvailable = classExists(POLYBLUR_CONFIG) |
| 22 | + val evergreenAvailable = classExists(EVERGREEN_FPS) |
| 23 | + eventHandler { _: TickEvent.End -> |
| 24 | + if (!PolyPlusConfig.onboardingCompleted) return@eventHandler |
| 25 | + var changed = false |
| 26 | + if (!PolyPlusConfig.onboardingFeaturesApplied) { |
| 27 | + applyCoreSettings() |
| 28 | + changed = true |
| 29 | + } |
| 30 | + if (polyBlurAvailable && !PolyPlusConfig.onboardingPolyBlurApplied) { |
| 31 | + if (applyPolyBlur(PolyPlusConfig.onboardingMotionBlur)) { |
| 32 | + PolyPlusConfig.onboardingPolyBlurApplied = true |
| 33 | + changed = true |
| 34 | + } |
| 35 | + } |
| 36 | + if (evergreenAvailable && !PolyPlusConfig.onboardingEvergreenApplied) { |
| 37 | + if (applyEvergreenHuds()) { |
| 38 | + PolyPlusConfig.onboardingEvergreenApplied = true |
| 39 | + changed = true |
| 40 | + } |
| 41 | + } |
| 42 | + if (changed) PolyPlusConfig.save() |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + fun applySavedSettings() { |
| 47 | + applyCoreSettings() |
| 48 | + if (applyPolyBlur(PolyPlusConfig.onboardingMotionBlur)) { |
| 49 | + PolyPlusConfig.onboardingPolyBlurApplied = true |
| 50 | + } |
| 51 | + if (applyEvergreenHuds()) { |
| 52 | + PolyPlusConfig.onboardingEvergreenApplied = true |
| 53 | + } |
| 54 | + PolyPlusConfig.save() |
| 55 | + } |
| 56 | + |
| 57 | + private fun applyCoreSettings() { |
| 58 | + applyTheme(PolyPlusConfig.onboardingLightTheme, PolyPlusConfig.onboardingUiStyle) |
| 59 | + applyToggleSprint(PolyPlusConfig.onboardingToggleSprint) |
| 60 | + PolyPlusConfig.onboardingFeaturesApplied = true |
| 61 | + } |
| 62 | + |
| 63 | + private fun applyTheme(light: Boolean, style: Int) { |
| 64 | + val theme = when { |
| 65 | + style == 1 && light -> MinecraftLight |
| 66 | + style == 1 -> MinecraftDark |
| 67 | + light -> PolyGlassLight |
| 68 | + else -> PolyGlassDark |
| 69 | + } |
| 70 | + ThemeRegistry.activate(theme) |
| 71 | + } |
| 72 | + |
| 73 | + private fun applyToggleSprint(enabled: Boolean) { |
| 74 | + runCatching { |
| 75 | + Minecraft.getInstance().options.toggleSprint().set(enabled) |
| 76 | + Minecraft.getInstance().options.save() |
| 77 | + }.onFailure { logger.warn("Could not apply toggle sprint preference", it) } |
| 78 | + } |
| 79 | + |
| 80 | + private fun applyEvergreenHuds(): Boolean { |
| 81 | + val choices = listOf( |
| 82 | + HudChoice(PolyPlusConfig.onboardingHudFps, EVERGREEN_FPS), |
| 83 | + HudChoice(PolyPlusConfig.onboardingHudCps, EVERGREEN_CPS), |
| 84 | + HudChoice(PolyPlusConfig.onboardingHudPing, EVERGREEN_PING), |
| 85 | + HudChoice(PolyPlusConfig.onboardingHudTime, EVERGREEN_TIME), |
| 86 | + ) |
| 87 | + var found = false |
| 88 | + choices.forEach { choice -> |
| 89 | + val type = hudClass(choice.className) ?: return@forEach |
| 90 | + found = applyHud(type, choice.enabled) || found |
| 91 | + } |
| 92 | + |
| 93 | + val positionType = hudClass(EVERGREEN_POSITION) |
| 94 | + if (positionType != null) { |
| 95 | + val enabled = PolyPlusConfig.onboardingHudCoords || PolyPlusConfig.onboardingHudDirection |
| 96 | + found = applyHud(positionType, enabled) { hud -> |
| 97 | + setBoolean(hud, "setShowDirection", PolyPlusConfig.onboardingHudDirection) |
| 98 | + setBoolean(hud, "setShowAxis", PolyPlusConfig.onboardingHudCoords) |
| 99 | + setBoolean(hud, "setShowX", PolyPlusConfig.onboardingHudCoords || PolyPlusConfig.onboardingHudDirection) |
| 100 | + setBoolean(hud, "setShowY", PolyPlusConfig.onboardingHudCoords) |
| 101 | + setBoolean(hud, "setShowZ", PolyPlusConfig.onboardingHudCoords || PolyPlusConfig.onboardingHudDirection) |
| 102 | + } || found |
| 103 | + } |
| 104 | + return found |
| 105 | + } |
| 106 | + |
| 107 | + private fun applyHud(type: Class<out Hud>, enabled: Boolean, configure: (Hud) -> Unit = {}): Boolean { |
| 108 | + val provider = HudManager.getProvider(type) ?: return false |
| 109 | + styleHud(provider) |
| 110 | + configure(provider) |
| 111 | + HudManager.toggleAllHuds(provider, !enabled) |
| 112 | + HudManager.getHudsOfType(type).forEach { |
| 113 | + styleHud(it) |
| 114 | + configure(it) |
| 115 | + it.hidden = !enabled |
| 116 | + runCatching { it.save() } |
| 117 | + } |
| 118 | + return true |
| 119 | + } |
| 120 | + |
| 121 | + private fun styleHud(hud: Hud) { |
| 122 | + val polyGlass = PolyPlusConfig.onboardingHudStyle == 0 |
| 123 | + hud.font = if (polyGlass) Font.Poppins else Font.Minecraft |
| 124 | + hud.bgRadius = if (polyGlass) 4f else 0f |
| 125 | + hud.showBackground = true |
| 126 | + } |
| 127 | + |
| 128 | + private fun applyPolyBlur(value: Int): Boolean { |
| 129 | + val strength = value.coerceIn(0, 10) |
| 130 | + return runCatching { |
| 131 | + val config = Class.forName(POLYBLUR_CONFIG) |
| 132 | + val instance = config.getField("INSTANCE").get(null) |
| 133 | + config.getMethod("setEnabled", Boolean::class.javaPrimitiveType).invoke(instance, strength > 0) |
| 134 | + if (strength > 0) { |
| 135 | + config.getMethod("setStrength", Float::class.javaPrimitiveType).invoke(instance, strength.toFloat()) |
| 136 | + } |
| 137 | + config.getMethod("save").invoke(instance) |
| 138 | + true |
| 139 | + }.onFailure { |
| 140 | + if (it !is ClassNotFoundException) logger.warn("Could not apply PolyBlur preference", it) |
| 141 | + }.getOrDefault(false) |
| 142 | + } |
| 143 | + |
| 144 | + private fun classExists(name: String) = runCatching { Class.forName(name, false, javaClass.classLoader) }.isSuccess |
| 145 | + |
| 146 | + @Suppress("UNCHECKED_CAST") |
| 147 | + private fun hudClass(name: String): Class<out Hud>? = |
| 148 | + runCatching { Class.forName(name, false, javaClass.classLoader) as Class<out Hud> }.getOrNull() |
| 149 | + |
| 150 | + private fun setBoolean(target: Any, setter: String, value: Boolean) { |
| 151 | + runCatching { target.javaClass.getMethod(setter, Boolean::class.javaPrimitiveType).invoke(target, value) } |
| 152 | + .onFailure { logger.warn("Could not apply EvergreenHUD option {}", setter, it) } |
| 153 | + } |
| 154 | + |
| 155 | + private data class HudChoice( |
| 156 | + val enabled: Boolean, |
| 157 | + val className: String, |
| 158 | + ) |
| 159 | + |
| 160 | + private const val POLYBLUR_CONFIG = "org.polyfrost.polyblur.client.PolyBlurConfig" |
| 161 | + private const val EVERGREEN_FPS = "org.polyfrost.evergreenhud.client.hud.FpsHud" |
| 162 | + private const val EVERGREEN_CPS = "org.polyfrost.evergreenhud.client.hud.CpsHud" |
| 163 | + private const val EVERGREEN_PING = "org.polyfrost.evergreenhud.client.hud.PingHud" |
| 164 | + private const val EVERGREEN_TIME = "org.polyfrost.evergreenhud.client.hud.clock.DigitalClockHud" |
| 165 | + private const val EVERGREEN_POSITION = "org.polyfrost.evergreenhud.client.hud.PositionHud" |
| 166 | +} |
0 commit comments