Skip to content

Commit

Permalink
Add MOTD manager
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlgo committed Nov 26, 2023
1 parent ce1cb9b commit 3474e12
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 30 deletions.
9 changes: 6 additions & 3 deletions src/main/java/dev/mikchan/mcnp/motd/Creators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import dev.mikchan.mcnp.motd.commands.creator.ICommandManagerCreator
import dev.mikchan.mcnp.motd.commands.creator.acf.ACFCommandManagerCreator
import dev.mikchan.mcnp.motd.config.creator.IConfigCreator
import dev.mikchan.mcnp.motd.config.creator.boostedYaml.BoostedYamlConfigCreator
import dev.mikchan.mcnp.motd.image.creator.IImagePoolCreator
import dev.mikchan.mcnp.motd.image.creator.ImagePoolCreator
import dev.mikchan.mcnp.motd.image.creator.IImageManagerCreator
import dev.mikchan.mcnp.motd.image.creator.ImageManagerCreator
import dev.mikchan.mcnp.motd.motd.creator.IMOTDManagerCreator
import dev.mikchan.mcnp.motd.motd.creator.MOTDManagerCreator

object Creators {
var config: IConfigCreator = BoostedYamlConfigCreator()
var commandManager: ICommandManagerCreator = ACFCommandManagerCreator()
var imagePool: IImagePoolCreator = ImagePoolCreator()
var imageManager: IImageManagerCreator = ImageManagerCreator()
var motdManager: IMOTDManagerCreator = MOTDManagerCreator()
}
8 changes: 5 additions & 3 deletions src/main/java/dev/mikchan/mcnp/motd/MOTDPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package dev.mikchan.mcnp.motd

import dev.mikchan.mcnp.motd.commands.manager.ICommandManager
import dev.mikchan.mcnp.motd.config.config.IConfig
import dev.mikchan.mcnp.motd.image.pool.IImagePool
import dev.mikchan.mcnp.motd.image.manager.IImageManager
import dev.mikchan.mcnp.motd.motd.manager.IMOTDManager
import org.bstats.bukkit.Metrics
import org.bukkit.plugin.java.JavaPlugin

Expand All @@ -13,13 +14,14 @@ class MOTDPlugin : JavaPlugin() {
}

val config: IConfig by lazy { Creators.config.build(this) }
val imagePool: IImagePool by lazy { Creators.imagePool.build(this) }
val imageManager: IImageManager by lazy { Creators.imageManager.build(this) }
val motdManager: IMOTDManager by lazy { Creators.motdManager.build(this) }

private val commandManager: ICommandManager by lazy { Creators.commandManager.build(this) }

override fun onEnable() {
commandManager.registerAll()
imagePool.preload()
imageManager.preload()

Metrics(this, BSTATS_ID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal class AdminCommand(private val plugin: MOTDPlugin) : BaseCommand() {
@Description("Reloads config")
fun onReload(sender: CommandSender) {
if (plugin.config.reload()) {
plugin.imagePool.reload()
plugin.imageManager.reload()
sender.sendMessage("${ChatColor.GREEN}Config is successfully reloaded!")
} else {
sender.sendMessage("${ChatColor.RED}Unable to reload config!")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.mikchan.mcnp.motd.image.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.image.manager.IImageManager

interface IImageManagerCreator {
fun build(plugin: MOTDPlugin): IImageManager
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.mikchan.mcnp.motd.image.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.image.manager.IImageManager
import dev.mikchan.mcnp.motd.image.manager.ImagePool

internal class ImageManagerCreator : IImageManagerCreator {
override fun build(plugin: MOTDPlugin): IImageManager {
return ImagePool(plugin)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.mikchan.mcnp.motd.image.pool
package dev.mikchan.mcnp.motd.image.manager

import org.bukkit.util.CachedServerIcon

interface IImagePool {
interface IImageManager {
fun get(name: String): CachedServerIcon?
fun getRandom(): CachedServerIcon?

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.mikchan.mcnp.motd.image.pool
package dev.mikchan.mcnp.motd.image.manager

import dev.mikchan.mcnp.motd.MOTDPlugin
import org.bukkit.util.CachedServerIcon
import java.io.File
import kotlin.io.path.useDirectoryEntries

internal class ImagePool(private val plugin: MOTDPlugin) : IImagePool {
internal class ImagePool(private val plugin: MOTDPlugin) : IImageManager {
private val iconFolder
get() = run {
val res = File(plugin.dataFolder, "images")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.mikchan.mcnp.motd.motd.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.motd.manager.IMOTDManager

interface IMOTDManagerCreator {
fun build(plugin: MOTDPlugin): IMOTDManager
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.mikchan.mcnp.motd.motd.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.motd.manager.MOTDManager
import dev.mikchan.mcnp.motd.motd.manager.IMOTDManager

internal class MOTDManagerCreator : IMOTDManagerCreator {
override fun build(plugin: MOTDPlugin): IMOTDManager {
return MOTDManager(plugin)
}
}
10 changes: 10 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/motd/manager/IMOTD.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.mikchan.mcnp.motd.motd.manager

import org.bukkit.util.CachedServerIcon

@Suppress("SpellCheckingInspection")
interface IMOTD {
val firstLine: String?
val secondLine: String?
val image: CachedServerIcon?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.mikchan.mcnp.motd.motd.manager

interface IMOTDManager {
fun getRandom(): IMOTD?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.mikchan.mcnp.motd.motd.manager

import dev.mikchan.mcnp.motd.MOTDPlugin

internal class MOTDManager(private val plugin: MOTDPlugin) : IMOTDManager {
override fun getRandom(): IMOTD? {
return plugin.config.motd.ifEmpty { null }?.random()?.let { MOTDProxy(it, plugin.imageManager) }
}
}
17 changes: 17 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/motd/manager/MOTDProxy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.mikchan.mcnp.motd.motd.manager

import dev.mikchan.mcnp.motd.config.config.IMOTDConfig
import dev.mikchan.mcnp.motd.image.manager.IImageManager
import org.bukkit.util.CachedServerIcon

internal class MOTDProxy(
private val config: IMOTDConfig,
private val imageManager: IImageManager,
) : IMOTD {
override val firstLine: String?
get() = config.firstLine
override val secondLine: String?
get() = config.secondLine
override val image: CachedServerIcon?
get() = config.image?.let { imageManager.get(it) }
}

0 comments on commit 3474e12

Please sign in to comment.