Skip to content

Commit

Permalink
Merge pull request #10 from MikChanNoPlugins/dev/dev
Browse files Browse the repository at this point in the history
v2.1.4
  • Loading branch information
wtlgo authored Mar 8, 2023
2 parents 115ef18 + 97242f5 commit c3a7a37
Show file tree
Hide file tree
Showing 43 changed files with 365 additions and 324 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static def determineVersion(String defaultVersion) {
}

group = 'dev.mikchan.mcnp'
version = determineVersion('2.1.1')
version = determineVersion('2.1.4')

repositories {
mavenCentral()
Expand Down
55 changes: 46 additions & 9 deletions src/main/java/dev/mikchan/mcnp/chat/FactorySelector.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,70 @@
package dev.mikchan.mcnp.chat

import dev.mikchan.mcnp.chat.contract.IChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.pre19spigot.Pre19SpigotChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.SpigotChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.latest.SpigotLatestChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_16_5.SpigotV1m16p5ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_17.SpigotV1m17ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_17_1.SpigotV1m17p1ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_18.SpigotV1m18ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_18_1.SpigotV1m18p1ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_18_2.SpigotV1m18p2ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_19.SpigotV1m19ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_19_1.SpigotV1m19p1ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_19_2.SpigotV1m19p2ChatPluginFactory
import dev.mikchan.mcnp.chat.implementation.spigot.v1_19_3.SpigotV1m19p3ChatPluginFactory


internal object FactorySelector {
@Suppress("unused")
private class Version(val major: Int, val minor: Int, val patch: Int)

private fun getVersion(plugin: ChatPlugin): Version? {
val versionRegex = Regex("""(\d+)\.(\d+)\.(\d+)""")
val versionRegex = Regex("""(\d+)\.(\d+)(?:\.(\d+))?""")
val match = versionRegex.find(plugin.server.bukkitVersion) ?: return null
val (sMajor, sMinor, sPatch) = match.destructured

val major = sMajor.toIntOrNull() ?: return null
val minor = sMinor.toIntOrNull() ?: return null
val patch = sPatch.toIntOrNull() ?: return null
val patch = sPatch.toIntOrNull() ?: 0

return Version(major, minor, patch)
}

fun selectFactory(plugin: ChatPlugin): IChatPluginFactory {
val version = getVersion(plugin) ?: return SpigotChatPluginFactory(plugin)
val version = getVersion(plugin) ?: return SpigotLatestChatPluginFactory(plugin)

if (version.major == 1 && version.minor < 19) {
return Pre19SpigotChatPluginFactory(plugin)
}
return when (version.major) {
1 -> when (version.minor) {
16 -> when (version.patch) {
5 -> SpigotV1m16p5ChatPluginFactory(plugin)
else -> SpigotV1m16p5ChatPluginFactory(plugin)
}

17 -> when (version.patch) {
0 -> SpigotV1m17ChatPluginFactory(plugin)
1 -> SpigotV1m17p1ChatPluginFactory(plugin)
else -> SpigotV1m17p1ChatPluginFactory(plugin)
}

18 -> when (version.patch) {
0 -> SpigotV1m18ChatPluginFactory(plugin)
1 -> SpigotV1m18p1ChatPluginFactory(plugin)
2 -> SpigotV1m18p2ChatPluginFactory(plugin)
else -> SpigotV1m18p2ChatPluginFactory(plugin)
}

return SpigotChatPluginFactory(plugin)
19 -> when (version.patch) {
0 -> SpigotV1m19ChatPluginFactory(plugin)
1 -> SpigotV1m19p1ChatPluginFactory(plugin)
2 -> SpigotV1m19p2ChatPluginFactory(plugin)
else -> SpigotV1m19p3ChatPluginFactory(plugin)
}

else -> SpigotLatestChatPluginFactory(plugin)
}

// Minecraft 2?
else -> SpigotLatestChatPluginFactory(plugin)
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/dev/mikchan/mcnp/chat/contract/config/IConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ interface IConfig {
var privateTemplate: String

/**
* The console private message template.
* The console to player private message template.
*
* This is the template which is used for private messages that were sent from the console.
* Any `:player_to` will be replaced with [toTemplate], and `:message:` with the message text.
*/
var consoleTemplate: String

/**
* The player to console private message template.
*
* This is the template which is used for private messages that were sent from the console.
* Any `:player_from` will be replaced with [fromTemplate], and `:message:` with the message text.
*/
var fromConsoleTemplate: String

/**
* The player name template.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface IFormatter {
fun formatPrivate(from: Player, to: Player, message: String): String

/**
* Formats a private message sent from the console.
* Formats a private message sent from the console to player.
*
* @param to The player that receives the message.
* @param message The text of the message.
Expand All @@ -27,6 +27,16 @@ interface IFormatter {
*/
fun formatConsole(to: Player, message: String): String

/**
* Formats a private message sent from the console to player.
*
* @param from The player that sends the message.
* @param message The text of the message.
*
* @return The formatted message.
*/
fun formatToConsole(from: Player, message: String): String

/**
* Formats a global message.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package dev.mikchan.mcnp.chat.implementation.spigot.commands
package dev.mikchan.mcnp.chat.implementation.base.commands

import dev.mikchan.mcnp.chat.ChatPlugin
import dev.mikchan.mcnp.chat.implementation.spigot.commands.command.*
import dev.mikchan.mcnp.chat.contract.commands.ICommandManager

internal class SpigotCommandManager(private val plugin: ChatPlugin) : ICommandManager {
private val messageHistory: MutableMap<String, String> = mutableMapOf()
internal typealias MessageHistory = MutableMap<String, String>
internal typealias CommandExecutors = Map<String, ICommand>

private val executors: Map<String, ICommand> = mapOf(
"mcn_chat" to ReloadCommand(plugin),
"msg" to MsgCommand(plugin, messageHistory),
"reply" to ReplyCommand(plugin, messageHistory),
"spy" to SpyCommand(plugin),
)
internal open class BaseCommandManager(
private val plugin: ChatPlugin,
commandsBuilder: (plugin: ChatPlugin, messageHistory: MessageHistory) -> CommandExecutors,
) : ICommandManager {
private val messageHistory: MessageHistory = mutableMapOf()

private val executors: CommandExecutors = commandsBuilder(plugin, messageHistory)

override fun enableAll() {
disableAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.mikchan.mcnp.chat.implementation.spigot.commands.command
package dev.mikchan.mcnp.chat.implementation.base.commands

import org.bukkit.command.CommandExecutor
import org.bukkit.command.TabCompleter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package dev.mikchan.mcnp.chat.implementation.spigot.events
package dev.mikchan.mcnp.chat.implementation.base.event

import dev.mikchan.mcnp.chat.ChatPlugin
import dev.mikchan.mcnp.chat.contract.events.IEventManager
import dev.mikchan.mcnp.chat.implementation.spigot.events.listeners.ChatListener
import dev.mikchan.mcnp.chat.implementation.spigot.events.listeners.MCNCListener
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener

internal class SpigotEventManager(private val plugin: ChatPlugin) : IEventManager {
private val listeners: List<Listener> = listOf(
ChatListener(plugin),
MCNCListener(plugin),
)

internal open class BaseEventManager(
private val plugin: ChatPlugin,
listenerBuilder: (plugin: ChatPlugin) -> List<Listener>,
) : IEventManager {
private val listeners: List<Listener> = listenerBuilder(plugin)

override fun unregister() {
HandlerList.unregisterAll(plugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal open class BaseChatListener(private val plugin: ChatPlugin) {
if (!event.player.hasPermission("mcn.chat.global") && !isPreview) throw NoPermissionException()

val recipients = plugin.server.onlinePlayers.toSet()
val formattedMessage = plugin.formatter.formatGlobal(event.player, "%2\$s")
val formattedMessage = plugin.formatter.formatGlobal(event.player, msg)

return MCNChatEvent(event.player, recipients, true, msg, formattedMessage, isPreview, event.isAsynchronous)
}
Expand All @@ -27,7 +27,7 @@ internal open class BaseChatListener(private val plugin: ChatPlugin) {
val recipients = plugin.server.onlinePlayers.toList().filter { player -> player.world == event.player.world }
.filter { player -> player.location.distance(event.player.location) < localRadius }.toSet()

val formattedMessage = plugin.formatter.formatLocal(event.player, "%2\$s")
val formattedMessage = plugin.formatter.formatLocal(event.player, msg)

return MCNChatEvent(event.player, recipients, false, msg, formattedMessage, isPreview, event.isAsynchronous)
}
Expand All @@ -54,18 +54,16 @@ internal open class BaseChatListener(private val plugin: ChatPlugin) {
if (plugin.config.substituteEvents) {
if (!isPreview) {
plugin.server.scheduler.scheduleSyncDelayedTask(plugin) {
val fMsg = String.format(mcncEvent.formattedMessage, mcncEvent.sender, mcncEvent.message)
plugin.server.consoleSender.sendMessage(fMsg)
plugin.server.consoleSender.sendMessage(mcncEvent.formattedMessage)
for (recipient in mcncEvent.recipients) {
recipient.sendMessage(mcncEvent.sender.uniqueId, fMsg)
recipient.sendMessage(mcncEvent.sender.uniqueId, mcncEvent.formattedMessage)
}
}
}

return false
} else {
event.message = mcncEvent.message
event.format = mcncEvent.formattedMessage
event.format = mcncEvent.formattedMessage.replace("%", "%%")
event.recipients.clear()
event.recipients.addAll(mcncEvent.recipients)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ internal abstract class BaseFormatter(private val plugin: ChatPlugin) : IFormatt
globalPlayer: () -> String?,
localPlayer: () -> String?,
spyPlayer: () -> String?,
preProcess: (s: String) -> String?,
postProcess: (s: String) -> String?,
message: () -> String?
): String {
var res = template

res = preProcess(res) ?: res
res = ChatColor.translateAlternateColorCodes('&', res)

if (res.contains(":player_from:")) {
Expand Down Expand Up @@ -52,7 +49,6 @@ internal abstract class BaseFormatter(private val plugin: ChatPlugin) : IFormatt
res = res.replace(":spy_player:", defPlayer)
}

res = postProcess(res) ?: res

if (res.contains(":message:")) {
val msg = message()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.mikchan.mcnp.chat.implementation.spigot.config
package dev.mikchan.mcnp.chat.implementation.boosted.config

import dev.dejvokep.boostedyaml.YamlDocument
import dev.dejvokep.boostedyaml.dvs.versioning.BasicVersioning
Expand Down Expand Up @@ -88,6 +88,13 @@ internal class BoostedYamlConfig(document: File, resource: InputStream) : IConfi
config.save()
}

override var fromConsoleTemplate: String
get() = config.getString("fromConsoleTemplate", "<:player_from: to CONSOLE> :message:")
set(value) {
config.set("fromConsoleTemplate", value)
config.save()
}

override var playerTemplate: String
get() = config.getString("playerTemplate", "%player_name%")
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ internal class FallbackConfig : IConfig {
override var consoleTemplate: String
get() = "<CONSOLE to :player_to:> :message:"
set(_) {}

override var fromConsoleTemplate: String
get() = "<:player_from: to CONSOLE> :message:"
set(_) {}

override var substituteEvents: Boolean
get() = false
Expand Down
Loading

0 comments on commit c3a7a37

Please sign in to comment.