Added scroll uses
This commit is contained in:
@@ -24,6 +24,7 @@ import com.willfp.ecomponent.menuStateVar
|
|||||||
import com.willfp.ecoscrolls.EcoScrollsPlugin
|
import com.willfp.ecoscrolls.EcoScrollsPlugin
|
||||||
import com.willfp.ecoscrolls.scrolls.Scroll
|
import com.willfp.ecoscrolls.scrolls.Scroll
|
||||||
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.ViolationContext
|
||||||
import com.willfp.libreforge.effects.Effects
|
import com.willfp.libreforge.effects.Effects
|
||||||
import com.willfp.libreforge.toDispatcher
|
import com.willfp.libreforge.toDispatcher
|
||||||
@@ -289,7 +290,7 @@ private class AllowSlot(plugin: EcoScrollsPlugin) : MenuSlot(plugin, Inscription
|
|||||||
val scrollItem = capturedScrollItem[player]
|
val scrollItem = capturedScrollItem[player]
|
||||||
?: throw IllegalStateException("Scroll item is null")
|
?: throw IllegalStateException("Scroll item is null")
|
||||||
|
|
||||||
scrollItem.amount -= 1
|
scrollItem.useScroll()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cheat to update the menu
|
// Cheat to update the menu
|
||||||
|
|||||||
@@ -9,6 +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 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")
|
||||||
@@ -39,6 +40,41 @@ var PersistentDataContainer.scroll: Scroll?
|
|||||||
this.set(SCROLL_KEY, PersistentDataType.STRING, value.id)
|
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>
|
var ItemStack.scrolls: Set<ScrollLevel>
|
||||||
get() = this.fast().scrolls
|
get() = this.fast().scrolls
|
||||||
set(value) {
|
set(value) {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ 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,
|
||||||
@@ -50,6 +51,8 @@ class Scroll(
|
|||||||
|
|
||||||
val maxLevel = config.getInt("max-level")
|
val maxLevel = config.getInt("max-level")
|
||||||
|
|
||||||
|
val maxUses = config.getInt("max-uses")
|
||||||
|
|
||||||
private val itemName = config.getString("item.name")
|
private val itemName = config.getString("item.name")
|
||||||
private val itemLore = config.getStrings("item.lore")
|
private val itemLore = config.getStrings("item.lore")
|
||||||
|
|
||||||
@@ -107,16 +110,42 @@ class Scroll(
|
|||||||
|
|
||||||
private val lore = config.getStrings("lore")
|
private val lore = config.getStrings("lore")
|
||||||
|
|
||||||
private val levelPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("level")) {
|
private val scrollPlaceholderContext = object : PlaceholderInjectable {
|
||||||
override fun getValue(p0: String, p1: PlaceholderContext): String? {
|
private val levelPlaceholder = object : DynamicInjectablePlaceholder(Pattern.compile("level")) {
|
||||||
return p1.itemStack?.getScrollLevel(this@Scroll)?.level?.toString()
|
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> {
|
override fun getPlaceholderInjections(): List<InjectablePlaceholder> {
|
||||||
return listOf(
|
return listOf(
|
||||||
levelPlaceholder
|
levelPlaceholder,
|
||||||
|
usesLeftPlaceholder,
|
||||||
|
usesPlaceholder,
|
||||||
|
maxUsesPlaceholder
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,24 +252,27 @@ class Scroll(
|
|||||||
placeholderContext(
|
placeholderContext(
|
||||||
player = player,
|
player = player,
|
||||||
item = itemStack,
|
item = itemStack,
|
||||||
injectable = levelInjectable
|
injectable = scrollPlaceholderContext
|
||||||
)
|
)
|
||||||
).map { Display.PREFIX + it }
|
).map { Display.PREFIX + it }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun displayScroll(fis: FastItemStack, player: Player?) {
|
fun displayScroll(fis: FastItemStack, player: Player?) {
|
||||||
fis.displayName = itemName.formatEco(player = player)
|
val context = placeholderContext(player = player, item = fis.unwrap())
|
||||||
fis.lore = itemLore.formatEco(placeholderContext(player = player))
|
.withInjectableContext(scrollPlaceholderContext)
|
||||||
|
|
||||||
|
fis.displayName = itemName.formatEco(context)
|
||||||
|
fis.lore = itemLore.formatEco(context)
|
||||||
.map { Display.PREFIX + it } + fis.lore
|
.map { Display.PREFIX + it } + fis.lore
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getPlaceholder(identifier: String, context: PlaceholderContext): String? {
|
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
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
val expression = config.getString("placeholders.$identifier")
|
val expression = config.getString("placeholders.$identifier")
|
||||||
return expression.formatEco(context.withInjectableContext(levelInjectable))
|
return expression.formatEco(context.withInjectableContext(scrollPlaceholderContext))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun conflictsWith(other: Scroll): Boolean {
|
fun conflictsWith(other: Scroll): Boolean {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.willfp.ecoscrolls.util
|
|||||||
import com.willfp.eco.core.items.isEcoEmpty
|
import com.willfp.eco.core.items.isEcoEmpty
|
||||||
import com.willfp.ecoscrolls.EcoScrollsPlugin
|
import com.willfp.ecoscrolls.EcoScrollsPlugin
|
||||||
import com.willfp.ecoscrolls.scrolls.scroll
|
import com.willfp.ecoscrolls.scrolls.scroll
|
||||||
|
import com.willfp.ecoscrolls.scrolls.useScroll
|
||||||
import org.bukkit.GameMode
|
import org.bukkit.GameMode
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import org.bukkit.event.EventHandler
|
import org.bukkit.event.EventHandler
|
||||||
@@ -38,7 +39,7 @@ class DragAndDropListener(private val plugin: EcoScrollsPlugin) : Listener {
|
|||||||
val didInscribe = plugin.inscriptionHandler.tryInscribe(current, scroll, player)
|
val didInscribe = plugin.inscriptionHandler.tryInscribe(current, scroll, player)
|
||||||
|
|
||||||
if (didInscribe) {
|
if (didInscribe) {
|
||||||
cursor.amount -= 1
|
cursor.useScroll()
|
||||||
event.isCancelled = true
|
event.isCancelled = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,9 +10,14 @@ name: "&6Example Scroll"
|
|||||||
# The max level of the scroll
|
# The max level of the scroll
|
||||||
max-level: 1
|
max-level: 1
|
||||||
|
|
||||||
|
# The amount of times the scroll can be used
|
||||||
|
max-uses: 1
|
||||||
|
|
||||||
# Options for the physical scroll item
|
# Options for the physical scroll item
|
||||||
item:
|
item:
|
||||||
item: paper glint
|
item: paper glint
|
||||||
|
|
||||||
|
# Name and lore can use %uses%, %max_uses%, and %uses_left% placeholders
|
||||||
name: "&6&lExample Scroll"
|
name: "&6&lExample Scroll"
|
||||||
lore:
|
lore:
|
||||||
- "&7This is an example scroll."
|
- "&7This is an example scroll."
|
||||||
|
|||||||
Reference in New Issue
Block a user