Added material group API.

Also edited some javadoc.
This commit is contained in:
alexcrea 2024-07-08 15:36:19 +02:00
parent e1f6c3f5a8
commit fca7bbb416
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
7 changed files with 164 additions and 15 deletions

View file

@ -25,6 +25,7 @@ public class ConflictAPI {
/** /**
* Write and add a conflict. * Write and add a conflict.
* Will not write the conflict if it already exists.
* *
* @param builder the conflict builder to base on * @param builder the conflict builder to base on
* @return true if successful * @return true if successful
@ -122,7 +123,7 @@ public class ConflictAPI {
* You should use {@link #addConflict(ConflictBuilder)} or {@link #writeConflict(ConflictBuilder)} instead * You should use {@link #addConflict(ConflictBuilder)} or {@link #writeConflict(ConflictBuilder)} instead
* *
* @param builder the builder * @param builder the builder
* @param updatePlanned if we should plan an update * @param updatePlanned if we should plan a global update for conflicts
* @return true if successful * @return true if successful
*/ */
public boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){ public boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){

View file

@ -83,7 +83,7 @@ public class EnchantmentApi {
} }
/** /**
* Get by key the enchantment. * Get by key a enchantment.
* *
* @param key the key used to fetch * @param key the key used to fetch
* @return the custom anvil enchantment * @return the custom anvil enchantment
@ -94,7 +94,7 @@ public class EnchantmentApi {
} }
/** /**
* Get by name the enchantment. * Get by name a enchantment.
* *
* @param name the name used to fetch * @param name the name used to fetch
* @return the custom anvil enchantment * @return the custom anvil enchantment

View file

@ -0,0 +1,158 @@
package xyz.alexcrea.cuanvil.api;
import io.delilaheve.CustomAnvil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
import xyz.alexcrea.cuanvil.group.ExcludeGroup;
import xyz.alexcrea.cuanvil.group.IncludeGroup;
import xyz.alexcrea.cuanvil.group.ItemGroupManager;
import xyz.alexcrea.cuanvil.gui.config.global.GroupConfigGui;
import java.util.EnumSet;
import java.util.Set;
/**
* The type Material group api.
*/
@SuppressWarnings("unused")
public class MaterialGroupApi {
private static int saveChangeTask = -1;
private static int reloadChangeTask = -1;
/**
* Write and add a group.
* Will not write the group if it already exists.
*
* @param group the group to add
* @return true if successful
*/
public boolean addMaterialGroup(@NotNull AbstractMaterialGroup group){
ItemGroupManager itemGroupManager = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager();
if(itemGroupManager.get(group.getName()) != null) return false;
if(!writeMaterialGroup(group, false)) return false;
if(group instanceof IncludeGroup includeGroup){
GroupConfigGui.INSTANCE.updateValueForGeneric(includeGroup, true);
}
return true;
}
/**
* Write a material group to the config file and plan an update of groups.
* <p>
* You may want to use {@link #addMaterialGroup(AbstractMaterialGroup)} instead as it is more performance in most case as this function will reload every conflict.
*
* @param group the group to write
* @return true if successful
*/
public boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group){
return writeMaterialGroup(group, true);
}
/**
* Write a material group to the config file.
* <p>
* You should use {@link #addMaterialGroup(AbstractMaterialGroup)} or {@link #writeMaterialGroup(AbstractMaterialGroup)} instead
*
* @param group the group to write
* @param updatePlanned if we should plan a global update for material groups
* @return true if successful
*/
public boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group, boolean updatePlanned){
String name = group.getName();
if(name.contains(".")) {
CustomAnvil.instance.getLogger().warning("Group " + name +" contain . in its name but should not. this material group is ignored.");
return false;
}
if(group instanceof IncludeGroup includeGroup){
writeKnownGroup("include", includeGroup);
}else if(group instanceof ExcludeGroup excludeGroup){
writeKnownGroup("exclude", excludeGroup);
}else{
writeUnknownGroup(group);
}
prepareSaveTask();
if(updatePlanned) prepareUpdateTask();
return true;
}
private void writeKnownGroup(@NotNull String groupType, @NotNull AbstractMaterialGroup group){
FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig();
String basePath = group.getName() + ".";
Set<Material> materialSet = group.getNonGroupInheritedMaterials();
Set<AbstractMaterialGroup> groupSet = group.getGroups();
config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, groupType);
if(!materialSet.isEmpty()){
config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSet);
}
if(!groupSet.isEmpty()){
config.set(basePath + ItemGroupManager.GROUP_LIST_PATH, groupSet);
}
}
private void writeUnknownGroup(@NotNull AbstractMaterialGroup group) {
FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig();
String basePath = group.getName() + ".";
EnumSet<Material> materials = group.getMaterials();
config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include");
if(!materials.isEmpty()){
config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materials);
}
}
/**
* Prepare a task to reload every conflict.
*/
private static void prepareSaveTask() {
if(saveChangeTask != -1) return;
saveChangeTask = Bukkit.getScheduler().scheduleSyncDelayedTask(CustomAnvil.instance, ()->{
ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true);
saveChangeTask = -1;
}, 0L);
}
/**
* Prepare a task to save configuration.
*/
private static void prepareUpdateTask() {
if(reloadChangeTask != -1) return;
reloadChangeTask = Bukkit.getScheduler().scheduleSyncDelayedTask(CustomAnvil.instance, ()->{
ConfigHolder.ITEM_GROUP_HOLDER.reload();
GroupConfigGui.INSTANCE.reloadValues();
reloadChangeTask = -1;
}, 0L);
}
/**
* Get by name a group.
*
* @param groupName the group name used to fetch
* @return the abstract group of this name
*/
@Nullable
public static AbstractMaterialGroup getGroup(@NotNull String groupName){
return ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().get(groupName);
}
}

View file

@ -1,7 +1,6 @@
package xyz.alexcrea.cuanvil.enchant; package xyz.alexcrea.cuanvil.enchant;
import io.delilaheve.util.ItemUtil; import io.delilaheve.util.ItemUtil;
import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -11,13 +10,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.dependency.DependencyManager; import xyz.alexcrea.cuanvil.dependency.DependencyManager;
import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency; import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency;
import xyz.alexcrea.cuanvil.group.ConflictType;
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
/** /**
* Represent an enchantment compatible with Custom Anvil. * Represent an enchantment compatible with Custom Anvil.

View file

@ -1,20 +1,16 @@
package xyz.alexcrea.cuanvil.enchant; package xyz.alexcrea.cuanvil.enchant;
import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.group.ConflictType;
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier;
public abstract class CAEnchantmentBase implements CAEnchantment { public abstract class CAEnchantmentBase implements CAEnchantment {

View file

@ -5,13 +5,11 @@ import com.willfp.ecoenchants.target.EnchantmentTarget;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment;
import xyz.alexcrea.cuanvil.group.ConflictType;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
public class CAEcoEnchant extends CAVanillaEnchantment implements AdditionalTestEnchantment { public class CAEcoEnchant extends CAVanillaEnchantment implements AdditionalTestEnchantment {

View file

@ -9,7 +9,6 @@ import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import java.util.* import java.util.*
import kotlin.collections.ArrayList
class EnchantConflictManager { class EnchantConflictManager {