Fix Custom Recipe not being registered.

Fix conflict from api not working.
Add remove api for custom recipe, material group and conflict.
This commit is contained in:
alexcrea 2024-07-09 20:21:36 +02:00
parent a40d2c6530
commit 6f1e53f68e
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
8 changed files with 93 additions and 10 deletions

View file

@ -1,6 +1,7 @@
plugins { plugins {
kotlin("jvm") version "1.9.24" kotlin("jvm") version "1.9.24"
java java
id("org.jetbrains.dokka").version("1.9.20")
id("com.github.johnrengelman.shadow").version("7.1.2") id("com.github.johnrengelman.shadow").version("7.1.2")
} }

View file

@ -41,7 +41,7 @@ public class ConflictAPI {
EnchantConflictGroup conflict = builder.build(); EnchantConflictGroup conflict = builder.build();
// Register conflict // Register conflict
ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList().add(conflict); ConfigHolder.CONFLICT_HOLDER.getConflictManager().addConflict(conflict);
// Add conflict to gui // Add conflict to gui
EnchantConflictGui.INSTANCE.updateValueForGeneric(conflict, true); EnchantConflictGui.INSTANCE.updateValueForGeneric(conflict, true);
@ -110,6 +110,25 @@ public class ConflictAPI {
return result; return result;
} }
/**
* Remove a conflict.
*
* @param conflict The conflict to remove
* @return True if successful.
*/
public static boolean removeConflict(@NotNull EnchantConflictGroup conflict){
// Remove from registry
ConfigHolder.CONFLICT_HOLDER.getConflictManager().removeConflict(conflict);
// Write as null and save to file
ConfigHolder.CONFLICT_HOLDER.getConfig().set(conflict.getName(), null);
prepareSaveTask();
// Remove from gui
EnchantConflictGui.INSTANCE.removeGeneric(conflict);
return true;
}
/** /**
* Prepare a task to save conflict configuration. * Prepare a task to save conflict configuration.

View file

@ -11,6 +11,9 @@ import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/**
* Custom Anvil api for custom anvil recipes.
*/
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class CustomAnvilRecipeApi { public class CustomAnvilRecipeApi {
@ -47,16 +50,39 @@ public class CustomAnvilRecipeApi {
return false; return false;
} }
// Add to registry
ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().cleanAddNew(recipe);
// Save to file // Save to file
recipe.saveToFile(false, false); recipe.saveToFile(false, false);
prepareSaveTask(); prepareSaveTask();
// Update gui // Add from gui
CustomRecipeConfigGui.INSTANCE.updateValueForGeneric(recipe, true); CustomRecipeConfigGui.INSTANCE.updateValueForGeneric(recipe, true);
return true; return true;
} }
/**
* Remove a custom anvil recipe.
*
* @param recipe The recipe to remove
* @return True if successful.
*/
public static boolean removeRecipe(@NotNull AnvilCustomRecipe recipe){
// Remove from registry
ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().cleanRemove(recipe);
// Write as null and save to file
ConfigHolder.CUSTOM_RECIPE_HOLDER.getConfig().set(recipe.getName(), null);
prepareSaveTask();
// Remove from gui
CustomRecipeConfigGui.INSTANCE.removeGeneric(recipe);
return true;
}
/** /**
* Prepare a task to save custom recipe configuration. * Prepare a task to save custom recipe configuration.
*/ */

View file

@ -121,6 +121,30 @@ public class MaterialGroupApi {
} }
/**
* Remove a material group.
* Caution ! It will not be removed from depending conflict or other material group at runtime.
* For that reason, it is not recommended to use this function.
*
* @param group The recipe to remove
* @return True if successful.
*/
public static boolean removeGroup(@NotNull AbstractMaterialGroup group){
// Remove from registry
ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().groupMap.remove(group.getName());
// Write as null and save to file
ConfigHolder.ITEM_GROUP_HOLDER.getConfig().set(group.getName(), null);
prepareSaveTask();
// Remove from gui
if(group instanceof IncludeGroup includeGroup){
GroupConfigGui.INSTANCE.removeGeneric(includeGroup);
}
return true;
}
/** /**
* Prepare a task to reload every conflict. * Prepare a task to reload every conflict.
*/ */

View file

@ -118,10 +118,7 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager(); EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager();
// Remove from enchantment // Remove from enchantment
for (CAEnchantment enchantment : this.enchantConflict.getEnchants()) { manager.removeConflict(this.enchantConflict);
enchantment.removeConflict(this.enchantConflict);
}
manager.conflictList.remove(this.enchantConflict);
// Remove from parent // Remove from parent
this.parent.removeGeneric(this.enchantConflict); this.parent.removeGeneric(this.enchantConflict);

View file

@ -5,7 +5,7 @@ import org.bukkit.Material
import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment
class EnchantConflictGroup( class EnchantConflictGroup(
private val name: String, val name: String,
private val cantConflict: AbstractMaterialGroup, private val cantConflict: AbstractMaterialGroup,
var minBeforeBlock: Int var minBeforeBlock: Int
) { ) {

View file

@ -52,12 +52,21 @@ class EnchantConflictManager {
val section = config.getConfigurationSection(key)!! val section = config.getConfigurationSection(key)!!
val conflict = createConflict(section, itemManager, key) val conflict = createConflict(section, itemManager, key)
addConflictToEnchantments(conflict) addConflict(conflict)
conflictList.add(conflict)
} }
} }
fun addConflict(conflict: EnchantConflictGroup){
addConflictToEnchantments(conflict)
conflictList.add(conflict)
}
fun removeConflict(conflict: EnchantConflictGroup){
removeConflictFromEnchantments(conflict)
conflictList.remove(conflict)
}
// Add the conflict to enchantments // Add the conflict to enchantments
private fun addConflictToEnchantments(conflict: EnchantConflictGroup) { private fun addConflictToEnchantments(conflict: EnchantConflictGroup) {
conflict.getEnchants().forEach { enchant -> conflict.getEnchants().forEach { enchant ->
@ -65,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 // create and read a conflict from a yaml section
private fun createConflict( private fun createConflict(
section: ConfigurationSection, section: ConfigurationSection,

View file

@ -7,7 +7,7 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
class AnvilCustomRecipe( class AnvilCustomRecipe(
private val name: String, val name: String,
var exactCount: Boolean, var exactCount: Boolean,
//var exactLeft: Boolean, //var exactLeft: Boolean,
//var exactRight: Boolean, //var exactRight: Boolean,