MVagueEnchants/src/main/kotlin/pw/hamzantal/vagueenchants/enchantments/TokenMiner.kt

76 lines
2.4 KiB
Kotlin

package pw.hamzantal.vagueenchants.enchantments
import hazae41.minecraft.kutils.bukkit.listen
import hazae41.minecraft.kutils.bukkit.msg
import hazae41.minecraft.kutils.bukkit.schedule
import org.bukkit.Bukkit
import org.bukkit.enchantments.Enchantment
import org.bukkit.enchantments.EnchantmentTarget
import org.bukkit.entity.Player
import org.bukkit.event.Listener
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.inventory.ItemStack
import pw.hamzantal.vagueenchants.*
import pw.hamzantal.vagueenchants.commands.c
import java.util.*
import kotlin.math.max
class TokenMiner(id: Int) : CustomEnchant(id, Config.tokenMiner, "TOKENMINER"), Listener {
init {
VagueEnchants.enchants += this
val pl = VagueEnchants.pl
pl.registerEnchantment(this)
pl.listen(callback = this::onBreak)
}
private val r = Random()
private val cachedValues = hashMapOf<Player, Pair<Pair<Int, Int>, Long>>()
private fun onBreak(e: BlockBreakEvent) {
val p = e.player
val level = p.inventory.itemInMainHand.enchantments[this] ?: 0
if (p !in cachedValues || cachedValues[p]!!.second < System.currentTimeMillis())
cachedValues[p] =
(config.minTokens.x(level).int to config.maxTokens.x(level).int) to System.currentTimeMillis() + 30 * 1000
val chance = cachedValues[p]!!.first
if (!e.block.location.canMine(p)) return
val (minV, maxV) = chance
val toGive = max(1, r.nextInt(max(1, maxV - minV)) + minV )
setTokens(p.uuid, toGive + getTokens(p.uuid))
}
override fun inspect(p: Player, level: Int) {
val cost = config.cost.x(level + 1)
val (minTokens, maxTokens) = config.minTokens.x(level).int to config.maxTokens.x(level).int
p.msg("&7Description: ${config.description}")
p.msg("&7Current level: $level")
p.msg("&7Maximum level: ${config.max}")
p.msg("&7Minimum tokens: $minTokens")
p.msg("&7Maximum tokens: $maxTokens")
if (level < maxLevel) p.msg("&7Next level cost: $cost")
else p.msg("&7Level maxed")
}
override fun loreString(level: Int): String {
return "&7Token Miner ${loreLevel(level)}".c
}
override fun canEnchantItem(i: ItemStack) = i.type in listOf(
ToolUtils.pickaxes, ToolUtils.axes, ToolUtils.shovels
).flatten()
override fun getItemTarget() = EnchantmentTarget.TOOL
override fun getName() = "TOKENMINER"
override fun isTreasure() = false
override fun getMaxLevel() = config.max
override fun getStartLevel() = 1
override fun conflictsWith(o: Enchantment) = false
}