Skip to content

Commit

Permalink
update: 重构链式组装模式且新增debug模式
Browse files Browse the repository at this point in the history
  • Loading branch information
BingZi-233 committed Jan 8, 2024
1 parent 1a98c86 commit 6c52b01
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 11 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
<a href='https://ci-dev.bingzi.online/job/BilibiliVideo/'>
<img src='https://ci-dev.bingzi.online/job/BilibiliVideo/badge/icon' alt="Build Status">
</a>
<a href='https://sq.bingzi.online/project/information?id=BilibiliVideo'>
<img src='https://sq.bingzi.online/api/project_badges/measure?project=BilibiliVideo&metric=bugs&token=sqb_dbb7c6fff0406277e807aea516f1f87bea5c3550' alt="Bug">
</a>
<a href='https://sq.bingzi.online/project/information?id=BilibiliVideo'>
<img src='https://sq.bingzi.online/api/project_badges/measure?project=BilibiliVideo&metric=sqale_rating&token=sqb_dbb7c6fff0406277e807aea516f1f87bea5c3550' alt="Bug">
</a>
<a href='https://sq.bingzi.online/project/information?id=BilibiliVideo'>
<img src='https://sq.bingzi.online/api/project_badges/measure?project=BilibiliVideo&metric=alert_status&token=sqb_dbb7c6fff0406277e807aea516f1f87bea5c3550' alt="Bug">
</a>
<a href='https://sq.bingzi.online/project/information?id=BilibiliVideo'>
<img src='https://sq.bingzi.online/api/project_badges/measure?project=BilibiliVideo&metric=vulnerabilities&token=sqb_dbb7c6fff0406277e807aea516f1f87bea5c3550' alt="Bug">
</a>
</p>

## WIKI
Expand Down Expand Up @@ -114,3 +126,7 @@ BV1Qs411d7pD:
7. **接受条款:** 使用本软件即代表您同意并接受本免责声明的所有条款和条件。
如有任何疑问或异议,请立即停止使用本软件并与作者联系。
## 相关平台
![](https://sq.bingzi.online/api/project_badges/quality_gate?project=BilibiliVideo&token=sqb_dbb7c6fff0406277e807aea516f1f87bea5c3550)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=online.bingiz.bilibili.video
version=1.4.3
version=1.4.4
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.command.*
import taboolib.common.platform.function.getProxyPlayer
import taboolib.common.platform.function.submit
import taboolib.module.chat.colored
import taboolib.module.lang.sendInfoMessage
import taboolib.platform.util.bukkitPlugin

Expand Down Expand Up @@ -127,8 +128,9 @@ object MainCommand {
@CommandBody(permission = "BilibiliVideo.command.version", permissionDefault = PermissionDefault.OP)
val version = subCommand {
execute<ProxyCommandSender> { sender, _, _ ->
sender.sendInfoMessage("插件版本: ${bukkitPlugin.description.version}")
sender.sendInfoMessage("插件作者: ${bukkitPlugin.description.authors.joinToString(", ")}")
sender.sendInfoMessage("&a&l插件名称 > ${bukkitPlugin.description.name}".colored())
sender.sendInfoMessage("&a&l插件版本 > ${bukkitPlugin.description.version}".colored())
sender.sendInfoMessage("&a&l插件作者 > ${bukkitPlugin.description.authors.joinToString(", ")}".colored())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package online.bingiz.bilibili.video.internal.config

import online.bingiz.bilibili.video.internal.cache.baffleCache
import online.bingiz.bilibili.video.internal.helper.debugStatus
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common5.Baffle
Expand Down Expand Up @@ -36,5 +37,7 @@ object SettingConfig {
baffleCache = Baffle.of(cooldown, TimeUnit.SECONDS)
// 变更是否需要关注
needFollow = config.getBoolean("needFollow")
// 调试模式是否开启
debugStatus = config.getBoolean("debug")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import online.bingiz.bilibili.video.internal.database.Database.Companion.setData
import online.bingiz.bilibili.video.internal.engine.drive.BilibiliApiDrive
import online.bingiz.bilibili.video.internal.engine.drive.BilibiliPassportDrive
import online.bingiz.bilibili.video.internal.entity.*
import online.bingiz.bilibili.video.internal.handler.CoinsHandler
import online.bingiz.bilibili.video.internal.handler.FavouredHandler
import online.bingiz.bilibili.video.internal.handler.FollowingHandler
import online.bingiz.bilibili.video.internal.handler.LikeHandler
import online.bingiz.bilibili.video.internal.handler.ApiType
import online.bingiz.bilibili.video.internal.helper.*
import org.bukkit.Bukkit
import retrofit2.Call
Expand Down Expand Up @@ -58,9 +55,9 @@ object NetworkEngine {

/**
* Show action
* Show模式动作
* Show模式动作处理链
*/
private val showAction = FollowingHandler().setNextHandler(LikeHandler().setNextHandler(CoinsHandler().setNextHandler(FavouredHandler())))
private val showAction = ApiType.buildHandler(ApiType.COINS, ApiType.LIKE, ApiType.FAVOURED, ApiType.FOLLOWING)

/**
* Generate bilibili QRCode url
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package online.bingiz.bilibili.video.internal.expand

import online.bingiz.bilibili.video.internal.cache.midCache
import online.bingiz.bilibili.video.internal.cache.unameCache
import org.bukkit.entity.Player
import taboolib.platform.compat.PlaceholderExpansion

Expand All @@ -14,6 +16,13 @@ object PlaceholderExpand : PlaceholderExpansion {
get() = "BilibiliVideo"

override fun onPlaceholderRequest(player: Player?, args: String): String {
return "N/A"
if (player == null) {
return "N/A"
}
return when (args) {
"uid" -> midCache[player.uniqueId] ?: "N/A-缓存"
"uname" -> unameCache[player.uniqueId] ?: "N/A-缓存"
else -> "N/A-未知参数"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package online.bingiz.bilibili.video.internal.handler

import online.bingiz.bilibili.video.internal.helper.debugStatus
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.function.info

abstract class ApiHandler {
private var nextHandler: ApiHandler? = null
Expand All @@ -13,6 +15,11 @@ abstract class ApiHandler {
abstract fun handle(player: ProxyPlayer, bvid: String, sessData: String): Boolean

protected fun callNextHandler(player: ProxyPlayer, bvid: String, sessData: String): Boolean {
return nextHandler?.handle(player, bvid, sessData) ?: true
return nextHandler?.handle(player, bvid, sessData) ?: let {
if (debugStatus) {
info("最终处理器 > 玩家: ${player.name} | 视频: $bvid | 通过")
}
true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package online.bingiz.bilibili.video.internal.handler

/**
* Api type
*
*
* @constructor Create empty Api type
*/
enum class ApiType(val apiHandler: ApiHandler) {
COINS(CoinsHandler()),
FAVOURED(FavouredHandler()),
FOLLOWING(FollowingHandler()),
LIKE(LikeHandler());

companion object {
/**
* Build handler
* 构建处理器依据指定顺序
*
* @param apiType
* @return
*/
fun buildHandler(vararg apiType: ApiType): ApiHandler {
requireNotNull(apiType) {
error("至少需要一个 ApiType 来生成处理程序")
}
for (i in 0 until apiType.size - 1) {
apiType[i].apiHandler.setNextHandler(apiType[i + 1].apiHandler)
}
return apiType[0].apiHandler
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package online.bingiz.bilibili.video.internal.handler

import online.bingiz.bilibili.video.internal.engine.NetworkEngine
import online.bingiz.bilibili.video.internal.helper.debugStatus
import online.bingiz.bilibili.video.internal.helper.infoAsLang
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.function.info
import taboolib.common.platform.function.warning

/**
* Coins handler
Expand All @@ -12,18 +15,27 @@ import taboolib.common.platform.ProxyPlayer
*/
class CoinsHandler : ApiHandler() {
override fun handle(player: ProxyPlayer, bvid: String, sessData: String): Boolean {
if (debugStatus) {
info("硬币处理器 > 玩家: ${player.name} | 视频: $bvid | 接受处理")
}
NetworkEngine.bilibiliAPI.hasCoins(bvid, sessData).execute().let {
if (it.isSuccessful) {
it.body()?.data?.multiply?.let { count ->
if (count < 1) {
player.infoAsLang("GetTripleStatusFailureNotCoins")
if (debugStatus) {
warning("硬币处理器 > 玩家: ${player.name} | 视频: $bvid | 硬币不足")
}
return false
}
}
} else {
player.infoAsLang("NetworkRequestFailureCode", it.code())
}
}
if (debugStatus) {
info("硬币处理器 > 玩家: ${player.name} | 视频: $bvid | 移交处理")
}
return callNextHandler(player, bvid, sessData)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package online.bingiz.bilibili.video.internal.handler

import online.bingiz.bilibili.video.internal.engine.NetworkEngine
import online.bingiz.bilibili.video.internal.helper.debugStatus
import online.bingiz.bilibili.video.internal.helper.infoAsLang
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.function.info
import taboolib.common.platform.function.warning

/**
* Favoured handler
Expand All @@ -12,18 +15,27 @@ import taboolib.common.platform.ProxyPlayer
*/
class FavouredHandler : ApiHandler() {
override fun handle(player: ProxyPlayer, bvid: String, sessData: String): Boolean {
if (debugStatus) {
info("收藏处理器 > 玩家: ${player.name} | 视频: $bvid | 接受处理")
}
NetworkEngine.bilibiliAPI.hasFavoured(bvid, sessData).execute().let { resultResponse ->
if (resultResponse.isSuccessful) {
resultResponse.body()?.data?.let {
if (it.favoured.not()) {
player.infoAsLang("GetTripleStatusFailureNotFavoured")
if (debugStatus) {
warning("收藏处理器 > 玩家: ${player.name} | 视频: $bvid | 未收藏")
}
return false
}
}
} else {
player.infoAsLang("NetworkRequestFailureCode", resultResponse.code())
}
}
if (debugStatus) {
info("收藏处理器 > 玩家: ${player.name} | 视频: $bvid | 移交处理")
}
return callNextHandler(player, bvid, sessData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package online.bingiz.bilibili.video.internal.handler

import online.bingiz.bilibili.video.internal.config.SettingConfig
import online.bingiz.bilibili.video.internal.engine.NetworkEngine
import online.bingiz.bilibili.video.internal.helper.debugStatus
import online.bingiz.bilibili.video.internal.helper.infoAsLang
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.function.info
import taboolib.common.platform.function.warning

/**
* Following handler
Expand All @@ -13,12 +16,18 @@ import taboolib.common.platform.ProxyPlayer
*/
class FollowingHandler : ApiHandler() {
override fun handle(player: ProxyPlayer, bvid: String, sessData: String): Boolean {
if (debugStatus) {
info("关注处理器 > 玩家: ${player.name} | 视频: $bvid | 接受处理")
}
if (SettingConfig.needFollow) {
NetworkEngine.bilibiliAPI.hasFollowing(bvid, sessData).execute().let { resultResponse ->
if (resultResponse.isSuccessful) {
resultResponse.body()?.data?.let {
if (it.card.following.not()) {
player.infoAsLang("GetTripleStatusFailureNotFollowing")
if (debugStatus) {
warning("关注处理器 > 玩家: ${player.name} | 视频: $bvid | 未关注")
}
return false
}
}
Expand All @@ -27,6 +36,9 @@ class FollowingHandler : ApiHandler() {
}
}
}
if (debugStatus) {
info("关注处理器 > 玩家: ${player.name} | 视频: $bvid | 移交处理")
}
return callNextHandler(player, bvid, sessData)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package online.bingiz.bilibili.video.internal.handler

import online.bingiz.bilibili.video.internal.engine.NetworkEngine
import online.bingiz.bilibili.video.internal.helper.debugStatus
import online.bingiz.bilibili.video.internal.helper.infoAsLang
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.function.info
import taboolib.common.platform.function.warning

/**
* Like handler
Expand All @@ -12,18 +15,27 @@ import taboolib.common.platform.ProxyPlayer
*/
class LikeHandler : ApiHandler() {
override fun handle(player: ProxyPlayer, bvid: String, sessData: String): Boolean {
if (debugStatus) {
info("点赞处理器 > 玩家: ${player.name} | 视频: $bvid | 接受处理")
}
NetworkEngine.bilibiliAPI.hasLike(bvid, sessData).execute().let {
if (it.isSuccessful) {
it.body()?.data?.let { count ->
if (count < 1) {
player.infoAsLang("GetTripleStatusFailureNotLike")
if (debugStatus) {
warning("点赞处理器 > 玩家: ${player.name} | 视频: $bvid | 未点赞")
}
return false
}
}
} else {
player.infoAsLang("NetworkRequestFailureCode", it.code())
}
}
if (debugStatus) {
info("点赞处理器 > 玩家: ${player.name} | 视频: $bvid | 移交处理")
}
return callNextHandler(player, bvid, sessData)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package online.bingiz.bilibili.video.internal.helper


var debugStatus: Boolean = false
3 changes: 3 additions & 0 deletions src/main/resources/setting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ cooldown: 60
# 是否需要关注
# * 本项为实验项目,可能存在不稳定的情况
needFollow: false

# 调试模式,用于诊断一些问题
debug: false

0 comments on commit 6c52b01

Please sign in to comment.