Improved NMS registration more, added customisable maximum repair cost

This commit is contained in:
Will FP 2023-12-09 13:58:20 +00:00
parent 48fd2dd67d
commit 26dcf92e5b
6 changed files with 32 additions and 6 deletions

View File

@ -2,7 +2,6 @@ package com.willfp.ecoenchants.proxy.v1_20_R3
import com.willfp.ecoenchants.enchant.EcoEnchant
import com.willfp.ecoenchants.enchant.EcoEnchants
import com.willfp.ecoenchants.enchant.impl.EcoEnchantBase
import com.willfp.ecoenchants.enchant.registration.modern.ModernEnchantmentRegistererProxy
import com.willfp.ecoenchants.proxy.v1_20_R3.registration.DelegatedCraftEnchantment
import com.willfp.ecoenchants.proxy.v1_20_R3.registration.ModifiedVanillaCraftEnchantment
@ -11,6 +10,7 @@ import net.minecraft.core.MappedRegistry
import net.minecraft.core.Registry
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.core.registries.Registries
import net.minecraft.world.item.enchantment.Enchantments
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.v1_20_R3.CraftRegistry
import org.bukkit.craftbukkit.v1_20_R3.CraftServer
@ -30,19 +30,31 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy {
.get(Bukkit.getServer())
as HashMap<Class<*>, org.bukkit.Registry<*>>
private val vanillaEnchantments = Enchantments::class.java
.declaredFields
.filter { it.type == net.minecraft.world.item.enchantment.Enchantment::class.java }
.map { it.get(null) as net.minecraft.world.item.enchantment.Enchantment }
.mapNotNull { BuiltInRegistries.ENCHANTMENT.getKey(it) }
.map { CraftNamespacedKey.fromMinecraft(it) }
.toSet()
override fun replaceRegistry() {
val server = Bukkit.getServer() as CraftServer
@Suppress("UNCHECKED_CAST")
registries[Enchantment::class.java] = CraftRegistry(
Enchantment::class.java,
Enchantment::class.java as Class<in Enchantment?>,
server.handle.server.registryAccess().registryOrThrow(Registries.ENCHANTMENT)
) { key, registry ->
val enchant = EcoEnchants.getByID(key.key)
val isVanilla = vanillaEnchantments.contains(key)
val eco = EcoEnchants.getByID(key.key)
if (enchant == null) {
if (isVanilla) {
ModifiedVanillaCraftEnchantment(key, registry)
} else if (eco != null) {
eco as Enchantment
} else {
enchant as Enchantment
null
}
}
}

View File

@ -7,9 +7,11 @@ import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.EquipmentSlot
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.MobType
import net.minecraft.world.inventory.AnvilMenu
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.enchantment.Enchantment
import net.minecraft.world.item.enchantment.EnchantmentCategory
import net.minecraft.world.item.enchantment.EnchantmentHelper
import org.bukkit.craftbukkit.v1_20_R3.enchantments.CraftEnchantment
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack
import java.util.Objects

View File

@ -106,6 +106,11 @@ class EcoEnchantsPlugin : LibreforgePlugin() {
legacyRegisterVanillaEnchantmentData(this)
}
// Replace registry on reload to manage some enchantment removal logic
if (Prerequisite.HAS_1_20_3.isMet) {
this.getProxy(ModernEnchantmentRegistererProxy::class.java).replaceRegistry()
}
DisplayCache.reload()
EnchantSorter.reload(this)
ExtraItemSupport.reload(this)

View File

@ -14,6 +14,7 @@ import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.EnchantmentStorageMeta
@ -38,6 +39,10 @@ interface OpenInventoryProxy {
fun getOpenInventory(player: Player): Any
}
interface AnvilRepairCostProxy {
fun setMaxRepairCost(inventory: AnvilInventory, cost: Int)
}
@Suppress("DEPRECATION")
class AnvilSupport(
private val plugin: EcoPlugin
@ -133,6 +138,7 @@ class AnvilSupport(
outItem.fast().repairCost = (repairCost + 1) * 2 - 1
}
event.inventory.maximumRepairCost = plugin.configYml.getInt("anvil.max-repair-cost").infiniteIfNegative()
event.inventory.repairCost = cost
event.result = outItem
event.inventory.setItem(2, outItem)

View File

@ -29,6 +29,7 @@ anvil:
cost-exponent: 0.95 # The exponent for each enchant level to prevent constant "Too Expensive!" problems
enchant-limit: -1 # The limit for the amount of enchantments on an item (-1 to disable)
use-rework-penalty: true # If the rework penalty should be applied
max-repair-cost: 256 # Override the maximum repair cost (vanilla default is 40) (-1 to make it infinite)
# Options for how enchantments are displayed on items
display:

View File

@ -4,7 +4,7 @@ messages:
not-player: "&cThis command must be run by a player"
invalid-command: "&cUnknown subcommand!"
reloaded: "Reloaded!"
reloaded-1203: "Reloaded! If you have removed enchantments, you must restart your server for the changes to take effect."
reloaded-1203: "Reloaded! You might need to restart your server if you have removed enchantments."
invalid-player: "&cInvalid Player!"
requires-player: "&cRequires a Player!"
enabled-descriptions: "&fYou have successfully &aenabled &fenchantment descriptions!"