Added grindstone support, improved ess integration

This commit is contained in:
Auxilor 2022-09-02 11:42:22 +01:00
parent 8240cc76f0
commit c4590fb0eb
7 changed files with 116 additions and 12 deletions

View File

@ -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)
)
}

View File

@ -16,7 +16,7 @@ interface EnchantRegistrationIntegration : Integration {
object EnchantRegistrations {
private val registered = mutableSetOf<EnchantRegistrationIntegration>()
fun register(@NotNull integration: EnchantRegistrationIntegration) {
fun register(integration: EnchantRegistrationIntegration) {
registered.add(integration)
}

View File

@ -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<String, Enchantment>).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<NamespacedKey, Enchantment>).apply {
(get(null) as MutableMap<String, Enchantment>).apply {
for (enchant in values.filterIsInstance<EcoEnchant>()) {
remove(enchant.key)
remove(enchantment.id)
remove(enchantment.id.replace("_",""))
}
}
}

View File

@ -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)

View File

@ -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<Enchantment, Int>()
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
}
}
}

View File

@ -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)

View File

@ -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: "<gradient:#FB57EC:#EF1DEC>"
limit: 1
high-level-bias: 0.7
no-grindstone: false # If enchants of this type should not be removable in the grindstone