Add some testing (#38)

This commit is contained in:
alexcrea 2024-11-18 07:29:51 +01:00 committed by GitHub
parent c42140a45a
commit ed58c9c107
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 1911 additions and 150 deletions

View file

@ -182,7 +182,7 @@ public class AnvilRecipeBuilder {
*/
@Nullable // null if missing argument
public AnvilCustomRecipe build() {
if(leftItem == null || rightItem == null) return null;
if(leftItem == null || resultItem == null) return null;
return new AnvilCustomRecipe(
this.name,

View file

@ -19,7 +19,8 @@ import java.util.List;
@SuppressWarnings("unused")
public class ConflictAPI {
private ConflictAPI() {}
private ConflictAPI() {
}
private static Object saveChangeTask = null;
private static Object reloadChangeTask = null;
@ -27,31 +28,32 @@ public class ConflictAPI {
/**
* Write and add a conflict.
* Will not write the conflict if it already exists.
* Will not be successful if the conflict is empty.
*
* @param builder The conflict builder to be based on
* @return True if successful.
*/
public static boolean addConflict(@NotNull ConflictBuilder builder){
public static boolean addConflict(@NotNull ConflictBuilder builder) {
return addConflict(builder, false);
}
/**
* Write and add a conflict.
* Will not write the conflict if it already exists.
* Will not be successful if the conflict is empty.
*
* @param builder The conflict builder to be based on
* @param builder The conflict builder to be based on
* @param overrideDeleted If we should write even if the conflict was previously deleted.
* @return True if successful.
*/
public static boolean addConflict(@NotNull ConflictBuilder builder, boolean overrideDeleted){
public static boolean addConflict(@NotNull ConflictBuilder builder, boolean overrideDeleted) {
FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig();
// Test if conflict can be added
if(!overrideDeleted && ConfigHolder.CONFLICT_HOLDER.isDeleted(builder.getName())) return false;
if(config.contains(builder.getName())) return false;
if(!writeConflict(builder, false)) return false;
if (!overrideDeleted && ConfigHolder.CONFLICT_HOLDER.isDeleted(builder.getName())) return false;
if (config.contains(builder.getName())) return false;
if (!writeConflict(builder, false)) return false;
EnchantConflictGroup conflict = builder.build();
// Register conflict
@ -59,7 +61,7 @@ public class ConflictAPI {
// Add conflict to gui
EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance();
if(conflictGui != null) conflictGui.updateValueForGeneric(conflict, true);
if (conflictGui != null) conflictGui.updateValueForGeneric(conflict, true);
return true;
}
@ -69,10 +71,10 @@ public class ConflictAPI {
* <p>
* You may want to use {@link #addConflict(ConflictBuilder)} instead as it is more performance in most case as this function will reload every conflict.
*
* @param builder The builder
* @return True if successful.
* @param builder the builder
* @return true if was written successfully.
*/
public static boolean writeConflict(@NotNull ConflictBuilder builder){
public static boolean writeConflict(@NotNull ConflictBuilder builder) {
return writeConflict(builder, true);
}
@ -83,14 +85,14 @@ public class ConflictAPI {
*
* @param builder The builder
* @param updatePlanned If we should plan a global update for conflicts
* @return True if successful.
* @return true if was written successfully.
*/
public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){
public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned) {
FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig();
String name = builder.getName();
if(name.contains(".")) {
CustomAnvil.instance.getLogger().warning("Conflict " + name +" contain \".\" in its name but should not. this conflict is ignored.");
if (name.contains(".")) {
CustomAnvil.instance.getLogger().warning("Conflict " + name + " contain \".\" in its name but should not. this conflict is ignored.");
logConflictOrigin(builder);
return false;
}
@ -99,24 +101,27 @@ public class ConflictAPI {
List<String> enchantments = extractEnchantments(builder);
List<String> excludedGroups = new ArrayList<>(builder.getExcludedGroupNames());
if(!enchantments.isEmpty()) config.set(basePath + "enchantments", enchantments);
if(!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups);
if(builder.getMaxBeforeConflict() > 0) config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict());
if (!enchantments.isEmpty()) config.set(basePath + "enchantments", enchantments);
if (!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups);
if (builder.getMaxBeforeConflict() > 0)
config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict());
if (!config.isConfigurationSection(name)) return false;
prepareSaveTask();
if(updatePlanned) prepareUpdateTask();
if (updatePlanned) prepareUpdateTask();
return true;
}
/**
* Extract every enchantment names from a builder.
*
* @param builder The builder storing the enchantments
* @return Builder's stored enchantment.
*/
@NotNull
private static List<String> extractEnchantments(@NotNull ConflictBuilder builder){
private static List<String> extractEnchantments(@NotNull ConflictBuilder builder) {
List<String> result = new ArrayList<>(builder.getEnchantmentNames());
for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()) {
result.add(enchantmentKey.toString());
@ -131,7 +136,7 @@ public class ConflictAPI {
* @param conflict The conflict to remove
* @return True if successful.
*/
public static boolean removeConflict(@NotNull EnchantConflictGroup conflict){
public static boolean removeConflict(@NotNull EnchantConflictGroup conflict) {
// Remove from registry
ConfigHolder.CONFLICT_HOLDER.getConflictManager().removeConflict(conflict);
@ -141,8 +146,7 @@ public class ConflictAPI {
// Remove from gui
EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance();
if(conflictGui != null) conflictGui.removeGeneric(conflict);
if (conflictGui != null) conflictGui.removeGeneric(conflict);
return true;
}
@ -151,9 +155,9 @@ public class ConflictAPI {
* Prepare a task to save conflict configuration.
*/
private static void prepareSaveTask() {
if(saveChangeTask != null) return;
if (saveChangeTask != null) return;
saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{
saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> {
ConfigHolder.CONFLICT_HOLDER.saveToDisk(true);
saveChangeTask = null;
});
@ -163,28 +167,28 @@ public class ConflictAPI {
* Prepare a task to reload every conflict.
*/
private static void prepareUpdateTask() {
if(reloadChangeTask != null) return;
if (reloadChangeTask != null) return;
reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{
reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> {
ConfigHolder.CONFLICT_HOLDER.reload();
EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance();
if(conflictGui != null) conflictGui.reloadValues();
if (conflictGui != null) conflictGui.reloadValues();
reloadChangeTask = null;
});
}
static void logConflictOrigin(@NotNull ConflictBuilder builder){
static void logConflictOrigin(@NotNull ConflictBuilder builder) {
CustomAnvil.instance.getLogger().warning("Conflict " + builder.getName() + " came from " + builder.getSourceName() + ".");
}
/**
* Get every registered conflict.
*
* @return An immutable collection of conflict.
*/
@NotNull
public static List<EnchantConflictGroup> getRegisteredConflict(){
public static List<EnchantConflictGroup> getRegisteredConflict() {
List<EnchantConflictGroup> mutableList = ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList();
return Collections.unmodifiableList(mutableList);
}

View file

@ -36,7 +36,7 @@ public class ConflictBuilder {
* @param maxBeforeConflict Maximum number of conflicting enchantment before conflict is active
* @param source The conflict source
*/
public ConflictBuilder(@NotNull String name, int maxBeforeConflict, @Nullable Plugin source){
public ConflictBuilder(@NotNull String name, int maxBeforeConflict, @Nullable Plugin source) {
this.source = source;
this.name = name;
@ -54,7 +54,7 @@ public class ConflictBuilder {
* @param name The conflict name
* @param source The conflict source
*/
public ConflictBuilder(@NotNull String name, @Nullable Plugin source){
public ConflictBuilder(@NotNull String name, @Nullable Plugin source) {
this(name, 0, source);
}
@ -63,7 +63,7 @@ public class ConflictBuilder {
*
* @param name The conflict name
*/
public ConflictBuilder(@NotNull String name){
public ConflictBuilder(@NotNull String name) {
this(name, null);
}
@ -84,7 +84,7 @@ public class ConflictBuilder {
*/
@NotNull
public String getSourceName() {
if(source == null) return "an unknown source";
if (source == null) return "an unknown source";
return source.getName();
}
@ -177,7 +177,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder addEnchantment(@NotNull String enchantmentName){
public ConflictBuilder addEnchantment(@NotNull String enchantmentName) {
enchantmentNames.add(enchantmentName);
return this;
}
@ -189,7 +189,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder addEnchantment(@NotNull NamespacedKey enchantmentKey){
public ConflictBuilder addEnchantment(@NotNull NamespacedKey enchantmentKey) {
enchantmentKeys.add(enchantmentKey);
return this;
}
@ -201,7 +201,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder addEnchantment(@NotNull CAEnchantment enchantment){
public ConflictBuilder addEnchantment(@NotNull CAEnchantment enchantment) {
addEnchantment(enchantment.getKey());
return this;
}
@ -213,7 +213,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder removeEnchantment(@NotNull String enchantmentName){
public ConflictBuilder removeEnchantment(@NotNull String enchantmentName) {
enchantmentNames.remove(enchantmentName);
return this;
}
@ -225,7 +225,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder removeEnchantment(@NotNull NamespacedKey enchantmentKey){
public ConflictBuilder removeEnchantment(@NotNull NamespacedKey enchantmentKey) {
enchantmentKeys.remove(enchantmentKey);
return removeEnchantment(enchantmentKey.getKey());
}
@ -237,7 +237,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder removeEnchantment(@NotNull CAEnchantment enchantment){
public ConflictBuilder removeEnchantment(@NotNull CAEnchantment enchantment) {
return removeEnchantment(enchantment.getKey());
}
@ -256,7 +256,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder addExcludedGroup(@NotNull String groupName){
public ConflictBuilder addExcludedGroup(@NotNull String groupName) {
excludedGroupNames.add(groupName);
return this;
}
@ -276,7 +276,7 @@ public class ConflictBuilder {
* @return this conflict builder instance.
*/
@NotNull
public ConflictBuilder addExcludedGroup(@NotNull AbstractMaterialGroup group){
public ConflictBuilder addExcludedGroup(@NotNull AbstractMaterialGroup group) {
return addExcludedGroup(group.getName());
}
@ -295,7 +295,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder removeExcludedGroup(@NotNull String groupName){
public ConflictBuilder removeExcludedGroup(@NotNull String groupName) {
excludedGroupNames.remove(groupName);
return this;
}
@ -315,7 +315,7 @@ public class ConflictBuilder {
* @return This conflict builder instance.
*/
@NotNull
public ConflictBuilder removeExcludedGroup(@NotNull AbstractMaterialGroup group){
public ConflictBuilder removeExcludedGroup(@NotNull AbstractMaterialGroup group) {
return removeExcludedGroup(group.getName());
}
@ -328,7 +328,7 @@ public class ConflictBuilder {
public ConflictBuilder copy() {
ConflictBuilder copy = new ConflictBuilder(this.name, this.source);
setMaxBeforeConflict(this.maxBeforeConflict);
copy.setMaxBeforeConflict(this.maxBeforeConflict);
// Set Enchantments
for (NamespacedKey key : this.enchantmentKeys) {
@ -345,11 +345,13 @@ public class ConflictBuilder {
return copy;
}
/**
* Build a new Enchant conflict group by this builder.
*
* @return An Enchant conflict group with this builder parameters.
*/
public EnchantConflictGroup build(){
public EnchantConflictGroup build() {
AbstractMaterialGroup materials = extractGroups();
EnchantConflictGroup conflict = new EnchantConflictGroup(getName(), materials, getMaxBeforeConflict());
appendEnchantments(conflict);
@ -359,10 +361,21 @@ public class ConflictBuilder {
/**
* Register this conflict if not yet registered.
* Equivalent to {@link ConflictAPI#addConflict(ConflictBuilder)}
* Equivalent to {@link ConflictAPI#addConflict(ConflictBuilder, boolean) ConflictAPI.addConflict(this, true)}}
*
* @return True if successful.
*/
public boolean registerIfAbsent(){
public boolean registerIfAbsent() {
return ConflictAPI.addConflict(this, true);
}
/**
* Register this conflict if not yet registered or deleted.
* Equivalent to {@link ConflictAPI#addConflict(ConflictBuilder) ConflictAPI.addConflict(this)}
*
* @return True if successful.
*/
public boolean registerIfNew() {
return ConflictAPI.addConflict(this);
}
@ -371,15 +384,15 @@ public class ConflictBuilder {
*
* @param conflict The conflict target
*/
protected void appendEnchantments(@NotNull EnchantConflictGroup conflict){
for (String enchantmentName : getEnchantmentNames()){
if(appendEnchantments(conflict, EnchantmentApi.getListByName(enchantmentName)) == 0){
protected void appendEnchantments(@NotNull EnchantConflictGroup conflict) {
for (String enchantmentName : getEnchantmentNames()) {
if (appendEnchantments(conflict, EnchantmentApi.getListByName(enchantmentName)) == 0) {
CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentName + " for conflict " + getName());
ConflictAPI.logConflictOrigin(this);
}
}
for (NamespacedKey enchantmentKey : getEnchantmentKeys()){
if(!appendEnchantment(conflict, EnchantmentApi.getByKey(enchantmentKey))){
for (NamespacedKey enchantmentKey : getEnchantmentKeys()) {
if (!appendEnchantment(conflict, EnchantmentApi.getByKey(enchantmentKey))) {
CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentKey + " for conflict " + getName());
ConflictAPI.logConflictOrigin(this);
}
@ -393,8 +406,8 @@ public class ConflictBuilder {
* @param enchantment The enchantment
* @return True if successful.
*/
protected static boolean appendEnchantment(@NotNull EnchantConflictGroup conflict, @Nullable CAEnchantment enchantment){
if(enchantment == null)
protected static boolean appendEnchantment(@NotNull EnchantConflictGroup conflict, @Nullable CAEnchantment enchantment) {
if (enchantment == null)
return false;
conflict.addEnchantment(enchantment);
return true;
@ -403,14 +416,14 @@ public class ConflictBuilder {
/**
* Append a list of enchantments.
*
* @param conflict The conflict target
* @param conflict The conflict target
* @param enchantments List of enchantment to add
* @return Number of enchantment added
*/
protected static int appendEnchantments(@NotNull EnchantConflictGroup conflict, @NotNull List<CAEnchantment> enchantments){
protected static int appendEnchantments(@NotNull EnchantConflictGroup conflict, @NotNull List<CAEnchantment> enchantments) {
int numberValid = 0;
for (CAEnchantment enchantment : enchantments) {
if(appendEnchantment(conflict, enchantment)){
if (appendEnchantment(conflict, enchantment)) {
numberValid++;
}
}
@ -423,14 +436,14 @@ public class ConflictBuilder {
*
* @return The abstract material group from the builder.
*/
protected AbstractMaterialGroup extractGroups(){
protected AbstractMaterialGroup extractGroups() {
ItemGroupManager itemGroupManager = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager();
IncludeGroup group = new IncludeGroup(EnchantConflictManager.DEFAULT_GROUP_NAME);
for (String groupName : getExcludedGroupNames()) {
AbstractMaterialGroup materialGroup = itemGroupManager.get(groupName);
if(materialGroup == null){
if (materialGroup == null) {
CustomAnvil.instance.getLogger().warning("Material group " + groupName + " do not exist but is ask by conflict " + getName());
ConflictAPI.logConflictOrigin(this);
continue;

View file

@ -78,6 +78,7 @@ public class CustomAnvilRecipeApi {
return true;
}
// TODO remove by name and/or by builder (as name is keept) (and maybe create a get by name)
/**
* Remove a custom anvil recipe.
*
@ -86,7 +87,8 @@ public class CustomAnvilRecipeApi {
*/
public static boolean removeRecipe(@NotNull AnvilCustomRecipe recipe){
// Remove from registry
ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().cleanRemove(recipe);
boolean result = ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().cleanRemove(recipe);
if(!result) return false;
// Delete and save to file
ConfigHolder.CUSTOM_RECIPE_HOLDER.delete(recipe.getName());

View file

@ -22,49 +22,51 @@ import java.util.*;
@SuppressWarnings("unused")
public class MaterialGroupApi {
private MaterialGroupApi(){}
private MaterialGroupApi() {
}
private static Object saveChangeTask = null;
private static Object reloadChangeTask = null;
/**
* Write and add a group.
* Will not write the group if it already exists.
* Will not be successful if the group is empty.
*
* @param group The group to add
* @return true if successful.
*/
public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group){
public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group) {
return addMaterialGroup(group, false);
}
/**
* Write and add a group.
* Will not write the group if it already exists.
* Will not be successful if the group is empty.
*
* @param group The group to add
* @param group The group to add
* @param overrideDeleted If we should write even if the group was previously deleted.
* @return true if successful.
*/
public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group, boolean overrideDeleted){
public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group, boolean overrideDeleted) {
ItemGroupManager itemGroupManager = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager();
// Test if it exists/existed
if(!overrideDeleted && ConfigHolder.ITEM_GROUP_HOLDER.isDeleted(group.getName())) return false;
if(itemGroupManager.get(group.getName()) != null) return false;
if (!overrideDeleted && ConfigHolder.ITEM_GROUP_HOLDER.isDeleted(group.getName())) return false;
if (itemGroupManager.get(group.getName()) != null) return false;
// Add group
itemGroupManager.getGroupMap().put(group.getName(), group);
if(!writeMaterialGroup(group, false)) return false;
if (!writeMaterialGroup(group, false)) return false;
if(group instanceof IncludeGroup includeGroup){
if (group instanceof IncludeGroup includeGroup) {
GroupConfigGui configGui = GroupConfigGui.getCurrentInstance();
if(configGui != null) configGui.updateValueForGeneric(includeGroup, true);
if (configGui != null) configGui.updateValueForGeneric(includeGroup, true);
}
if(ConfigOptions.INSTANCE.getVerboseDebugLog()){
if (ConfigOptions.INSTANCE.getVerboseDebugLog()) {
CustomAnvil.instance.getLogger().info("Registered group " + group.getName());
}
@ -77,9 +79,9 @@ public class MaterialGroupApi {
* 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.
* @return true if was written successfully.
*/
public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group){
public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group) {
return writeMaterialGroup(group, true);
}
@ -90,64 +92,69 @@ public class MaterialGroupApi {
*
* @param group the group to write
* @param updatePlanned if we should plan a global update for material groups
* @return true if successful.
* @return true if was written successfully.
*/
public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group, boolean updatePlanned){
public static 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.");
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);
boolean changed;
if (group instanceof IncludeGroup includeGroup) {
changed = writeKnownGroup("include", includeGroup);
} else if (group instanceof ExcludeGroup excludeGroup) {
changed = writeKnownGroup("exclude", excludeGroup);
} else {
changed = writeUnknownGroup(group);
}
if (!changed) return false;
prepareSaveTask();
if(updatePlanned) prepareUpdateTask();
if (updatePlanned) prepareUpdateTask();
return true;
}
private static void writeKnownGroup(@NotNull String groupType, @NotNull AbstractMaterialGroup group){
private static boolean 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()){
if (!materialSet.isEmpty()) {
config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materialSet));
}
if(!groupSet.isEmpty()){
config.set(basePath + ItemGroupManager.GROUP_LIST_PATH, materialGroupSEtToStringList(groupSet));
if (!groupSet.isEmpty()) {
config.set(basePath + ItemGroupManager.GROUP_LIST_PATH, materialGroupSetToStringList(groupSet));
}
if (!config.isConfigurationSection(group.getName())) return false;
config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, groupType);
return true;
}
private static void writeUnknownGroup(@NotNull AbstractMaterialGroup group) {
private static boolean 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, materialSetToStringList(materials));
}
if (materials.isEmpty()) return false;
config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include");
config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materials));
return true;
}
public static List<String> materialSetToStringList(@NotNull Set<Material> materials){
public static List<String> materialSetToStringList(@NotNull Set<Material> materials) {
return materials.stream().map(material -> material.getKey().getKey().toLowerCase()).toList();
}
public static List<String> materialGroupSEtToStringList(@NotNull Set<AbstractMaterialGroup> groups){
public static List<String> materialGroupSetToStringList(@NotNull Set<AbstractMaterialGroup> groups) {
return groups.stream().map(AbstractMaterialGroup::getName).toList();
}
@ -157,20 +164,21 @@ public class MaterialGroupApi {
* For that reason, it is not recommended to use this function.
*
* @param group The recipe to remove
* @return True if successful.
* @return True if the group was present.
*/
public static boolean removeGroup(@NotNull AbstractMaterialGroup group){
public static boolean removeGroup(@NotNull AbstractMaterialGroup group) {
// Remove from registry
ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().groupMap.remove(group.getName());
AbstractMaterialGroup removed = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().groupMap.remove(group.getName());
if (removed == null) return false;
// Delete and save to file
ConfigHolder.ITEM_GROUP_HOLDER.delete(group.getName());
prepareSaveTask();
// Remove from gui
if(group instanceof IncludeGroup includeGroup){
if (group instanceof IncludeGroup includeGroup) {
GroupConfigGui configGui = GroupConfigGui.getCurrentInstance();
if(configGui != null) configGui.removeGeneric(includeGroup);
if (configGui != null) configGui.removeGeneric(includeGroup);
}
return true;
@ -180,9 +188,9 @@ public class MaterialGroupApi {
* Prepare a task to reload every conflict.
*/
private static void prepareSaveTask() {
if(saveChangeTask != null) return;
if (saveChangeTask != null) return;
saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{
saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> {
ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true);
saveChangeTask = null;
});
@ -192,13 +200,13 @@ public class MaterialGroupApi {
* Prepare a task to save configuration.
*/
private static void prepareUpdateTask() {
if(reloadChangeTask != null) return;
if (reloadChangeTask != null) return;
reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{
reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> {
ConfigHolder.ITEM_GROUP_HOLDER.reload();
GroupConfigGui configGui = GroupConfigGui.getCurrentInstance();
if(configGui != null) configGui.reloadValues();
if (configGui != null) configGui.reloadValues();
reloadChangeTask = null;
});
@ -212,16 +220,17 @@ public class MaterialGroupApi {
* @return the abstract group of this name. null if not found.
*/
@Nullable
public static AbstractMaterialGroup getGroup(@NotNull String groupName){
public static AbstractMaterialGroup getGroup(@NotNull String groupName) {
return ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().get(groupName);
}
/**
* Get every registered material groups.
*
* @return An immutable map of group name as its key and group as mapped value.
*/
@NotNull
public static Map<String, AbstractMaterialGroup> getRegisteredGroups(){
public static Map<String, AbstractMaterialGroup> getRegisteredGroups() {
return Collections.unmodifiableMap(ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap());
}

View file

@ -117,9 +117,10 @@ public class UnitRepairApi {
String repairableName = repairable.name();
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
config.set(unitName.toLowerCase() + repairableName.toUpperCase(), null);
config.set(unitName.toUpperCase() + repairableName.toLowerCase(), null);
config.set(unitName.toUpperCase() + repairableName.toUpperCase(), null);
config.set(unitName.toLowerCase() + "." + repairableName.toUpperCase(), null);
config.set(unitName.toUpperCase() + "." + repairableName.toLowerCase(), null);
config.set(unitName.toUpperCase() + "." + repairableName.toUpperCase(), null);
config.set(unitName.toLowerCase() + "." + repairableName.toLowerCase(), null);
// Test if it was the last value of this section
boolean lastValue = false;
@ -142,7 +143,7 @@ public class UnitRepairApi {
// We only need to "delete" as the lower case to be counted as deleted
ConfigHolder.UNIT_REPAIR_HOLDER.delete(unitName.toLowerCase() + repairableName.toLowerCase());
ConfigHolder.UNIT_REPAIR_HOLDER.delete(unitName.toLowerCase() + "." + repairableName.toLowerCase());
prepareSaveTask();
// Remove from gui

View file

@ -29,7 +29,8 @@ public class BukkitEnchantBulkOperation implements BulkGetEnchantOperation, Bulk
@Override
public void bulkClear(@NotNull ItemStack item) {
if (item.getType() != Material.ENCHANTED_BOOK) {
item.getEnchantments().forEach((enchantment, leve) ->
item.getEnchantments().forEach((enchantment, level) ->
item.removeEnchantment(enchantment)
);
}
@ -43,5 +44,6 @@ public class BukkitEnchantBulkOperation implements BulkGetEnchantOperation, Bulk
bookMeta.removeStoredEnchant(enchantment)
);
}
}
}

View file

@ -2,6 +2,7 @@ package xyz.alexcrea.cuanvil.dependency
import io.delilaheve.CustomAnvil
import org.bukkit.Bukkit
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
@ -91,11 +92,11 @@ object DependencyManager {
}
fun tryEventPreAnvilBypass(event: PrepareAnvilEvent): Boolean {
fun tryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
var bypass = false
// Test if disenchantment used special prepare anvil
if(disenchantmentCompatibility?.testPrepareAnvil(event) == true) bypass = true
if(disenchantmentCompatibility?.testPrepareAnvil(event, player) == true) bypass = true
// Test excellent enchantments used special prepare anvil
if(!bypass && (excellentEnchantsCompatibility?.testPrepareAnvil(event) == true)) bypass = true

View file

@ -5,6 +5,7 @@ import cz.kominekjan.disenchantment.events.DisenchantEvent
import cz.kominekjan.disenchantment.events.ShatterClickEvent
import cz.kominekjan.disenchantment.events.ShatterEvent
import io.delilaheve.CustomAnvil
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
@ -70,7 +71,7 @@ class DisenchantmentDependency {
}
fun testPrepareAnvil(event: PrepareAnvilEvent): Boolean {
fun testPrepareAnvil(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
val previousResult = event.result
event.result = null
@ -79,14 +80,14 @@ class DisenchantmentDependency {
if(event.result != null) {
CustomAnvil.log("Detected pre anvil item extract bypass.")
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost)
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, player, event.inventory.repairCost)
return true
}
splitEvent.onDisenchantmentEvent(event)
if(event.result != null) {
CustomAnvil.log("Detected pre anvil split enchant bypass.")
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, event.inventory.repairCost)
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, player, event.inventory.repairCost)
return true
}

View file

@ -41,19 +41,20 @@ class PrepareAnvilListener : Listener {
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun anvilCombineCheck(event: PrepareAnvilEvent) {
// Should find player
val player: HumanEntity = event.viewers.first()
// Test if the event should bypass custom anvil.
if(DependencyManager.tryEventPreAnvilBypass(event)) return
if(DependencyManager.tryEventPreAnvilBypass(event, player)) return
val inventory = event.inventory
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
val second = inventory.getItem(ANVIL_INPUT_RIGHT)
// Should find player
val player = event.view.player
if (!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return
// Test custom recipe
if(testCustomRecipe(event, inventory, first, second)) return
if(testCustomRecipe(event, inventory, player, first, second)) return
// Test rename lonely item
if(second == null) {
@ -75,7 +76,9 @@ class PrepareAnvilListener : Listener {
}
private fun testCustomRecipe(event: PrepareAnvilEvent, inventory: AnvilInventory, first: ItemStack, second: ItemStack?): Boolean {
private fun testCustomRecipe(event: PrepareAnvilEvent, inventory: AnvilInventory,
player: HumanEntity,
first: ItemStack, second: ItemStack?): Boolean {
val recipe = CustomRecipeUtil.getCustomRecipe(first, second)
CustomAnvil.verboseLog("custom recipe not null? ${recipe != null}")
if(recipe == null) return false
@ -87,7 +90,7 @@ class PrepareAnvilListener : Listener {
event.result = resultItem
DependencyManager.treatAnvilResult(event, resultItem)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, recipe.xpCostPerCraft * amount, true)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, recipe.xpCostPerCraft * amount, true)
return true
}
@ -109,7 +112,7 @@ class PrepareAnvilListener : Listener {
anvilCost += AnvilXpUtil.calculatePenalty(first, null, resultItem)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
}
private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory, player: HumanEntity): Int {
@ -177,7 +180,7 @@ class PrepareAnvilListener : Listener {
event.result = resultItem
DependencyManager.treatAnvilResult(event, resultItem)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
}
private fun testUnitRepair(event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
@ -203,7 +206,7 @@ class PrepareAnvilListener : Listener {
event.result = resultItem
DependencyManager.treatAnvilResult(event, resultItem)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost)
AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
return true
}

View file

@ -9,17 +9,17 @@ class CustomAnvilRecipeManager {
lateinit var recipeList: ArrayList<AnvilCustomRecipe>
lateinit var recipeByMat: LinkedHashMap<Material, ArrayList<AnvilCustomRecipe>>
lateinit var recipeByMat: HashMap<Material, ArrayList<AnvilCustomRecipe>>
fun prepareRecipes(config: FileConfiguration) {
recipeList = ArrayList()
recipeByMat = LinkedHashMap()
recipeByMat = HashMap()
// read all configs
val keys = config.getKeys(false)
for (key in keys) {
val recipe = AnvilCustomRecipe.getFromConfig(key)
if(recipe == null){
if (recipe == null) {
CustomAnvil.log("Can't load recipe $key")
continue
}
@ -30,34 +30,34 @@ class CustomAnvilRecipeManager {
}
fun cleanAddNew(recipe: AnvilCustomRecipe){
fun cleanAddNew(recipe: AnvilCustomRecipe) {
recipeList.add(recipe)
val leftItem = recipe.leftItem
if(leftItem != null){
if (leftItem != null) {
addToMatMap(recipe, leftItem)
}
}
fun cleanSetLeftItem(recipe: AnvilCustomRecipe, leftItem: ItemStack?){
fun cleanSetLeftItem(recipe: AnvilCustomRecipe, leftItem: ItemStack?) {
// Remove left item mat if exist
val oldLeftItem = recipe.leftItem
if(oldLeftItem != null){
if (oldLeftItem != null) {
val oldMat = oldLeftItem.type
val test = recipeByMat[oldMat]
test!!.remove(recipe)
}
if(leftItem != null){
if (leftItem != null) {
addToMatMap(recipe, leftItem)
}
recipe.leftItem = leftItem
}
private fun addToMatMap(recipe: AnvilCustomRecipe, leftItem: ItemStack){
private fun addToMatMap(recipe: AnvilCustomRecipe, leftItem: ItemStack) {
var recipeList = recipeByMat[leftItem.type]
if(recipeList == null){
if (recipeList == null) {
recipeList = ArrayList()
recipeByMat[leftItem.type] = recipeList
}
@ -65,11 +65,14 @@ class CustomAnvilRecipeManager {
}
fun cleanRemove(recipe: AnvilCustomRecipe) {
fun cleanRemove(recipe: AnvilCustomRecipe): Boolean {
recipeList.remove(recipe)
cleanSetLeftItem(recipe, null)
val exist = recipeList.remove(recipe)
if (exist) {
cleanSetLeftItem(recipe, null)
}
return exist;
}
}

View file

@ -6,6 +6,7 @@ import io.delilaheve.util.EnchantmentUtil.enchantmentName
import io.delilaheve.util.ItemUtil.findEnchantments
import io.delilaheve.util.ItemUtil.isEnchantedBook
import org.bukkit.GameMode
import org.bukkit.entity.HumanEntity
import org.bukkit.entity.Player
import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.InventoryView
@ -25,6 +26,7 @@ object AnvilXpUtil {
fun setAnvilInvXp(
inventory: AnvilInventory,
view: InventoryView,
player: HumanEntity,
anvilCost: Int,
ignoreRules: Boolean = false
) {
@ -38,8 +40,6 @@ object AnvilXpUtil {
anvilCost
}
val player = view.player
/* 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. */