Skip to content

Commit

Permalink
milestone tong works
Browse files Browse the repository at this point in the history
  • Loading branch information
amadeusmz committed May 12, 2024
1 parent 3d7c4aa commit 12b0212
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dotman-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.4-R0.1-SNAPSHOT")

// libs
compileOnly("net.minevn:minevnlib-plugin:1.0.4")
compileOnly("net.minevn:minevnlib-plugin:1.0.5")
compileOnly("minevn.depend:playerpoints:3.2.6")
compileOnly("me.clip:placeholderapi:2.11.3")

Expand Down
37 changes: 29 additions & 8 deletions dotman-plugin/src/main/java/net/minevn/dotman/DotMan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package net.minevn.dotman
import net.minevn.dotman.commands.AdminCmd
import net.minevn.dotman.commands.MainCmd
import net.minevn.dotman.commands.TopNapCmd
import net.minevn.dotman.config.FileConfig
import net.minevn.dotman.config.Language
import net.minevn.dotman.config.MainConfig
import net.minevn.dotman.config.Milestones
import net.minevn.dotman.config.*
import net.minevn.dotman.database.ConfigDAO
import net.minevn.dotman.database.PlayerDataDAO
import net.minevn.dotman.database.PlayerInfoDAO
Expand Down Expand Up @@ -39,7 +36,8 @@ class DotMan : MineVNPlugin(), Listener {
// configurations
lateinit var config: MainConfig private set
lateinit var language: Language private set
lateinit var minestones: Milestones private set
lateinit var milestones: Milestones private set
lateinit var milestonesMaster: MilestonesMaster private set

override fun onEnable() {
instance = this
Expand Down Expand Up @@ -78,7 +76,16 @@ class DotMan : MineVNPlugin(), Listener {
initDatabase(config.config.getConfigurationSection("database")!!)
migrate()
language = Language()
minestones = Milestones()
milestones = Milestones()
if (::milestonesMaster.isInitialized) {
milestonesMaster.getAll().forEach {
it.bar?.removeAll()
it.bar?.isVisible = false
it.barTask?.cancel()
it.bar = null
}
}
milestonesMaster = MilestonesMaster()

// init Gui configs
CardTypeUI()
Expand Down Expand Up @@ -132,10 +139,24 @@ class DotMan : MineVNPlugin(), Listener {
dataDAO.insertAllType(uuidStr, TOP_KEY_POINT_FROM_CARD, pointAmount)

// Mốc nạp
val currentPlayer = dataDAO.getData(uuidStr, "${TOP_KEY_DONATE_TOTAL}_ALL")
val currentServer = dataDAO.getSumData("${TOP_KEY_DONATE_TOTAL}_ALL")
Bukkit.getPlayer(uuid)?.takeIf { it.isOnline }?.let { player ->
minestones.getAll().filter { it.type == "all" }.forEach {
it.check(player, dataDAO.getData(uuidStr, "${TOP_KEY_DONATE_TOTAL}_ALL"), amount)
milestones.getAll().filter { it.type == "all" }.forEach {
it.check(player, currentPlayer, amount)
}
milestonesMaster.getAll().filter { it.type == "all" }.forEach {
it.sumCheck(currentServer, amount)
}
}
}

@EventHandler
fun onPlayerJoin(e: PlayerJoinEvent) {
val player = e.player
val milestones = milestonesMaster.getAll()
milestones.forEach {
it.bar?.addPlayer(player)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.minevn.libs.bukkit.runSync
import org.bukkit.Bukkit
import java.util.concurrent.ConcurrentHashMap

const val TOP_EXPIRE = 5 * 60 * 1000L // 5 phút
const val TOP_EXPIRE = 1 * 60 * 1000L // 1 phút
const val TOP_COUNT = 100

const val TOP_KEY_DONATE_TOTAL = "DONATE_TOTAL"
Expand Down
66 changes: 63 additions & 3 deletions dotman-plugin/src/main/java/net/minevn/dotman/config/Milestones.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package net.minevn.dotman.config

import net.minevn.dotman.DotMan
import net.minevn.dotman.TOP_KEY_DONATE_TOTAL
import net.minevn.dotman.database.PlayerDataDAO
import net.minevn.dotman.utils.Utils.Companion.info
import net.minevn.dotman.utils.Utils.Companion.warning
import net.minevn.libs.bukkit.runSync
import org.bukkit.Bukkit
import org.bukkit.boss.BarColor
import org.bukkit.boss.BarStyle
import org.bukkit.boss.BossBar
import org.bukkit.entity.Player
import org.bukkit.scheduler.BukkitTask

class Milestones : FileConfig("mocnap") {

Expand Down Expand Up @@ -42,7 +49,7 @@ class Milestones : FileConfig("mocnap") {
}
}.filterNotNull()

info("Đã nạp ${components.size} mốc nạp")
info("Đã nạp ${components.size} mốc nạp.")
}

override fun reload() {
Expand All @@ -52,7 +59,39 @@ class Milestones : FileConfig("mocnap") {

fun getAll() = components.toList()

class Component(val type: String, val amount: Int, val commands: List<String>) {
class Component(val type: String, val amount: Int, val commands: List<String>, val bossBar: String? = null,
val from: Int = 0, barColor : BarColor = BarColor.GREEN,
barStyle: BarStyle = BarStyle.SEGMENTED_10) {

var bar: BossBar? = null
var barTask: BukkitTask? = null; private set

init {
if (bossBar != null) {
bar = Bukkit.createBossBar("§r", barColor, barStyle).apply {
isVisible = false
barTask = Bukkit.getScheduler().runTaskTimerAsynchronously(DotMan.instance, Runnable {
val current = PlayerDataDAO.getInstance().getSumData("${TOP_KEY_DONATE_TOTAL}_$type")
if (current in from until amount) {
if (!isVisible) {
isVisible = true
runSync { Bukkit.getOnlinePlayers().forEach {addPlayer(it)} }
}
val title = bossBar
.replace("%CURRENT%", current.toString())
.replace("%TARGET%", amount.toString())
progress = current.toDouble() / amount
setTitle(title)
} else {
if (isVisible) {
removeAll()
isVisible = false
}
}
}, 0, 20 * 5)
}
}
}

private val typeName = when (type) {
"all" -> "toàn thời gian"
Expand All @@ -65,6 +104,7 @@ class Milestones : FileConfig("mocnap") {
* Kiểm tra xem người chơi đã đạt mốc nạp này chưa,
* nếu đạt thì thực hiện các lệnh trong config
*
* @param player người chơi
* @param current số tiền sau khi nạp
* @param amount số tiền nạp
*/
Expand All @@ -74,8 +114,28 @@ class Milestones : FileConfig("mocnap") {
runSync {
commands.forEach {
val command = it.replace("%player%", player.name)
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command.format(player.name))
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command)
}
}
}
}

/**
* Kiểm tra xem toàn server đã đạt mốc nạp này chưa,
* nếu đạt thì thực hiện các lệnh trong config
*
* @param current số tiền sau khi nạp
* @param amount số tiền nạp
*/
fun sumCheck(current: Int, amount: Int) {
if (current >= this.amount && current - amount < this.amount) {
runSync {
commands.forEach {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), it)
}
bar?.removeAll()
bar?.isVisible = false
bar = null
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.minevn.dotman.config

import net.minevn.dotman.config.Milestones.Component
import net.minevn.dotman.utils.Utils.Companion.getBarColor
import net.minevn.dotman.utils.Utils.Companion.getBarStyle
import net.minevn.dotman.utils.Utils.Companion.info
import net.minevn.dotman.utils.Utils.Companion.warning

class MilestonesMaster : FileConfig("mocnaptong") {
private var components: List<Component> = emptyList()

init {
loadComponents()
}

@Suppress("UNCHECKED_CAST")
private fun loadComponents() {
var premiumWarning = false
components = (config.getList("mocnaptong") ?: emptyList()).map {
try {
it as Map<*, *>
val bossBar = it.getOrDefault("bossbar", null) as String?
val from = it.getOrDefault("from", 0) as Int
val barColor = getBarColor(it.getOrDefault("bossbar-color", null) as String?)
val barStyle = getBarStyle(it.getOrDefault("bossbar-style", null) as String?)
val component = Component(it["type"] as String, it["amount"] as Int, it["commands"] as List<String>,
bossBar, from, barColor, barStyle)

if (component.type !in listOf("all", "week", "month")) {
warning("Loại mốc nạp \"${component.type}\" không hợp lệ. Chỉ chấp nhận all, week, month")
return@map null
}
if (component.type != "all") {
if (!premiumWarning) {
premiumWarning = true
warning("Tính năng mốc nạp theo tuần, tháng chỉ có ở phiên bản DotMan premium. " +
"Hãy mua plugin để ủng hộ author nhé!")
}
return@map null
}

component
} catch (e: Exception) {
e.warning("Có một mốc nạp không hợp lệ, hãy liên hệ developer để được hỗ trợ")
null
}
}.filterNotNull()

info("Đã nạp ${components.size} mốc nạp tổng.")
}

override fun reload() {
super.reload()
loadComponents()
}

fun getAll() = components.toList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract class PlayerDataDAO : DataAccess() {
abstract fun insertDataScript(): String
abstract fun getTopScript(): String
abstract fun getDataScript(): String
abstract fun getSumDataScript(): String

fun insertData(uuid: String, key: String, value: Int) {
insertDataScript().statement {
Expand All @@ -34,6 +35,14 @@ abstract class PlayerDataDAO : DataAccess() {
}.firstOrNull() ?: 0
}

fun getSumData(key: String) = getSumDataScript().statement {
setString(1, key)

fetchRecords {
getInt("value")
}.firstOrNull() ?: 0
}

fun insertAllType(uuid: String, key: String, value: Int) = TopType.entries.forEach {
insertData(uuid, it.parseKey(key), value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ class PlayerDataDAOImpl : PlayerDataDAO() {
FROM "dotman_player_data"
WHERE "uuid" = ? AND "key" = ?;
""".trimIndent()

override fun getSumDataScript() = """
SELECT SUM("value") as "value"
FROM "dotman_player_data"
WHERE "key" = ?;
""".trimIndent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ class PlayerDataDAOImpl : PlayerDataDAO() {
FROM `dotman_player_data`
WHERE `uuid` = ? AND `key` = ?;
""".trimIndent()

override fun getSumDataScript() = """
SELECT SUM(`value`) as `value`
FROM `dotman_player_data`
WHERE `key` = ?;
""".trimIndent()
}
26 changes: 26 additions & 0 deletions dotman-plugin/src/main/resources/mocnaptong.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Mốc nạp tổng và các lệnh thực thi khi đạt mốc
# Có thể dùng cho: Server booster, tiến độ nạp, ...
mocnaptong:
- type: all # Toàn thời gian
# Lưu ý: Đối với mốc nạp toàn thời gian, tiền độ donate sẽ tính theo donate của server từ trước đến nay.
#bossbar: 'Donate server: %current%/%target% VNĐ' # Bossbar hiển thị tiến độ (bỏ comment để hiển thị)
#from: 0 # mức hiển bossbar
bossbar-style: GREEN
bossbar-color: SEGMENTED_10
amount: 10000000 # Giá trị tích lũy
commands: # Các lệnh chạy cho player khi đạt mốc
- 'say Donate của server đã đạt 10 triệu!'

- type: week # Mốc nạp tuần (tính năng premium)
bossbar-style: GREEN
bossbar-color: SEGMENTED_10
amount: 1000000
commands:
- 'say Tuần này donate của server đã đạt 1 triệu!'

- type: month # Mốc nạp tháng (tính năng premium)
bossbar-style: GREEN
bossbar-color: SEGMENTED_10
amount: 5000000
commands:
- 'say Tháng này donate của server đã đạt 5 triệu!'

0 comments on commit 12b0212

Please sign in to comment.