Rewrote how scroll uses are tracked
This commit is contained in:
@@ -9,7 +9,7 @@ import org.bukkit.persistence.PersistentDataContainer
|
|||||||
import org.bukkit.persistence.PersistentDataType
|
import org.bukkit.persistence.PersistentDataType
|
||||||
|
|
||||||
private val SCROLL_KEY = plugin.createNamespacedKey("scroll")
|
private val SCROLL_KEY = plugin.createNamespacedKey("scroll")
|
||||||
private val SCROLL_USES_KEY = plugin.createNamespacedKey("scroll_uses")
|
private val SCROLL_USES_LEFT_KEY = plugin.createNamespacedKey("scroll_uses_left")
|
||||||
private val SCROLLS_KEY = plugin.createNamespacedKey("scrolls")
|
private val SCROLLS_KEY = plugin.createNamespacedKey("scrolls")
|
||||||
private val SCROLL_ID_KEY = plugin.createNamespacedKey("scroll")
|
private val SCROLL_ID_KEY = plugin.createNamespacedKey("scroll")
|
||||||
private val SCROLL_LEVEL_KEY = plugin.createNamespacedKey("level")
|
private val SCROLL_LEVEL_KEY = plugin.createNamespacedKey("level")
|
||||||
@@ -40,37 +40,39 @@ var PersistentDataContainer.scroll: Scroll?
|
|||||||
this.set(SCROLL_KEY, PersistentDataType.STRING, value.id)
|
this.set(SCROLL_KEY, PersistentDataType.STRING, value.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ItemStack.scrollUses: Int
|
var ItemStack.scrollUsesLeft: Int
|
||||||
get() = this.fast().scrollUses
|
get() = this.fast().scrollUsesLeft
|
||||||
set(value) {
|
set(value) {
|
||||||
this.fast().scrollUses = value
|
this.fast().scrollUsesLeft = value
|
||||||
}
|
}
|
||||||
|
|
||||||
var FastItemStack.scrollUses: Int
|
var FastItemStack.scrollUsesLeft: Int
|
||||||
get() = this.persistentDataContainer.scrollUses
|
get() = this.persistentDataContainer.scrollUsesLeft
|
||||||
set(value) {
|
set(value) {
|
||||||
this.persistentDataContainer.scrollUses = value
|
this.persistentDataContainer.scrollUsesLeft = value
|
||||||
}
|
}
|
||||||
|
|
||||||
var PersistentDataContainer.scrollUses: Int
|
var PersistentDataContainer.scrollUsesLeft: Int
|
||||||
get() {
|
get() {
|
||||||
return this.get(SCROLL_USES_KEY, PersistentDataType.INTEGER) ?: 0
|
return this.get(SCROLL_USES_LEFT_KEY, PersistentDataType.INTEGER) ?: 0
|
||||||
}
|
}
|
||||||
set(value) {
|
set(value) {
|
||||||
if (value == 0) {
|
if (value == 0) {
|
||||||
this.remove(SCROLL_USES_KEY)
|
this.remove(SCROLL_USES_LEFT_KEY)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.set(SCROLL_USES_KEY, PersistentDataType.INTEGER, value)
|
this.set(SCROLL_USES_LEFT_KEY, PersistentDataType.INTEGER, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ItemStack.useScroll() {
|
fun ItemStack.useScroll() {
|
||||||
val scroll = this.scroll ?: return
|
if (this.scroll == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.scrollUses++
|
this.scrollUsesLeft--
|
||||||
|
|
||||||
if (this.scrollUses >= scroll.maxUses) {
|
if (this.scrollUsesLeft <= 0) {
|
||||||
this.amount--
|
this.amount--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import com.willfp.eco.core.placeholder.templates.DynamicInjectablePlaceholder
|
|||||||
import com.willfp.eco.core.price.ConfiguredPrice
|
import com.willfp.eco.core.price.ConfiguredPrice
|
||||||
import com.willfp.eco.core.recipe.Recipes
|
import com.willfp.eco.core.recipe.Recipes
|
||||||
import com.willfp.eco.core.registry.KRegistrable
|
import com.willfp.eco.core.registry.KRegistrable
|
||||||
import com.willfp.eco.util.evaluateExpression
|
|
||||||
import com.willfp.eco.util.evaluateExpressionOrNull
|
import com.willfp.eco.util.evaluateExpressionOrNull
|
||||||
import com.willfp.eco.util.formatEco
|
import com.willfp.eco.util.formatEco
|
||||||
import com.willfp.eco.util.toNumeral
|
import com.willfp.eco.util.toNumeral
|
||||||
@@ -29,7 +28,6 @@ import org.bukkit.entity.Player
|
|||||||
import org.bukkit.inventory.ItemStack
|
import org.bukkit.inventory.ItemStack
|
||||||
import java.util.Objects
|
import java.util.Objects
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
import kotlin.math.max
|
|
||||||
|
|
||||||
class Scroll(
|
class Scroll(
|
||||||
plugin: EcoScrollsPlugin,
|
plugin: EcoScrollsPlugin,
|
||||||
@@ -65,6 +63,7 @@ class Scroll(
|
|||||||
fis.scroll = this
|
fis.scroll = this
|
||||||
fis.displayName = itemName.formatEco()
|
fis.displayName = itemName.formatEco()
|
||||||
fis.lore = itemLore.formatEco().map { Display.PREFIX + it } + fis.lore
|
fis.lore = itemLore.formatEco().map { Display.PREFIX + it } + fis.lore
|
||||||
|
fis.scrollUsesLeft = maxUses
|
||||||
fis.unwrap()
|
fis.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,15 +130,15 @@ class Scroll(
|
|||||||
private val usesLeftPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("uses_left")) {
|
private val usesLeftPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("uses_left")) {
|
||||||
override fun getValue(p0: String, p1: PlaceholderContext): String? {
|
override fun getValue(p0: String, p1: PlaceholderContext): String? {
|
||||||
val item = p1.itemStack ?: return null
|
val item = p1.itemStack ?: return null
|
||||||
val scroll = item.scroll ?: return null
|
return item.scrollUsesLeft.toString()
|
||||||
return (scroll.maxUses - item.scrollUses).toString()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val usesPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("uses")) {
|
private val usesPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("uses")) {
|
||||||
override fun getValue(p0: String, p1: PlaceholderContext): String? {
|
override fun getValue(p0: String, p1: PlaceholderContext): String? {
|
||||||
val item = p1.itemStack ?: return null
|
val item = p1.itemStack ?: return null
|
||||||
return item.scrollUses.toString()
|
val scroll = item.scroll ?: return null
|
||||||
|
return (scroll.maxUses - item.scrollUsesLeft).toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user