-
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.
Merge pull request #4 from MikChanNoPlugins/dev/dev
v2.1.0
- Loading branch information
Showing
15 changed files
with
240 additions
and
11 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
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
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
43
src/main/java/dev/mikchan/mcnp/chat/contract/log/IChatLogger.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,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) | ||
} |
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
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
79 changes: 79 additions & 0 deletions
79
src/main/java/dev/mikchan/mcnp/chat/implementation/spigot/log/FileChatLogger.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,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})" | ||
} | ||
) | ||
} | ||
} |
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