Write conflict to file when write conflict is called.

Allow custom default rarity.
Update ConflictBuilder javadoc.
Allow null source for ConflictBuilder.
Log conflict origin on warning.
This commit is contained in:
alexcrea 2024-07-08 00:15:07 +02:00
parent 06b3dc89c2
commit e1f6c3f5a8
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
5 changed files with 174 additions and 44 deletions

View file

@ -11,6 +11,9 @@ import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.group.*; import xyz.alexcrea.cuanvil.group.*;
import xyz.alexcrea.cuanvil.gui.config.global.EnchantConflictGui; import xyz.alexcrea.cuanvil.gui.config.global.EnchantConflictGui;
import java.util.HashSet;
import java.util.Set;
/** /**
* Custom Anvil api for conflict registry. * Custom Anvil api for conflict registry.
*/ */
@ -51,11 +54,13 @@ public class ConflictAPI {
for (String enchantmentName : builder.getEnchantmentNames()){ for (String enchantmentName : builder.getEnchantmentNames()){
if(appendEnchantment(conflict, EnchantmentApi.getByName(enchantmentName))){ if(appendEnchantment(conflict, EnchantmentApi.getByName(enchantmentName))){
CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentName + " for conflict " + builder.getName()); CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentName + " for conflict " + builder.getName());
logConflictOrigin(builder);
} }
} }
for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()){ for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()){
if(!appendEnchantment(conflict, EnchantmentApi.getByKey(enchantmentKey))){ if(!appendEnchantment(conflict, EnchantmentApi.getByKey(enchantmentKey))){
CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentKey + " for conflict " + builder.getName()); CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentKey + " for conflict " + builder.getName());
logConflictOrigin(builder);
} }
} }
} }
@ -89,6 +94,7 @@ public class ConflictAPI {
if(materialGroup == null){ if(materialGroup == null){
CustomAnvil.instance.getLogger().warning("Material group " + groupName + " do not exist but is ask by conflict " + builder.getName()); CustomAnvil.instance.getLogger().warning("Material group " + groupName + " do not exist but is ask by conflict " + builder.getName());
logConflictOrigin(builder);
continue; continue;
} }
@ -124,16 +130,41 @@ public class ConflictAPI {
String name = builder.getName(); String name = builder.getName();
if(name.contains(".")) { if(name.contains(".")) {
CustomAnvil.instance.getLogger().warning("Conflict \"" + name +"\" contain . in its name but should not. this conflict is ignored."); CustomAnvil.instance.getLogger().warning("Conflict " + name +" contain . in its name but should not. this conflict is ignored.");
logConflictOrigin(builder);
return false; return false;
} }
String basePath = name+".";
Set<String> enchantments = extractEnchantments(builder);
Set<String> excludedGroups = 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());
prepareSaveTask(); prepareSaveTask();
if(updatePlanned) prepareUpdateTask(); if(updatePlanned) prepareUpdateTask();
return true; return true;
} }
/**
* Extract every enchantment names from a builder.
* @param builder the builder storing the enchantments
* @return builder's stored enchantment
*/
@NotNull
private Set<String> extractEnchantments(@NotNull ConflictBuilder builder){
Set<String> result = new HashSet<>(builder.getEnchantmentNames());
for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()) {
result.add(enchantmentKey.getKey());
}
return result;
}
/** /**
* Prepare a task to reload every conflict. * Prepare a task to reload every conflict.
*/ */
@ -160,5 +191,8 @@ public class ConflictAPI {
} }
private void logConflictOrigin(@NotNull ConflictBuilder builder){
CustomAnvil.instance.getLogger().warning("Conflict " + builder.getName() +" came from " + builder.getSourceName() + ".");
}
} }

View file

@ -3,6 +3,7 @@ package xyz.alexcrea.cuanvil.api;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
@ -15,7 +16,7 @@ import java.util.Set;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class ConflictBuilder { public class ConflictBuilder {
private final @NotNull Plugin source; private final @Nullable Plugin source;
private @NotNull String name; private @NotNull String name;
private final @NotNull Set<String> enchantmentNames; private final @NotNull Set<String> enchantmentNames;
@ -31,7 +32,7 @@ public class ConflictBuilder {
* @param source the source * @param source the source
* @param name the name * @param name the name
*/ */
public ConflictBuilder(@NotNull Plugin source, @NotNull String name){ public ConflictBuilder(@NotNull String name, @Nullable Plugin source){
this.source = source; this.source = source;
this.name = name; this.name = name;
@ -39,64 +40,96 @@ public class ConflictBuilder {
this.enchantmentKeys = new HashSet<>(); this.enchantmentKeys = new HashSet<>();
this.excludedGroupNames = new HashSet<>(); this.excludedGroupNames = new HashSet<>();
this.maxBeforeConflict = 0;
}
/**
* Instantiates a new Conflict builder.
*
* @param name the conflict name
*/
public ConflictBuilder(@NotNull String name){
this(name, null);
} }
/** /**
* Gets conflict source. * Gets conflict source.
* *
* @return the source * @return the conflict source
*/ */
public @NotNull Plugin getSource() { @Nullable
public Plugin getSource() {
return source; return source;
} }
/**
* Gets conflict source name.
*
* @return the conflict source
*/
@NotNull
public String getSourceName() {
if(source == null) return "an unknown source";
return source.getName();
}
/** /**
* Gets conflict name. * Gets conflict name.
* *
* @return the name * @return the name
*/ */
public @NotNull String getName() { @NotNull
public String getName() {
return name; return name;
} }
/** /**
* Gets stored enchantment names. * Gets stored conflicting enchantment names.
* *
* @return the enchantment names * @return the enchantment names
*/ */
public @NotNull Set<String> getEnchantmentNames() { @NotNull
public Set<String> getEnchantmentNames() {
return enchantmentNames; return enchantmentNames;
} }
/** /**
* Gets stored enchantment keys. * Gets stored conflicting enchantment keys.
* *
* @return the enchantment keys * @return the enchantment keys
*/ */
public @NotNull Set<NamespacedKey> getEnchantmentKeys() { @NotNull
public Set<NamespacedKey> getEnchantmentKeys() {
return enchantmentKeys; return enchantmentKeys;
} }
/** /**
* Gets stored group names. * Gets stored excluded group names.
* *
* @return the group names * @return the group names
*/ */
public @NotNull Set<String> getExcludedGroupNames() { @NotNull
public Set<String> getExcludedGroupNames() {
return excludedGroupNames; return excludedGroupNames;
} }
/** /**
* Gets max before conflict. * Gets max number of conflicting enchantment before conflict is active.
* <p>
* This value represent how many enchantment contained on this conflict can be applied to before conflict is considered active.
* That mean new enchantment will not be able to be added to the item and present enchantment will not have its level upgraded.
* <p>
* In vanilla. material restriction have this value set to 0 and enchantment conflict set to 1.
* *
* @return the max before conflict * @return the max number of conflicting enchantment before conflict. 0 by default.
*/ */
public int getMaxBeforeConflict() { public int getMaxBeforeConflict() {
return maxBeforeConflict; return maxBeforeConflict;
} }
/** /**
* Sets name. * Sets conflict name.
* *
* @param name the name * @param name the name
* @return the name * @return the name
@ -107,7 +140,12 @@ public class ConflictBuilder {
} }
/** /**
* Sets max before conflict. * Sets max number of conflicting enchantment before conflict is active.
* <p>
* This value represent how many enchantment contained on this conflict can be applied to before conflict is considered active.
* That mean new enchantment will not be able to be added to the item and present enchantment will not have its level upgraded.
* <p>
* In vanilla. material restriction have this value set to 0 and enchantment conflict set to 1.
* *
* @param maxBeforeConflict the max before conflict * @param maxBeforeConflict the max before conflict
* @return the max before conflict * @return the max before conflict
@ -118,55 +156,60 @@ public class ConflictBuilder {
} }
/** /**
* Add enchantment by name. * Add a conflicting enchantment by name.
* *
* @param enchantmentName the enchantment name * @param enchantmentName the enchantment name
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder addEnchantment(@NotNull String enchantmentName){ public ConflictBuilder addEnchantment(@NotNull String enchantmentName){
enchantmentNames.add(enchantmentName); enchantmentNames.add(enchantmentName);
return this; return this;
} }
/** /**
* Add enchantment by key. * Add a conflicting enchantment by key.
* *
* @param enchantmentKey the enchantment key * @param enchantmentKey the enchantment key
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder addEnchantment(@NotNull NamespacedKey enchantmentKey){ public ConflictBuilder addEnchantment(@NotNull NamespacedKey enchantmentKey){
enchantmentKeys.add(enchantmentKey); enchantmentKeys.add(enchantmentKey);
return this; return this;
} }
/** /**
* Add enchantment by instance. * Add a conflicting enchantment by instance.
* *
* @param enchantment the enchantment * @param enchantment the enchantment
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder addEnchantment(@NotNull CAEnchantment enchantment){ public ConflictBuilder addEnchantment(@NotNull CAEnchantment enchantment){
addEnchantment(enchantment.getKey()); addEnchantment(enchantment.getKey());
return this; return this;
} }
/** /**
* Remove enchantment by name. * Remove conflicting enchantment by name.
* *
* @param enchantmentName the enchantment name * @param enchantmentName the enchantment name
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder removeEnchantment(@NotNull String enchantmentName){ public ConflictBuilder removeEnchantment(@NotNull String enchantmentName){
enchantmentNames.remove(enchantmentName); enchantmentNames.remove(enchantmentName);
return this; return this;
} }
/** /**
* Remove enchantment by key. * Remove conflicting enchantment by key.
* *
* @param enchantmentKey the enchantment key * @param enchantmentKey the enchantment key
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder removeEnchantment(@NotNull NamespacedKey enchantmentKey){ public ConflictBuilder removeEnchantment(@NotNull NamespacedKey enchantmentKey){
enchantmentKeys.remove(enchantmentKey); enchantmentKeys.remove(enchantmentKey);
return removeEnchantment(enchantmentKey.getKey()); return removeEnchantment(enchantmentKey.getKey());
@ -178,50 +221,87 @@ public class ConflictBuilder {
* @param enchantment the enchantment * @param enchantment the enchantment
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder removeEnchantment(@NotNull CAEnchantment enchantment){ public ConflictBuilder removeEnchantment(@NotNull CAEnchantment enchantment){
return removeEnchantment(enchantment.getKey()); return removeEnchantment(enchantment.getKey());
} }
/** /**
* Add group by name. * Add an excluded group by name.
* <p>
* If left item of an anvil craft is included on one of the excluded group it will ignore this conflict.
* <p>
* This allows to create conflict only for some item. Material restriction can be written like that.
* <p>
* For example: If we exclude a material group containing every pickaxe and add efficiency enchantment
* with {@link #setMaxBeforeConflict(int) maxBeforeConflict} set to 0.
* Then only pickaxe will be able to have efficiency.
* *
* @param groupName the group name * @param groupName the group name
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder addExcludedGroup(@NotNull String groupName){ public ConflictBuilder addExcludedGroup(@NotNull String groupName){
excludedGroupNames.add(groupName); excludedGroupNames.add(groupName);
return this; return this;
} }
/** /**
* Add group by instance. * Add an excluded group by instance.
* <p>
* If left item of an anvil craft is included on one of the excluded group it will ignore this conflict.
* <p>
* This allows to create conflict only for some item. Material restriction can be written like that.
* <p>
* For example: If we exclude a material group containing every pickaxe and add efficiency enchantment
* with {@link #setMaxBeforeConflict(int) maxBeforeConflict} set to 0.
* Then only pickaxe will be able to have efficiency.
* *
* @param group the group * @param group the group
* @return this conflict builder instance * @return this conflict builder instance
*/ */
@NotNull
public ConflictBuilder addExcludedGroup(@NotNull AbstractMaterialGroup group){ public ConflictBuilder addExcludedGroup(@NotNull AbstractMaterialGroup group){
return addExcludedGroup(group.getName()); return addExcludedGroup(group.getName());
} }
/** /**
* Remove group by name. * Remove an excluded group by name.
* <p>
* If left item of an anvil craft is included on one of the excluded group it will ignore this conflict.
* <p>
* This allows to create conflict only for some item. Material restriction can be written like that.
* <p>
* For example: If we exclude a material group containing every pickaxe and add efficiency enchantment
* with {@link #setMaxBeforeConflict(int) maxBeforeConflict} set to 0.
* Then only pickaxe will be able to have efficiency.
* *
* @param groupName the group name * @param groupName the group name
* @return this conflict builder instance * @return this conflict builder instance
*/ */
public ConflictBuilder removeGroup(@NotNull String groupName){ @NotNull
public ConflictBuilder removeExcludedGroup(@NotNull String groupName){
excludedGroupNames.remove(groupName); excludedGroupNames.remove(groupName);
return this; return this;
} }
/** /**
* Remove group by instance. * Remove an excluded group by instance.
* <p>
* If left item of an anvil craft is included on one of the excluded group it will ignore this conflict.
* <p>
* This allows to create conflict only for some item. Material restriction can be written like that.
* <p>
* For example: If we exclude a material group containing every pickaxe and add efficiency enchantment
* with {@link #setMaxBeforeConflict(int) maxBeforeConflict} set to 0.
* Then only pickaxe will be able to have efficiency.
* *
* @param group the group * @param group the group
* @return this conflict builder instance * @return this conflict builder instance
*/ */
public ConflictBuilder removeGroup(@NotNull AbstractMaterialGroup group){ @NotNull
return removeGroup(group.getName()); public ConflictBuilder removeExcludedGroup(@NotNull AbstractMaterialGroup group){
return removeExcludedGroup(group.getName());
} }
/** /**
@ -229,8 +309,9 @@ public class ConflictBuilder {
* *
* @return a copy of this conflict builder * @return a copy of this conflict builder
*/ */
@NotNull
public ConflictBuilder copy() { public ConflictBuilder copy() {
ConflictBuilder clone = new ConflictBuilder(this.source, this.name); ConflictBuilder clone = new ConflictBuilder(this.name, this.source);
setMaxBeforeConflict(this.maxBeforeConflict); setMaxBeforeConflict(this.maxBeforeConflict);

View file

@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
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 xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
import xyz.alexcrea.cuanvil.enchant.wrapped.CAVanillaEnchantment; import xyz.alexcrea.cuanvil.enchant.wrapped.CAVanillaEnchantment;
/** /**
@ -27,7 +28,21 @@ public class EnchantmentApi {
} }
/** /**
* Register an enchantment by minecraft registered enchantment. * Register an enchantment by minecraft registered enchantment instance.
*
* @param enchantment the enchantment to register
* @param defaultRarity the default rarity of the provided enchantment
* @return true if successful
*/
public static boolean registerEnchantment(@NotNull Enchantment enchantment, @Nullable EnchantmentRarity defaultRarity){
if(defaultRarity == null)
return registerEnchantment(new CAVanillaEnchantment(enchantment));
return registerEnchantment(new CAVanillaEnchantment(enchantment, defaultRarity));
}
/**
* Register an enchantment by minecraft registered enchantment instance.
* *
* @param enchantment the enchantment to register * @param enchantment the enchantment to register
* @return true if successful * @return true if successful

View file

@ -1,31 +1,31 @@
package xyz.alexcrea.cuanvil.enchant; package xyz.alexcrea.cuanvil.enchant;
// because spigot (1.18) do not support enchantment rarity, I need to do it myself... // because spigot (1.18) do not look like to provide access to enchantment rarity I need to do it myself...
public enum EnchantmentRarity { public class EnchantmentRarity {
NO_RARITY(0, 0), public static final EnchantmentRarity NO_RARITY = new EnchantmentRarity(0, 0);
COMMON(1), public static final EnchantmentRarity COMMON = new EnchantmentRarity(1);
UNCOMMON(2), public static final EnchantmentRarity UNCOMMON = new EnchantmentRarity(2);
RARE(4), public static final EnchantmentRarity RARE = new EnchantmentRarity(4);
VERY_RARE(8); public static final EnchantmentRarity VERY_RARE = new EnchantmentRarity(8);
private final int itemValue; private final int itemValue;
private final int bookValue; private final int bookValue;
EnchantmentRarity(int itemValue, int bookValue) { public EnchantmentRarity(int itemValue, int bookValue) {
this.itemValue = itemValue; this.itemValue = itemValue;
this.bookValue = bookValue; this.bookValue = bookValue;
} }
EnchantmentRarity(int itemValue) { public EnchantmentRarity(int itemValue) {
this(itemValue, Math.max(1, itemValue / 2)); this(itemValue, Math.max(1, itemValue / 2));
} }
public int getBookValue() { public final int getBookValue() {
return bookValue; return bookValue;
} }
public int getItemValue() { public final int getItemValue() {
return itemValue; return itemValue;
} }

View file

@ -28,7 +28,7 @@ class EnchantConflictManager {
private const val FUTURE_USE_PATH = "useInFuture" private const val FUTURE_USE_PATH = "useInFuture"
// Default name for a joining group // Default name for a joining group
public const val DEFAULT_GROUP_NAME = "joinedGroup" const val DEFAULT_GROUP_NAME = "joinedGroup"
// 1.20.5 compatibility TODO better update system // 1.20.5 compatibility TODO better update system
private val SWEEPING_EDGE_ENCHANT = private val SWEEPING_EDGE_ENCHANT =
@ -175,7 +175,7 @@ class EnchantConflictManager {
if(doConflict){ if(doConflict){
return ConflictType.ENCHANTMENT_CONFLICT return ConflictType.ENCHANTMENT_CONFLICT
} }
;
} }
} }
@ -188,7 +188,7 @@ class EnchantConflictManager {
} }
return result; return result
} }
private fun createPartialResult(item: ItemStack, enchantments: Map<CAEnchantment, Int>): ItemStack { private fun createPartialResult(item: ItemStack, enchantments: Map<CAEnchantment, Int>): ItemStack {