Skip to content

Commit

Permalink
Add formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlgo committed Nov 26, 2023
1 parent 95280e0 commit bb0fa5e
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 1 deletion.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ repositories {
name = "aikar-repo"
url = "https://repo.aikar.co/content/groups/aikar/"
}
maven {
name = "placeholderapi"
url = "https://repo.extendedclip.com/content/repositories/placeholderapi/"
}
}

dependencies {
compileOnly "org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.21"
compileOnly "me.clip:placeholderapi:2.11.5"

implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.21"
implementation "dev.dejvokep:boosted-yaml-spigot:1.4"

shadow "org.bstats:bstats-bukkit:3.0.1"
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/Creators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ 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.formatter.creator.FormatterCreator
import dev.mikchan.mcnp.motd.formatter.creator.IFormatterCreator
import dev.mikchan.mcnp.motd.formatter.creator.papi.PapiFormatterCreator
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 @@ -17,4 +20,5 @@ object Creators {
var imageManager: IImageManagerCreator = ImageManagerCreator()
var motdManager: IMOTDManagerCreator = MOTDManagerCreator()
var eventManager: IEventManagerCreator = EventManagerCreator()
var formatter: IFormatterCreator = PapiFormatterCreator(FormatterCreator())
}
2 changes: 2 additions & 0 deletions src/main/java/dev/mikchan/mcnp/motd/MOTDPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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.formatter.formatter.IFormatter
import dev.mikchan.mcnp.motd.image.manager.IImageManager
import dev.mikchan.mcnp.motd.motd.manager.IMOTDManager
import org.bstats.bukkit.Metrics
Expand All @@ -17,6 +18,7 @@ class MOTDPlugin : JavaPlugin() {
val config: IConfig by lazy { Creators.config.build(this) }
val imageManager: IImageManager by lazy { Creators.imageManager.build(this) }
val motdManager: IMOTDManager by lazy { Creators.motdManager.build(this) }
val formatter: IFormatter by lazy { Creators.formatter.build(this) }

private val commandManager: ICommandManager by lazy { Creators.commandManager.build(this) }
private val eventManager: IEventManager by lazy { Creators.eventManager.build(this) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.mikchan.mcnp.motd.formatter.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.formatter.formatter.Formatter
import dev.mikchan.mcnp.motd.formatter.formatter.IFormatter

internal class FormatterCreator : IFormatterCreator {
override fun build(plugin: MOTDPlugin): IFormatter {
return Formatter()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.mikchan.mcnp.motd.formatter.creator

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.formatter.formatter.IFormatter

interface IFormatterCreator {
fun build(plugin: MOTDPlugin): IFormatter
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.mikchan.mcnp.motd.formatter.creator.papi

import dev.mikchan.mcnp.motd.MOTDPlugin
import dev.mikchan.mcnp.motd.formatter.creator.IFormatterCreator
import dev.mikchan.mcnp.motd.formatter.formatter.IFormatter
import dev.mikchan.mcnp.motd.formatter.formatter.papi.PapiFormatter
import me.clip.placeholderapi.PlaceholderAPIPlugin

internal class PapiFormatterCreator(private val fallback: IFormatterCreator) : IFormatterCreator {
override fun build(plugin: MOTDPlugin): IFormatter {
val papi = plugin.server.pluginManager.getPlugin("PlaceholderAPI") as? PlaceholderAPIPlugin?
return papi?.let {
plugin.logger.info("PlaceholderAPI found! Hooking in...")
PapiFormatter()
} ?: fallback.build(plugin)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.mikchan.mcnp.motd.formatter.formatter

import org.bukkit.ChatColor
import org.bukkit.OfflinePlayer

internal class Formatter : IFormatter {
override fun format(str: String, player: OfflinePlayer?): String {
return ChatColor.translateAlternateColorCodes('&', str)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.mikchan.mcnp.motd.formatter.formatter

import org.bukkit.OfflinePlayer

interface IFormatter {
fun format(str: String, player: OfflinePlayer? = null): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.mikchan.mcnp.motd.formatter.formatter.papi

import dev.mikchan.mcnp.motd.formatter.formatter.IFormatter
import me.clip.placeholderapi.PlaceholderAPI
import org.bukkit.ChatColor
import org.bukkit.OfflinePlayer

internal class PapiFormatter : IFormatter {
override fun format(str: String, player: OfflinePlayer?): String {
val format = PlaceholderAPI.setPlaceholders(player, str)
return ChatColor.translateAlternateColorCodes('&', format)
}
}
3 changes: 3 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ api-version: '1.20'

load: STARTUP

softdepend:
- PlaceholderAPI

permissions:
mcn.motd.admin:
default: op
Expand Down

0 comments on commit bb0fa5e

Please sign in to comment.