Skip to content

Commit

Permalink
Add image pool
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlgo committed Nov 26, 2023
1 parent 07ba6e1 commit 0a76c34
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/Creators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ 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.imagePool.creator.IImagePoolCreator
import dev.mikchan.mcnp.motd.imagePool.creator.ImagePoolCreator

object Creators {
var config: IConfigCreator = BoostedYamlConfigCreator()
var commandManager: ICommandManagerCreator = ACFCommandManagerCreator()
var imagePool: IImagePoolCreator = ImagePoolCreator()
}
4 changes: 4 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/MOTDPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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.imagePool.pool.IImagePool
import org.bstats.bukkit.Metrics
import org.bukkit.plugin.java.JavaPlugin

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

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

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

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

Metrics(this, BSTATS_ID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal class AdminCommand(private val plugin: MOTDPlugin) : BaseCommand() {
@Description("Reloads config")
fun onReload(sender: CommandSender) {
if (plugin.config.reload()) {
plugin.imagePool.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.imagePool.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.imagePool.pool.IImagePool

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

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.imagePool.pool.IImagePool
import dev.mikchan.mcnp.motd.imagePool.pool.ImagePool

internal class ImagePoolCreator : IImagePoolCreator {
override fun build(plugin: MOTDPlugin): IImagePool {
return ImagePool(plugin)
}
}
11 changes: 11 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/imagePool/pool/IImagePool.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.mikchan.mcnp.motd.imagePool.pool

import org.bukkit.util.CachedServerIcon

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

fun preload()
fun reload()
}
46 changes: 46 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/imagePool/pool/ImagePool.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.mikchan.mcnp.motd.imagePool.pool

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 {
private val iconFolder
get() = run {
val res = File(plugin.dataFolder, "images")
if (!res.exists()) {
res.mkdir()
}
res.toPath()
}

private var cache = mapOf<String, CachedServerIcon>()

override fun get(name: String): CachedServerIcon? {
return cache["${name}.png"]
}

override fun getRandom(): CachedServerIcon? {
val cache = this.cache
if (cache.isEmpty()) return null
return cache.values.random()
}

override fun preload() {
cache = iconFolder.useDirectoryEntries("*.png") { entries ->
entries.map { path ->
path.fileName.toString() to try {
plugin.server.loadServerIcon(path.toFile())
} catch (_: Exception) {
null
}
}.toMap().filter { it.value != null }.mapValues { it as CachedServerIcon }
}
}


override fun reload() {
preload()
}
}

0 comments on commit 0a76c34

Please sign in to comment.