Allow mutliples enchantment to use same name and expose api.

This commit is contained in:
alexcrea 2024-09-08 17:33:09 +02:00
parent cc697f2d88
commit b0b1777883
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
8 changed files with 87 additions and 20 deletions

View file

@ -119,7 +119,7 @@ public class ConflictAPI {
private static List<String> extractEnchantments(@NotNull ConflictBuilder builder){
List<String> result = new ArrayList<>(builder.getEnchantmentNames());
for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()) {
result.add(enchantmentKey.getKey());
result.add(enchantmentKey.toString());
}
return result;

View file

@ -10,6 +10,7 @@ import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.group.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
@ -372,7 +373,7 @@ public class ConflictBuilder {
*/
protected void appendEnchantments(@NotNull EnchantConflictGroup conflict){
for (String enchantmentName : getEnchantmentNames()){
if(appendEnchantment(conflict, EnchantmentApi.getByName(enchantmentName))){
if(appendEnchantments(conflict, EnchantmentApi.getListByName(enchantmentName)) == 0){
CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentName + " for conflict " + getName());
ConflictAPI.logConflictOrigin(this);
}
@ -399,6 +400,24 @@ public class ConflictBuilder {
return true;
}
/**
* Append a list of enchantments.
*
* @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){
int numberValid = 0;
for (CAEnchantment enchantment : enchantments) {
if(appendEnchantment(conflict, enchantment)){
numberValid++;
}
}
return numberValid;
}
/**
* Extract group abstract material group.
*

View file

@ -18,6 +18,7 @@ import xyz.alexcrea.cuanvil.gui.config.global.EnchantCostConfigGui;
import xyz.alexcrea.cuanvil.gui.config.global.EnchantLimitConfigGui;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
@ -134,12 +135,24 @@ public class EnchantmentApi {
*
* @param name The name used to fetch
* @return The custom anvil enchantment of this name. null if not found.
* @deprecated use {@link #getListByName(String)}
*/
@Deprecated(since = "1.6.1")
@Nullable
public static CAEnchantment getByName(@NotNull String name){
return CAEnchantment.getByName(name);
}
/**
* Get list of enchantment using the provided name.
*
* @param name The name used to fetch
* @return List of custom anvil enchantments of this name. May be empty if not found.
*/
public static List<CAEnchantment> getListByName(@NotNull String name){
return CAEnchantment.getListByName(name);
}
/**
* Get every registered custom anvil enchantments.
* @return An immutable map of enchantment key as map key and custom anvil enchantment as value.
@ -167,9 +180,9 @@ public class EnchantmentApi {
private static void writeDefaultConfig(FileConfiguration defaultConfig, CAEnchantment enchantment) {
defaultConfig.set("enchant_limits." + enchantment.getKey().getKey(), enchantment.defaultMaxLevel());
defaultConfig.set("enchant_limits." + enchantment.getKey(), enchantment.defaultMaxLevel());
String basePath = "enchant_values." + enchantment.getKey().getKey();
String basePath = "enchant_values." + enchantment.getKey();
EnchantmentRarity rarity = enchantment.defaultRarity();
defaultConfig.set(basePath + ".item", rarity.getItemValue());

View file

@ -12,6 +12,7 @@ import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -226,12 +227,24 @@ public interface CAEnchantment {
}
/**
* Gets a list of all the unoptimised enchantments.
* @param name The enchantment name
* @return List of enchantment.
* Gets the enchantment by the provided name.
* @param name Name to fetch.
* @return Registered enchantment. null if absent.
*
* @deprecated use {@link #getListByName(String)}
*/
@Deprecated(since = "1.6.1")
static @Nullable CAEnchantment getByName(@NotNull String name){
return CAEnchantmentRegistry.getInstance().getByName(name);
}
/**
* Gets list of enchantment using the provided name.
* @param name Name to fetch.
* @return List of registered enchantment.
*/
static List<CAEnchantment> getListByName(@NotNull String name){
return CAEnchantmentRegistry.getInstance().getListByName(name);
}
}

View file

@ -22,7 +22,7 @@ public class CAEnchantmentRegistry {
// Register enchantment functions
private final HashMap<NamespacedKey, CAEnchantment> byKeyMap;
private final HashMap<String, CAEnchantment> byNameMap;
private final HashMap<String, List<CAEnchantment>> byNameMap;
private final SortedSet<CAEnchantment> nameSortedEnchantments;
@ -90,7 +90,10 @@ public class CAEnchantmentRegistry {
}
byKeyMap.put(enchantment.getKey(), enchantment);
byNameMap.put(enchantment.getName(), enchantment);
byNameMap.putIfAbsent(enchantment.getName(), new ArrayList<>());
byNameMap.get(enchantment.getName()).add(enchantment);
nameSortedEnchantments.add(enchantment);
if(!enchantment.isGetOptimised()){
@ -117,7 +120,7 @@ public class CAEnchantmentRegistry {
public boolean unregister(@Nullable CAEnchantment enchantment){
if(enchantment == null) return false;
byKeyMap.remove(enchantment.getKey());
byNameMap.remove(enchantment.getName());
byNameMap.get(enchantment.getName()).remove(enchantment);
nameSortedEnchantments.remove(enchantment);
@ -140,10 +143,26 @@ public class CAEnchantmentRegistry {
* Gets the enchantment by the provided name.
* @param name Name to fetch.
* @return Registered enchantment. null if absent.
*
* @deprecated use {@link #getListByName(String)}
*/
@Deprecated(since = "1.6.1")
@Nullable
public CAEnchantment getByName(@NotNull String name){
return byNameMap.get(name);
List<CAEnchantment> enchantments = getListByName(name);
if(enchantments.isEmpty()) return null;
return enchantments.get(0);
}
/**
* Gets list of enchantment using the provided name.
* @param name Name to fetch.
* @return List of registered enchantment.
*/
@NotNull
public List<CAEnchantment> getListByName(@NotNull String name){
return byNameMap.getOrDefault(name, Collections.emptyList());
}
/**

View file

@ -41,7 +41,7 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
String key = enchant.getKey().toString().toLowerCase(Locale.ROOT);
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key.replace(":", "_"));
return new IntSettingsGui.IntSettingFactory(prettyKey + "Limit", this,
return new IntSettingsGui.IntSettingFactory(prettyKey + " Limit", this,
SECTION_NAME + '.' + key, ConfigHolder.DEFAULT_CONFIG,
Collections.singletonList(
"§7Maximum applied level of " + prettyKey

View file

@ -15,6 +15,9 @@ class EnchantConflictGroup(
fun addEnchantment(enchant: CAEnchantment) {
enchantments.add(enchant)
}
fun addEnchantments(enchants: List<CAEnchantment>) {
enchantments.addAll(enchants)
}
fun allowed(enchants: Set<CAEnchantment>, mat: Material): Boolean {
if (enchantments.size < minBeforeBlock) {

View file

@ -30,9 +30,9 @@ class EnchantConflictManager {
const val DEFAULT_GROUP_NAME = "joinedGroup"
// 1.20.5 compatibility TODO better update system
private val SWEEPING_EDGE_ENCHANT =
private val SWEEPING_EDGE_ENCHANT = Collections.singletonList<CAEnchantment>(
CAEnchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
CAEnchantment.getByKey(Enchantment.SWEEPING_EDGE.key)
CAEnchantment.getByKey(Enchantment.SWEEPING_EDGE.key))
}
@ -94,14 +94,14 @@ class EnchantConflictManager {
// Read and add enchantment to conflict
val enchantList = section.getStringList(ENCH_LIST_PATH)
for (enchantName in enchantList) {
val enchant = getEnchantByIdentifier(enchantName)
if (enchant == null) {
val enchants = getEnchantByIdentifier(enchantName)
if (enchants.isEmpty()) {
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict $conflictName")
}
continue
}
conflict.addEnchantment(enchant)
conflict.addEnchantments(enchants)
}
if (conflict.getEnchants().isEmpty()) {
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
@ -112,11 +112,11 @@ class EnchantConflictManager {
return conflict
}
private fun getEnchantByIdentifier(enchantName: String): CAEnchantment? {
private fun getEnchantByIdentifier(enchantName: String): List<CAEnchantment> {
val key = NamespacedKey.fromString(enchantName)
if(key != null){
val enchantment = CAEnchantment.getByKey(key)
if(enchantment != null) return enchantment
if(enchantment != null) return Collections.singletonList(enchantment)
}
@ -127,7 +127,7 @@ class EnchantConflictManager {
}
}
return CAEnchantment.getByName(enchantName)
return CAEnchantment.getListByName(enchantName)
}