Commands actually work now, rearrange some files
This commit is contained in:
parent
b71318ec97
commit
e953674253
|
@ -2,6 +2,7 @@ package matterlink
|
|||
|
||||
import matterlink.bridge.MessageHandler
|
||||
import matterlink.bridge.ServerChatHelper
|
||||
import matterlink.bridge.command.Commands
|
||||
import matterlink.command.CommandMatterlink
|
||||
import matterlink.eventhandlers.*
|
||||
import net.minecraftforge.common.MinecraftForge
|
||||
|
@ -39,17 +40,21 @@ object MatterLink {
|
|||
PropertiesUtil(Properties()),
|
||||
System.out)
|
||||
|
||||
val bridgeCommands = Commands
|
||||
|
||||
@Mod.EventHandler
|
||||
fun preInit(event: FMLPreInitializationEvent) {
|
||||
logger = event.modLog
|
||||
logger.info("Building bridge!")
|
||||
|
||||
MatterLinkConfig(event.suggestedConfigurationFile)
|
||||
|
||||
bridgeCommands.register()
|
||||
}
|
||||
|
||||
@Mod.EventHandler
|
||||
fun serverStarting(event: FMLServerStartingEvent) {
|
||||
logger.debug("Registering bridge commands")
|
||||
logger.debug("Registering server commands")
|
||||
event.registerServerCommand(CommandMatterlink())
|
||||
MessageHandler.start()
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package matterlink.bridge
|
||||
|
||||
import matterlink.MatterLink
|
||||
import matterlink.bridge.command.BridgeCommand
|
||||
import matterlink.cfg
|
||||
import net.minecraft.util.text.TextComponentString
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler
|
||||
|
|
|
@ -1,16 +1,12 @@
|
|||
package matterlink.bridge
|
||||
package matterlink.bridge.command
|
||||
|
||||
import matterlink.MatterLink
|
||||
import matterlink.cfg
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class BridgeCommand(val name: String, command: (String) -> Boolean) {
|
||||
val execute: (String) -> Boolean = command //return true for success and false for failure
|
||||
|
||||
init {
|
||||
registerCommand(this)
|
||||
}
|
||||
|
||||
companion object Handler {
|
||||
private val commandMap: HashMap<String, BridgeCommand> = HashMap()
|
||||
|
||||
|
@ -24,34 +20,16 @@ class BridgeCommand(val name: String, command: (String) -> Boolean) {
|
|||
return if (commandMap.containsKey(cmd[0])) (commandMap[cmd[0]]!!.execute)(args) else false
|
||||
}
|
||||
|
||||
fun registerCommand(cmd: BridgeCommand): Boolean {
|
||||
fun register(cmd: BridgeCommand): Boolean {
|
||||
if (cmd.name.isBlank() || commandMap.containsKey(cmd.name)) return false
|
||||
commandMap[cmd.name] = cmd
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private val commands = object {
|
||||
private val playerlist = BridgeCommand(
|
||||
"players",
|
||||
fun(args: String): Boolean {
|
||||
if (args.isNotEmpty()) return false
|
||||
|
||||
var output: String = ""
|
||||
|
||||
for (player: String in FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames) {
|
||||
output = output + player + " "
|
||||
}
|
||||
|
||||
MessageHandler.transmit(ApiMessage(
|
||||
username = cfg!!.relay.systemUser,
|
||||
text = output
|
||||
))
|
||||
|
||||
return true
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
fun registerAll(vararg cmds: BridgeCommand) {
|
||||
for (cmd in cmds) {
|
||||
if (!register(cmd)) MatterLink.logger.error("Failed to register command: " + cmd.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package matterlink.bridge.command
|
||||
|
||||
import matterlink.bridge.ApiMessage
|
||||
import matterlink.bridge.MessageHandler
|
||||
import matterlink.cfg
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler
|
||||
|
||||
object Commands {
|
||||
val listPlayers = BridgeCommand(
|
||||
"players",
|
||||
fun(args: String): Boolean {
|
||||
if (args.isNotEmpty()) return false
|
||||
|
||||
var output: String = ""
|
||||
|
||||
for (player: String in FMLCommonHandler.instance().minecraftServerInstance.playerList.onlinePlayerNames) {
|
||||
output = output + player + " "
|
||||
}
|
||||
|
||||
MessageHandler.transmit(ApiMessage(
|
||||
username = cfg!!.relay.systemUser,
|
||||
text = output
|
||||
))
|
||||
|
||||
return true
|
||||
}
|
||||
)
|
||||
|
||||
fun register() {
|
||||
BridgeCommand.registerAll(listPlayers)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue