Skip to content

Commit

Permalink
Fix sonarqube issues (#16)
Browse files Browse the repository at this point in the history
* fix: reduce complexity

* fix: split registermanager

---------

Co-authored-by: Liam Sage <[email protected]>
  • Loading branch information
InvalidJoker and CoasterFreakDE authored Apr 23, 2024
1 parent 2dee8b3 commit 4053d5f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ class BuildEvents: Listener {

@EventHandler
fun onLiquidGet(event: PlayerBucketFillEvent) {
if (!event.player.canBuild) {
if (event.block.type == Material.WATER) {
Bukkit.getScheduler().runTaskLater(Plugin.instance, Runnable {
event.block.type = Material.WATER
}, 1)
}
if (!event.player.canBuild && event.block.type == Material.WATER) {
Bukkit.getScheduler().runTaskLater(Plugin.instance, Runnable {
event.block.type = Material.WATER
}, 1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@ class ChatEvent: Listener {

@EventHandler
fun onChat(event: AsyncChatEvent): Unit = with(event) {
val player = player

val placeHolderText = chatFormat
val messagePlain = message().asPlainString

val format =
text(parsePlaceholders(placeHolderText, player)).append(text(parsePlaceholders(messagePlain, player)))

event.renderer { _, _, _, _ ->
return@renderer format
return@renderer text(parsePlaceholders(chatFormat, player)).append(text(parsePlaceholders(message().asPlainString, player)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package net.blockventuremc.modules.general.model

enum class Ranks(val color: String) {
Staff("#FF0000"),
Plus("#FF0000"),
Default("#FF0000");
Plus("#71368a"),
Default("#2ecc71");

fun isHigherOrEqual(rank: Ranks): Boolean {
return this.ordinal <= rank.ordinal
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/net/blockventuremc/utils/ItemBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni
return this
}

@FunctionalInterface
fun interface Performer<T> {
fun perform(itemBuilder: T): T
}
Expand Down Expand Up @@ -189,7 +188,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni

fun lore(vararg lores: String): ItemBuilder {
val meta = itemStack.itemMeta
var lore = listOf<Component>()
val lore = mutableListOf<Component>()

lores.forEach {
val lines = it.split("\n")
Expand Down Expand Up @@ -338,7 +337,7 @@ class ItemBuilder(material: Material, count: Int = 1, dsl: ItemBuilder.() -> Uni
)

fun fromItemStack(itemStack: ItemStack): ItemBuilder {
var mat = if (ItemBuilder.invalidMaterials.contains(itemStack.type)) Material.GRASS_BLOCK else itemStack.type
var mat = if (invalidMaterials.contains(itemStack.type)) Material.GRASS_BLOCK else itemStack.type
val builder = ItemBuilder(mat)
builder.itemStack = itemStack
return builder
Expand Down
60 changes: 34 additions & 26 deletions src/main/kotlin/net/blockventuremc/utils/RegisterManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,7 @@ import org.reflections8.Reflections
import kotlin.time.measureTime

object RegisterManager {
fun registerAll() {
val reflections = Reflections("net.blockventuremc.modules")

val timeListeners = measureTime {
for (clazz in reflections.getSubTypesOf(Listener::class.java)) {
try {
val constructor = clazz.declaredConstructors.find { it.parameterCount == 0 } ?: continue

if (clazz.`package`.name.contains("conversations")) continue

constructor.isAccessible = true

val event = constructor.newInstance() as Listener

Bukkit.getPluginManager().registerEvents(event, Plugin.instance)
Bukkit.getConsoleSender()
.sendMessage("Listener ${event.javaClass.simpleName} registered")
} catch (exception: InstantiationError) {
exception.printStackTrace()
} catch (exception: IllegalAccessException) {
exception.printStackTrace()
}
}
}
println("Registered listeners in $timeListeners")

private fun registerCommands(reflections: Reflections) {

val timeCommands = measureTime {
for (clazz in reflections.getTypesAnnotatedWith(BlockCommand::class.java)) {
Expand Down Expand Up @@ -79,4 +54,37 @@ object RegisterManager {
}
println("Registered commands in $timeCommands")
}

private fun registerListeners(reflections: Reflections) {
val timeListeners = measureTime {
for (clazz in reflections.getSubTypesOf(Listener::class.java)) {
try {
val constructor = clazz.declaredConstructors.find { it.parameterCount == 0 } ?: continue

if (clazz.`package`.name.contains("conversations")) continue

constructor.isAccessible = true

val event = constructor.newInstance() as Listener

Bukkit.getPluginManager().registerEvents(event, Plugin.instance)
Bukkit.getConsoleSender()
.sendMessage("Listener ${event.javaClass.simpleName} registered")
} catch (exception: InstantiationError) {
exception.printStackTrace()
} catch (exception: IllegalAccessException) {
exception.printStackTrace()
}
}
}
println("Registered listeners in $timeListeners")
}
fun registerAll() {
val reflections = Reflections("net.blockventuremc.modules")

registerListeners(reflections)

registerCommands(reflections)

}
}

0 comments on commit 4053d5f

Please sign in to comment.