Added scroll uses

This commit is contained in:
Auxilor
2024-07-05 18:58:50 +01:00
parent 64993942dc
commit d455fbc0b2
5 changed files with 88 additions and 13 deletions

View File

@@ -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

View File

@@ -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<ScrollLevel>
get() = this.fast().scrolls
set(value) {

View File

@@ -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<InjectablePlaceholder> {
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 {

View File

@@ -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
}
}

View File

@@ -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."