Skip to content

Commit

Permalink
Add functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlgo committed Nov 26, 2023
1 parent ea2202d commit 95280e0
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 3 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,6 +4,8 @@ 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.events.creator.EventManagerCreator
import dev.mikchan.mcnp.motd.events.creator.IEventManagerCreator
import dev.mikchan.mcnp.motd.image.creator.IImageManagerCreator
import dev.mikchan.mcnp.motd.image.creator.ImageManagerCreator
import dev.mikchan.mcnp.motd.motd.creator.IMOTDManagerCreator
Expand All @@ -14,4 +16,5 @@ object Creators {
var commandManager: ICommandManagerCreator = ACFCommandManagerCreator()
var imageManager: IImageManagerCreator = ImageManagerCreator()
var motdManager: IMOTDManagerCreator = MOTDManagerCreator()
var eventManager: IEventManagerCreator = EventManagerCreator()
}
5 changes: 5 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.events.manager.IEventManager
import dev.mikchan.mcnp.motd.image.manager.IImageManager
import dev.mikchan.mcnp.motd.motd.manager.IMOTDManager
import org.bstats.bukkit.Metrics
Expand All @@ -18,15 +19,19 @@ class MOTDPlugin : JavaPlugin() {
val motdManager: IMOTDManager by lazy { Creators.motdManager.build(this) }

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

override fun onEnable() {
commandManager.registerAll()
eventManager.registerAll()

imageManager.preload()

Metrics(this, BSTATS_ID)
}

override fun onDisable() {
commandManager.unregisterAll()
eventManager.unregisterAll()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dev.mikchan.mcnp.motd.commands.manager.acf.commands.AdminCommand
internal class ACFCommandManager(private val plugin: MOTDPlugin) : ICommandManager {
private val manager by lazy { PaperCommandManager(plugin) }
override fun registerAll() {
unregisterAll()
manager.registerCommand(AdminCommand(plugin))
}

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

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.events.manager.EventManager
import dev.mikchan.mcnp.motd.events.manager.IEventManager

internal class EventManagerCreator : IEventManagerCreator {
override fun build(plugin: MOTDPlugin): IEventManager {
return EventManager(plugin)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.mikchan.mcnp.motd.events.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.events.manager.IEventManager

interface IEventManagerCreator {
fun build(plugin: MOTDPlugin): IEventManager
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.mikchan.mcnp.motd.events.listeners

import dev.mikchan.mcnp.motd.MOTDPlugin
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.server.ServerListPingEvent

internal class MOTDListener(private val plugin: MOTDPlugin) : Listener {
@EventHandler(ignoreCancelled = true)
fun onServerListPingEvent(event: ServerListPingEvent) {
if (!plugin.config.enabled) return
val motd = plugin.motdManager.getRandom() ?: return

if (motd.firstLine != null || motd.secondLine != "") {
val firstLine = motd.firstLine ?: ""
val secondLine = motd.secondLine ?: ""
event.motd = "${firstLine}\n${secondLine}"
}

val image = motd.image?.let {
if (plugin.config.randomImages) {
plugin.imageManager.getRandom()
} else {
null
}
}

if (image != null) {
event.setServerIcon(image)
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/events/manager/EventManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.mikchan.mcnp.motd.events.manager

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.events.listeners.MOTDListener
import org.bukkit.event.HandlerList

internal class EventManager(private val plugin: MOTDPlugin) : IEventManager {
override fun registerAll() {
unregisterAll()
plugin.server.pluginManager.registerEvents(MOTDListener(plugin), plugin)
}

override fun unregisterAll() {
HandlerList.unregisterAll(plugin)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.mikchan.mcnp.motd.events.manager

interface IEventManager {
fun registerAll()
fun unregisterAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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
import dev.mikchan.mcnp.motd.image.manager.ImageManager

internal class ImageManagerCreator : IImageManagerCreator {
override fun build(plugin: MOTDPlugin): IImageManager {
return ImagePool(plugin)
return ImageManager(plugin)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ interface IImageManager {

fun preload()
fun reload()
fun unload()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.bukkit.util.CachedServerIcon
import java.io.File
import kotlin.io.path.useDirectoryEntries

internal class ImagePool(private val plugin: MOTDPlugin) : IImageManager {
internal class ImageManager(private val plugin: MOTDPlugin) : IImageManager {
private val iconFolder
get() = run {
val res = File(plugin.dataFolder, "icons")
Expand Down Expand Up @@ -45,4 +45,8 @@ internal class ImagePool(private val plugin: MOTDPlugin) : IImageManager {
override fun reload() {
preload()
}

override fun unload() {
cache = mapOf()
}
}

0 comments on commit 95280e0

Please sign in to comment.