custom conflict now work.

This commit is contained in:
alexcrea 2024-02-01 19:38:49 +01:00
parent baa0c064da
commit 9f35c1a98d
6 changed files with 201 additions and 26 deletions

View file

@ -9,6 +9,7 @@ import io.delilaheve.util.ItemUtil.isBook
import io.delilaheve.util.ItemUtil.repairCost
import io.delilaheve.util.ItemUtil.repairFrom
import io.delilaheve.util.ItemUtil.setEnchantmentsUnsafe
import org.bukkit.entity.HumanEntity
import org.bukkit.entity.Player
import org.bukkit.event.Event
import org.bukkit.event.EventHandler
@ -18,6 +19,7 @@ import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
import org.bukkit.inventory.ItemStack
import org.bukkit.permissions.Permission
import kotlin.math.min
@ -63,7 +65,17 @@ class AnvilEventListener : Listener {
if (ConfigOptions.limitRepairCost) {
repairCost = min(repairCost, ConfigOptions.limitRepairValue)
}
event.result = resultItem
// Try to find player
val player = event.view.player
// Set object only if allowed
if(itemAllowed(resultItem,player)){
event.result = resultItem
} else{
event.result = null
return
}
/* Because Minecraft likes to have the final say in the repair cost displayed
* we need to wait for the event to end before overriding it, this ensures that
* we have the final say in the process. */
@ -87,16 +99,27 @@ class AnvilEventListener : Listener {
fun anvilExtractionCheck(event: InventoryClickEvent) {
val player = event.whoClicked as? Player ?: return
val inventory = event.inventory as? AnvilInventory ?: return
val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return
if(!player.hasPermission(bypassPermission)){
if (output.findEnchantments().hasConflicts() && !player.hasPermission(requirePermission)) {
return
}
}
if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return }
val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return
// Should be true most of the time
// But if permissions change in the anvil it can be false
if(!itemAllowed(output,player)){
event.result = Event.Result.DENY
return
}
event.result = Event.Result.ALLOW
}
private fun itemAllowed(item: ItemStack, player: HumanEntity): Boolean{
if(!player.hasPermission(bypassPermission)){
if(player.hasPermission(requirePermission)){
if(UnsafeEnchants.conflictManager.isConflicting(item))
return false
} else if(item.findEnchantments().hasConflicts())
return false
}
return true
}
}

View file

@ -1,9 +1,13 @@
package io.delilaheve
import io.delilaheve.util.ConfigOptions
import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.group.EnchantConflictManager
import xyz.alexcrea.group.ItemGroupManager
import java.io.File
import java.io.FileReader
/**
* Bukkit/Spigot/Paper plugin to alter enchantment max
@ -18,11 +22,13 @@ class UnsafeEnchants : JavaPlugin() {
const val unsafeBypassPermission = "ue.unsafe_all"
// Item Grouping Configuration file name
const val itemGroupingConfigName = "item_groups.yml"
// Conflict Configuration file name
const val enchantConflicConfigName = "enchant_conflict.yml"
// Current plugin instance
lateinit var instance: UnsafeEnchants
// Current item grouping configuration instance
lateinit var itemGroups: ItemGroupManager
lateinit var conflictManager: EnchantConflictManager
/**
* Logging handler
@ -41,24 +47,50 @@ class UnsafeEnchants : JavaPlugin() {
instance = this
saveDefaultConfig()
// Save default material grouping config
saveResource(itemGroupingConfigName,false)
// Load material grouping config
val itemGroupConfig = YamlConfiguration()
val configReader = this.getTextResource(itemGroupingConfigName)
if(configReader == null){
logger.severe("could no load item grouping configuration")
}else{
itemGroupConfig.load(configReader)
}
val itemGroupConfig = reloadResource(itemGroupingConfigName) ?: return
// Read material groups from config
itemGroups = ItemGroupManager()
itemGroups.prepareGroups(itemGroupConfig)
val itemGroupsManager = ItemGroupManager()
itemGroupsManager.prepareGroups(itemGroupConfig)
// Load enchantment conflicts config
val conflictConfig = reloadResource(enchantConflicConfigName) ?: return
// Read conflicts from config and material group manager
conflictManager = EnchantConflictManager()
conflictManager.prepareConflicts(conflictConfig,itemGroupsManager)
server.pluginManager.registerEvents(
AnvilEventListener(),
this
)
}
private fun reloadResource(resourceName: String,
hardFailSafe:Boolean = true): YamlConfiguration?{
// Save default resource
val file = File(dataFolder,resourceName)
if(!file.exists()){
saveResource(resourceName,false)
}
// Load resource
val yamlConfig = YamlConfiguration()
try {
val configReader = FileReader(file)
yamlConfig.load(configReader)
} catch (test: Exception){
if(hardFailSafe){
// This is important and may impact gameplay if it does not load.
// Failsafe is to stop the plugin
logger.severe("Resource $resourceName Could not be load or reload.")
logger.severe("Disabling plugin.")
Bukkit.getPluginManager().disablePlugin(this)
}else{
logger.warning("Resource $resourceName Could not be load or reload.")
}
return null
}
return yamlConfig
}
}

View file

@ -62,7 +62,7 @@ object EnchantmentUtil {
enchantment2.conflictsWith(enchantment1)
}
if (hasConflict) {
return hasConflict
return true
}
}
return false