-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/dev/mikchan/mcnp/motd/imagePool/creator/IImagePoolCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/mikchan/mcnp/motd/imagePool/creator/ImagePoolCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/main/java/dev/mikchan/mcnp/motd/imagePool/pool/IImagePool.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/main/java/dev/mikchan/mcnp/motd/imagePool/pool/ImagePool.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |