Added rarity system

This commit is contained in:
Auxilor
2024-07-20 10:49:20 +01:00
parent bee4a8e3cf
commit 69a503798a
16 changed files with 203 additions and 7 deletions

View File

@@ -37,9 +37,10 @@ allprojects {
}
dependencies {
compileOnly("com.willfp:eco:6.71.0")
compileOnly("com.willfp:eco:6.72.0")
compileOnly("org.jetbrains:annotations:23.0.0")
compileOnly("org.jetbrains.kotlin:kotlin-stdlib:1.9.20")
compileOnly("com.github.ben-manes.caffeine:caffeine:3.1.5")
}
java {

View File

@@ -2,7 +2,7 @@ group = "com.willfp"
version = rootProject.version
dependencies {
compileOnly("io.papermc.paper:paper-api:1.20-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
}
publishing {

View File

@@ -4,12 +4,14 @@ import com.willfp.eco.core.command.impl.PluginCommand
import com.willfp.eco.core.display.DisplayModule
import com.willfp.ecoitems.commands.CommandEcoItems
import com.willfp.ecoitems.display.ItemsDisplay
import com.willfp.ecoitems.display.RarityDisplay
import com.willfp.ecoitems.items.EcoItemFinder
import com.willfp.ecoitems.items.EcoItems
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.Rarities
import com.willfp.ecoitems.util.DiscoverRecipeListener
import com.willfp.libreforge.conditions.Conditions
import com.willfp.libreforge.loader.LibreforgePlugin
@@ -21,9 +23,6 @@ internal lateinit var plugin: EcoItemsPlugin
private set
class EcoItemsPlugin : LibreforgePlugin() {
/**
* Internal constructor called by bukkit on plugin load.
*/
init {
plugin = this
}
@@ -36,6 +35,7 @@ class EcoItemsPlugin : LibreforgePlugin() {
override fun loadConfigCategories(): List<ConfigCategory> {
return listOf(
Rarities,
EcoItems,
EcoItemsRecipes
)
@@ -55,7 +55,10 @@ class EcoItemsPlugin : LibreforgePlugin() {
)
}
override fun createDisplayModule(): DisplayModule {
return ItemsDisplay(this)
override fun loadDisplayModules(): List<DisplayModule> {
return listOf(
ItemsDisplay(this),
RarityDisplay(this)
)
}
}

View File

@@ -0,0 +1,42 @@
package com.willfp.ecoitems.display
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.display.Display
import com.willfp.eco.core.display.DisplayModule
import com.willfp.eco.core.display.DisplayPriority
import com.willfp.eco.core.display.DisplayProperties
import com.willfp.eco.core.fast.fast
import com.willfp.ecoitems.rarity.Rarities
import com.willfp.ecoitems.rarity.ecoItemRarity
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
class RarityDisplay(plugin: EcoPlugin) : DisplayModule(plugin, DisplayPriority.HIGHEST.weight + 1) {
override fun display(itemStack: ItemStack, player: Player?, properties: DisplayProperties, vararg args: Any) {
if (!plugin.configYml.getBool("rarity.enabled")) {
return
}
if (properties.inGui) {
return
}
val baseRarity = itemStack.ecoItemRarity
if (baseRarity == null) {
if (!plugin.configYml.getBool("rarity.display-default")) {
return
}
}
val rarity = baseRarity ?: Rarities.defaultRarity
val fis = itemStack.fast()
val lore = fis.lore.toMutableList()
if (plugin.configYml.getBool("rarity.blank-lore-line")) {
lore += Display.PREFIX
}
lore += rarity.displayLore
fis.lore = lore
}
}

View File

@@ -8,6 +8,7 @@ import com.willfp.eco.core.items.Items
import com.willfp.eco.core.items.builder.ItemStackBuilder
import com.willfp.eco.core.recipe.Recipes
import com.willfp.eco.core.registry.Registrable
import com.willfp.ecoitems.rarity.Rarities
import com.willfp.libreforge.Holder
import com.willfp.libreforge.ViolationContext
import com.willfp.libreforge.conditions.Conditions
@@ -81,6 +82,8 @@ class EcoItem(
val baseAttackSpeed = config.getDoubleOrNull("base-attack-speed")
val rarity = Rarities[config.getString("rarity")]
override fun getID(): String {
return this.id.key
}

View File

@@ -0,0 +1,46 @@
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.items.HashedItem
import com.willfp.ecoitems.EcoItemsPlugin
import com.willfp.ecoitems.items.ecoItem
import com.willfp.ecoitems.plugin
import com.willfp.libreforge.loader.LibreforgePlugin
import com.willfp.libreforge.loader.configs.RegistrableCategory
import org.bukkit.inventory.ItemStack
import java.util.Optional
import java.util.concurrent.TimeUnit
import kotlin.jvm.optionals.getOrNull
object Rarities : RegistrableCategory<Rarity>("rarity", "rarities") {
override fun clear(plugin: LibreforgePlugin) {
registry.clear()
cache.invalidateAll()
}
override fun acceptConfig(plugin: LibreforgePlugin, id: String, config: Config) {
registry.register(Rarity(id, config, plugin as EcoItemsPlugin))
}
val defaultRarity: Rarity
get() = Rarities[plugin.configYml.getString("rarity.default")]
?: throw IllegalStateException("Invalid default rarity!")
}
private val cache = Caffeine.newBuilder()
.expireAfterAccess(5, TimeUnit.SECONDS)
.build<HashedItem, Optional<Rarity>>()
val ItemStack.ecoItemRarity: Rarity?
get() {
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) })
}.getOrNull()
}

View File

@@ -0,0 +1,37 @@
package com.willfp.ecoitems.rarity
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.display.Display
import com.willfp.eco.core.items.Items
import com.willfp.eco.core.recipe.parts.EmptyTestableItem
import com.willfp.eco.core.registry.KRegistrable
import com.willfp.ecoitems.EcoItemsPlugin
import org.bukkit.inventory.ItemStack
class Rarity(
override val id: String,
val config: Config,
val plugin: EcoItemsPlugin
) : KRegistrable {
val items = config.getStrings("items")
.map { Items.lookup(it) }
.filterNot { it is EmptyTestableItem }
val weight = config.getInt("weight")
val lore = config.getFormattedStrings("lore")
val displayLore = lore.map { Display.PREFIX + it }
fun matches(item: ItemStack): Boolean {
return items.any { it.matches(item) }
}
override fun equals(other: Any?): Boolean {
return other is Rarity && other.id == this.id
}
override fun hashCode(): Int {
return id.hashCode()
}
}

View File

@@ -4,3 +4,9 @@
#
discover-recipes: true # If all recipes should be automatically discovered.
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

View File

@@ -44,6 +44,9 @@ slot: mainhand
base-damage: 12 # (Optional) The item base damage
base-attack-speed: 1.5 # (Optional) The item base attack speed
# (Optional) The rarity of the item
rarity: rare
# The effects of the item (i.e. the functionality)
# See here: https://plugins.auxilor.io/effects/configuring-an-effect
effects:

View File

@@ -0,0 +1,19 @@
# The ID of the rarity is the name of the .yml file,
# for example common.yml has the ID of common
# You can place rarities anywhere in this folder,
# including in subfolders if you want to organize your rarity configs
# _example.yml is not loaded.
# The lore added to items with this rarity
lore:
- "&a&lCOMMON"
# The weight of the rarity. Higher weights take precedence over lower weights,
# so if an item has multiple rarities, the one with the highest weight will be used.
weight: 1
# The items that have this rarity
# Read here: https://plugins.auxilor.io/all-plugins/the-item-lookup-system
# EcoItems items should specify the rarity in their config rather than here
items:
- diamond

View File

@@ -0,0 +1,6 @@
lore:
- "&a&lCOMMON"
weight: 1
items: []

View File

@@ -0,0 +1,6 @@
lore:
- "&d&lEPIC"
weight: 1
items: []

View File

@@ -0,0 +1,6 @@
lore:
- "&6&lLEGENDARY"
weight: 1
items: []

View File

@@ -0,0 +1,6 @@
lore:
- "&c&lMYTHIC"
weight: 1
items: []

View File

@@ -0,0 +1,6 @@
lore:
- "&9&lRARE"
weight: 1
items: []

View File

@@ -0,0 +1,6 @@
lore:
- "&e&lUNCOMMON"
weight: 1
items: []