-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4230c8c
commit b8a4724
Showing
16 changed files
with
190 additions
and
37 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
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
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
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/fardragi/nyaruko/database/tables/GroupCommands.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,16 @@ | ||
package com.fardragi.nyaruko.database.tables | ||
|
||
import org.jetbrains.exposed.dao.id.IdTable | ||
|
||
object GroupCommands : IdTable<UInt>("group_commands") { | ||
override val id = uinteger("id").autoIncrement().entityId() | ||
val groupId = reference("group_id", Groups) | ||
val command = reference("command_id", Commands) | ||
val permission = bool("permission").default(true) | ||
|
||
override val primaryKey = PrimaryKey(id) | ||
|
||
init { | ||
uniqueIndex(groupId, command) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/fardragi/nyaruko/database/tables/Groups.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,12 @@ | ||
package com.fardragi.nyaruko.database.tables | ||
|
||
import org.jetbrains.exposed.dao.id.IdTable | ||
|
||
object Groups : IdTable<UInt>("groups") { | ||
override val id = uinteger("id").autoIncrement().entityId() | ||
val name = varchar("name", 255).uniqueIndex() | ||
val order = uinteger("order").uniqueIndex() | ||
val default = bool("default").uniqueIndex().default(false) | ||
|
||
override val primaryKey = PrimaryKey(id) | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/fardragi/nyaruko/database/tables/UserGroups.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,15 @@ | ||
package com.fardragi.nyaruko.database.tables | ||
|
||
import org.jetbrains.exposed.dao.id.IdTable | ||
|
||
object UserGroups : IdTable<UInt>("user_groups") { | ||
override val id = uinteger("id").autoIncrement().entityId() | ||
val userId = reference("user_id", Users) | ||
val groupId = reference("group_id", Groups) | ||
|
||
override val primaryKey = PrimaryKey(id) | ||
|
||
init { | ||
uniqueIndex(userId, groupId) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.fardragi.nyaruko.models | ||
|
||
import com.fardragi.nyaruko.database.tables.Groups | ||
import org.jetbrains.exposed.dao.Entity | ||
import org.jetbrains.exposed.dao.EntityClass | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
|
||
class Group(id: EntityID<UInt>) : Entity<UInt>(id) { | ||
companion object : EntityClass<UInt, Group>(Groups) | ||
|
||
var name by Groups.name | ||
var order by Groups.order | ||
var default by Groups.default | ||
} |
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,15 @@ | ||
package com.fardragi.nyaruko.models | ||
|
||
import com.fardragi.nyaruko.database.tables.UserGroups | ||
import org.jetbrains.exposed.dao.Entity | ||
import org.jetbrains.exposed.dao.EntityClass | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
|
||
class UserGroup(id: EntityID<UInt>) : Entity<UInt>(id) { | ||
companion object : EntityClass<UInt, UserGroup>(UserGroups) | ||
|
||
var userId by UserGroups.userId | ||
var groupId by UserGroups.groupId | ||
var user by User referencedOn UserGroups.userId | ||
var group by Group referencedOn UserGroups.groupId | ||
} |
20 changes: 4 additions & 16 deletions
20
src/main/kotlin/com/fardragi/nyaruko/permission/PermissionModule.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 |
---|---|---|
@@ -1,23 +1,11 @@ | ||
package com.fardragi.nyaruko.permission | ||
|
||
import com.fardragi.nyaruko.enums.PermissionLevel | ||
import com.fardragi.nyaruko.services.CommandService | ||
import com.fardragi.nyaruko.permission.handlers.RegisterPermissionsHandler | ||
import com.fardragi.nyaruko.shared.IModule | ||
import net.minecraft.command.CommandBase | ||
import net.minecraft.server.MinecraftServer | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class PermissionModule() : IModule, KoinComponent { | ||
private val commandService: CommandService by inject() | ||
|
||
class PermissionModule() : IModule { | ||
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>) | ||
RegisterPermissionsHandler().register() | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/com/fardragi/nyaruko/permission/handlers/RegisterPermissionsHandler.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,35 @@ | ||
package com.fardragi.nyaruko.permission.handlers | ||
|
||
import com.fardragi.nyaruko.enums.PermissionLevel | ||
import com.fardragi.nyaruko.services.CommandService | ||
import com.fardragi.nyaruko.services.GroupService | ||
import com.fardragi.nyaruko.shared.events.ServerStartingEvent | ||
import com.fardragi.nyaruko.shared.handlers.NyarukoHandlerBase | ||
import cpw.mods.fml.common.eventhandler.EventPriority | ||
import cpw.mods.fml.common.eventhandler.SubscribeEvent | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import net.minecraft.command.CommandBase | ||
import net.minecraft.server.MinecraftServer | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class RegisterPermissionsHandler : NyarukoHandlerBase(), KoinComponent { | ||
private val commandService: CommandService by inject() | ||
private val groupService: GroupService by inject() | ||
|
||
@SubscribeEvent(priority = EventPriority.HIGHEST) | ||
fun onServerStart(event: ServerStartingEvent) { | ||
val commands = MinecraftServer.getServer().commandManager.commands.mapValues { (_, v) -> | ||
return@mapValues if (v is CommandBase) | ||
PermissionLevel.fromLevel(v.requiredPermissionLevel) | ||
else PermissionLevel.Op | ||
} | ||
|
||
CoroutineScope(Dispatchers.Default).launch { | ||
groupService.checkDefault() | ||
commandService.registerCommands(commands as HashMap<String, PermissionLevel>) | ||
} | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/fardragi/nyaruko/services/GroupService.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,38 @@ | ||
package com.fardragi.nyaruko.services | ||
|
||
import com.fardragi.nyaruko.database.query | ||
import com.fardragi.nyaruko.database.tables.Groups | ||
import com.fardragi.nyaruko.exceptions.NotFoundException | ||
import com.fardragi.nyaruko.models.Group | ||
import com.fardragi.nyaruko.models.User | ||
import com.fardragi.nyaruko.models.UserGroup | ||
|
||
class GroupService { | ||
suspend fun checkDefault() { | ||
query { | ||
val group = Group.find { Groups.default eq true }.firstOrNull() | ||
|
||
if (group == null) { | ||
Group.new { | ||
name = "default" | ||
order = Group.all().maxByOrNull { Groups.order }?.order?.plus(1u) ?: 0u | ||
default = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
suspend fun registerUserInDefaultGroup(user: User) { | ||
query { | ||
val group = Group.find { Groups.default eq true }.firstOrNull() ?: throw NotFoundException( | ||
Group::class.simpleName, | ||
"default" | ||
) | ||
|
||
UserGroup.new { | ||
userId = user.id | ||
groupId = group.id | ||
} | ||
} | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/fardragi/nyaruko/shared/events/ServerStartingEvent.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,3 @@ | ||
package com.fardragi.nyaruko.shared.events | ||
|
||
class ServerStartingEvent() : NyarukoEvent() |