Skip to content

Commit

Permalink
Merge pull request #4 from MikChanNoPlugins/dev/dev
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
wtlgo authored Mar 3, 2023
2 parents 0069bf6 + dc548aa commit 9fe45c0
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 11 deletions.
3 changes: 1 addition & 2 deletions 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.0.0')
version = determineVersion('2.1.0')

repositories {
mavenCentral()
Expand Down Expand Up @@ -48,7 +48,6 @@ dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.8.10'
compileOnly 'dev.dejvokep:boosted-yaml-spigot:1.3'


implementation "org.bstats:bstats-bukkit:3.0.1"
shadow "org.bstats:bstats-bukkit:3.0.1"

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/mikchan/mcnp/chat/ChatPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.mikchan.mcnp.chat.contract.config.IConfig
import dev.mikchan.mcnp.chat.contract.events.IEventManager
import dev.mikchan.mcnp.chat.contract.formatting.IFormatter
import dev.mikchan.mcnp.chat.contract.keys.IKeys
import dev.mikchan.mcnp.chat.contract.log.IChatLogger
import dev.mikchan.mcnp.chat.contract.users.IUserManager
import org.bstats.bukkit.Metrics
import org.bukkit.plugin.java.JavaPlugin
Expand All @@ -26,6 +27,7 @@ class ChatPlugin : JavaPlugin() {
val userManager: IUserManager by lazy { factory.createUserManager() }
val eventManager: IEventManager by lazy { factory.createEventManager() }
val keys: IKeys by lazy { factory.createKeys() }
val chatLogger: IChatLogger by lazy { factory.createChatLogger() }

override fun onEnable() {
commandManager.enableAll()
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/dev/mikchan/mcnp/chat/contract/IChatPluginFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,59 @@ import dev.mikchan.mcnp.chat.contract.config.IConfig
import dev.mikchan.mcnp.chat.contract.events.IEventManager
import dev.mikchan.mcnp.chat.contract.formatting.IFormatter
import dev.mikchan.mcnp.chat.contract.keys.IKeys
import dev.mikchan.mcnp.chat.contract.log.IChatLogger
import dev.mikchan.mcnp.chat.contract.users.IUserManager

/**
* An abstract factory
*/
interface IChatPluginFactory {
/**
* Creates a configuration
*
* @return A new configuration object
*/
fun createConfig(): IConfig

/**
* Creates a command manager
*
* @return A new command manager
*/
fun createCommandManager(): ICommandManager

/**
* Creates an event manager
*
* @return A new event manager
*/
fun createEventManager(): IEventManager

/**
* Creates a formatter
*
* @return A new formatter
*/
fun createFormatter(): IFormatter

/**
* Creates a key store
*
* @return A new key store
*/
fun createKeys(): IKeys

/**
* Creates a user manager
*
* @return A new user manager
*/
fun createUserManager(): IUserManager

/**
* Creates a chat logger
*
* @return A new chat logger
*/
fun createChatLogger(): IChatLogger
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ interface IConfig {
*/
fun reload(): Boolean

/**
* Is logging enabled.
*/
var enableLog: Boolean

/**
* Is local chat enabled.
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/dev/mikchan/mcnp/chat/contract/keys/IKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ package dev.mikchan.mcnp.chat.contract.keys
import org.bukkit.NamespacedKey
import org.bukkit.persistence.PersistentDataType

/**
* Key representation
*/
interface IKey<T> {
/**
* The key name
*/
val key: NamespacedKey

/**
* The key type
*/
val type: PersistentDataType<T, T>
}
6 changes: 6 additions & 0 deletions src/main/java/dev/mikchan/mcnp/chat/contract/keys/IKeys.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package dev.mikchan.mcnp.chat.contract.keys

/**
* Namespaced keys collection interface
*/
interface IKeys {
/**
* Namespaced key related to spy feature
*/
val spy: IKey<Byte>
}
43 changes: 43 additions & 0 deletions src/main/java/dev/mikchan/mcnp/chat/contract/log/IChatLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.mikchan.mcnp.chat.contract.log

import org.bukkit.entity.Player

/**
* A chat logger interface
*/
interface IChatLogger {
/**
* Logs global message
*
* @param player The player who sent the message
* @param message The message
*/
fun logGlobal(player: Player, message: String)

/**
* Logs local message
*
* @param player The player who sent the message
* @param message The message
*/
fun logLocal(player: Player, message: String)

/**
* Logs private message
*
* @param from The player who sent the message
* @param to The player who receives the message
* @param message The message
*/
fun logPrivate(from: Player, to: Player, message: String)

/**
* Logs console private message
*
* @param player The player which is related to this log
* @param fromConsole If `true` logs a `Console -> Player` type of message,
* and if `false` logs `Player -> Console`
* @param message The message
*/
fun logConsole(player: Player, fromConsole: Boolean, message: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ internal class FallbackConfig : IConfig {
return true
}

override var enableLog: Boolean
get() = false
set(_) {}

override var enableLocal: Boolean
get() = false
set(_) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package dev.mikchan.mcnp.chat.implementation.spigot

import dev.mikchan.mcnp.chat.ChatPlugin
import dev.mikchan.mcnp.chat.contract.IChatPluginFactory
import dev.mikchan.mcnp.chat.contract.commands.ICommandManager
import dev.mikchan.mcnp.chat.contract.config.IConfig
import dev.mikchan.mcnp.chat.contract.events.IEventManager
import dev.mikchan.mcnp.chat.contract.formatting.IFormatter
import dev.mikchan.mcnp.chat.contract.keys.IKeys
import dev.mikchan.mcnp.chat.contract.log.IChatLogger
import dev.mikchan.mcnp.chat.contract.users.IUserManager
import dev.mikchan.mcnp.chat.implementation.fallback.config.FallbackConfig
import dev.mikchan.mcnp.chat.implementation.fallback.formatting.FallbackFormatter
import dev.mikchan.mcnp.chat.implementation.spigot.commands.SpigotCommandManager
import dev.mikchan.mcnp.chat.implementation.spigot.config.BoostedYamlConfig
import dev.mikchan.mcnp.chat.implementation.spigot.events.SpigotEventManager
import dev.mikchan.mcnp.chat.implementation.spigot.formatting.PAPIFormatter
import dev.mikchan.mcnp.chat.implementation.spigot.keys.SpigotKeys
import dev.mikchan.mcnp.chat.implementation.spigot.log.FileChatLogger
import dev.mikchan.mcnp.chat.implementation.spigot.users.SpigotUserManager
import dev.mikchan.mcnp.chat.contract.IChatPluginFactory
import dev.mikchan.mcnp.chat.contract.commands.ICommandManager
import dev.mikchan.mcnp.chat.contract.config.IConfig
import dev.mikchan.mcnp.chat.contract.events.IEventManager
import dev.mikchan.mcnp.chat.contract.formatting.IFormatter
import dev.mikchan.mcnp.chat.contract.keys.IKeys
import dev.mikchan.mcnp.chat.contract.users.IUserManager
import me.clip.placeholderapi.PlaceholderAPIPlugin
import java.io.File

Expand Down Expand Up @@ -57,4 +59,8 @@ internal class SpigotChatPluginFactory(private val plugin: ChatPlugin) : IChatPl
override fun createUserManager(): IUserManager {
return SpigotUserManager(plugin)
}

override fun createChatLogger(): IChatLogger {
return FileChatLogger(plugin)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ internal class MsgCommand(private val plugin: ChatPlugin, private val history: M

if (sender is Player) {
user.sendMessage(sender.uniqueId, formattedMessage)
plugin.chatLogger.logPrivate(sender, user, message)
} else {
sender.sendMessage(formattedMessage)
plugin.chatLogger.logConsole(user, true, message)
}

if (user != sender) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ internal class ReplyCommand(private val plugin: ChatPlugin, private val history:
sender.sendMessage(recipient.uniqueId, formattedMessage)
} else {
sender.sendMessage(formattedMessage)

}

if (recipient != sender) {
Expand All @@ -55,6 +54,14 @@ internal class ReplyCommand(private val plugin: ChatPlugin, private val history:
}
}

if (sender is Player && recipient is Player) {
plugin.chatLogger.logPrivate(sender, recipient, msg)
} else if (sender is Player && recipient !is Player) {
plugin.chatLogger.logConsole(sender, false, msg)
} else if (recipient is Player) {
plugin.chatLogger.logConsole(recipient, true, msg)
}

if (recipientUUID != null) {
history[recipientUUID] = senderUUID
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ internal class BoostedYamlConfig(document: File, resource: InputStream) : IConfi
return config.reload()
}

override var enableLog: Boolean
get() = config.getBoolean("enableLog", false)
set(value) {
config.set("enableLog", value)
config.save()
}

override var enableLocal: Boolean
get() = config.getBoolean("enableLocal", false)
set(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@ internal class MCNCListener(private val plugin: ChatPlugin) : Listener {
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onLogEvent(event: MCNChatEvent) {
if(event.isCancelled) return
if(event.isPreview) return

if(event.isGlobal) {
plugin.chatLogger.logGlobal(event.sender, event.message)
}
else {
plugin.chatLogger.logLocal(event.sender, event.message)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package dev.mikchan.mcnp.chat.implementation.spigot.log

import dev.mikchan.mcnp.chat.ChatPlugin
import dev.mikchan.mcnp.chat.contract.log.IChatLogger
import org.bukkit.entity.Player
import java.io.File
import java.io.FileOutputStream
import java.io.PrintStream
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.thread
import kotlin.concurrent.withLock
import kotlin.random.Random

internal class FileChatLogger(val plugin: ChatPlugin) : IChatLogger {
private val mutex = ReentrantLock()
private val folder = File(plugin.dataFolder.path, "logs")
private var lastFileName: String? = null
private var stream: PrintStream? = null

private val printer: PrintStream
get() {
val now = SimpleDateFormat("yyyy-MM-dd").format(Date())

val stream = stream
if (lastFileName == now && stream != null) {
return stream
}

val newFileName = "${now}.log"
lastFileName = now

folder.mkdirs()
val newFile = File(folder, newFileName)
val newStream = PrintStream(FileOutputStream(newFile, true))

this.stream?.close()
this.stream = newStream

return newStream
}

private fun getTime(): String {
return SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US).format(Date())
}

private fun write(log: String) {
if (!plugin.config.enableLog) return

thread(name = "mcn-chat-log-${Random.nextInt()}") {
mutex.withLock {
printer.println("[${getTime()}]$log")
}
}
}

override fun logGlobal(player: Player, message: String) {
write("[GLOBAL] ${player.name}: $message (at ${player.location})")
}

override fun logLocal(player: Player, message: String) {
write("[LOCAL] ${player.name}: $message (at ${player.location})")
}

override fun logPrivate(from: Player, to: Player, message: String) {
write("[PRIVATE] ${from.name} to ${to.name}: $message (at ${from.location})")
}

override fun logConsole(player: Player, fromConsole: Boolean, message: String) {
write(
if (fromConsole) {
"[PRIVATE] CONSOLE, ${player.name}: $message"
} else {
"[PRIVATE] ${player.name}, CONSOLE: $message (at ${player.location})"
}
)
}
}
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This is an automatic property, do not change!
configVersion: 3
configVersion: 4

# Should enable logging messages?
enableLog: false

# Local chat settings
# If enabled, by default all messages sent by a player
Expand Down

0 comments on commit 9fe45c0

Please sign in to comment.