Improved rarity system

This commit is contained in:
Auxilor
2024-07-20 12:38:02 +01:00
parent 69a503798a
commit c64b2fb581
6 changed files with 86 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package com.willfp.ecoitems
import com.willfp.eco.core.command.impl.PluginCommand
import com.willfp.eco.core.display.DisplayModule
import com.willfp.eco.core.items.Items
import com.willfp.ecoitems.commands.CommandEcoItems
import com.willfp.ecoitems.display.ItemsDisplay
import com.willfp.ecoitems.display.RarityDisplay
@@ -11,6 +12,7 @@ import com.willfp.ecoitems.items.EcoItemsRecipes
import com.willfp.ecoitems.items.ItemAttributeListener
import com.willfp.ecoitems.items.ItemListener
import com.willfp.ecoitems.libreforge.ConditionHasEcoItem
import com.willfp.ecoitems.rarity.ArgParserRarity
import com.willfp.ecoitems.rarity.Rarities
import com.willfp.ecoitems.util.DiscoverRecipeListener
import com.willfp.libreforge.conditions.Conditions
@@ -28,6 +30,8 @@ class EcoItemsPlugin : LibreforgePlugin() {
}
override fun handleEnable() {
Items.registerArgParser(ArgParserRarity)
Conditions.register(ConditionHasEcoItem)
registerHolderProvider(EcoItemFinder.toHolderProvider())

View File

@@ -29,6 +29,10 @@ class RarityDisplay(plugin: EcoPlugin) : DisplayModule(plugin, DisplayPriority.H
}
}
if (baseRarity?.id == "none") {
return
}
val rarity = baseRarity ?: Rarities.defaultRarity
val fis = itemStack.fast()

View File

@@ -0,0 +1,17 @@
package com.willfp.ecoitems.rarity
import com.willfp.eco.core.items.args.LookupArgParser
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import java.util.function.Predicate
object ArgParserRarity : LookupArgParser {
override fun parseArguments(args: Array<out String>?, meta: ItemMeta): Predicate<ItemStack>? {
val rarityArg = args?.firstOrNull { it.startsWith("rarity:") } ?: return null
val rarity = Rarities[rarityArg.split(":").getOrNull(1)] ?: return null
meta.persistentDataContainer.nbtRarity = rarity
return Predicate { it.ecoItemRarity == rarity }
}
}

View File

@@ -2,6 +2,8 @@ package com.willfp.ecoitems.rarity
import com.github.benmanes.caffeine.cache.Caffeine
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.fast.FastItemStack
import com.willfp.eco.core.fast.fast
import com.willfp.eco.core.items.HashedItem
import com.willfp.ecoitems.EcoItemsPlugin
import com.willfp.ecoitems.items.ecoItem
@@ -9,6 +11,8 @@ import com.willfp.ecoitems.plugin
import com.willfp.libreforge.loader.LibreforgePlugin
import com.willfp.libreforge.loader.configs.RegistrableCategory
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataContainer
import org.bukkit.persistence.PersistentDataType
import java.util.Optional
import java.util.concurrent.TimeUnit
import kotlin.jvm.optionals.getOrNull
@@ -34,13 +38,46 @@ private val cache = Caffeine.newBuilder()
val ItemStack.ecoItemRarity: Rarity?
get() {
val nbtRarity = this.nbtRarity
val configuredRarity = this.ecoItem?.rarity
val item = HashedItem.of(this)
return cache.get(item) {
Optional.ofNullable(configuredRarity ?: Rarities.values()
.sortedByDescending { it.weight }
.firstOrNull { it.matches(this) })
Optional.ofNullable(
nbtRarity ?: configuredRarity ?: Rarities.values()
.sortedByDescending { it.weight }
.firstOrNull { it.matches(this) }
)
}.getOrNull()
}
private val rarityKey = plugin.createNamespacedKey("rarity")
var ItemStack.nbtRarity: Rarity?
get() {
return this.fast().nbtRarity
}
set(value) {
this.fast().nbtRarity = value
}
var FastItemStack.nbtRarity: Rarity?
get() {
return this.persistentDataContainer.nbtRarity
}
set(value) {
this.persistentDataContainer.nbtRarity = value
}
var PersistentDataContainer.nbtRarity: Rarity?
get() {
return Rarities[this.get(rarityKey, PersistentDataType.STRING)]
}
set(value) {
if (value != null) {
this.set(rarityKey, PersistentDataType.STRING, value.id)
} else {
this.remove(rarityKey)
}
}

View File

@@ -3,10 +3,18 @@
# by Auxilor
#
discover-recipes: true # If all recipes should be automatically discovered.
# If all recipes should be automatically discovered.
discover-recipes: true
rarity:
enabled: false # If the rarity system should be enabled
blank-lore-line: true # If there should be a blank lore line separating the rarity from the item lore
default: common # The default rarity for unspecified items
display-default: true # If the default rarity should be displayed in the lore
# If the rarity system should be enabled
enabled: false
# If there should be a blank lore line separating the rarity from the item lore
blank-lore-line: true
# The default rarity for unspecified items
default: common
# If items with no specified rarity should be given the default rarity
display-default: true

View File

@@ -0,0 +1,8 @@
# 'None' is a special rarity that is used to indicate that an item has no rarity.
# If an item has this rarity, then nothing will be added to the lore.
# Useful if default rarities are displayed, and you want to exclude an item from that.
# Additionally useful in item lookup: you can do rarity:none to prevent the rarity from being displayed.
weight: 100 # Uses a higher weight to ensure that it is not overridden by other rarities
items: [ ]