From 77aedf8c8121fb55b1a5e8205fd5c158602012ca Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 28 Mar 2023 19:10:11 +0100 Subject: [PATCH] Lots of updating --- .../willfp/ecoenchants/EcoEnchantsPlugin.kt | 3 + .../commands/CommandGiveRandomBook.kt | 2 +- .../com/willfp/ecoenchants/display/Sorters.kt | 4 +- .../willfp/ecoenchants/enchants/EcoEnchant.kt | 6 +- .../ecoenchants/enchants/EcoEnchantLike.kt | 4 +- .../libreforge/TriggerEnchantType.kt | 45 +++++++++++ .../ecoenchants/rarity/EnchantmentRarities.kt | 76 ++----------------- .../ecoenchants/rarity/EnchantmentRarity.kt | 7 +- .../ecoenchants/target/EnchantmentTarget.kt | 11 +-- .../ecoenchants/target/EnchantmentTargets.kt | 66 ++-------------- .../ecoenchants/type/EnchantmentType.kt | 20 +++-- .../ecoenchants/type/EnchantmentTypes.kt | 72 ++---------------- .../core-plugin/src/main/resources/plugin.yml | 10 +-- gradle.properties | 2 +- 14 files changed, 99 insertions(+), 229 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/libreforge/TriggerEnchantType.kt diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt index 4cc40d7e..6e67ad22 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt @@ -23,10 +23,13 @@ import com.willfp.ecoenchants.mechanics.LootSupport import com.willfp.ecoenchants.mechanics.VillagerSupport import com.willfp.ecoenchants.target.EnchantLookup.clearEnchantCache import com.willfp.ecoenchants.target.EnchantLookup.heldEnchantLevels +import com.willfp.ecoenchants.type.EnchantmentTypes +import com.willfp.libreforge.integrations.ecoenchants.impl.TriggerEnchantType import com.willfp.libreforge.loader.LibreforgePlugin import com.willfp.libreforge.loader.configs.ConfigCategory import com.willfp.libreforge.registerHolderProvider import com.willfp.libreforge.registerPlayerRefreshFunction +import com.willfp.libreforge.triggers.Triggers import org.bukkit.event.Listener class EcoEnchantsPlugin : LibreforgePlugin() { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt index 5f09be9f..1191e025 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt @@ -36,7 +36,7 @@ class CommandGiveRandomBook(plugin: EcoPlugin) : PluginCommand( val filterName = args.getOrNull(1) val filter = if (filterName != null) { - EnchantmentTypes.getByID(filterName) ?: EnchantmentRarities.getByID(filterName) + EnchantmentTypes[filterName] ?: EnchantmentRarities[filterName] } else null val minLevel = args.getOrNull(2)?.toIntOrNull() ?: 1 diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/Sorters.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/Sorters.kt index abe2e829..f4882dde 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/Sorters.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/display/Sorters.kt @@ -62,7 +62,7 @@ object TypeSorter : EnchantmentSorter { fun update(plugin: EcoEnchantsPlugin) { types.clear() types.addAll(plugin.configYml.getStrings("display.sort.type-order").mapNotNull { - EnchantmentTypes.getByID(it) + EnchantmentTypes[it] }) } @@ -91,7 +91,7 @@ object RaritySorter : EnchantmentSorter { fun update(plugin: EcoEnchantsPlugin) { rarities.clear() rarities.addAll(plugin.configYml.getStrings("display.sort.rarity-order").mapNotNull { - EnchantmentRarities.getByID(it) + EnchantmentRarities[it] }) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt index 3e853a85..fa5eb6da 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchant.kt @@ -58,15 +58,15 @@ abstract class EcoEnchant( val conditions: ConditionList val targets = config.getStrings("targets") - .mapNotNull { EnchantmentTargets.getByID(it) } + .mapNotNull { EnchantmentTargets[it] } val slots: Set get() = targets.map { it.slot }.toSet() - override val type = EnchantmentTypes.getByID(config.getString("type")) ?: EnchantmentTypes.values().first() + override val type = EnchantmentTypes[config.getString("type")] ?: EnchantmentTypes.values().first() override val enchantmentRarity = - EnchantmentRarities.getByID(config.getString("rarity")) ?: EnchantmentRarities.values().first() + EnchantmentRarities[config.getString("rarity")] ?: EnchantmentRarities.values().first() private val conflictNames = config.getStrings("conflicts") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt index 73ce55a2..846daa76 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchants/EcoEnchantLike.kt @@ -104,11 +104,11 @@ class VanillaEcoEnchantLike( override val config = plugin.vanillaEnchantsYml.getSubsection(enchant.key.key) override val type: EnchantmentType = - EnchantmentTypes.getByID(plugin.vanillaEnchantsYml.getString("${enchant.key.key}.type")) + EnchantmentTypes[plugin.vanillaEnchantsYml.getString("${enchant.key.key}.type")] ?: EnchantmentTypes.values().first() override val enchantmentRarity: EnchantmentRarity = - EnchantmentRarities.getByID(plugin.vanillaEnchantsYml.getString("${enchant.key.key}.rarity")) + EnchantmentRarities[plugin.vanillaEnchantsYml.getString("${enchant.key.key}.rarity")] ?: EnchantmentRarities.values().first() override val displayName = plugin.vanillaEnchantsYml.getFormattedString("${enchant.key.key}.name") diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/libreforge/TriggerEnchantType.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/libreforge/TriggerEnchantType.kt new file mode 100644 index 00000000..67707cfb --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/libreforge/TriggerEnchantType.kt @@ -0,0 +1,45 @@ +package com.willfp.ecoenchants.libreforge + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.fast.fast +import com.willfp.ecoenchants.enchants.EcoEnchant +import com.willfp.libreforge.triggers.Trigger +import com.willfp.libreforge.triggers.TriggerData +import com.willfp.libreforge.triggers.TriggerParameter +import org.bukkit.event.EventHandler +import org.bukkit.event.EventPriority +import org.bukkit.event.enchantment.EnchantItemEvent + +class TriggerEnchantType( + private val plugin: EcoPlugin, + private val type: String +) : Trigger("enchant_$type") { + override val parameters = setOf( + TriggerParameter.PLAYER, + TriggerParameter.LOCATION, + TriggerParameter.ITEM + ) + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + fun handleLevelling(event: EnchantItemEvent) { + val player = event.enchanter + + plugin.scheduler.runLater({ + if ( + event.item.fast().getEnchants(true).keys + .filterIsInstance() + .any { it.type.id.equals(type, ignoreCase = true) } + ) { + this.dispatch( + player, + TriggerData( + player = player, + location = player.location, + item = event.item, + value = event.expLevelCost.toDouble() + ) + ) + } + }, 2) + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarities.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarities.kt index ce0c073d..e73e38c7 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarities.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarities.kt @@ -1,82 +1,16 @@ package com.willfp.ecoenchants.rarity -import com.google.common.collect.HashBiMap -import com.google.common.collect.ImmutableSet -import com.willfp.eco.core.config.updating.ConfigUpdater +import com.willfp.eco.core.registry.Registry import com.willfp.ecoenchants.EcoEnchantsPlugin -import com.willfp.ecoenchants.enchants.EcoEnchants @Suppress("UNUSED") -object EnchantmentRarities { - private val BY_ID = HashBiMap.create() - - /** - * Get all registered [EnchantmentRarity]s. - * - * @return A list of all [EnchantmentRarity]s. - */ - @JvmStatic - fun values(): Set { - return ImmutableSet.copyOf(BY_ID.values) - } - - /** - * Get [String]s for all registered [EnchantmentRarity]s. - * - * @return A list of all [EnchantmentRarity]s. - */ - @JvmStatic - fun keySet(): Set { - return ImmutableSet.copyOf(BY_ID.keys) - } - - /** - * Get [EnchantmentRarity] matching key. - * - * @param id The key to search for. - * @return The matching [EnchantmentRarity], or null if not found. - */ - @JvmStatic - fun getByID(id: String?): EnchantmentRarity? { - return if (id == null) { - null - } else BY_ID[id] - } - - /** - * Update all [EnchantmentRarity]s. - * - * @param plugin Instance of EcoEnchants. - */ +object EnchantmentRarities : Registry() { @JvmStatic fun update(plugin: EcoEnchantsPlugin) { - for (type in values()) { - removeRarity(type) - } + clear() + for (config in plugin.rarityYml.getSubsections("rarities")) { - EnchantmentRarity(config) + register(EnchantmentRarity(config)) } } - - /** - * Remove [EnchantmentRarity] from EcoEnchants. - * - * @param rarity The [EnchantmentRarity] to remove. - */ - @JvmStatic - fun removeRarity(rarity: EnchantmentRarity) { - BY_ID.remove(rarity.id) - } - - /** - * Add new [EnchantmentRarity] to EcoEnchants. - * - * Only for internal use, rarities are automatically added in the constructor. - * - * @param rarity The [EnchantmentRarity] to add. - */ - internal fun addNewRarity(rarity: EnchantmentRarity) { - BY_ID.remove(rarity.id) - BY_ID[rarity.id] = rarity - } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarity.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarity.kt index e0a30aaf..694266b9 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarity.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/rarity/EnchantmentRarity.kt @@ -1,12 +1,13 @@ package com.willfp.ecoenchants.rarity import com.willfp.eco.core.config.interfaces.Config +import com.willfp.eco.core.registry.Registrable import java.util.* @Suppress("DEPRECATION") class EnchantmentRarity( internal val config: Config -) { +) : Registrable { val id = config.getString("id") val displayName = config.getFormattedString("display-name") val tableChance = config.getDouble("table-chance") @@ -14,8 +15,8 @@ class EnchantmentRarity( val villagerChance = config.getDouble("villager-chance") val lootChance = config.getDouble("loot-chance") - init { - EnchantmentRarities.addNewRarity(this) + override fun getID(): String { + return id } override fun equals(other: Any?): Boolean { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTarget.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTarget.kt index 93ac9afc..1e73cd75 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTarget.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTarget.kt @@ -4,12 +4,13 @@ import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.items.Items import com.willfp.eco.core.items.TestableItem import com.willfp.eco.core.recipe.parts.EmptyTestableItem +import com.willfp.eco.core.registry.Registrable import com.willfp.ecoenchants.EcoEnchantsPlugin import org.bukkit.entity.Player import org.bukkit.inventory.ItemStack import java.util.Objects -interface EnchantmentTarget { +interface EnchantmentTarget : Registrable { val id: String val displayName: String val slot: TargetSlot @@ -23,6 +24,10 @@ interface EnchantmentTarget { } return false } + + override fun getID(): String { + return this.id + } } class ConfiguredEnchantmentTarget( @@ -37,10 +42,6 @@ class ConfiguredEnchantmentTarget( .map { Items.lookup(it) } .filterNot { it is EmptyTestableItem } - init { - EnchantmentTargets.addNewTarget(this) - } - override fun equals(other: Any?): Boolean { if (other !is EnchantmentTarget) { return false diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTargets.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTargets.kt index 2bba01d2..32763cf4 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTargets.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/target/EnchantmentTargets.kt @@ -3,6 +3,7 @@ package com.willfp.ecoenchants.target import com.github.benmanes.caffeine.cache.Caffeine import com.google.common.collect.ImmutableSet import com.willfp.eco.core.items.HashedItem +import com.willfp.eco.core.registry.Registry import com.willfp.ecoenchants.EcoEnchantsPlugin import com.willfp.ecoenchants.enchants.EcoEnchant import com.willfp.ecoenchants.enchants.EcoEnchants @@ -12,34 +13,14 @@ import org.bukkit.enchantments.Enchantment import org.bukkit.inventory.ItemStack import java.util.concurrent.TimeUnit -object EnchantmentTargets { - private val BY_ID = mutableMapOf() - +object EnchantmentTargets: Registry() { init { - BY_ID["all"] = AllEnchantmentTarget + register(AllEnchantmentTarget) update(EcoEnchantsPlugin.instance) } - /** - * Get EnchantTarget matching name. - * - * @param name The name to search for. - * @return The matching EnchantTarget, or null if not found. - */ - @JvmStatic - fun getByID(name: String): EnchantmentTarget? { - return BY_ID[name] - } - - /** - * Get target from item. - * - * @param item The item. - * @return The target. - */ - @JvmStatic - fun getForItem(item: ItemStack): List { - return BY_ID.values + private fun getForItem(item: ItemStack): List { + return values() .filter { !it.id.equals("all", ignoreCase = true) } .filter { it.matches(item) } } @@ -54,54 +35,21 @@ object EnchantmentTargets { EcoEnchants.values().filter { it.canEnchantItem(this) } } - /** - * Get all targets. - * - * @return A set of all targets. - */ - @JvmStatic - fun values(): Set { - return ImmutableSet.copyOf(BY_ID.values) - } - @JvmStatic fun update(plugin: EcoEnchantsPlugin) { for (target in values()) { if (target is AllEnchantmentTarget) { continue } - removeTarget(target) + remove(target) } for (config in plugin.targetsYml.getSubsections("targets")) { - ConfiguredEnchantmentTarget(config) + register(ConfiguredEnchantmentTarget(config)) } AllEnchantmentTarget.updateItems() } - - /** - * Remove [EnchantmentTarget] from EcoEnchants. - * - * @param target The [EnchantmentTarget] to remove. - */ - @JvmStatic - fun removeTarget(target: EnchantmentTarget) { - BY_ID.remove(target.id) - } - - /** - * Add new [EnchantmentTarget] to EcoEnchants. - * - * Only for internal use, targets are automatically added in the - * constructor. - * - * @param target The [EnchantmentTarget] to add. - */ - internal fun addNewTarget(target: EnchantmentTarget) { - BY_ID.remove(target.id) - BY_ID[target.id] = target - } } private val enchantableCache = Caffeine.newBuilder() diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentType.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentType.kt index 5d167ae9..1763cec8 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentType.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentType.kt @@ -1,23 +1,23 @@ package com.willfp.ecoenchants.type +import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.config.interfaces.Config +import com.willfp.eco.core.registry.Registrable +import com.willfp.ecoenchants.libreforge.TriggerEnchantType import com.willfp.ecoenchants.mechanics.infiniteIfNegative +import com.willfp.libreforge.triggers.Triggers import java.util.* -@Suppress("DEPRECATION") class EnchantmentType( + private val plugin: EcoPlugin, internal val config: Config -) { +): Registrable { val id = config.getString("id") val format = config.getString("format") val limit = config.getInt("limit").infiniteIfNegative() val highLevelBias = config.getDouble("high-level-bias") val noGrindstone = config.getBool("no-grindstone") - init { - EnchantmentTypes.addNewType(this) - } - override fun equals(other: Any?): Boolean { if (this === other) { return true @@ -30,6 +30,14 @@ class EnchantmentType( return other.id == this.id } + override fun onRegister() { + Triggers.register(TriggerEnchantType(plugin, this.id)) + } + + override fun getID(): String { + return this.id + } + override fun hashCode(): Int { return Objects.hash(id) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentTypes.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentTypes.kt index 3aa386f4..9d840677 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentTypes.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/type/EnchantmentTypes.kt @@ -1,84 +1,22 @@ package com.willfp.ecoenchants.type -import com.google.common.collect.HashBiMap -import com.google.common.collect.ImmutableSet -import com.willfp.eco.core.config.updating.ConfigUpdater +import com.willfp.eco.core.registry.Registry import com.willfp.ecoenchants.EcoEnchantsPlugin -import com.willfp.ecoenchants.enchants.EcoEnchants -import com.willfp.ecoenchants.rarity.EnchantmentRarities -import com.willfp.ecoenchants.target.EnchantmentTargets @Suppress("UNUSED") -object EnchantmentTypes { - private val BY_ID = HashBiMap.create() - - /** - * Get all registered [EnchantmentType]s. - * - * @return A list of all [EnchantmentType]s. - */ - @JvmStatic - fun values(): Set { - return ImmutableSet.copyOf(BY_ID.values) - } - - /** - * Get [String]s for all registered [EnchantmentType]s. - * - * @return A list of all [EnchantmentType]s. - */ - @JvmStatic - fun keySet(): Set { - return ImmutableSet.copyOf(BY_ID.keys) - } - - /** - * Get [EnchantmentType] matching key. - * - * @param id The key to search for. - * @return The matching [EnchantmentType], or null if not found. - */ - @JvmStatic - fun getByID(id: String?): EnchantmentType? { - return if (id == null) { - null - } else BY_ID[id] - } - +object EnchantmentTypes: Registry() { /** * Update all [EnchantmentType]s. * * @param plugin Instance of EcoEnchants. */ - @JvmStatic fun update(plugin: EcoEnchantsPlugin) { for (type in values()) { - removeType(type) + clear() } + for (config in plugin.typesYml.getSubsections("types")) { - EnchantmentType(config) + register(EnchantmentType(plugin, config)) } } - - /** - * Remove [EnchantmentType] from EcoEnchants. - * - * @param type The [EnchantmentType] to remove. - */ - @JvmStatic - fun removeType(type: EnchantmentType) { - BY_ID.remove(type.id) - } - - /** - * Add new [EnchantmentType] to EcoEnchants. - * - * Only for internal use, types are automatically added in the constructor. - * - * @param type The [EnchantmentType] to add. - */ - internal fun addNewType(type: EnchantmentType) { - BY_ID.remove(type.id) - BY_ID[type.id] = type - } } diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 721e9244..10a0585b 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -36,8 +36,6 @@ permissions: ecoenchants.command.giverandombook: true ecoenchants.command.enchantinfo: true ecoenchants.command.gui: true - ecoenchants.command.import: true - ecoenchants.command.export: true ecoenchants.anvil.*: description: All anvil perks default: op @@ -64,10 +62,4 @@ permissions: default: true ecoenchants.command.gui: description: Allows the use of /ecoenchants gui. - default: true - ecoenchants.command.import: - description: Allows the use of /ecoenchants import. - default: op - ecoenchants.command.export: - description: Allows the use of /ecoenchants export. - default: op \ No newline at end of file + default: true \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 1ed908fb..82aa258d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ #libreforge-updater -#Mon Mar 13 13:39:16 GMT 2023 +#Tue Mar 28 18:28:43 BST 2023 kotlin.code.style=official libreforge-version=4.0.0 version=10.0.0