Skip to content

Commit

Permalink
properly implement DI
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed May 22, 2023
1 parent b00291b commit 13ae27b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import com.github.shynixn.mccoroutine.bukkit.launch
import com.mineinabyss.chatty.components.*
import com.mineinabyss.chatty.helpers.*
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.idofront.commands.arguments.optionArg
import com.mineinabyss.idofront.commands.arguments.stringArg
import com.mineinabyss.idofront.commands.execution.IdofrontCommandExecutor
import com.mineinabyss.idofront.commands.extensions.actions.ensureSenderIsPlayer
import com.mineinabyss.idofront.commands.extensions.actions.playerAction
import com.mineinabyss.idofront.config.config
import com.mineinabyss.idofront.events.call
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mineinabyss.idofront.textcomponents.serialize
Expand All @@ -31,38 +29,16 @@ import kotlin.collections.set
class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
override val commands = commands(chatty.plugin) {
"chatty"(desc = "Chatty commands") {
/*"reload" {
val option by optionArg(listOf("all", "config", "messages", "emotefixer")) { default = "all" }
"reload" {
action {
when (option) {
"all" -> {
chatty.config = config("config") { chatty.fromPluginPath(loadDefault = true) }
chatty.messages = config("messages") { chatty.fromPluginPath(loadDefault = true) }
chatty.emotefixer = config("emotefixer") { chatty.fromPluginPath(loadDefault = true) }
sender.sendConsoleMessage("<green>Reloaded everything!")
}
"config" -> {
chatty.config = config("config") { chatty.fromPluginPath(loadDefault = true) }
sender.sendConsoleMessage("<green>Reloaded configs!")
}
"messages" -> {
chatty.messages = config("messages") { chatty.fromPluginPath(loadDefault = true) }
sender.sendConsoleMessage("<green>Reloaded messages!")
}
"emotefixer" -> {
chatty.emotefixer = config("emotefixer") { chatty.fromPluginPath(loadDefault = true) }
sender.sendConsoleMessage("<green>Reloaded emotefixer!")
}
}
chatty.plugin.createChattyContext()
sender.sendConsoleMessage("<green>Chatty has been reloaded!")
}
// chatty.config.reload() is a thing but does not regen or remove stuff so
// chatty.config.reload()
// chatty.messages.reload()
// chatty.emoteFixer.reload()
}*/
}
("message" / "msg")(desc = "Private message another player") {
ensureSenderIsPlayer()
val player by stringArg()
Expand Down Expand Up @@ -275,46 +251,23 @@ class ChattyCommands : IdofrontCommandExecutor(), TabCompleter {
val otherPrefix = chatty.config.nicknames.nickNameOtherPrefix
return if (command.name == "chatty") {
when (args.size) {
1 -> listOf(
"message",
"ping",
"reload",
"channels",
"nickname",
"spy",
"commandspy"
).filter { s -> s.startsWith(args[0]) }

1 -> listOf("message", "ping", "reload", "channels", "nickname", "spy", "commandspy").filter { s -> s.startsWith(args[0]) }
2 -> when (args[0]) {
"ping" -> listOf("toggle", "sound").filter { s -> s.startsWith(args[1]) }
"message", "msg" -> onlinePlayers.filter { s -> s.startsWith(args[1], true) }
"spy" ->
chatty.config.channels.keys.toList().filter { s ->
s.startsWith(
args[1],
true
) && getChannelFromId(s)?.channelType != ChannelType.GLOBAL
s.startsWith(args[1], true) && getChannelFromId(s)?.channelType != ChannelType.GLOBAL
}

"reload", "rl" -> listOf(
"all",
"config",
"messages",
"emotefixer"
).filter { s -> s.startsWith(args[1]) }

else -> emptyList()
}

3 -> when {
args[1] == "sound" -> getAlternativePingSounds.filter { s -> s.startsWith(args[2], true) }
args[1].startsWith(otherPrefix) -> onlinePlayers.filter { s ->
s.replace(otherPrefix.toString(), "").startsWith(args[2], true)
}

else -> emptyList()
}

else -> emptyList()
}
} else emptyList()
Expand Down
37 changes: 22 additions & 15 deletions chatty-paper/src/main/kotlin/com/mineinabyss/chatty/ChattyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.mineinabyss.chatty.listeners.ChattyProxyListener
import com.mineinabyss.chatty.listeners.DiscordListener
import com.mineinabyss.chatty.listeners.PlayerListener
import com.mineinabyss.chatty.placeholders.PlaceholderAPIHook
import com.mineinabyss.geary.autoscan.autoscan
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.idofront.config.config
import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.platforms.Platforms
Expand All @@ -20,19 +22,10 @@ class ChattyPlugin : JavaPlugin() {
}

override fun onEnable() {
val chattyContext = object : ChattyContext {
override val plugin: ChattyPlugin = this@ChattyPlugin
override val config: ChattyConfig by config("config") { fromPluginPath(loadDefault = true) }
override val messages: ChattyMessages by config("messages") { fromPluginPath(loadDefault = true) }
override val emotefixer: DiscordEmoteFixer by config("emotefixer") { fromPluginPath(loadDefault = true) }
override val isPlaceholderApiLoaded: Boolean = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")
override val isDiscordSRVLoaded: Boolean = Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")
}

DI.add<ChattyContext>(chattyContext)

saveDefaultAssets()

createChattyContext()

// Register the proxy listener
registerProxyChannels()

Expand All @@ -45,13 +38,27 @@ class ChattyPlugin : JavaPlugin() {
if (chatty.isDiscordSRVLoaded)
DiscordSRV.api.subscribe(DiscordListener())

/*geary {
*//*autoscan(classLoader, "com.mineinabyss.chatty") {
geary {
autoscan(classLoader, "com.mineinabyss.chatty") {
all()
}*//*
}
ChattyCommands()
listeners(ChatListener(), PlayerListener())
}*/
}
}

fun createChattyContext() {
DI.remove<ChattyContext>()
val chattyContext = object : ChattyContext {
override val plugin: ChattyPlugin = this@ChattyPlugin
override val config: ChattyConfig by config("config") { fromPluginPath(loadDefault = true) }
override val messages: ChattyMessages by config("messages") { fromPluginPath(loadDefault = true) }
override val emotefixer: DiscordEmoteFixer by config("emotefixer") { fromPluginPath(loadDefault = true) }
override val isPlaceholderApiLoaded: Boolean = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")
override val isDiscordSRVLoaded: Boolean = Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")
}

DI.add<ChattyContext>(chattyContext)
}

override fun onDisable() {
Expand Down

0 comments on commit 13ae27b

Please sign in to comment.