mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Allow mutliples enchantment to use same name and expose api.
This commit is contained in:
parent
cc697f2d88
commit
b0b1777883
8 changed files with 87 additions and 20 deletions
|
|
@ -119,7 +119,7 @@ public class ConflictAPI {
|
||||||
private static List<String> extractEnchantments(@NotNull ConflictBuilder builder){
|
private static List<String> extractEnchantments(@NotNull ConflictBuilder builder){
|
||||||
List<String> result = new ArrayList<>(builder.getEnchantmentNames());
|
List<String> result = new ArrayList<>(builder.getEnchantmentNames());
|
||||||
for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()) {
|
for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()) {
|
||||||
result.add(enchantmentKey.getKey());
|
result.add(enchantmentKey.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
import xyz.alexcrea.cuanvil.group.*;
|
import xyz.alexcrea.cuanvil.group.*;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -372,7 +373,7 @@ public class ConflictBuilder {
|
||||||
*/
|
*/
|
||||||
protected void appendEnchantments(@NotNull EnchantConflictGroup conflict){
|
protected void appendEnchantments(@NotNull EnchantConflictGroup conflict){
|
||||||
for (String enchantmentName : getEnchantmentNames()){
|
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());
|
CustomAnvil.instance.getLogger().warning("Could not find enchantment " + enchantmentName + " for conflict " + getName());
|
||||||
ConflictAPI.logConflictOrigin(this);
|
ConflictAPI.logConflictOrigin(this);
|
||||||
}
|
}
|
||||||
|
|
@ -399,6 +400,24 @@ public class ConflictBuilder {
|
||||||
return true;
|
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.
|
* Extract group abstract material group.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import xyz.alexcrea.cuanvil.gui.config.global.EnchantCostConfigGui;
|
||||||
import xyz.alexcrea.cuanvil.gui.config.global.EnchantLimitConfigGui;
|
import xyz.alexcrea.cuanvil.gui.config.global.EnchantLimitConfigGui;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -134,12 +135,24 @@ public class EnchantmentApi {
|
||||||
*
|
*
|
||||||
* @param name The name used to fetch
|
* @param name The name used to fetch
|
||||||
* @return The custom anvil enchantment of this name. null if not found.
|
* @return The custom anvil enchantment of this name. null if not found.
|
||||||
|
* @deprecated use {@link #getListByName(String)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated(since = "1.6.1")
|
||||||
@Nullable
|
@Nullable
|
||||||
public static CAEnchantment getByName(@NotNull String name){
|
public static CAEnchantment getByName(@NotNull String name){
|
||||||
return CAEnchantment.getByName(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.
|
* Get every registered custom anvil enchantments.
|
||||||
* @return An immutable map of enchantment key as map key and custom anvil enchantment as value.
|
* @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) {
|
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();
|
EnchantmentRarity rarity = enchantment.defaultRarity();
|
||||||
|
|
||||||
defaultConfig.set(basePath + ".item", rarity.getItemValue());
|
defaultConfig.set(basePath + ".item", rarity.getItemValue());
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ 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.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -226,12 +227,24 @@ public interface CAEnchantment {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list of all the unoptimised enchantments.
|
* Gets the enchantment by the provided name.
|
||||||
* @param name The enchantment name
|
* @param name Name to fetch.
|
||||||
* @return List of enchantment.
|
* @return Registered enchantment. null if absent.
|
||||||
|
*
|
||||||
|
* @deprecated use {@link #getListByName(String)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated(since = "1.6.1")
|
||||||
static @Nullable CAEnchantment getByName(@NotNull String name){
|
static @Nullable CAEnchantment getByName(@NotNull String name){
|
||||||
return CAEnchantmentRegistry.getInstance().getByName(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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ public class CAEnchantmentRegistry {
|
||||||
|
|
||||||
// Register enchantment functions
|
// Register enchantment functions
|
||||||
private final HashMap<NamespacedKey, CAEnchantment> byKeyMap;
|
private final HashMap<NamespacedKey, CAEnchantment> byKeyMap;
|
||||||
private final HashMap<String, CAEnchantment> byNameMap;
|
private final HashMap<String, List<CAEnchantment>> byNameMap;
|
||||||
|
|
||||||
private final SortedSet<CAEnchantment> nameSortedEnchantments;
|
private final SortedSet<CAEnchantment> nameSortedEnchantments;
|
||||||
|
|
||||||
|
|
@ -90,7 +90,10 @@ public class CAEnchantmentRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
byKeyMap.put(enchantment.getKey(), enchantment);
|
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);
|
nameSortedEnchantments.add(enchantment);
|
||||||
|
|
||||||
if(!enchantment.isGetOptimised()){
|
if(!enchantment.isGetOptimised()){
|
||||||
|
|
@ -117,7 +120,7 @@ public class CAEnchantmentRegistry {
|
||||||
public boolean unregister(@Nullable CAEnchantment enchantment){
|
public boolean unregister(@Nullable CAEnchantment enchantment){
|
||||||
if(enchantment == null) return false;
|
if(enchantment == null) return false;
|
||||||
byKeyMap.remove(enchantment.getKey());
|
byKeyMap.remove(enchantment.getKey());
|
||||||
byNameMap.remove(enchantment.getName());
|
byNameMap.get(enchantment.getName()).remove(enchantment);
|
||||||
|
|
||||||
nameSortedEnchantments.remove(enchantment);
|
nameSortedEnchantments.remove(enchantment);
|
||||||
|
|
||||||
|
|
@ -140,10 +143,26 @@ public class CAEnchantmentRegistry {
|
||||||
* Gets the enchantment by the provided name.
|
* Gets the enchantment by the provided name.
|
||||||
* @param name Name to fetch.
|
* @param name Name to fetch.
|
||||||
* @return Registered enchantment. null if absent.
|
* @return Registered enchantment. null if absent.
|
||||||
|
*
|
||||||
|
* @deprecated use {@link #getListByName(String)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated(since = "1.6.1")
|
||||||
@Nullable
|
@Nullable
|
||||||
public CAEnchantment getByName(@NotNull String name){
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ public class EnchantLimitConfigGui extends AbstractEnchantConfigGui<IntSettingsG
|
||||||
String key = enchant.getKey().toString().toLowerCase(Locale.ROOT);
|
String key = enchant.getKey().toString().toLowerCase(Locale.ROOT);
|
||||||
String prettyKey = CasedStringUtil.snakeToUpperSpacedCase(key.replace(":", "_"));
|
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,
|
SECTION_NAME + '.' + key, ConfigHolder.DEFAULT_CONFIG,
|
||||||
Collections.singletonList(
|
Collections.singletonList(
|
||||||
"§7Maximum applied level of " + prettyKey
|
"§7Maximum applied level of " + prettyKey
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@ class EnchantConflictGroup(
|
||||||
fun addEnchantment(enchant: CAEnchantment) {
|
fun addEnchantment(enchant: CAEnchantment) {
|
||||||
enchantments.add(enchant)
|
enchantments.add(enchant)
|
||||||
}
|
}
|
||||||
|
fun addEnchantments(enchants: List<CAEnchantment>) {
|
||||||
|
enchantments.addAll(enchants)
|
||||||
|
}
|
||||||
|
|
||||||
fun allowed(enchants: Set<CAEnchantment>, mat: Material): Boolean {
|
fun allowed(enchants: Set<CAEnchantment>, mat: Material): Boolean {
|
||||||
if (enchantments.size < minBeforeBlock) {
|
if (enchantments.size < minBeforeBlock) {
|
||||||
|
|
|
||||||
|
|
@ -30,9 +30,9 @@ class EnchantConflictManager {
|
||||||
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 = Collections.singletonList<CAEnchantment>(
|
||||||
CAEnchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
|
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
|
// Read and add enchantment to conflict
|
||||||
val enchantList = section.getStringList(ENCH_LIST_PATH)
|
val enchantList = section.getStringList(ENCH_LIST_PATH)
|
||||||
for (enchantName in enchantList) {
|
for (enchantName in enchantList) {
|
||||||
val enchant = getEnchantByIdentifier(enchantName)
|
val enchants = getEnchantByIdentifier(enchantName)
|
||||||
if (enchant == null) {
|
if (enchants.isEmpty()) {
|
||||||
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
|
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")
|
CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict $conflictName")
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
conflict.addEnchantment(enchant)
|
conflict.addEnchantments(enchants)
|
||||||
}
|
}
|
||||||
if (conflict.getEnchants().isEmpty()) {
|
if (conflict.getEnchants().isEmpty()) {
|
||||||
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
|
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
|
||||||
|
|
@ -112,11 +112,11 @@ class EnchantConflictManager {
|
||||||
return conflict
|
return conflict
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEnchantByIdentifier(enchantName: String): CAEnchantment? {
|
private fun getEnchantByIdentifier(enchantName: String): List<CAEnchantment> {
|
||||||
val key = NamespacedKey.fromString(enchantName)
|
val key = NamespacedKey.fromString(enchantName)
|
||||||
if(key != null){
|
if(key != null){
|
||||||
val enchantment = CAEnchantment.getByKey(key)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue