Skip to content

Commit

Permalink
chore: fix some stuff with mongo sometimes lagging out
Browse files Browse the repository at this point in the history
  • Loading branch information
98ping committed Jul 18, 2023
1 parent daca235 commit 16ff73e
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ltd.matrixstudios.alchemist.models.expirables

import java.util.*

class Expirable(
var expired: Boolean,
var addedAt: Long,
var duration: Long,

var removedAt: Long
) {
fun isActive() : Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class Grantable<T>(
var reason: String,
var expirable: Expirable,
var removedBy: UUID?,
var removedReason: String?
var removedReason: String?,
){

abstract fun getGrantable(): T?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class Punishment(
addedBy,
addedReason,
Expirable(false, System.currentTimeMillis(), duration, 0L),
null,
null
null, null
) {

var punishmentType: String = punishmentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ data class RankGrant(
addedBy,
addedReason,
Expirable(false, System.currentTimeMillis(), duration, 0L),
null,
null
null, null
) {

var internalActor: DefaultActor = actor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class TagGrant(
addedBy,
addedReason,
Expirable(false, System.currentTimeMillis(), duration, 0L),
null,
null
null, null
) {

var internalActor: DefaultActor = actor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object TagGrantService : ExpiringService<TagGrant>() {
var handler = Alchemist.dataHandler.createStoreType<UUID, TagGrant>(DataStoreType.MONGO)


fun getValues() : CompletableFuture<Collection<TagGrant>> {
fun getValues(): CompletableFuture<Collection<TagGrant>> {
return handler.retrieveAllAsync()
}

Expand All @@ -24,14 +24,7 @@ object TagGrantService : ExpiringService<TagGrant>() {
}

override fun clearOutModels() {
getValues().get().forEach {
if (!it.expirable.isActive() && it.removedBy == null) {
it.removedBy = UUID.fromString("00000000-0000-0000-0000-000000000000")
it.removedReason = "Expired"

save(it)
}
}
getValues().get().forEach { it.expirable.isActive() }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class AlchemistSpigotPlugin : JavaPlugin() {
val connectionPool = URIMongoConnectionPool().apply {
this.databaseName = config.getString("mongo.database")
this.uri = uri

}

Alchemist.start(connectionPool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object AlchemistAPI {
val profile = quickFindProfile(uuid).get()

if (profile != null) {
finalString = profile.getCurrentRank()!!.color + profile.username
finalString = (profile.getCurrentRank()?.color ?: "&f") + profile.username
}

return finalString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GeneralPunishmentMenu(var profile: GameProfile, var punishmentType: Punish
val buttons = hashMapOf<Int, Button>()

var index = 0
for (punishment in punishments) {
for (punishment in punishments.filter { it.getGrantable() == punishmentType }) {
buttons[index++] = GeneralPunishmentButton(punishment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ object BukkitProfileAdaptation

fun handlePunishmentsUsingEvent(profileId: UUID, event: AsyncPlayerPreLoginEvent) {
val profile = AlchemistAPI.syncFindProfile(profileId) ?: return
PunishmentService.recalculateUUID(profileId)

if (profile.hasActivePunishment(PunishmentType.BAN) || profile.hasActivePunishment(PunishmentType.BLACKLIST)) {
val option = profile.hasActivePunishment(PunishmentType.BAN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ object BukkitPunishmentFunctions {
}

fun dispatch(punishment: Punishment, silent: Boolean) {
AsynchronousRedisSender.send(PunishmentDispatchPacket(punishment.getGrantable(), punishment.target, punishment.executor, silent, punishment.reason))
AsynchronousRedisSender.send(PunishmentExecutePacket(punishment.getGrantable(), punishment.target, punishment.reason))
AsynchronousRedisSender.send(UpdatePunishmentsRequest(punishment.target))

PunishmentService.save(punishment)
PunishmentNotification(punishment).send()

PunishmentService.save(punishment)
AsynchronousRedisSender.send(PunishmentDispatchPacket(punishment.getGrantable(), punishment.target, punishment.executor, silent, punishment.reason))
AsynchronousRedisSender.send(PunishmentExecutePacket(punishment.getGrantable(), punishment.target, punishment.reason, punishment))
AsynchronousRedisSender.send(UpdatePunishmentsRequest(punishment.target))
}

fun dispatchKick(punishment: Punishment, silent: Boolean) {
AsynchronousRedisSender.send(PunishmentDispatchPacket(punishment.getGrantable(), punishment.target, punishment.executor, silent, punishment.reason))
AsynchronousRedisSender.send(PunishmentExecutePacket(punishment.getGrantable(), punishment.target, punishment.reason))
AsynchronousRedisSender.send(PunishmentExecutePacket(punishment.getGrantable(), punishment.target, punishment.reason, punishment))
}

fun isSilent(reason: String) : Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ltd.matrixstudios.alchemist.punishment.packets
package ltd.matrixstudios.alchemist.punishment.packets

import ltd.matrixstudios.alchemist.AlchemistSpigotPlugin
import ltd.matrixstudios.alchemist.api.AlchemistAPI
import ltd.matrixstudios.alchemist.models.grant.types.Punishment
import ltd.matrixstudios.alchemist.punishments.PunishmentType
import ltd.matrixstudios.alchemist.redis.RedisPacket
import ltd.matrixstudios.alchemist.util.Chat
Expand All @@ -13,7 +14,8 @@ import java.util.*
class PunishmentExecutePacket(
var punishmentType: PunishmentType,
var target: UUID,
var reason: String
var reason: String,
val punishment: Punishment
) : RedisPacket("punishment-execute") {

override fun action() {
Expand All @@ -40,30 +42,23 @@ class PunishmentExecutePacket(

if (punishmentType == PunishmentType.BAN) {

val profile = AlchemistAPI.syncFindProfile(target) ?: return
val punishment = profile.getActivePunishments(PunishmentType.BAN).firstOrNull()
val msgs = AlchemistSpigotPlugin.instance.config.getStringList("banned-join")

msgs.replaceAll { it.replace("<reason>", punishment!!.reason) }
msgs.replaceAll { it.replace("<reason>", punishment?.reason) }
msgs.replaceAll {
it.replace(
"<expires>",
if (punishment!!.expirable.duration == Long.MAX_VALUE) "Never" else TimeUtil.formatDuration(
if (punishment.expirable.duration == Long.MAX_VALUE) "Never" else TimeUtil.formatDuration(
punishment.expirable.addedAt + punishment.expirable.duration - System.currentTimeMillis()
)
)
}

player.kickPlayer(msgs.map { Chat.format(it) }.joinToString("\n"))
} else if (punishmentType == PunishmentType.BLACKLIST) {
val profile = AlchemistAPI.syncFindProfile(target) ?: return
val punishments = profile.getActivePunishments(PunishmentType.BLACKLIST).toMutableList()
punishments.addAll(profile.getActivePunishments(PunishmentType.BAN))

val punishment = profile.getActivePunishments(PunishmentType.BLACKLIST).firstOrNull()
val msgs = AlchemistSpigotPlugin.instance.config.getStringList("blacklisted-join")

msgs.replaceAll { it.replace("<reason>", punishment!!.reason) }
msgs.replaceAll { it.replace("<reason>", punishment.reason) }
msgs.replaceAll {
it.replace(
"<expires>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ import java.util.*
object ClearOutExpirablesTask : BukkitRunnable() {

override fun run() {
RankGrantService.getValues().thenApply { rankGrants ->
rankGrants.forEach {
if (!it.expirable.isActive() && it.removedBy == null) {
it.removedBy = UUID.fromString("00000000-0000-0000-0000-000000000000")
it.removedReason = "Expired"
val rankGrants = RankGrantService.handler.retrieveAll()
rankGrants.forEach {
if (!it.expirable.isActive() && it.removedBy == null) {
it.removedBy = UUID.fromString("00000000-0000-0000-0000-000000000000")
it.removedReason = "Expired"

AsynchronousRedisSender.send(PermissionUpdatePacket(it.target))
AsynchronousRedisSender.send(PermissionUpdatePacket(it.target))

RankGrantService.save(it)
}
RankGrantService.save(it)
}
}

PunishmentService.getValues().thenApply { punishments ->
punishments.forEach {
if (!it.expirable.isActive() && it.removedBy == null) {
it.removedBy = UUID.fromString("00000000-0000-0000-0000-000000000000")
it.removedReason = "Expired"

PunishmentService.save(it)
}
}
val punishments = PunishmentService.handler.retrieveAll()
punishments.forEach {
if (!it.expirable.isActive() && it.removedBy == null) {
it.removedBy = UUID.fromString("00000000-0000-0000-0000-000000000000")
it.removedReason = "Expired"
PunishmentService.save(it)
}
}
}
}
}

0 comments on commit 16ff73e

Please sign in to comment.