diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/EcoItemsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/EcoItemsPlugin.kt index 5147619..ad87c47 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/EcoItemsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/EcoItemsPlugin.kt @@ -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()) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/RarityDisplay.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/RarityDisplay.kt index c9f3aa5..9b6f5bb 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/RarityDisplay.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/display/RarityDisplay.kt @@ -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() diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/rarity/ArgParserRarity.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/rarity/ArgParserRarity.kt new file mode 100644 index 0000000..6611e29 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/rarity/ArgParserRarity.kt @@ -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?, meta: ItemMeta): Predicate? { + 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 } + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/rarity/Rarities.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/rarity/Rarities.kt index 39bb3d5..73e02b5 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/rarity/Rarities.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/rarity/Rarities.kt @@ -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) + } + } diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index b2aa5e9..4da0175 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -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 diff --git a/eco-core/core-plugin/src/main/resources/rarities/none.yml b/eco-core/core-plugin/src/main/resources/rarities/none.yml new file mode 100644 index 0000000..a993001 --- /dev/null +++ b/eco-core/core-plugin/src/main/resources/rarities/none.yml @@ -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: [ ]