Skip to content

Commit 69645db

Browse files
authored
refactor: simplify owner validation in VelocityComponentService (#228)
2 parents 685dc8a + 38c152b commit 69645db

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
77
javaVersion=25
88
mcVersion=1.21.11
99
group=dev.slne.surf
10-
version=1.21.11-2.59.2
10+
version=1.21.11-2.59.3
1111
relocationPrefix=dev.slne.surf.surfapi.libs
1212
snapshot=false
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package dev.slne.surf.surfapi.velocity.server.component
22

33
import com.google.auto.service.AutoService
4+
import com.velocitypowered.api.plugin.PluginContainer
45
import dev.slne.surf.surfapi.core.server.component.ComponentService
56
import dev.slne.surf.surfapi.velocity.server.velocityMain
67
import net.kyori.adventure.text.logger.slf4j.ComponentLogger
78
import java.nio.file.Path
9+
import kotlin.contracts.ExperimentalContracts
10+
import kotlin.contracts.contract
811
import kotlin.io.path.Path
9-
import kotlin.jvm.optionals.getOrNull
1012

1113
@AutoService(ComponentService::class)
1214
class VelocityComponentService : ComponentService() {
@@ -15,26 +17,30 @@ class VelocityComponentService : ComponentService() {
1517
}
1618

1719
override fun getClassloader(owner: Any): ClassLoader {
18-
return getInstanceFromOwner(owner).javaClass.classLoader
20+
ensureOwnerIsPluginContainer(owner)
21+
return owner.javaClass.classLoader
1922
}
2023

2124
override fun isPluginLoaded(pluginId: String): Boolean {
2225
return velocityMain.server.pluginManager.isLoaded(pluginId)
2326
}
2427

2528
override fun getLogger(owner: Any): ComponentLogger {
26-
return ComponentLogger.logger(getPluginContainerFromOwner(owner).description.id)
29+
ensureOwnerIsPluginContainer(owner)
30+
return ComponentLogger.logger(owner.description.id)
2731
}
2832

2933
override fun getDataPath(owner: Any): Path {
30-
return PLUGIN_PATH.resolve(getPluginContainerFromOwner(owner).description.id)
34+
ensureOwnerIsPluginContainer(owner)
35+
return PLUGIN_PATH.resolve(owner.description.id)
3136
}
3237

33-
private fun getPluginContainerFromOwner(owner: Any) =
34-
velocityMain.server.pluginManager.ensurePluginContainer(owner)
38+
@OptIn(ExperimentalContracts::class)
39+
private fun ensureOwnerIsPluginContainer(owner: Any): PluginContainer {
40+
contract {
41+
returns() implies (owner is PluginContainer)
42+
}
3543

36-
private fun getInstanceFromOwner(owner: Any): Any {
37-
return getPluginContainerFromOwner(owner).instance.getOrNull()
38-
?: error("Failed to get instance from owner: $owner")
44+
return owner as? PluginContainer ?: error("Owner must be a PluginContainer")
3945
}
4046
}

0 commit comments

Comments
 (0)