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 2ed1b893..45854137 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 @@ -21,6 +21,7 @@ import com.willfp.ecoenchants.integrations.plugins.CMIIntegration import com.willfp.ecoenchants.integrations.plugins.EssentialsIntegration import com.willfp.ecoenchants.mechanics.AnvilSupport import com.willfp.ecoenchants.mechanics.EnchantingTableSupport +import com.willfp.ecoenchants.mechanics.GrindstoneSupport import com.willfp.ecoenchants.mechanics.LootSupport import com.willfp.ecoenchants.mechanics.VillagerSupport import com.willfp.ecoenchants.target.ActiveEnchantUpdateListeners @@ -62,7 +63,8 @@ class EcoEnchantsPlugin : LibReforgePlugin() { EnchantingTableSupport(this), LootSupport(this), AnvilSupport(this), - LoreConversion(this) + LoreConversion(this), + GrindstoneSupport(this) ) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/EnchantRegistrationIntegration.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/EnchantRegistrationIntegration.kt index 07a279b5..6ffc76f8 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/EnchantRegistrationIntegration.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/EnchantRegistrationIntegration.kt @@ -16,7 +16,7 @@ interface EnchantRegistrationIntegration : Integration { object EnchantRegistrations { private val registered = mutableSetOf() - fun register(@NotNull integration: EnchantRegistrationIntegration) { + fun register(integration: EnchantRegistrationIntegration) { registered.add(integration) } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/plugins/EssentialsIntegration.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/plugins/EssentialsIntegration.kt index ed74eebc..3c70bf3a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/plugins/EssentialsIntegration.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/integrations/plugins/EssentialsIntegration.kt @@ -7,21 +7,33 @@ import com.willfp.ecoenchants.integrations.EnchantRegistrationIntegration import org.bukkit.NamespacedKey import org.bukkit.enchantments.Enchantment +@Suppress("UNCHECKED_CAST") class EssentialsIntegration: EnchantRegistrationIntegration { override fun registerEnchants() { for (enchantment in EcoEnchants.values()) { + // why aren't you using the api you PRd in + // because essentials named mending to repairing etc + Enchantments::class.java.getDeclaredField("ENCHANTMENTS") + .apply { + isAccessible = true + (get(null) as MutableMap).apply { + put(enchantment.id, enchantment) + put(enchantment.id.replace("_",""), enchantment) + } + } + Enchantments.registerEnchantment(enchantment.id, enchantment) } } - @Suppress("UNCHECKED_CAST", "DEPRECATION") override fun removeEnchant(enchantment: EcoEnchant) { - Enchantment::class.java.getDeclaredField("ENCHANTMENTS") + Enchantments::class.java.getDeclaredField("ENCHANTMENTS") .apply { isAccessible = true - (get(null) as MutableMap).apply { + (get(null) as MutableMap).apply { for (enchant in values.filterIsInstance()) { - remove(enchant.key) + remove(enchantment.id) + remove(enchantment.id.replace("_","")) } } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/AnvilSupport.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/AnvilSupport.kt index 21d5ece0..f2f4e254 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/AnvilSupport.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/AnvilSupport.kt @@ -50,13 +50,8 @@ class AnvilSupport( ProxyConstants.NMS_VERSION.substring(1) + "\$AnvilContainer" - /** - * Called when items are placed into an anvil. - * - * @param event The event to listen to. - */ @EventHandler(priority = EventPriority.HIGHEST) - fun onAnvilPrepare(@NotNull event: PrepareAnvilEvent) { + fun onAnvilPrepare(event: PrepareAnvilEvent) { val player = event.viewers.getOrNull(0) as? Player ?: return if (this.plugin.getProxy(OpenInventoryProxy::class.java) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/GrindstoneSupport.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/GrindstoneSupport.kt new file mode 100644 index 00000000..e57a7545 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/GrindstoneSupport.kt @@ -0,0 +1,91 @@ +package com.willfp.ecoenchants.mechanics + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.fast.fast +import com.willfp.eco.util.StringUtils +import com.willfp.ecoenchants.enchants.EcoEnchants +import com.willfp.ecoenchants.enchants.wrap +import org.bukkit.ChatColor +import org.bukkit.Material +import org.bukkit.enchantments.Enchantment +import org.bukkit.entity.EntityType +import org.bukkit.entity.ExperienceOrb +import org.bukkit.entity.Player +import org.bukkit.event.EventHandler +import org.bukkit.event.EventPriority +import org.bukkit.event.Listener +import org.bukkit.event.inventory.InventoryClickEvent +import org.bukkit.event.inventory.InventoryType +import org.bukkit.event.inventory.PrepareAnvilEvent +import org.bukkit.inventory.GrindstoneInventory +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.Damageable +import org.bukkit.inventory.meta.EnchantmentStorageMeta +import java.util.* +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min +import kotlin.math.pow +import kotlin.math.roundToInt + +@Suppress("DEPRECATION") +class GrindstoneSupport( + private val plugin: EcoPlugin +) : Listener { + fun onGrindstone(event: InventoryClickEvent) { + val player = event.whoClicked as? Player ?: return + + if (player.openInventory.topInventory.type != InventoryType.GRINDSTONE) { + return + } + + if (event.slotType != InventoryType.SlotType.RESULT) { + return + } + + val inventory = player.openInventory.topInventory as GrindstoneInventory + + val topEnchants = inventory.getItem(0)?.fast()?.getEnchants(true) ?: emptyMap() + val bottomEnchants = inventory.getItem(1)?.fast()?.getEnchants(true) ?: emptyMap() + + val toKeep = mutableMapOf() + + for ((enchant, level) in topEnchants) { + if (enchant.wrap().type.noGrindstone) { + toKeep[enchant] = level + } + } + + for ((enchant, level) in bottomEnchants) { + if (enchant.wrap().type.noGrindstone) { + val current = toKeep[enchant] ?: 0 + toKeep[enchant] = max(level, current) + } + } + + plugin.scheduler.run { + val result = inventory.getItem(2) + if (result == null || event.isCancelled) { + return@run + } + + val meta = result.itemMeta ?: return@run + + if (toKeep.isEmpty()) { + return@run + } + + if (meta is EnchantmentStorageMeta) { + for ((enchant, level) in toKeep) { + meta.addStoredEnchant(enchant, level, true) + } + } else { + for ((enchant, level) in toKeep) { + meta.addEnchant(enchant, level, true) + } + } + + result.itemMeta = meta + } + } +} 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 4496a917..5d167ae9 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 @@ -12,6 +12,7 @@ class EnchantmentType( 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) diff --git a/eco-core/core-plugin/src/main/resources/types.yml b/eco-core/core-plugin/src/main/resources/types.yml index 0a292067..425ef476 100644 --- a/eco-core/core-plugin/src/main/resources/types.yml +++ b/eco-core/core-plugin/src/main/resources/types.yml @@ -7,13 +7,16 @@ types: format: "&7" # The format of enchantments of this type in display limit: -1 # The maximum amount of enchants of this type on an item (-1 to disable) high-level-bias: 0 # The bias against getting high levels of enchants with this type. Between 0-1, 0 is no bias, 1 is only level 1. + no-grindstone: false # If enchants of this type should not be removable in the grindstone - id: curse format: "&c" limit: -1 high-level-bias: 0 + no-grindstone: true # If enchants of this type should not be removable in the grindstone - id: special format: "" limit: 1 high-level-bias: 0.7 + no-grindstone: false # If enchants of this type should not be removable in the grindstone