Skip to content

Commit

Permalink
refactor: refactor antiCheat integration
Browse files Browse the repository at this point in the history
fix: fix map delete
  • Loading branch information
MC-XiaoHei committed Feb 19, 2024
1 parent 148a0ce commit 1924cf0
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 155 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ pauseInsteadOfStopRecordingOnPlayerQuit = false
# Description: Template for the recording storage path, supports ${name} and ${uuid} variables.
recordPath = "replay/player/${name}@${uuid}"

# enabled: Whether to enable the high-speed recording pause function, this function pauses recording when the player moves at high speed, default is false.
# threshold: Speed threshold for triggering high-speed recording pause, default is 20.00.
[pauseRecordingOnHighSpeed]
# enabled: Whether to enable the high-speed recording pause function, this function pauses recording when the player moves at high speed, default is false.
enabled = false
# threshold: Speed threshold for triggering high-speed recording pause, default is 20.00.
threshold = 20.0

[filter]
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ pauseInsteadOfStopRecordingOnPlayerQuit = false
# 描述: 录像存储路径模板,支持 ${name} 和 ${uuid} 变量。
recordPath = "replay/player/${name}@${uuid}"

# enabled: 是否启用高速录制暂停功能,此功能在玩家高速运动时暂停录制,默认为 false。
# threshold: 触发高速录制暂停的速度阈值,默认为 20.00。
[pauseRecordingOnHighSpeed]
# enabled: 是否启用高速录制暂停功能,此功能在玩家高速运动时暂停录制,默认为 false。
enabled = false
# threshold: 触发高速录制暂停的速度阈值,默认为 20.00。
threshold = 20.0

[filter]
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/cn/xor7/iseeyou/ConfigData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ data class ConfigData(
var recordPath: String = "replay/player/\${name}@\${uuid}",
var clearOutdatedRecordFile: OutdatedRecordRetentionConfig = OutdatedRecordRetentionConfig(),
var recordSuspiciousPlayer: RecordSuspiciousPlayerConfig = RecordSuspiciousPlayerConfig(),
var recordMatrixSuspiciousPlayer: RecordMatrixSuspiciousPlayerConfig = RecordMatrixSuspiciousPlayerConfig()
// var asyncSave: Boolean = false,
) {
fun isConfigValid(): String? {
Expand Down Expand Up @@ -61,17 +60,12 @@ data class OutdatedRecordRetentionConfig(
)

data class RecordSuspiciousPlayerConfig(
var enabledThemis: Boolean = true,
var enableThemisIntegration: Boolean = false,
var enableMatrixIntegration: Boolean = false,
var recordMinutes: Long = 5,
var recordPath: String = "replay/suspicious/Themis/\${name}@\${uuid}",
)

data class RecordMatrixSuspiciousPlayerConfig(
var enabledMatrix: Boolean = true,
var recordMinutes: Long = 5,
var recordPath: String = "replay/suspicious/Matrix/\${name}@\${uuid}",
)

data class FilterConfig(
var checkBy: String = "name",
var recordMode: String = "blacklist",
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/cn/xor7/iseeyou/ISeeYou.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cn.xor7.iseeyou


import cn.xor7.iseeyou.matrix.MatrixListener
import cn.xor7.iseeyou.matrix.matrixSuspiciousPhotographers
import cn.xor7.iseeyou.themis.ThemisListener
import cn.xor7.iseeyou.themis.suspiciousPhotographers
import cn.xor7.iseeyou.anticheat.AntiCheatListener
import cn.xor7.iseeyou.anticheat.listeners.MatrixListener
import cn.xor7.iseeyou.anticheat.listeners.ThemisListener
import cn.xor7.iseeyou.anticheat.suspiciousPhotographers
import org.bukkit.Bukkit
import org.bukkit.command.CommandExecutor
import org.bukkit.configuration.InvalidConfigurationException
Expand Down Expand Up @@ -58,12 +57,16 @@ class ISeeYou : JavaPlugin(), CommandExecutor {
logger.warning("Failed to initialize configuration. Plugin will not enable.")
Bukkit.getPluginManager().disablePlugin(this)
}
if (Bukkit.getPluginManager().isPluginEnabled("Themis") || toml!!.data.recordSuspiciousPlayer.enabledThemis) {
Bukkit.getPluginManager().registerEvents(ThemisListener, this)
}
if (Bukkit.getPluginManager().isPluginEnabled("Matrix") || toml!!.data.recordMatrixSuspiciousPlayer.enabledMatrix) {
Bukkit.getServer().pluginManager.registerEvents(MatrixListener, this)
}

Bukkit.getPluginManager().registerEvents(AntiCheatListener, this)

if (Bukkit.getPluginManager().isPluginEnabled("Themis") ||
toml!!.data.recordSuspiciousPlayer.enableThemisIntegration
) Bukkit.getPluginManager().registerEvents(ThemisListener(), this)

if (Bukkit.getPluginManager().isPluginEnabled("Matrix") ||
toml!!.data.recordSuspiciousPlayer.enableMatrixIntegration
) Bukkit.getPluginManager().registerEvents(MatrixListener(), this)
}

private fun setupConfig() {
Expand All @@ -84,7 +87,6 @@ class ISeeYou : JavaPlugin(), CommandExecutor {
photographers.clear()
highSpeedPausedPhotographers.clear()
suspiciousPhotographers.clear()
matrixSuspiciousPhotographers.clear()
instance = null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cn.xor7.iseeyou.themis
package cn.xor7.iseeyou.anticheat

import cn.xor7.iseeyou.EventListener
import cn.xor7.iseeyou.instance
import cn.xor7.iseeyou.toml
import com.gmail.olexorus.themis.api.ActionEvent
import com.gmail.olexorus.themis.api.ViolationEvent
import me.rerere.matrix.api.events.PlayerViolationEvent
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerQuitEvent
Expand All @@ -17,47 +18,46 @@ import java.util.*

val suspiciousPhotographers: MutableMap<String, SuspiciousPhotographer> = mutableMapOf()

object ThemisListener : Listener {
object AntiCheatListener : Listener {
init {
object : BukkitRunnable() {
override fun run() {
val currentTime = System.currentTimeMillis()
suspiciousPhotographers.forEach { (_, susPhotographer) ->
if (currentTime - susPhotographer.lastTagged >
suspiciousPhotographers.entries.removeIf {
if (currentTime - it.value.lastTagged >
Duration.ofMinutes(toml!!.data.recordSuspiciousPlayer.recordMinutes).toMillis()
) {
susPhotographer.photographer.stopRecording()
suspiciousPhotographers.remove(susPhotographer.name)
}
it.value.photographer.stopRecording()
return@removeIf true
} else false
}
}
}.runTaskTimer(instance!!, 0, 20 * 60 * 5)
}

@EventHandler
fun onAction(e: ActionEvent) {
val suspiciousPhotographer = suspiciousPhotographers[e.player.name]
fun onAntiCheatAction(player: Player) {
val suspiciousPhotographer = suspiciousPhotographers[player.name]
if (suspiciousPhotographer != null) {
suspiciousPhotographers[e.player.name] =
suspiciousPhotographers[player.name] =
suspiciousPhotographer.copy(lastTagged = System.currentTimeMillis())
} else {
val photographer = Bukkit
.getPhotographerManager()
.createPhotographer(
(e.player.name + "_sus_" + UUID.randomUUID().toString().replace("-".toRegex(), ""))
(player.name + "_sus_" + UUID.randomUUID().toString().replace("-".toRegex(), ""))
.substring(0, 16),
e.player.location
player.location
)
if (photographer == null) {
throw RuntimeException(
"Error on create photographer for player: {name: " + e.player.name + " , UUID:" + e.player.uniqueId + "}"
"Error on create suspicious photographer for player: {name: " + player.name + " , UUID:" + player.uniqueId + "}"
)
}
photographer.setFollowPlayer(e.player)
photographer.setFollowPlayer(player)
val currentTime = LocalDateTime.now()
val recordPath: String = toml!!.data.recordSuspiciousPlayer.recordPath
.replace("\${name}", e.player.name)
.replace("\${uuid}", e.player.uniqueId.toString())
.replace("\${name}", player.name)
.replace("\${uuid}", player.uniqueId.toString())
File(recordPath).mkdirs()
val recordFile =
File(recordPath + "/" + currentTime.format(EventListener.DATE_FORMATTER) + ".mcpr")
Expand All @@ -66,9 +66,9 @@ object ThemisListener : Listener {
}
recordFile.createNewFile()
photographer.setRecordFile(recordFile)
suspiciousPhotographers[e.player.name] = SuspiciousPhotographer(
suspiciousPhotographers[player.name] = SuspiciousPhotographer(
photographer = photographer,
name = e.player.name,
name = player.name,
lastTagged = System.currentTimeMillis()
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.xor7.iseeyou.themis
package cn.xor7.iseeyou.anticheat

import top.leavesmc.leaves.entity.Photographer

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cn.xor7.iseeyou.anticheat.listeners

import cn.xor7.iseeyou.anticheat.AntiCheatListener
import com.gmail.olexorus.themis.api.ActionEvent
import me.rerere.matrix.api.events.PlayerViolationEvent
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener

class MatrixListener : Listener {
@EventHandler
fun onPlayerViolation(e: PlayerViolationEvent) = AntiCheatListener.onAntiCheatAction(e.player)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cn.xor7.iseeyou.anticheat.listeners

import cn.xor7.iseeyou.anticheat.AntiCheatListener
import com.gmail.olexorus.themis.api.ActionEvent
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener

class ThemisListener : Listener {
@EventHandler
fun onAction(e: ActionEvent) = AntiCheatListener.onAntiCheatAction(e.player)
}
101 changes: 0 additions & 101 deletions src/main/java/cn/xor7/iseeyou/matrix/MatrixListener.kt

This file was deleted.

This file was deleted.

0 comments on commit 1924cf0

Please sign in to comment.