mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add an api (#14)
This allows developers of custom enchantment plugins to be compatible or use functionality of Custom Anvil.
This commit is contained in:
commit
de5fa240a1
40 changed files with 1885 additions and 327 deletions
|
|
@ -4,6 +4,8 @@ import io.delilaheve.util.ConfigOptions
|
|||
import org.bukkit.Bukkit
|
||||
import org.bukkit.configuration.file.YamlConfiguration
|
||||
import org.bukkit.plugin.java.JavaPlugin
|
||||
import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent
|
||||
import xyz.alexcrea.cuanvil.api.event.CAEnchantRegistryReadyEvent
|
||||
import xyz.alexcrea.cuanvil.command.EditConfigExecutor
|
||||
import xyz.alexcrea.cuanvil.command.ReloadExecutor
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
|
|
@ -80,8 +82,6 @@ class CustomAnvil : JavaPlugin() {
|
|||
override fun onEnable() {
|
||||
instance = this
|
||||
|
||||
val pluginManager = Bukkit.getPluginManager();
|
||||
|
||||
// Disable old plugin name if exist
|
||||
val potentialPlugin = Bukkit.getPluginManager().getPlugin("UnsafeEnchantsPlus")
|
||||
if (potentialPlugin != null) {
|
||||
|
|
@ -90,40 +90,53 @@ class CustomAnvil : JavaPlugin() {
|
|||
logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus")
|
||||
}
|
||||
|
||||
// Load dependency
|
||||
DependencyManager.loadDependency()
|
||||
|
||||
// Register enchantments
|
||||
CAEnchantmentRegistry.getInstance().registerStartupEnchantments()
|
||||
// Add commands
|
||||
prepareCommand()
|
||||
|
||||
// Load chat listener
|
||||
chatListener = ChatEventListener()
|
||||
pluginManager.registerEvents(chatListener, this)
|
||||
server.pluginManager.registerEvents(chatListener, this)
|
||||
|
||||
// Load dependency
|
||||
DependencyManager.loadDependency()
|
||||
|
||||
// Register anvil events
|
||||
server.pluginManager.registerEvents(AnvilEventListener(DependencyManager.packetManager), this)
|
||||
|
||||
// Load metrics
|
||||
Metrics(this, bstatsPluginId)
|
||||
|
||||
// Load other thing later.
|
||||
// It is so other dependent plugins can implement there event listener before we fire them.
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(this, {loadEnchantmentSystem()}, 0L)
|
||||
}
|
||||
|
||||
private fun loadEnchantmentSystem(){
|
||||
// Load default configuration
|
||||
if (!ConfigHolder.loadDefaultConfig()) return
|
||||
|
||||
// Register enchantments
|
||||
CAEnchantmentRegistry.getInstance().registerBukkit()
|
||||
DependencyManager.registerEnchantments()
|
||||
|
||||
val enchantReadyEvent = CAEnchantRegistryReadyEvent()
|
||||
server.pluginManager.callEvent(enchantReadyEvent)
|
||||
|
||||
// Load config
|
||||
val success = ConfigHolder.loadConfig()
|
||||
if (!success) return
|
||||
if (!ConfigHolder.loadNonDefaultConfig()) return
|
||||
|
||||
// temporary: handle 1.21 update
|
||||
Update_1_21.handleUpdate()
|
||||
|
||||
// Handle custom enchant config
|
||||
DependencyManager.handleConfigChanges(this)
|
||||
val configReadyEvent = CAConfigReadyEvent()
|
||||
server.pluginManager.callEvent(configReadyEvent)
|
||||
|
||||
// Load gui constants //TODO maybe something better later
|
||||
MainConfigGui.getInstance().init(DependencyManager.packetManager)
|
||||
GuiSharedConstant.loadConstants()
|
||||
|
||||
// Load metrics
|
||||
Metrics(this, bstatsPluginId)
|
||||
|
||||
// Add commands to reload the plugin
|
||||
prepareCommand()
|
||||
|
||||
server.pluginManager.registerEvents(
|
||||
AnvilEventListener(DependencyManager.packetManager),
|
||||
this
|
||||
)
|
||||
// Register enchantment of compatible plugin and load configuration change.
|
||||
DependencyManager.handleCompatibilityConfig()
|
||||
}
|
||||
|
||||
fun reloadResource(
|
||||
|
|
|
|||
|
|
@ -29,7 +29,6 @@ object ItemUtil {
|
|||
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<CAEnchantment, Int>) {
|
||||
CAEnchantment.clearEnchants(this)
|
||||
|
||||
//TODO maybe faster methode to add vanilla enchantment. maybe move this function to wrapped enchantment
|
||||
enchantments.forEach { (enchantment, level) ->
|
||||
enchantment.addEnchantmentUnsafe(this, level)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,8 +36,8 @@ class ReloadExecutor : CommandExecutor {
|
|||
|
||||
// Then update all global gui containing value from config
|
||||
BasicConfigGui.getInstance()?.updateGuiValues()
|
||||
EnchantCostConfigGui.INSTANCE.updateGuiValues()
|
||||
EnchantLimitConfigGui.INSTANCE.updateGuiValues()
|
||||
EnchantCostConfigGui.getInstance()?.updateGuiValues()
|
||||
EnchantLimitConfigGui.getInstance()?.updateGuiValues()
|
||||
|
||||
EnchantConflictGui.INSTANCE.reloadValues()
|
||||
GroupConfigGui.INSTANCE.reloadValues()
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package xyz.alexcrea.cuanvil.dependency
|
||||
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.plugin.Plugin
|
||||
import xyz.alexcrea.cuanvil.dependency.protocolib.NoProtocoLib
|
||||
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager
|
||||
import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper
|
||||
import java.io.File
|
||||
|
||||
object DependencyManager {
|
||||
|
||||
|
|
@ -19,7 +17,7 @@ object DependencyManager {
|
|||
// ProtocolLib dependency
|
||||
packetManager =
|
||||
if(pluginManager.isPluginEnabled("ProtocolLib")) ProtocoLibWrapper()
|
||||
else NoProtocoLib()
|
||||
else NoProtocoLib()
|
||||
|
||||
// Enchantment Squared dependency
|
||||
if(pluginManager.isPluginEnabled("EnchantsSquared")){
|
||||
|
|
@ -35,11 +33,14 @@ object DependencyManager {
|
|||
|
||||
}
|
||||
|
||||
fun handleConfigChanges(plugin: Plugin) {
|
||||
val folder = File(plugin.dataFolder, "compatibility")
|
||||
|
||||
fun handleCompatibilityConfig() {
|
||||
enchantmentSquaredCompatibility?.registerPluginConfiguration()
|
||||
ecoEnchantCompatibility?.registerPluginConfiguration(folder)
|
||||
|
||||
}
|
||||
|
||||
fun registerEnchantments() {
|
||||
enchantmentSquaredCompatibility?.registerEnchantments()
|
||||
ecoEnchantCompatibility?.registerEnchantments()
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,14 +2,10 @@ package xyz.alexcrea.cuanvil.dependency
|
|||
|
||||
import com.willfp.ecoenchants.enchant.EcoEnchants
|
||||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.configuration.file.YamlConfiguration
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
import org.bukkit.plugin.Plugin
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||
import xyz.alexcrea.cuanvil.api.EnchantmentApi
|
||||
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEcoEnchant
|
||||
import java.io.File
|
||||
|
||||
class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
|
||||
|
||||
|
|
@ -22,57 +18,14 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
|
|||
}
|
||||
|
||||
fun registerEnchantments() {
|
||||
val registery = CAEnchantmentRegistry.getInstance()
|
||||
for (ecoEnchant in EcoEnchants.values()) {
|
||||
val enchantments: CAEnchantment = CAEcoEnchant(ecoEnchant)
|
||||
|
||||
registery.unregister(registery.getByKey(ecoEnchant.enchantment.key)) // As eco enchants are considered real enchantment, we need to unregister it.
|
||||
registery.register(enchantments)
|
||||
}
|
||||
}
|
||||
|
||||
fun registerPluginConfiguration(folder: File){
|
||||
val compatibilityFile = File(folder, "ecoEnchant.yml")
|
||||
|
||||
if(compatibilityFile.exists()){
|
||||
folder.mkdirs()
|
||||
compatibilityFile.createNewFile()
|
||||
}
|
||||
|
||||
val config = YamlConfiguration.loadConfiguration(compatibilityFile)
|
||||
val defaultConfig = ConfigHolder.DEFAULT_CONFIG.config
|
||||
var doSave = false
|
||||
CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...")
|
||||
|
||||
for (ecoEnchant in EcoEnchants.values()) {
|
||||
val enchantment = CAEnchantmentRegistry.getInstance().getByKey(ecoEnchant.enchantmentKey)
|
||||
|
||||
if(enchantment == null){
|
||||
CustomAnvil.instance.logger.warning("Could not find " + ecoEnchant.enchantmentKey + "testing compatibility.")
|
||||
continue
|
||||
}
|
||||
|
||||
// Write enchantment value if needed
|
||||
val testPath = "default.${enchantment.key.key}"
|
||||
if(!config.getBoolean(testPath, false)){
|
||||
doSave = true
|
||||
config[testPath] = true
|
||||
|
||||
defaultConfig["enchant_limits.${enchantment.key.key}"] = enchantment.defaultMaxLevel()
|
||||
|
||||
val rarity = enchantment.defaultRarity()
|
||||
defaultConfig["enchant_values.${enchantment.key.key}.item"] = rarity.itemValue
|
||||
defaultConfig["enchant_values.${enchantment.key.key}.book"] = rarity.bookValue
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(doSave){
|
||||
config.save(compatibilityFile)
|
||||
ConfigHolder.DEFAULT_CONFIG.saveToDisk(true)
|
||||
|
||||
CustomAnvil.instance.logger.info("Saved default for new eco enchant enchantments.")
|
||||
EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant.
|
||||
EnchantmentApi.registerEnchantment(CAEcoEnchant(ecoEnchant))
|
||||
}
|
||||
|
||||
CustomAnvil.instance.logger.info("Eco Enchant should now work as expected !")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,14 +3,19 @@ package xyz.alexcrea.cuanvil.dependency
|
|||
import io.delilaheve.CustomAnvil
|
||||
import me.athlaeos.enchantssquared.enchantments.CustomEnchant
|
||||
import me.athlaeos.enchantssquared.managers.CustomEnchantManager
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.plugin.Plugin
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.api.ConflictBuilder
|
||||
import xyz.alexcrea.cuanvil.api.EnchantmentApi
|
||||
import xyz.alexcrea.cuanvil.api.MaterialGroupApi
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||
import xyz.alexcrea.cuanvil.enchant.bulk.EnchantSquaredBulkOperation
|
||||
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment
|
||||
import xyz.alexcrea.cuanvil.group.IncludeGroup
|
||||
import java.util.*
|
||||
|
||||
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
|
||||
|
|
@ -31,14 +36,18 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
|||
}
|
||||
|
||||
fun registerEnchantments(){
|
||||
CustomAnvil.instance.logger.info("Preparing Enchantment Squared compatibility...")
|
||||
|
||||
// Register enchantments
|
||||
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
|
||||
CAEnchantmentRegistry.getInstance().register(
|
||||
CAEnchantSquaredEnchantment(
|
||||
enchant
|
||||
)
|
||||
)
|
||||
EnchantmentApi.registerEnchantment(CAEnchantSquaredEnchantment(enchant))
|
||||
}
|
||||
|
||||
// Register bulk operation
|
||||
val bulkOpperations = EnchantSquaredBulkOperation()
|
||||
EnchantmentApi.addBulkGet(bulkOpperations)
|
||||
EnchantmentApi.addBulkClean(bulkOpperations)
|
||||
|
||||
}
|
||||
|
||||
fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap<CAEnchantment, Int>) {
|
||||
|
|
@ -50,10 +59,6 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
|||
|
||||
}
|
||||
|
||||
fun clearEnchantments(item: ItemStack) {
|
||||
CustomEnchantManager.getInstance().removeAllEnchants(item)
|
||||
}
|
||||
|
||||
fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey{
|
||||
return NamespacedKey.fromString(enchant.type.lowercase(Locale.getDefault()), this.enchantmentSquaredPlugin)!!
|
||||
}
|
||||
|
|
@ -61,14 +66,8 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
|||
return CAEnchantment.getByKey(getKeyFromEnchant(enchant))!!
|
||||
}
|
||||
|
||||
|
||||
private val IS_READY_PATH = "enchantment_square_ready"
|
||||
fun registerPluginConfiguration(){
|
||||
val defaultConfig = ConfigHolder.DEFAULT_CONFIG.config
|
||||
val isReady = defaultConfig.getBoolean(IS_READY_PATH, false)
|
||||
if(isReady) return
|
||||
|
||||
CustomAnvil.instance.logger.info("Preparing configuration for Enchantment Squared...")
|
||||
CustomAnvil.instance.logger.info("Preparing Enchantment Squared config...")
|
||||
|
||||
// Prepare enchantments
|
||||
val esEnchantments = ArrayList<CAEnchantSquaredEnchantment>()
|
||||
|
|
@ -76,91 +75,62 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
|||
esEnchantments.add(getWrappedEnchant(enchant) as CAEnchantSquaredEnchantment)
|
||||
}
|
||||
|
||||
// Write default level limit and xp cost
|
||||
for (enchantment in esEnchantments) {
|
||||
defaultConfig["enchant_limits.${enchantment.key.key}"] = enchantment.defaultMaxLevel()
|
||||
|
||||
val rarity = enchantment.defaultRarity()
|
||||
defaultConfig["enchant_values.${enchantment.key.key}.item"] = rarity.itemValue
|
||||
defaultConfig["enchant_values.${enchantment.key.key}.book"] = rarity.bookValue
|
||||
}
|
||||
|
||||
// Write groups and conflicts
|
||||
writeMissingGroups()
|
||||
writeMaterialRestriction(esEnchantments)
|
||||
writeEnchantmentConflicts(esEnchantments)
|
||||
|
||||
// Set ready
|
||||
defaultConfig[IS_READY_PATH] = true
|
||||
|
||||
// Save
|
||||
ConfigHolder.DEFAULT_CONFIG.saveToDisk(true)
|
||||
ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true)
|
||||
ConfigHolder.CONFLICT_HOLDER.saveToDisk(true)
|
||||
|
||||
// Reload
|
||||
ConfigHolder.ITEM_GROUP_HOLDER.reload()
|
||||
|
||||
CustomAnvil.instance.logger.info("Enchantment Squared should now work as expected !")
|
||||
}
|
||||
|
||||
private fun writeMissingGroups(){
|
||||
// Write group that do not exist on custom anvil.
|
||||
// (Tools group regroup most of the tool items. I did not create a seperated group for theses)
|
||||
val groupConfig = ConfigHolder.ITEM_GROUP_HOLDER.config
|
||||
if(!groupConfig.isConfigurationSection("pickaxes")){
|
||||
groupConfig["pickaxes.type"] = "include"
|
||||
groupConfig["pickaxes.items"] = listOf("wooden_pickaxe", "stone_pickaxe", "iron_pickaxe", "diamond_pickaxe", "golden_pickaxe", "netherite_pickaxe")
|
||||
}
|
||||
val pickaxes = IncludeGroup("pickaxes")
|
||||
pickaxes.addAll(Material.WOODEN_PICKAXE, Material.STONE_PICKAXE, Material.IRON_PICKAXE, Material.DIAMOND_PICKAXE, Material.GOLDEN_PICKAXE, Material.NETHERITE_PICKAXE)
|
||||
MaterialGroupApi.addMaterialGroup(pickaxes)
|
||||
|
||||
if(!groupConfig.isConfigurationSection("shovels")){
|
||||
groupConfig["shovels.type"] = "include"
|
||||
groupConfig["shovels.items"] = listOf("wooden_shovel", "stone_shovel", "iron_shovel", "diamond_shovel", "golden_shovel", "netherite_shovel")
|
||||
}
|
||||
val shovels = IncludeGroup("shovels")
|
||||
shovels.addAll(Material.WOODEN_SHOVEL, Material.STONE_SHOVEL, Material.IRON_SHOVEL, Material.DIAMOND_SHOVEL, Material.GOLDEN_SHOVEL, Material.NETHERITE_SHOVEL)
|
||||
MaterialGroupApi.addMaterialGroup(shovels)
|
||||
|
||||
if(!groupConfig.isConfigurationSection("hoes")){
|
||||
groupConfig["hoes.type"] = "include"
|
||||
groupConfig["hoes.items"] = listOf("wooden_hoe", "stone_hoe", "iron_hoe", "diamond_hoe", "golden_hoe", "netherite_hoe")
|
||||
}
|
||||
val hoes = IncludeGroup("hoes")
|
||||
hoes.addAll(Material.WOODEN_HOE, Material.STONE_HOE, Material.IRON_HOE, Material.DIAMOND_HOE, Material.GOLDEN_HOE, Material.NETHERITE_HOE)
|
||||
MaterialGroupApi.addMaterialGroup(hoes)
|
||||
|
||||
if(!groupConfig.isConfigurationSection("shield")){
|
||||
groupConfig["shield.type"] = "include"
|
||||
groupConfig["shield.items"] = listOf("shield")
|
||||
}
|
||||
val shield = IncludeGroup("shield")
|
||||
shield.addToPolicy(Material.SHIELD)
|
||||
MaterialGroupApi.addMaterialGroup(shield)
|
||||
|
||||
if(!groupConfig.isConfigurationSection("elytra")){
|
||||
groupConfig["elytra.type"] = "include"
|
||||
groupConfig["elytra.items"] = listOf("elytra")
|
||||
}
|
||||
val elytra = IncludeGroup("elytra")
|
||||
elytra.addToPolicy(Material.ELYTRA)
|
||||
MaterialGroupApi.addMaterialGroup(elytra)
|
||||
|
||||
if(!groupConfig.isConfigurationSection("trinkets")){
|
||||
groupConfig["trinkets.type"] = "include"
|
||||
groupConfig["trinkets.items"] = listOf("rotten_flesh")
|
||||
}
|
||||
val trinkets = IncludeGroup("trinkets")
|
||||
trinkets.addToPolicy(Material.ROTTEN_FLESH)
|
||||
MaterialGroupApi.addMaterialGroup(trinkets)
|
||||
|
||||
}
|
||||
|
||||
private fun writeMaterialRestriction(esEnchantments: List<CAEnchantSquaredEnchantment>){
|
||||
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
|
||||
for (enchantment in esEnchantments) {
|
||||
val restrictionName = "restriction_${enchantment.key.key}"
|
||||
if(!conflictConfig.isConfigurationSection(restrictionName)){
|
||||
conflictConfig["$restrictionName.enchantments"] = listOf(enchantment.name)
|
||||
val conflict = ConflictBuilder("restriction_${enchantment.key.key}", CustomAnvil.instance)
|
||||
conflict.addEnchantment(enchantment)
|
||||
|
||||
// Get allowed groups
|
||||
val listOfAllowed = ArrayList<String>()
|
||||
listOfAllowed.add("enchanted_book") // enchanted book is allowed in any case.
|
||||
// enchanted book is allowed in any case.
|
||||
conflict.addExcludedGroup("enchanted_book")
|
||||
|
||||
for (esGroup in enchantment.enchant.compatibleItems) {
|
||||
val caGroup = esGroupToCAGroup(esGroup)
|
||||
if(caGroup == null){
|
||||
CustomAnvil.instance.logger.info("Could not find equivalent custom anvil group for $esGroup")
|
||||
continue
|
||||
}
|
||||
listOfAllowed.add(caGroup)
|
||||
// Get allowed groups
|
||||
for (esGroup in enchantment.enchant.compatibleItems) {
|
||||
val caGroup = esGroupToCAGroup(esGroup)
|
||||
if(caGroup == null){
|
||||
CustomAnvil.instance.logger.info("Could not find equivalent custom anvil group for $esGroup")
|
||||
continue
|
||||
}
|
||||
conflictConfig["$restrictionName.notAffectedGroups"] = listOfAllowed
|
||||
conflict.addExcludedGroup(caGroup)
|
||||
}
|
||||
|
||||
conflict.registerIfAbsent()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -181,18 +151,12 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
|
|||
}
|
||||
|
||||
private fun writeConflict(enchantment1: CAEnchantment, enchantment2: CAEnchantment){
|
||||
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
|
||||
val conflictPath = "${enchantment1.name}_with_${enchantment2.name}_conflict"
|
||||
val conflict = ConflictBuilder("${enchantment1.name}_with_${enchantment2.name}_conflict", CustomAnvil.instance)
|
||||
|
||||
if(!conflictConfig.isConfigurationSection(conflictPath)){
|
||||
conflictConfig["$conflictPath.enchantments"] = listOf(enchantment1.name, enchantment2.name)
|
||||
|
||||
val empty: List<String> = Collections.emptyList()
|
||||
conflictConfig["$conflictPath.notAffectedGroups"] = empty
|
||||
|
||||
conflictConfig["$conflictPath.maxEnchantmentBeforeConflict"] = 1
|
||||
}
|
||||
conflict.addEnchantment(enchantment1).addEnchantment(enchantment2)
|
||||
|
||||
conflict.setMaxBeforeConflict(1)
|
||||
conflict.registerIfAbsent()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -25,13 +25,37 @@ abstract class AbstractMaterialGroup(private val name: String) {
|
|||
|
||||
/**
|
||||
* Push a material to this group to follow this group policy
|
||||
* @return this instance.
|
||||
*/
|
||||
abstract fun addToPolicy(mat: Material)
|
||||
abstract fun addToPolicy(mat: Material): AbstractMaterialGroup
|
||||
|
||||
/**
|
||||
* Push a list of material to this group to follow this group policy
|
||||
* @return this instance.
|
||||
*/
|
||||
fun addAll(vararg materials: Material): AbstractMaterialGroup {
|
||||
for (material in materials) {
|
||||
addToPolicy(material)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a group to this group to follow this group policy
|
||||
* @return this instance.
|
||||
*/
|
||||
abstract fun addToPolicy(other: AbstractMaterialGroup)
|
||||
abstract fun addToPolicy(other: AbstractMaterialGroup): AbstractMaterialGroup
|
||||
|
||||
/**
|
||||
* Push a list of group to this group to follow this group policy
|
||||
* @return this instance.
|
||||
*/
|
||||
fun addAll(vararg otherList: AbstractMaterialGroup): AbstractMaterialGroup {
|
||||
for (group in otherList) {
|
||||
addToPolicy(group)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the group contained material as a set
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import org.bukkit.Material
|
|||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||
|
||||
class EnchantConflictGroup(
|
||||
private val name: String,
|
||||
val name: String,
|
||||
private val cantConflict: AbstractMaterialGroup,
|
||||
var minBeforeBlock: Int
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment
|
|||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class EnchantConflictManager {
|
||||
|
||||
|
|
@ -28,7 +27,7 @@ class EnchantConflictManager {
|
|||
private const val FUTURE_USE_PATH = "useInFuture"
|
||||
|
||||
// Default name for a joining group
|
||||
private const val DEFAULT_GROUP_NAME = "joinedGroup"
|
||||
const val DEFAULT_GROUP_NAME = "joinedGroup"
|
||||
|
||||
// 1.20.5 compatibility TODO better update system
|
||||
private val SWEEPING_EDGE_ENCHANT =
|
||||
|
|
@ -53,12 +52,21 @@ class EnchantConflictManager {
|
|||
val section = config.getConfigurationSection(key)!!
|
||||
val conflict = createConflict(section, itemManager, key)
|
||||
|
||||
addConflictToEnchantments(conflict)
|
||||
conflictList.add(conflict)
|
||||
addConflict(conflict)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun addConflict(conflict: EnchantConflictGroup){
|
||||
addConflictToEnchantments(conflict)
|
||||
conflictList.add(conflict)
|
||||
}
|
||||
|
||||
fun removeConflict(conflict: EnchantConflictGroup){
|
||||
removeConflictFromEnchantments(conflict)
|
||||
conflictList.remove(conflict)
|
||||
}
|
||||
|
||||
// Add the conflict to enchantments
|
||||
private fun addConflictToEnchantments(conflict: EnchantConflictGroup) {
|
||||
conflict.getEnchants().forEach { enchant ->
|
||||
|
|
@ -66,6 +74,13 @@ class EnchantConflictManager {
|
|||
}
|
||||
}
|
||||
|
||||
// Remove the conflict from enchantments
|
||||
private fun removeConflictFromEnchantments(conflict: EnchantConflictGroup) {
|
||||
conflict.getEnchants().forEach { enchant ->
|
||||
enchant.removeConflict(conflict)
|
||||
}
|
||||
}
|
||||
|
||||
// create and read a conflict from a yaml section
|
||||
private fun createConflict(
|
||||
section: ConfigurationSection,
|
||||
|
|
@ -140,7 +155,7 @@ class EnchantConflictManager {
|
|||
): AbstractMaterialGroup {
|
||||
val group = itemManager.get(groupName)
|
||||
if (group == null) {
|
||||
CustomAnvil.instance.logger.warning("Group $groupName do not exist but is ask by conflict $conflictName")
|
||||
CustomAnvil.instance.logger.warning("Material group $groupName do not exist but is ask by conflict $conflictName")
|
||||
return IncludeGroup("error_placeholder")
|
||||
}
|
||||
|
||||
|
|
@ -175,7 +190,7 @@ class EnchantConflictManager {
|
|||
if(doConflict){
|
||||
return ConflictType.ENCHANTMENT_CONFLICT
|
||||
}
|
||||
;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -188,14 +203,14 @@ class EnchantConflictManager {
|
|||
|
||||
}
|
||||
|
||||
return result;
|
||||
return result
|
||||
}
|
||||
|
||||
private fun createPartialResult(item: ItemStack, enchantments: Map<CAEnchantment, Int>): ItemStack {
|
||||
val newItem = item.clone()
|
||||
|
||||
CAEnchantment.clearEnchants(newItem)
|
||||
enchantments.forEach{//TODO maybe bulk add if possible
|
||||
enchantments.forEach{
|
||||
enchantment -> enchantment.key.addEnchantmentUnsafe(newItem, enchantment.value)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,14 +20,18 @@ class ExcludeGroup(name: String) : AbstractMaterialGroup(name) {
|
|||
return false
|
||||
}
|
||||
|
||||
override fun addToPolicy(mat: Material) {
|
||||
override fun addToPolicy(mat: Material): ExcludeGroup {
|
||||
includedMaterial.remove(mat)
|
||||
groupItems.remove(mat)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
override fun addToPolicy(other: AbstractMaterialGroup) {
|
||||
override fun addToPolicy(other: AbstractMaterialGroup): ExcludeGroup {
|
||||
includedGroup.add(other)
|
||||
groupItems.removeAll(other.getMaterials())
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {
|
||||
|
|
|
|||
|
|
@ -20,14 +20,18 @@ class IncludeGroup(name: String) : AbstractMaterialGroup(name) {
|
|||
return false
|
||||
}
|
||||
|
||||
override fun addToPolicy(mat: Material) {
|
||||
override fun addToPolicy(mat: Material): IncludeGroup {
|
||||
includedMaterial.add(mat)
|
||||
groupItems.add(mat)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
override fun addToPolicy(other: AbstractMaterialGroup) {
|
||||
override fun addToPolicy(other: AbstractMaterialGroup): IncludeGroup {
|
||||
includedGroup.add(other)
|
||||
groupItems.addAll(other.getMaterials())
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder
|
|||
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
|
||||
|
||||
class AnvilCustomRecipe(
|
||||
private val name: String,
|
||||
val name: String,
|
||||
var exactCount: Boolean,
|
||||
//var exactLeft: Boolean,
|
||||
//var exactRight: Boolean,
|
||||
|
|
@ -74,25 +74,30 @@ class AnvilCustomRecipe(
|
|||
|
||||
}
|
||||
|
||||
fun saveToFile(){
|
||||
fun saveToFile(writeFile: Boolean, doBackup: Boolean){
|
||||
val fileConfig = ConfigHolder.CUSTOM_RECIPE_HOLDER.config
|
||||
|
||||
fileConfig.set("$name.$EXACT_COUNT_CONFIG", exactCount)
|
||||
fileConfig["$name.$EXACT_COUNT_CONFIG"] = exactCount
|
||||
//fileConfig.set("$name.$EXACT_LEFT_CONFIG", exactLeft)
|
||||
//fileConfig.set("$name.$EXACT_RIGHT_CONFIG", exactRight)
|
||||
|
||||
fileConfig.set("$name.$XP_COST_CONFIG", xpCostPerCraft)
|
||||
fileConfig["$name.$XP_COST_CONFIG"] = xpCostPerCraft
|
||||
|
||||
fileConfig.set("$name.$LEFT_ITEM_CONFIG", leftItem)
|
||||
fileConfig.set("$name.$RIGHT_ITEM_CONFIG", rightItem)
|
||||
fileConfig.set("$name.$RESULT_ITEM_CONFIG", resultItem)
|
||||
fileConfig["$name.$LEFT_ITEM_CONFIG"] = leftItem
|
||||
fileConfig["$name.$RIGHT_ITEM_CONFIG"] = rightItem
|
||||
fileConfig["$name.$RESULT_ITEM_CONFIG"] = resultItem
|
||||
|
||||
|
||||
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
|
||||
ConfigHolder.CUSTOM_RECIPE_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE)
|
||||
if (writeFile) {
|
||||
ConfigHolder.CUSTOM_RECIPE_HOLDER.saveToDisk(doBackup)
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated("Should use saveToFile(Boolean, Boolean) instead") //TODO determine when an where to save/do backup and remove use of variable like TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE
|
||||
fun saveToFile(){
|
||||
saveToFile(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE, GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE)
|
||||
}
|
||||
|
||||
fun updateFromFile(){
|
||||
this.exactCount = ConfigHolder.CUSTOM_RECIPE_HOLDER.config.getBoolean(
|
||||
"$name.$EXACT_COUNT_CONFIG",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue