Skip to content

Commit c34b9cd

Browse files
committed
Finalize store, fix player preview bugs
1 parent ded2d29 commit c34b9cd

14 files changed

Lines changed: 991 additions & 490 deletions

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ mod.id=polyplus
1515
mod.version=1.0.0
1616
mod.group=org.polyfrost
1717

18-
oneconfig_version=1.0.0-beta.7
18+
oneconfig_version=1.0.0

src/main/kotlin/org/polyfrost/polyplus/client/PolyPlusConfig.kt

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,41 +41,17 @@ object PolyPlusConfig : Config("${PolyPlusConstants.ID}.json", PolyPlusConstants
4141
@JvmStatic @Include
4242
var onboardingPolyBlurApplied = false
4343

44-
@JvmStatic @Include
45-
var onboardingEvergreenApplied = false
46-
4744
@JvmStatic @Include
4845
var onboardingLightTheme = false
4946

5047
@JvmStatic @Include
5148
var onboardingUiStyle = 0
5249

53-
@JvmStatic @Include
54-
var onboardingHudStyle = 0
55-
5650
@JvmStatic @Include
5751
var onboardingToggleSprint = true
5852

5953
@JvmStatic @Include
60-
var onboardingHudFps = true
61-
62-
@JvmStatic @Include
63-
var onboardingHudCps = false
64-
65-
@JvmStatic @Include
66-
var onboardingHudPing = true
67-
68-
@JvmStatic @Include
69-
var onboardingHudTime = true
70-
71-
@JvmStatic @Include
72-
var onboardingHudCoords = false
73-
74-
@JvmStatic @Include
75-
var onboardingHudDirection = false
76-
77-
@JvmStatic @Include
78-
var onboardingMotionBlur = 5
54+
var onboardingMotionBlur = 3
7955

8056
@JvmStatic
8157
@Switch(

src/main/kotlin/org/polyfrost/polyplus/client/cosmetics/CosmeticStore.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package org.polyfrost.polyplus.client.cosmetics
33
import io.ktor.client.call.body
44
import io.ktor.client.request.get
55
import io.ktor.client.request.parameter
6+
import kotlinx.coroutines.async
7+
import kotlinx.coroutines.awaitAll
8+
import kotlinx.coroutines.coroutineScope
69
import org.apache.logging.log4j.LogManager
710
import org.polyfrost.polyplus.client.PolyPlusClient
811
import org.polyfrost.polyplus.client.PolyPlusConfig
912
import org.polyfrost.polyplus.client.network.http.responses.CosmeticSearchResponse
13+
import org.polyfrost.polyplus.client.network.http.responses.CosmeticType
1014
import org.polyfrost.polyplus.client.network.http.responses.CosmeticStoreView
1115

1216
object CosmeticStore {
@@ -42,6 +46,23 @@ object CosmeticStore {
4246
}.body<CosmeticSearchResponse>()
4347
}.onFailure { LOGGER.error("Failed to search cosmetics", it); org.polyfrost.polyplus.client.PolyPlusSentry.capture(it) }
4448

49+
private var cachedStockedTypes: List<CosmeticType>? = null
50+
51+
suspend fun stockedTypes(): List<CosmeticType> {
52+
cachedStockedTypes?.let { return it }
53+
val types = CosmeticType.entries.filter { it != CosmeticType.Unknown }
54+
val stocked = coroutineScope {
55+
types.map { type ->
56+
async {
57+
val count = search(page = 1, perPage = 1, types = listOf(type.serializedName))
58+
.getOrNull()?.pagination?.totalItems
59+
type.takeIf { count == null || count > 0 }
60+
}
61+
}.awaitAll()
62+
}.filterNotNull()
63+
return stocked.also { cachedStockedTypes = it }
64+
}
65+
4566
suspend fun view(id: Int): Result<CosmeticStoreView> = runCatching {
4667
PolyPlusClient.HTTP.get("${PolyPlusConfig.apiUrl}/cosmetics/view/$id").body<CosmeticStoreView>()
4768
}.onFailure { LOGGER.error("Failed to view cosmetic {}", id, it); org.polyfrost.polyplus.client.PolyPlusSentry.capture(it) }

src/main/kotlin/org/polyfrost/polyplus/client/features/OnboardingFeatures.kt

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import net.minecraft.client.Minecraft
44
import org.apache.logging.log4j.LogManager
55
import org.polyfrost.oneconfig.api.event.v1.eventHandler
66
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
107
import org.polyfrost.oneconfig.internal.ui.themes.MinecraftDark
118
import org.polyfrost.oneconfig.internal.ui.themes.MinecraftLight
129
import org.polyfrost.oneconfig.internal.ui.themes.PolyGlassDark
@@ -19,10 +16,9 @@ object OnboardingFeatures {
1916

2017
val polySprintAvailable: Boolean by lazy { classExists(POLYSPRINT_CONFIG) }
2118
val polyBlurAvailable: Boolean by lazy { classExists(POLYBLUR_CONFIG) }
22-
val evergreenAvailable: Boolean by lazy { classExists(EVERGREEN_FPS) }
2319

2420
val modsPageAvailable: Boolean
25-
get() = polySprintAvailable || polyBlurAvailable || evergreenAvailable
21+
get() = polySprintAvailable || polyBlurAvailable
2622

2723
fun initialize() {
2824
eventHandler { _: TickEvent.End ->
@@ -43,12 +39,6 @@ object OnboardingFeatures {
4339
changed = true
4440
}
4541
}
46-
if (evergreenAvailable && !PolyPlusConfig.onboardingEvergreenApplied) {
47-
if (applyEvergreenHuds()) {
48-
PolyPlusConfig.onboardingEvergreenApplied = true
49-
changed = true
50-
}
51-
}
5242
if (changed) PolyPlusConfig.save()
5343
}
5444
}
@@ -62,9 +52,6 @@ object OnboardingFeatures {
6252
if (applyPolyBlur(PolyPlusConfig.onboardingMotionBlur)) {
6353
PolyPlusConfig.onboardingPolyBlurApplied = true
6454
}
65-
if (applyEvergreenHuds()) {
66-
PolyPlusConfig.onboardingEvergreenApplied = true
67-
}
6855
PolyPlusConfig.save()
6956
}
7057

@@ -73,7 +60,7 @@ object OnboardingFeatures {
7360
PolyPlusConfig.onboardingFeaturesApplied = true
7461
}
7562

76-
private fun applyTheme(light: Boolean, style: Int) {
63+
fun applyTheme(light: Boolean, style: Int) {
7764
val theme = when {
7865
style == 1 && light -> MinecraftLight
7966
style == 1 -> MinecraftDark
@@ -90,54 +77,6 @@ object OnboardingFeatures {
9077
}.onFailure { logger.warn("Could not apply toggle sprint preference", it) }
9178
}
9279

93-
private fun applyEvergreenHuds(): Boolean {
94-
val choices = listOf(
95-
HudChoice(PolyPlusConfig.onboardingHudFps, EVERGREEN_FPS),
96-
HudChoice(PolyPlusConfig.onboardingHudCps, EVERGREEN_CPS),
97-
HudChoice(PolyPlusConfig.onboardingHudPing, EVERGREEN_PING),
98-
HudChoice(PolyPlusConfig.onboardingHudTime, EVERGREEN_TIME),
99-
)
100-
var found = false
101-
choices.forEach { choice ->
102-
val type = hudClass(choice.className) ?: return@forEach
103-
found = applyHud(type, choice.enabled) || found
104-
}
105-
106-
val positionType = hudClass(EVERGREEN_POSITION)
107-
if (positionType != null) {
108-
val enabled = PolyPlusConfig.onboardingHudCoords || PolyPlusConfig.onboardingHudDirection
109-
found = applyHud(positionType, enabled) { hud ->
110-
setBoolean(hud, "setShowDirection", PolyPlusConfig.onboardingHudDirection)
111-
setBoolean(hud, "setShowAxis", PolyPlusConfig.onboardingHudCoords)
112-
setBoolean(hud, "setShowX", PolyPlusConfig.onboardingHudCoords || PolyPlusConfig.onboardingHudDirection)
113-
setBoolean(hud, "setShowY", PolyPlusConfig.onboardingHudCoords)
114-
setBoolean(hud, "setShowZ", PolyPlusConfig.onboardingHudCoords || PolyPlusConfig.onboardingHudDirection)
115-
} || found
116-
}
117-
return found
118-
}
119-
120-
private fun applyHud(type: Class<out Hud>, enabled: Boolean, configure: (Hud) -> Unit = {}): Boolean {
121-
val provider = HudManager.getProvider(type) ?: return false
122-
styleHud(provider)
123-
configure(provider)
124-
HudManager.toggleAllHuds(provider, !enabled)
125-
HudManager.getHudsOfType(type).forEach {
126-
styleHud(it)
127-
configure(it)
128-
it.hidden = !enabled
129-
runCatching { it.save() }
130-
}
131-
return true
132-
}
133-
134-
private fun styleHud(hud: Hud) {
135-
val polyGlass = PolyPlusConfig.onboardingHudStyle == 0
136-
hud.font = if (polyGlass) Font.Poppins else Font.Minecraft
137-
hud.bgRadius = if (polyGlass) 4f else 0f
138-
hud.showBackground = true
139-
}
140-
14180
private fun applyPolyBlur(value: Int): Boolean {
14281
val strength = value.coerceIn(0, 10)
14382
return runCatching {
@@ -156,25 +95,6 @@ object OnboardingFeatures {
15695

15796
private fun classExists(name: String) = runCatching { Class.forName(name, false, javaClass.classLoader) }.isSuccess
15897

159-
@Suppress("UNCHECKED_CAST")
160-
private fun hudClass(name: String): Class<out Hud>? =
161-
runCatching { Class.forName(name, false, javaClass.classLoader) as Class<out Hud> }.getOrNull()
162-
163-
private fun setBoolean(target: Any, setter: String, value: Boolean) {
164-
runCatching { target.javaClass.getMethod(setter, Boolean::class.javaPrimitiveType).invoke(target, value) }
165-
.onFailure { logger.warn("Could not apply EvergreenHUD option {}", setter, it) }
166-
}
167-
168-
private data class HudChoice(
169-
val enabled: Boolean,
170-
val className: String,
171-
)
172-
17398
private const val POLYSPRINT_CONFIG = "org.polyfrost.polysprint.client.PolySprintConfig"
17499
private const val POLYBLUR_CONFIG = "org.polyfrost.polyblur.client.PolyBlurConfig"
175-
private const val EVERGREEN_FPS = "org.polyfrost.evergreenhud.client.hud.FpsHud"
176-
private const val EVERGREEN_CPS = "org.polyfrost.evergreenhud.client.hud.CpsHud"
177-
private const val EVERGREEN_PING = "org.polyfrost.evergreenhud.client.hud.PingHud"
178-
private const val EVERGREEN_TIME = "org.polyfrost.evergreenhud.client.hud.clock.DigitalClockHud"
179-
private const val EVERGREEN_POSITION = "org.polyfrost.evergreenhud.client.hud.PositionHud"
180100
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.polyfrost.polyplus.client.gui
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.produceState
5+
import androidx.compose.ui.graphics.ImageBitmap
6+
import androidx.compose.ui.graphics.toComposeImageBitmap
7+
import io.ktor.client.call.body
8+
import io.ktor.client.request.get
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.withContext
11+
import org.apache.logging.log4j.LogManager
12+
import org.jetbrains.skia.Image as SkiaImage
13+
import org.polyfrost.polyplus.client.PolyPlusClient
14+
import org.polyfrost.polyplus.client.PolyPlusConfig
15+
import java.util.concurrent.ConcurrentHashMap
16+
17+
object CoverImageCache {
18+
private val LOGGER = LogManager.getLogger()
19+
20+
private val cache = ConcurrentHashMap<Int, ImageBitmap>()
21+
private val failed = ConcurrentHashMap.newKeySet<Int>()
22+
23+
fun cached(assetId: Int): ImageBitmap? = cache[assetId]
24+
25+
suspend fun get(assetId: Int): ImageBitmap? {
26+
cache[assetId]?.let { return it }
27+
if (assetId in failed) return null
28+
29+
return withContext(Dispatchers.IO) {
30+
runCatching {
31+
val bytes = PolyPlusClient.HTTP.get("${PolyPlusConfig.apiUrl}/asset/$assetId").body<ByteArray>()
32+
SkiaImage.makeFromEncoded(bytes).toComposeImageBitmap()
33+
}.onFailure {
34+
failed += assetId
35+
LOGGER.error("Failed to load cover asset {}", assetId, it)
36+
}.getOrNull()?.also { cache[assetId] = it }
37+
}
38+
}
39+
}
40+
41+
@Composable
42+
fun rememberCoverImage(assetId: Int?): ImageBitmap? =
43+
produceState(assetId?.let { CoverImageCache.cached(it) }, assetId) {
44+
if (assetId != null && value == null) value = CoverImageCache.get(assetId)
45+
}.value

0 commit comments

Comments
 (0)