-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Devoxin/feature/load-from-external
External Jar Scanning
- Loading branch information
Showing
7 changed files
with
153 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package me.devoxin.flight | ||
|
||
object FlightInfo { | ||
val VERSION = "1.1.1" | ||
val VERSION = "1.2.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/me/devoxin/flight/api/DefaultHelpCommandConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package me.devoxin.flight.api | ||
|
||
data class DefaultHelpCommandConfig( | ||
var enabled: Boolean = true, | ||
var showParameterTypes: Boolean = false | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/kotlin/me/devoxin/flight/internal/CommandRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package me.devoxin.flight.internal | ||
|
||
import me.devoxin.flight.api.CommandWrapper | ||
import me.devoxin.flight.models.Cog | ||
import me.devoxin.flight.utils.Indexer | ||
|
||
class CommandRegistry : HashMap<String, CommandWrapper>() { | ||
|
||
fun findCommandByName(name: String): CommandWrapper? { | ||
return this[name] | ||
} | ||
|
||
fun findCommandByAlias(alias: String): CommandWrapper? { | ||
return this.values.firstOrNull { it.properties.aliases.contains(alias) } | ||
} | ||
|
||
fun removeByCog(cogName: String, ignoreCase: Boolean = true) { | ||
this.values.removeIf { | ||
it.cog.name().equals(cogName, ignoreCase) | ||
} | ||
} | ||
|
||
fun registerCommands(cog: Cog, indexer: Indexer? = null) { | ||
val i = indexer ?: Indexer(cog::class.java.`package`.name) | ||
val commands = i.getCommands(cog) | ||
|
||
for (command in commands) { | ||
val cmd = i.loadCommand(command, cog) | ||
this[cmd.name] = cmd | ||
} | ||
} | ||
|
||
fun registerCommands(packageName: String) { | ||
val indexer = Indexer(packageName) | ||
val cogs = indexer.getCogs() | ||
|
||
for (cogClass in cogs) { | ||
val cog = cogClass.getDeclaredConstructor().newInstance() | ||
registerCommands(cog, indexer) | ||
} | ||
} | ||
|
||
fun registerCommands(jarPath: String, packageName: String) { | ||
Indexer(packageName, jarPath).use { | ||
val cogClasses = it.getCogs() | ||
|
||
for (cls in cogClasses) { | ||
val cog = cls.getDeclaredConstructor().newInstance() | ||
registerCommands(cog, it) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Attempts to load the jar at the given path. If successful, | ||
* Flight will attempt to discover all cogs in the jar, under the given package name. | ||
* | ||
* Before registering the cogs, any existing cogs whose names match those found in the jar will | ||
* automatically be unregistered and removed. | ||
* | ||
* @param jarPath | ||
* A string-representation of the path to the jar file. | ||
* | ||
* @param packageName | ||
* The package name to scan for cogs/commands in. | ||
*/ | ||
fun reload(jarPath: String, packageName: String) {Indexer(packageName, jarPath).use { | ||
val cogClasses = it.getCogs() | ||
|
||
for (cls in cogClasses) { | ||
val cog = cls.getDeclaredConstructor().newInstance() | ||
removeByCog(cog.name()) | ||
registerCommands(cog, it) | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters