Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Refactor some classes and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT2 committed Aug 26, 2020
1 parent e463f22 commit d038c79
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 49 deletions.
30 changes: 28 additions & 2 deletions src/main/kotlin/Command.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.mojang.brigadier.tree.LiteralCommandNode
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import net.ayataka.kordis.event.events.message.MessageReceiveEvent
import org.reflections.Reflections
import java.util.concurrent.ConcurrentLinkedQueue

/**
Expand Down Expand Up @@ -33,12 +35,36 @@ class Cmd(val event: MessageReceiveEvent) {

object CommandManager {
/* Name, Literal Command */
val commands = hashMapOf<String, LiteralCommandNode<Cmd>>()
val commandClasses = hashMapOf<String, Command>()
private val commands = hashMapOf<String, LiteralCommandNode<Cmd>>()
private val commandClasses = hashMapOf<String, Command>()

fun isCommand(name: String) = commands.containsKey(name)

fun getCommand(name: String) = commands[name]

fun getCommandClass(name: String) = commandClasses[name]

/**
* Uses reflection to get a list of classes in the commands package which extend [Command]
* and register said classes's instances with Brigadier.
*/
fun registerCommands(dispatcher: CommandDispatcher<Cmd>) {
val reflections = Reflections("commands")

val subTypes: Set<Class<out Command>> = reflections.getSubTypesOf(Command::class.java)

println("Registering commands...")

for (command in subTypes) {
val literalCommand = command.getField("INSTANCE").get(null) as LiteralArgumentBuilder<Cmd>
val commandAsInstanceOfCommand = command.getField("INSTANCE").get(null) as Command
commandClasses[literalCommand.literal] = commandAsInstanceOfCommand
commands[literalCommand.literal] = dispatcher.register(literalCommand)
}

var registeredCommands = ""
commands.forEach { entry -> registeredCommands += "\n> ;${entry.key}" }

println("Registered commands!$registeredCommands\n")
}
}
53 changes: 7 additions & 46 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import Main.currentVersion
import CommandManager.registerCommands
import UpdateHelper.updateCheck
import com.mojang.brigadier.CommandDispatcher
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.mojang.brigadier.exceptions.CommandSyntaxException
import kotlinx.coroutines.runBlocking
import net.ayataka.kordis.Kordis
import net.ayataka.kordis.event.EventHandler
import net.ayataka.kordis.event.events.message.MessageReceiveEvent
import org.reflections.Reflections
import java.awt.Color
import java.io.File


fun main() = runBlocking {
Bot().start()
}

/**
* @author dominikaaaa
* @since 16/08/20 17:30
*/
class Bot {
private val dispatcher = CommandDispatcher<Cmd>()
private var hasUpdate = false
Expand All @@ -39,7 +40,7 @@ class Bot {
addListener(this@Bot)
}

registerCommands()
registerCommands(dispatcher)
println("Initialized bot!\n" +
"Startup took ${System.currentTimeMillis() - started}ms")
}
Expand All @@ -65,46 +66,6 @@ class Bot {
}
}
}

private fun updateCheck() {
if (File("noUpdateCheck").exists()) return
val versionConfig = FileManager.readConfig<VersionConfig>(ConfigType.VERSION, false)

if (versionConfig?.version == null) {
println("Couldn't access remote version when checking for update")
return
}

if (versionConfig.version != currentVersion) {
println("Not up to date:\nCurrent version: $currentVersion\nLatest Version: ${versionConfig.version}\n")
} else {
println("Up to date! Running on $currentVersion")
}
}

/**
* Uses reflection to get a list of classes in the commands package which extend [Command]
* and register said classes's instances with Brigadier.
*/
private fun registerCommands() {
val reflections = Reflections("commands")

val subTypes: Set<Class<out Command>> = reflections.getSubTypesOf(Command::class.java)

println("Registering commands...")

for (command in subTypes) {
val literalCommand = command.getField("INSTANCE").get(null) as LiteralArgumentBuilder<Cmd>
val commandAsInstanceOfCommand = command.getField("INSTANCE").get(null) as Command
CommandManager.commandClasses[literalCommand.literal] = commandAsInstanceOfCommand
CommandManager.commands[literalCommand.literal] = dispatcher.register(literalCommand)
}

var registeredCommands = ""
CommandManager.commands.forEach { entry -> registeredCommands += "\n> ;${entry.key}" }

println("Registered commands!$registeredCommands\n")
}
}

object Main {
Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/UpdateHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Main.currentVersion
import java.io.File

object UpdateHelper {
fun updateCheck() {
if (File("noUpdateCheck").exists()) return
val versionConfig = FileManager.readConfig<VersionConfig>(ConfigType.VERSION, false)

if (versionConfig?.version == null) {
println("Couldn't access remote version when checking for update")
return
}

if (versionConfig.version != currentVersion) {
println("Not up to date:\nCurrent version: $currentVersion\nLatest Version: ${versionConfig.version}\n")
} else {
println("Up to date! Running on $currentVersion")
}
}

/**
* TODO: FINISH
*/
fun updateBot(version: String) {
if (!File("autoUpdate").exists()) return

println()
}
}
1 change: 0 additions & 1 deletion src/main/kotlin/commands/DiscussCommand.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import Command
import Main
import Main.Colors
import arg
import doesLater
Expand Down

0 comments on commit d038c79

Please sign in to comment.