Skip to content

Commit

Permalink
Add: register all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Sep 12, 2024
1 parent 26a3cd8 commit 4230c8c
Show file tree
Hide file tree
Showing 24 changed files with 170 additions and 65 deletions.
10 changes: 3 additions & 7 deletions src/main/kotlin/com/fardragi/nyaruko/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.fardragi.nyaruko.config.DiscordConfig
import com.fardragi.nyaruko.core.CoreModule
import com.fardragi.nyaruko.database.DatabaseConnection
import com.fardragi.nyaruko.permission.PermissionModule
import com.fardragi.nyaruko.services.CommandService
import com.fardragi.nyaruko.services.SessionsService
import com.fardragi.nyaruko.services.UserService
import org.koin.core.module.dsl.createdAtStart
Expand All @@ -25,15 +26,10 @@ val appModule = module {
}

single { CoreModule() }
scope<CoreModule> { }

single { AuthModule() }
scope<AuthModule> {
scoped { UserService() }
}

single { PermissionModule() }
scope<PermissionModule> { }

single { UserService() }
single { SessionsService() }
single { CommandService() }
}
8 changes: 6 additions & 2 deletions src/main/kotlin/com/fardragi/nyaruko/NyarukoMod.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fardragi.nyaruko

import com.fardragi.nyaruko.server.ServerProxy
import com.fardragi.nyaruko.shared.IProxy
import cpw.mods.fml.common.Mod
import cpw.mods.fml.common.SidedProxy
import cpw.mods.fml.common.event.FMLInitializationEvent
Expand All @@ -19,24 +19,28 @@ import cpw.mods.fml.common.event.FMLServerStartingEvent
)
object NyarukoMod {
@SidedProxy(serverSide = "$GROUPNAME.server.ServerProxy", clientSide = "$GROUPNAME.client.ClientProxy")
lateinit var proxy: ServerProxy
lateinit var proxy: IProxy

@Mod.EventHandler
@Suppress("UNUSED")
fun onPreInit(event: FMLPreInitializationEvent) {
proxy.onPreInit(event)
}

@Mod.EventHandler
@Suppress("UNUSED")
fun onInit(event: FMLInitializationEvent) {
proxy.onInit(event)
}

@Mod.EventHandler
@Suppress("UNUSED")
fun onPostInit(event: FMLPostInitializationEvent) {
proxy.onPostInit(event)
}

@Mod.EventHandler
@Suppress("UNUSED")
fun onServerStarting(event: FMLServerStartingEvent) {
proxy.onServerStarting(event)
}
Expand Down
16 changes: 6 additions & 10 deletions src/main/kotlin/com/fardragi/nyaruko/auth/AuthModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import com.fardragi.nyaruko.auth.commands.RegisterCommand
import com.fardragi.nyaruko.auth.handlers.AuthHandler
import com.fardragi.nyaruko.auth.handlers.CheckHandler
import com.fardragi.nyaruko.shared.IModule
import org.koin.core.component.KoinScopeComponent
import org.koin.core.component.createScope

class AuthModule() : KoinScopeComponent, IModule {
override val scope by lazy { createScope(this) }
class AuthModule() : IModule {
override suspend fun start() {
AuthHandler().register()
CheckHandler().register()

override fun start() {
AuthHandler(scope.get(), scope.get(), scope.get()).register()
CheckHandler(scope.get()).register()

RegisterCommand(scope.get()).register()
LoginCommand(scope.get(), scope.get()).register()
RegisterCommand().register()
LoginCommand().register()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import com.fardragi.nyaruko.shared.commands.NyarukoCommandBase
import com.fardragi.nyaruko.shared.messages.DefaultMessage
import net.minecraft.command.ICommandSender
import net.minecraft.entity.player.EntityPlayerMP
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class LoginCommand() : NyarukoCommandBase(), KoinComponent {
private val userService: UserService by inject()
private val sessionsService: SessionsService by inject()

class LoginCommand(
private val userService: UserService,
private val sessionsService: SessionsService
) : NyarukoCommandBase() {
override fun getCommandName(): String {
return "login"
}
Expand Down Expand Up @@ -44,7 +46,7 @@ class LoginCommand(
}

override fun getRequiredPermissionLevel(): Int {
return PermissionLevel.True.level
return PermissionLevel.All.level
}

override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import com.fardragi.nyaruko.shared.commands.NyarukoCommandBase
import com.fardragi.nyaruko.shared.messages.DefaultMessage
import net.minecraft.command.ICommandSender
import net.minecraft.entity.player.EntityPlayerMP
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class RegisterCommand() : NyarukoCommandBase(), KoinComponent {
private val userService: UserService by inject()

class RegisterCommand(private val userService: UserService) : NyarukoCommandBase() {
override fun getCommandName(): String {
return "register"
}
Expand Down Expand Up @@ -44,7 +48,7 @@ class RegisterCommand(private val userService: UserService) : NyarukoCommandBase
}

override fun getRequiredPermissionLevel(): Int {
return PermissionLevel.True.level
return PermissionLevel.All.level
}

override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class AuthHandler() : NyarukoHandlerBase(), KoinComponent {
private val userService: UserService by inject()
private val discordConfig: DiscordConfig by inject()
private val sessionsService: SessionsService by inject()

class AuthHandler(
private val userService: UserService,
private val discordConfig: DiscordConfig,
private val sessionsService: SessionsService
) : NyarukoHandlerBase() {
@SubscribeEvent(priority = EventPriority.LOWEST)
fun onPlayerJoin(event: PlayerLoggedInEvent) {
val player = event.player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import com.fardragi.nyaruko.shared.handlers.NyarukoHandlerBase
import cpw.mods.fml.common.eventhandler.EventPriority
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.event.entity.item.ItemTossEvent
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class CheckHandler() : NyarukoHandlerBase(), KoinComponent {
private val sessionsService: SessionsService by inject()

class CheckHandler(private val sessionsService: SessionsService) : NyarukoHandlerBase() {
@SubscribeEvent(priority = EventPriority.HIGHEST)
fun onPlayerMove(event: PlayerMoveEvent) {
if (sessionsService.isAuthenticated(event.player.uniqueID) || !event.player.isTruePlayer())
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/fardragi/nyaruko/client/ClientProxy.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fardragi.nyaruko.client

import com.fardragi.nyaruko.server.ServerProxy
import com.fardragi.nyaruko.shared.IProxy

class ClientProxy : ServerProxy() {
class ClientProxy : IProxy {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private const val CATEGORY = "database"
class DatabaseConfig(config: Config) {
val host: String = config.configuration.getString("host", CATEGORY, "localhost", "Database host")
val port: Int = config.configuration.getInt("port", CATEGORY, 3306, 0, 65535, "Database port")
val name: String = config.configuration.getString("name", CATEGORY, "dragi_utils", "Database name")
val name: String = config.configuration.getString("name", CATEGORY, "nyaruko", "Database name")
val password: String = config.configuration.getString("password", CATEGORY, "root", "Database password")
val user: String = config.configuration.getString("user", CATEGORY, "root", "Database user")

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/fardragi/nyaruko/core/CoreModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.koin.core.component.createScope
class CoreModule() : KoinScopeComponent, IModule {
override val scope by lazy { createScope(this) }

override fun start() {
override suspend fun start() {
PlayerTickHandler().register()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fardragi.nyaruko.core.events

import com.fardragi.nyaruko.shared.NyarukoEvent
import com.fardragi.nyaruko.shared.events.NyarukoEvent
import com.fardragi.nyaruko.viewmodels.PositionViewModel
import cpw.mods.fml.common.eventhandler.Cancelable
import net.minecraft.entity.player.EntityPlayerMP
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.fardragi.nyaruko.database

import com.fardragi.nyaruko.config.DatabaseConfig
import com.fardragi.nyaruko.database.tables.UsersTable
import com.fardragi.nyaruko.database.tables.Commands
import com.fardragi.nyaruko.database.tables.Users
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -19,7 +20,7 @@ class DatabaseConnection(config: DatabaseConfig) {

CoroutineScope(Dispatchers.IO).launch {
query {
SchemaUtils.createMissingTablesAndColumns(UsersTable)
SchemaUtils.createMissingTablesAndColumns(Users, Commands)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/database/tables/Commands.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.fardragi.nyaruko.database.tables

import com.fardragi.nyaruko.enums.PermissionLevel
import org.jetbrains.exposed.dao.id.IdTable

object Commands : IdTable<UInt>("commands") {
override val id = uinteger("id").entityId().autoIncrement()
val name = varchar("name", 255)
val level = enumerationByName("level", 6, PermissionLevel::class)

override val primaryKey = PrimaryKey(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.fardragi.nyaruko.database.tables
import org.jetbrains.exposed.dao.id.IdTable
import java.util.UUID

object UsersTable : IdTable<UUID>("users") {
object Users : IdTable<UUID>("users") {
override val id = uuid("id").entityId()
val name = varchar("name", 50)
val hash = varchar("hash", 128).nullable()
Expand Down
14 changes: 12 additions & 2 deletions src/main/kotlin/com/fardragi/nyaruko/enums/PermissionLevel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.fardragi.nyaruko.enums

enum class PermissionLevel(val level: Int) {
True(0),
All(0),
Op(4),
False(5),
Nobody(5);

companion object {
fun fromLevel(level: Int): PermissionLevel {
return when (level) {
0 -> All
in 1..4 -> Op
else -> Nobody
}
}
}
}
13 changes: 13 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/models/Command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fardragi.nyaruko.models

import com.fardragi.nyaruko.database.tables.Commands
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID

class Command(id: EntityID<UInt>) : Entity<UInt>(id) {
companion object : EntityClass<UInt, Command>(Commands)

var name by Commands.name
var level by Commands.level
}
8 changes: 4 additions & 4 deletions src/main/kotlin/com/fardragi/nyaruko/models/User.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.fardragi.nyaruko.models

import com.fardragi.nyaruko.database.tables.UsersTable
import com.fardragi.nyaruko.database.tables.Users
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder
import java.util.UUID

class User(id: EntityID<UUID>) : Entity<UUID>(id) {
companion object : EntityClass<UUID, User>(UsersTable)
companion object : EntityClass<UUID, User>(Users)

var name by UsersTable.name
private var hash by UsersTable.hash
var name by Users.name
private var hash by Users.hash

val isRegistered get() = hash != null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.fardragi.nyaruko.permission

import com.fardragi.nyaruko.enums.PermissionLevel
import com.fardragi.nyaruko.services.CommandService
import com.fardragi.nyaruko.shared.IModule
import org.koin.core.component.KoinScopeComponent
import org.koin.core.component.createScope
import net.minecraft.command.CommandBase
import net.minecraft.server.MinecraftServer
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class PermissionModule() : KoinScopeComponent, IModule {
override val scope by lazy { createScope(this) }
class PermissionModule() : IModule, KoinComponent {
private val commandService: CommandService by inject()

override fun start() {
TODO("Not yet implemented")
override suspend fun start() {
val commands = MinecraftServer.getServer().commandManager.commands.mapValues { (_, v) ->
return@mapValues if (v is CommandBase)
PermissionLevel.fromLevel(v.requiredPermissionLevel)
else PermissionLevel.Op
}

commandService.registerCommands(commands as HashMap<String, PermissionLevel>)
}
}
22 changes: 12 additions & 10 deletions src/main/kotlin/com/fardragi/nyaruko/server/ServerProxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ package com.fardragi.nyaruko.server
import com.fardragi.nyaruko.appModule
import com.fardragi.nyaruko.auth.AuthModule
import com.fardragi.nyaruko.core.CoreModule
import com.fardragi.nyaruko.permission.PermissionModule
import com.fardragi.nyaruko.shared.IProxy
import cpw.mods.fml.common.event.FMLInitializationEvent
import cpw.mods.fml.common.event.FMLPostInitializationEvent
import cpw.mods.fml.common.event.FMLPreInitializationEvent
import cpw.mods.fml.common.event.FMLServerStartingEvent
import kotlinx.coroutines.runBlocking
import org.koin.core.KoinApplication
import org.koin.core.context.startKoin

open class ServerProxy {
class ServerProxy : IProxy {
private lateinit var app: KoinApplication

fun onPreInit(event: FMLPreInitializationEvent) {
override fun onPreInit(event: FMLPreInitializationEvent) {
app = startKoin {
modules(appModule)
}
}

fun onInit(event: FMLInitializationEvent) {
override fun onInit(event: FMLInitializationEvent) {
val coreModule = app.koin.get<CoreModule>()
val authModule = app.koin.get<AuthModule>()
val permissionModule = app.koin.get<PermissionModule>()

coreModule.start()
authModule.start()
runBlocking {
coreModule.start()
authModule.start()
permissionModule.start()
}
}

fun onPostInit(event: FMLPostInitializationEvent) {}
fun onServerStarting(event: FMLServerStartingEvent) {}
}
Loading

0 comments on commit 4230c8c

Please sign in to comment.