mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Added material group API.
Also edited some javadoc.
This commit is contained in:
parent
e1f6c3f5a8
commit
fca7bbb416
7 changed files with 164 additions and 15 deletions
|
|
@ -25,6 +25,7 @@ public class ConflictAPI {
|
|||
|
||||
/**
|
||||
* Write and add a conflict.
|
||||
* Will not write the conflict if it already exists.
|
||||
*
|
||||
* @param builder the conflict builder to base on
|
||||
* @return true if successful
|
||||
|
|
@ -122,7 +123,7 @@ public class ConflictAPI {
|
|||
* You should use {@link #addConflict(ConflictBuilder)} or {@link #writeConflict(ConflictBuilder)} instead
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){
|
||||
|
|
@ -135,7 +136,7 @@ public class ConflictAPI {
|
|||
return false;
|
||||
}
|
||||
|
||||
String basePath = name+".";
|
||||
String basePath = name + ".";
|
||||
|
||||
Set<String> enchantments = extractEnchantments(builder);
|
||||
Set<String> excludedGroups = builder.getExcludedGroupNames();
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ public class EnchantmentApi {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get by key the enchantment.
|
||||
* Get by key a enchantment.
|
||||
*
|
||||
* @param key the key used to fetch
|
||||
* @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
|
||||
* @return the custom anvil enchantment
|
||||
|
|
|
|||
158
src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java
Normal file
158
src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package xyz.alexcrea.cuanvil.enchant;
|
||||
|
||||
import io.delilaheve.util.ItemUtil;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
|
@ -11,13 +10,11 @@ import org.jetbrains.annotations.NotNull;
|
|||
import org.jetbrains.annotations.Nullable;
|
||||
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
|
||||
import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency;
|
||||
import xyz.alexcrea.cuanvil.group.ConflictType;
|
||||
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Represent an enchantment compatible with Custom Anvil.
|
||||
|
|
|
|||
|
|
@ -1,20 +1,16 @@
|
|||
package xyz.alexcrea.cuanvil.enchant;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import xyz.alexcrea.cuanvil.group.ConflictType;
|
||||
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class CAEnchantmentBase implements CAEnchantment {
|
||||
|
||||
|
|
|
|||
|
|
@ -5,13 +5,11 @@ import com.willfp.ecoenchants.target.EnchantmentTarget;
|
|||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment;
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||
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.function.Supplier;
|
||||
|
||||
public class CAEcoEnchant extends CAVanillaEnchantment implements AdditionalTestEnchantment {
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue