Skip to content

Commit

Permalink
AutoBan 1.0.16 with NetworkManager 2.15.0 compatability.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChimpGamer committed Apr 20, 2024
1 parent 987d817 commit bae39ce
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.7.10"
kotlin("jvm") version "1.9.23"
id("com.github.johnrengelman.shadow") version "7.1.2"
}

Expand All @@ -14,12 +14,12 @@ repositories {

dependencies {
compileOnly(kotlin("stdlib-jdk8"))
compileOnly("com.github.Carleslc:Simple-YAML:1.7.2")
compileOnly("nl.chimpgamer.networkmanager:api:2.12.0")
compileOnly("dev.dejvokep:boosted-yaml:1.3.4")
compileOnly("nl.chimpgamer.networkmanager:api:2.15.0-SNAPSHOT")
}

group = "nl.chimpgamer.networkmanager.extensions"
version = "1.0.15"
version = "1.0.16"
description = "AutoBan"

tasks {
Expand All @@ -42,7 +42,7 @@ tasks {

//relocate("net.kyori", "$shadedPackage.kyori")
relocate("kotlin", "$libPackage.kotlin")
relocate("org.simpleyaml", "$libPackage.simpleyaml")
relocate("dev.dejvokep.boostedyaml", "$libPackage.boostedyaml")
}

build {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package nl.chimpgamer.networkmanager.extensions.autoban

import nl.chimpgamer.networkmanager.api.event.events.PunishmentEvent
import nl.chimpgamer.networkmanager.api.extensions.NMExtension
import nl.chimpgamer.networkmanager.api.utils.PlatformType
import nl.chimpgamer.networkmanager.extensions.autoban.configuration.Settings
import nl.chimpgamer.networkmanager.extensions.autoban.listeners.PunishmentListener

class AutoBan : NMExtension() {
val settings = Settings(this)
lateinit var punishmentListener: PunishmentListener
private val punishmentListener = PunishmentListener(this)

override fun onEnable() {
if (!networkManager.platformType.isProxy) {
logger.severe("Hey, this NetworkManager extension is for BungeeCord and Velocity only!")
return
}
settings.load()
punishmentListener = PunishmentListener(this)
eventBus.subscribe(PunishmentEvent::class.java, punishmentListener::onPunishment)
punishmentListener.register()
}

override fun onDisable() {
//eventBus.unsubscribe(punishmentListener::onPunishment)
punishmentListener.unregister()
}

override fun onConfigsReload() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,70 @@
package nl.chimpgamer.networkmanager.extensions.autoban.configuration

import dev.dejvokep.boostedyaml.YamlDocument
import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings
import dev.dejvokep.boostedyaml.settings.general.GeneralSettings
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings
import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings
import nl.chimpgamer.networkmanager.api.models.punishments.Punishment
import nl.chimpgamer.networkmanager.api.utils.FileUtils
import nl.chimpgamer.networkmanager.api.utils.TimeUtils
import nl.chimpgamer.networkmanager.api.values.Message
import nl.chimpgamer.networkmanager.extensions.autoban.AutoBan
import nl.chimpgamer.networkmanager.extensions.autoban.models.PunishmentAction
import java.io.IOException
import java.util.logging.Level

class Settings(private val autoBan: AutoBan) : FileUtils(autoBan.dataFolder.path, "settings.yml") {
class Settings(private val autoBan: AutoBan) {
private val config: YamlDocument
val punishmentActions = HashSet<PunishmentAction>()

init {
val file = autoBan.dataFolder.resolve("settings.yml")
val inputStream = autoBan.getResource("settings.yml")
val generalSettings = GeneralSettings.builder().setUseDefaults(false).build()
val loaderSettings = LoaderSettings.builder().setAutoUpdate(false).build()
config = if (inputStream != null) {
YamlDocument.create(file, inputStream, generalSettings, loaderSettings, DumperSettings.DEFAULT, UpdaterSettings.DEFAULT)
} else {
YamlDocument.create(file, generalSettings, loaderSettings, DumperSettings.DEFAULT, UpdaterSettings.DEFAULT)
}
}

fun load() {
for (onActionTypeStr in config.getConfigurationSection("actions").getKeys(false)) {
val actionsSection = config.getSection("actions")
for (onActionTypeStr in actionsSection.getRoutesAsStrings(false)) {
val onActionType = try {
Punishment.Type.valueOf(onActionTypeStr)
} catch (ex: IllegalArgumentException) {
autoBan.logger.warning(ex.message)
autoBan.logger.warning("Action $onActionTypeStr has invalid onActionType: $onActionTypeStr")
autoBan.logger.log(Level.WARNING, "Action $onActionTypeStr has invalid onActionType: $onActionTypeStr", ex)
continue
}
for (countKey in config.getConfigurationSection("actions.$onActionTypeStr").getKeys(false)) {
val actionSection = actionsSection.getSection(onActionTypeStr)
for (countKey in actionSection.getRoutesAsStrings(false)) {
val count = try {
countKey.toInt()
} catch (ex: NumberFormatException) {
autoBan.logger.warning("Action $onActionTypeStr has invalid count: $countKey")
continue
}
val action = this.getString("actions.$onActionTypeStr.$countKey.action")
val action = actionSection.getString("$countKey.action")
if (action == null) {
autoBan.logger.warning("Action $onActionTypeStr.$countKey has no action.")
continue
}
val punishmentType = try {
Punishment.Type.valueOf(action)
} catch (ex: IllegalArgumentException) {
autoBan.logger.warning(ex.message)
autoBan.logger.warning("Action $onActionTypeStr.$countKey has invalid actionType: $action")
autoBan.logger.log(Level.WARNING, "Action $onActionTypeStr.$countKey has invalid actionType: $action", ex)
continue
}
var duration = -1L
val durationStr = this.getString("actions.$onActionTypeStr.$countKey.duration")
val reason = this.getString("actions.$onActionTypeStr.$countKey.reason", autoBan.networkManager.getMessage(Message.PUNISHMENT_NO_REASON))
val durationStr = actionSection.getString("$countKey.duration")
val reason = actionSection.getString("$countKey.reason", autoBan.networkManager.getMessage(Message.PUNISHMENT_NO_REASON))
if (durationStr != null && punishmentType.isTemp) {
try {
duration = TimeUtils.toMilliSec(durationStr)
} catch (ex: IllegalArgumentException) {
autoBan.logger.warning(ex.message)
autoBan.logger.warning("Action $onActionTypeStr.$countKey has invalid duration: $duration")
autoBan.logger.log(Level.WARNING, "Action $onActionTypeStr.$countKey has invalid duration: $duration", ex)
continue
}
}
Expand All @@ -54,13 +73,13 @@ class Settings(private val autoBan: AutoBan) : FileUtils(autoBan.dataFolder.path
}
}

override fun reload() {
super.reload()
punishmentActions.clear()
load()
}

init {
setupFile(autoBan.getResource("settings.yml"))
fun reload() {
try {
config.reload()
punishmentActions.clear()
load()
} catch (ex: IOException) {
autoBan.logger.log(Level.SEVERE, "Something went wrong trying to reload the settings.yml configuration file", ex)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package nl.chimpgamer.networkmanager.extensions.autoban.listeners

import nl.chimpgamer.networkmanager.api.event.EventSubscription
import nl.chimpgamer.networkmanager.api.event.events.PunishmentEvent
import nl.chimpgamer.networkmanager.extensions.autoban.AutoBan

class PunishmentListener(private val autoBan: AutoBan) {
private lateinit var subscription: EventSubscription<PunishmentEvent>

fun onPunishment(event: PunishmentEvent) {
private fun onPunishment(event: PunishmentEvent) {
val cachedPlayers = autoBan.networkManager.cacheManager.cachedPlayers
val cachedPunishments = autoBan.networkManager.cacheManager.cachedPunishments
val punishment = event.punishment
Expand All @@ -29,13 +31,19 @@ class PunishmentListener(private val autoBan: AutoBan) {
.reason(punishmentAction.reason
.replace("%count%", total.toString()))
.build()
if (newPunishment != null) {
autoBan.logger.info("${player.name} received a ${punishmentAction.actionType.name} by AutoBan. ${javaClass.name}")
cachedPunishments.executePunishment(newPunishment)
}
autoBan.logger.info("${player.name} received a ${punishmentAction.actionType.name} by AutoBan. ${javaClass.name}")
cachedPunishments.executePunishment(newPunishment)
break
}
}
}
}

fun register() {
subscription = autoBan.eventBus.subscribe(PunishmentEvent::class.java, ::onPunishment)
}

fun unregister() {
autoBan.eventBus.unsubscribe(subscription)
}
}
2 changes: 1 addition & 1 deletion src/main/resources/extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ version: ${version}
main: nl.chimpgamer.networkmanager.extensions.autoban.AutoBan
description: NetworkManager AutoBan Extension
authors: [ChimpGamer]
api-version: 2.12.0
api-version: 2.15.0

0 comments on commit bae39ce

Please sign in to comment.