From d455fbc0b263e3d9e458d7d5785d0eae2bb9c6bd Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 5 Jul 2024 18:58:50 +0100 Subject: [PATCH] Added scroll uses --- .../willfp/ecoscrolls/gui/InscriptionTable.kt | 3 +- .../com/willfp/ecoscrolls/scrolls/Items.kt | 36 +++++++++++++ .../com/willfp/ecoscrolls/scrolls/Scroll.kt | 54 +++++++++++++++---- .../ecoscrolls/util/DragAndDropListener.kt | 3 +- .../src/main/resources/scrolls/_example.yml | 5 ++ 5 files changed, 88 insertions(+), 13 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/gui/InscriptionTable.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/gui/InscriptionTable.kt index 704cc64..1e7ad0c 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/gui/InscriptionTable.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/gui/InscriptionTable.kt @@ -24,6 +24,7 @@ import com.willfp.ecomponent.menuStateVar import com.willfp.ecoscrolls.EcoScrollsPlugin import com.willfp.ecoscrolls.scrolls.Scroll import com.willfp.ecoscrolls.scrolls.scroll +import com.willfp.ecoscrolls.scrolls.useScroll import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.effects.Effects import com.willfp.libreforge.toDispatcher @@ -289,7 +290,7 @@ private class AllowSlot(plugin: EcoScrollsPlugin) : MenuSlot(plugin, Inscription val scrollItem = capturedScrollItem[player] ?: throw IllegalStateException("Scroll item is null") - scrollItem.amount -= 1 + scrollItem.useScroll() } // Cheat to update the menu diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Items.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Items.kt index e3663ff..5beca41 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Items.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Items.kt @@ -9,6 +9,7 @@ import org.bukkit.persistence.PersistentDataContainer import org.bukkit.persistence.PersistentDataType private val SCROLL_KEY = plugin.createNamespacedKey("scroll") +private val SCROLL_USES_KEY = plugin.createNamespacedKey("scroll_uses") private val SCROLLS_KEY = plugin.createNamespacedKey("scrolls") private val SCROLL_ID_KEY = plugin.createNamespacedKey("scroll") private val SCROLL_LEVEL_KEY = plugin.createNamespacedKey("level") @@ -39,6 +40,41 @@ var PersistentDataContainer.scroll: Scroll? this.set(SCROLL_KEY, PersistentDataType.STRING, value.id) } +var ItemStack.scrollUses: Int + get() = this.fast().scrollUses + set(value) { + this.fast().scrollUses = value + } + +var FastItemStack.scrollUses: Int + get() = this.persistentDataContainer.scrollUses + set(value) { + this.persistentDataContainer.scrollUses = value + } + +var PersistentDataContainer.scrollUses: Int + get() { + return this.get(SCROLL_USES_KEY, PersistentDataType.INTEGER) ?: 0 + } + set(value) { + if (value == 0) { + this.remove(SCROLL_USES_KEY) + return + } + + this.set(SCROLL_USES_KEY, PersistentDataType.INTEGER, value) + } + +fun ItemStack.useScroll() { + val scroll = this.scroll ?: return + + this.scrollUses++ + + if (this.scrollUses >= scroll.maxUses) { + this.amount-- + } +} + var ItemStack.scrolls: Set get() = this.fast().scrolls set(value) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Scroll.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Scroll.kt index 4298575..e414755 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Scroll.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/scrolls/Scroll.kt @@ -26,6 +26,7 @@ import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import java.util.Objects import java.util.regex.Pattern +import kotlin.math.max class Scroll( plugin: EcoScrollsPlugin, @@ -50,6 +51,8 @@ class Scroll( val maxLevel = config.getInt("max-level") + val maxUses = config.getInt("max-uses") + private val itemName = config.getString("item.name") private val itemLore = config.getStrings("item.lore") @@ -107,16 +110,42 @@ class Scroll( private val lore = config.getStrings("lore") - private val levelPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("level")) { - override fun getValue(p0: String, p1: PlaceholderContext): String? { - return p1.itemStack?.getScrollLevel(this@Scroll)?.level?.toString() + private val scrollPlaceholderContext = object : PlaceholderInjectable { + private val levelPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("level")) { + override fun getValue(p0: String, p1: PlaceholderContext): String? { + return p1.itemStack?.getScrollLevel(this@Scroll)?.level?.toString() + } + } + + private val usesLeftPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("uses_left")) { + override fun getValue(p0: String, p1: PlaceholderContext): String? { + val item = p1.itemStack ?: return null + val scroll = item.scroll ?: return null + return (scroll.maxUses - item.scrollUses).toString() + } + } + + private val usesPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("uses")) { + override fun getValue(p0: String, p1: PlaceholderContext): String? { + val item = p1.itemStack ?: return null + return item.scrollUses.toString() + } + } + + private val maxUsesPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("max_uses")) { + override fun getValue(p0: String, p1: PlaceholderContext): String? { + val item = p1.itemStack ?: return null + val scroll = item.scroll ?: return null + return scroll.maxUses.toString() + } } - } - private val levelInjectable = object : PlaceholderInjectable { override fun getPlaceholderInjections(): List { return listOf( - levelPlaceholder + levelPlaceholder, + usesLeftPlaceholder, + usesPlaceholder, + maxUsesPlaceholder ) } @@ -223,24 +252,27 @@ class Scroll( placeholderContext( player = player, item = itemStack, - injectable = levelInjectable + injectable = scrollPlaceholderContext ) ).map { Display.PREFIX + it } } fun displayScroll(fis: FastItemStack, player: Player?) { - fis.displayName = itemName.formatEco(player = player) - fis.lore = itemLore.formatEco(placeholderContext(player = player)) + val context = placeholderContext(player = player, item = fis.unwrap()) + .withInjectableContext(scrollPlaceholderContext) + + fis.displayName = itemName.formatEco(context) + fis.lore = itemLore.formatEco(context) .map { Display.PREFIX + it } + fis.lore } fun getPlaceholder(identifier: String, context: PlaceholderContext): String? { - if ((levelPlaceholder.getValue(levelPlaceholder.pattern.pattern(), context)?.toIntOrNull() ?: 0) < 1) { + if ((context.itemStack?.getScrollLevel(this)?.level ?: 0) < 1) { return null } val expression = config.getString("placeholders.$identifier") - return expression.formatEco(context.withInjectableContext(levelInjectable)) + return expression.formatEco(context.withInjectableContext(scrollPlaceholderContext)) } fun conflictsWith(other: Scroll): Boolean { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/util/DragAndDropListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/util/DragAndDropListener.kt index c5f48eb..01e6438 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/util/DragAndDropListener.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoscrolls/util/DragAndDropListener.kt @@ -3,6 +3,7 @@ package com.willfp.ecoscrolls.util import com.willfp.eco.core.items.isEcoEmpty import com.willfp.ecoscrolls.EcoScrollsPlugin import com.willfp.ecoscrolls.scrolls.scroll +import com.willfp.ecoscrolls.scrolls.useScroll import org.bukkit.GameMode import org.bukkit.entity.Player import org.bukkit.event.EventHandler @@ -38,7 +39,7 @@ class DragAndDropListener(private val plugin: EcoScrollsPlugin) : Listener { val didInscribe = plugin.inscriptionHandler.tryInscribe(current, scroll, player) if (didInscribe) { - cursor.amount -= 1 + cursor.useScroll() event.isCancelled = true } } diff --git a/eco-core/core-plugin/src/main/resources/scrolls/_example.yml b/eco-core/core-plugin/src/main/resources/scrolls/_example.yml index 4c2adcb..ffb1ccb 100644 --- a/eco-core/core-plugin/src/main/resources/scrolls/_example.yml +++ b/eco-core/core-plugin/src/main/resources/scrolls/_example.yml @@ -10,9 +10,14 @@ name: "&6Example Scroll" # The max level of the scroll max-level: 1 +# The amount of times the scroll can be used +max-uses: 1 + # Options for the physical scroll item item: item: paper glint + + # Name and lore can use %uses%, %max_uses%, and %uses_left% placeholders name: "&6&lExample Scroll" lore: - "&7This is an example scroll."