Skip to content

Commit c4e95e7

Browse files
committed
Refactor dependency injection to use default bindings map
Extracted default bindings and annotated bindings into separate helper methods to streamline dependency management. Updated `MinecraftGuiceModule` to utilize these maps, reducing repetitive code and improving maintainability. Introduced a constant for the plugin logger key to ensure consistency.
1 parent 657b700 commit c4e95e7

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/main/kotlin/dev/newspicel/di/DIJavaPlugin.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import dev.newspicel.di.serverevents.OnEnable
1010
import dev.newspicel.di.serverevents.OnLoad
1111
import dev.newspicel.di.serverevents.ServerEventsHelper
1212
import net.kyori.adventure.text.Component.text
13+
import org.bukkit.Server
1314
import org.bukkit.command.CommandSender
15+
import org.bukkit.plugin.Plugin
1416
import org.bukkit.plugin.java.JavaPlugin
1517
import org.reflections.Reflections
1618
import org.reflections.util.ConfigurationBuilder
1719
import java.util.logging.Level
20+
import java.util.logging.Logger
1821
import kotlin.reflect.KClass
1922
import kotlin.reflect.KParameter
2023
import kotlin.reflect.full.findAnnotations
@@ -124,6 +127,24 @@ abstract class DIJavaPlugin : JavaPlugin() {
124127
}
125128
}
126129

130+
protected fun getDefaultBindings(): Map<KClass<*>, Any> {
131+
return mapOf(
132+
JavaPlugin::class to this,
133+
GuiceJavaPlugin::class to this,
134+
DIJavaPlugin::class to this,
135+
Plugin::class to this,
136+
this::class to this,
137+
Server::class to server,
138+
ClassLoader::class to classLoader,
139+
)
140+
}
141+
142+
protected fun getDefaultAnnotatedBindings(): Map<KClass<*>, Pair<String, Any>> {
143+
return mapOf(
144+
Logger::class to (PLUGIN_LOGGER_KEY to logger),
145+
)
146+
}
147+
127148
// Lock this stuff down
128149
final override fun onTabComplete(sender: CommandSender, command: org.bukkit.command.Command, alias: String, args: Array<out String>): List<String> {
129150
sender.sendMessage(text("An error occurred while executing this command. Please contact the server administrators."))

src/main/kotlin/dev/newspicel/di/Extensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ package dev.newspicel.di
22

33
import java.util.logging.Logger
44

5-
fun logger(plugin: DIJavaPlugin) = lazy { plugin.getExistingBinding(Logger::class, "pluginLogger")!! }
5+
const val PLUGIN_LOGGER_KEY = "pluginLogger"
6+
7+
fun logger(plugin: DIJavaPlugin) = lazy { plugin.getExistingBinding(Logger::class, PLUGIN_LOGGER_KEY)!! }

src/main/kotlin/dev/newspicel/di/GuiceJavaPlugin.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ package dev.newspicel.di
33
import com.google.inject.*
44
import com.google.inject.name.Named
55
import com.google.inject.name.Names
6-
import org.bukkit.Server
7-
import org.bukkit.plugin.Plugin
8-
import org.bukkit.plugin.java.JavaPlugin
9-
import java.util.logging.Logger
106
import kotlin.reflect.KClass
117
import kotlin.reflect.full.findAnnotations
128
import kotlin.reflect.jvm.javaConstructor
@@ -16,7 +12,7 @@ abstract class GuiceJavaPlugin : DIJavaPlugin() {
1612
private lateinit var injector: Injector
1713

1814
override fun startDependencyInjection() {
19-
val module = MinecraftGuiceModule(this, stereotypesClasses, classLoader)
15+
val module = MinecraftGuiceModule(getDefaultBindings(), getDefaultAnnotatedBindings(), stereotypesClasses)
2016

2117
try {
2218
injector = Guice.createInjector(module) ?: error("Could not create Guice injector")
@@ -65,21 +61,18 @@ abstract class GuiceJavaPlugin : DIJavaPlugin() {
6561
}
6662

6763
class MinecraftGuiceModule(
68-
private val plugin: GuiceJavaPlugin,
64+
private val defaultBindings: Map<KClass<*>, Any>,
65+
private val defaultAnnotatedBindings: Map<KClass<*>, Pair<String, Any>>,
6966
private val allStereotypes: List<KClass<*>>,
70-
private val classLoader: ClassLoader,
7167
) : AbstractModule() {
72-
@Suppress("UNCHECKED_CAST")
7368
override fun configure() {
74-
bind(JavaPlugin::class.java).toInstance(plugin)
75-
bind(GuiceJavaPlugin::class.java).toInstance(plugin)
76-
bind(DIJavaPlugin::class.java).toInstance(plugin)
77-
bind(Plugin::class.java).toInstance(plugin)
78-
bind(plugin.javaClass).toInstance(plugin)
79-
bind(Server::class.java).toInstance(plugin.server)
80-
bind(ClassLoader::class.java).toInstance(classLoader)
69+
defaultBindings.forEach { (type, instance) ->
70+
bind(type.java as Class<Any>).toInstance(instance)
71+
}
8172

82-
bind(Logger::class.java).annotatedWith(Names.named("pluginLogger")).toInstance(plugin.logger)
73+
defaultAnnotatedBindings.forEach { (type, instance) ->
74+
bind(type.java as Class<Any>).annotatedWith(Names.named(instance.first)).toInstance(instance.second)
75+
}
8376

8477
allStereotypes.forEach { stereotype ->
8578
val type = stereotype.java as Class<Any>

0 commit comments

Comments
 (0)