Fix echantment reload issue (#17)

Add and remove added/removed eco enchantments
Handle enchantment order (sorted by name) now work everywhere with
add/remove working.
This commit is contained in:
alexcrea 2024-07-24 20:11:04 +02:00 committed by GitHub
commit 914331e0ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 103 additions and 21 deletions

View file

@ -51,7 +51,7 @@ Here is a list of supported plugins with support status:
- [Enchantment²](https://www.spigotmc.org/resources/enchants-squared-the-enchantsplus-rewrite-custom-enchantments-that-act-like-vanilla-ones.86747/): - [Enchantment²](https://www.spigotmc.org/resources/enchants-squared-the-enchantsplus-rewrite-custom-enchantments-that-act-like-vanilla-ones.86747/):
Officially supported by Custom Anvil but still experimental. Automatic configuration. Officially supported by Custom Anvil but still experimental. Automatic configuration.
- [EcoEnchant](https://www.spigotmc.org/resources/50-sale-%E2%8C%9B-ecoenchants-%E2%AD%95-250-enchantments-%E2%9C%85-create-custom-enchants-%E2%9C%A8-essentials-cmi-support.79573/): - [EcoEnchant](https://www.spigotmc.org/resources/50-sale-%E2%8C%9B-ecoenchants-%E2%AD%95-250-enchantments-%E2%9C%85-create-custom-enchants-%E2%9C%A8-essentials-cmi-support.79573/):
Officially supported by Custom Anvil but still experimental. Need a server restart to add newly added enchantment. Officially supported by Custom Anvil but still experimental. Need to use /anvilconfigreload or a server restart to add newly added enchantment.
Use EcoEnchant restriction system by default. Use EcoEnchant restriction system by default.
If you like Custom Anvil to support a specific custom enchantment plugin. If you like Custom Anvil to support a specific custom enchantment plugin.

View file

@ -24,6 +24,8 @@ public class CAEnchantmentRegistry {
private final HashMap<NamespacedKey, CAEnchantment> byKeyMap; private final HashMap<NamespacedKey, CAEnchantment> byKeyMap;
private final HashMap<String, CAEnchantment> byNameMap; private final HashMap<String, CAEnchantment> byNameMap;
private final SortedSet<CAEnchantment> nameSortedEnchantments;
private final List<CAEnchantment> unoptimisedGetValues; private final List<CAEnchantment> unoptimisedGetValues;
private final List<CAEnchantment> unoptimisedCleanValues; private final List<CAEnchantment> unoptimisedCleanValues;
@ -34,6 +36,8 @@ public class CAEnchantmentRegistry {
byKeyMap = new HashMap<>(); byKeyMap = new HashMap<>();
byNameMap = new HashMap<>(); byNameMap = new HashMap<>();
nameSortedEnchantments = new TreeSet<>(Comparator.comparing(CAEnchantment::getName));
unoptimisedGetValues = new ArrayList<>(); unoptimisedGetValues = new ArrayList<>();
unoptimisedCleanValues = new ArrayList<>(); unoptimisedCleanValues = new ArrayList<>();
@ -82,6 +86,7 @@ public class CAEnchantmentRegistry {
byKeyMap.put(enchantment.getKey(), enchantment); byKeyMap.put(enchantment.getKey(), enchantment);
byNameMap.put(enchantment.getName(), enchantment); byNameMap.put(enchantment.getName(), enchantment);
nameSortedEnchantments.add(enchantment);
if(!enchantment.isGetOptimised()){ if(!enchantment.isGetOptimised()){
unoptimisedGetValues.add(enchantment); unoptimisedGetValues.add(enchantment);
@ -109,6 +114,8 @@ public class CAEnchantmentRegistry {
byKeyMap.remove(enchantment.getKey()); byKeyMap.remove(enchantment.getKey());
byNameMap.remove(enchantment.getName()); byNameMap.remove(enchantment.getName());
nameSortedEnchantments.remove(enchantment);
unoptimisedGetValues.remove(enchantment); unoptimisedGetValues.remove(enchantment);
unoptimisedCleanValues.remove(enchantment); unoptimisedCleanValues.remove(enchantment);
return true; return true;
@ -145,10 +152,10 @@ public class CAEnchantmentRegistry {
/** /**
* Gets a map of all the registered enchantments. * Gets a map of all the registered enchantments.
* @return Map of enchantments. * @return Immutable map of enchantments.
*/ */
public Map<NamespacedKey, CAEnchantment> registeredEnchantments() { public Map<NamespacedKey, CAEnchantment> registeredEnchantments() {
return byKeyMap; return Collections.unmodifiableMap(byKeyMap);
} }
/** /**
@ -171,7 +178,7 @@ public class CAEnchantmentRegistry {
/** /**
* Get "clean optimised operation" for get enchantments. * Get "clean optimised operation" for get enchantments.
* @return Get mutable "clean enchantments optimised operation" list. * @return Mutable "clean enchantments optimised operation" list.
*/ */
public List<BulkCleanEnchantOperation> getOptimisedCleanOperators() { public List<BulkCleanEnchantOperation> getOptimisedCleanOperators() {
return optimisedCleanOperators; return optimisedCleanOperators;
@ -179,10 +186,17 @@ public class CAEnchantmentRegistry {
/** /**
* Get "get optimised operation" for get enchantments. * Get "get optimised operation" for get enchantments.
* @return Get mutable "get enchantments optimised operation" list. * @return Mutable "get enchantments optimised operation" list.
*/ */
public List<BulkGetEnchantOperation> getOptimisedGetOperators() { public List<BulkGetEnchantOperation> getOptimisedGetOperators() {
return optimisedGetOperators; return optimisedGetOperators;
} }
/**
* Get custom anvil enchantment sorted by name.
* @return An immutable sorted set of every registered enchantment sorted by name.
*/
public SortedSet<CAEnchantment> getNameSortedEnchantments() {
return Collections.unmodifiableSortedSet(nameSortedEnchantments);
}
} }

View file

@ -4,6 +4,7 @@ import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui; import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
@ -37,7 +38,7 @@ public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFa
@Override @Override
protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() { protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() {
return GuiSharedConstant.SORTED_ENCHANTMENT_LIST; return CAEnchantmentRegistry.getInstance().getNameSortedEnchantments();
} }
@Override @Override
@ -52,6 +53,46 @@ public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFa
); );
} }
@Override
public void updateValueForGeneric(CAEnchantment generic, boolean shouldUpdate) {
updateValueForGeneric(generic, shouldUpdate, true);
}
public void updateValueForGeneric(CAEnchantment generic, boolean shouldUpdate, boolean prepareSorting) {
if(!prepareSorting) {
super.updateValueForGeneric(generic, shouldUpdate);
return;
}
if(!this.factoryMap.containsKey(generic)){
// We need to sort elements again
super.updateValueForGeneric(generic, false);
// Clear page then refill all of them
this.firstPage.clear();
this.pages.clear();
this.pages.add(this.firstPage);
for (CAEnchantment enchantment : getEveryDisplayableInstanceOfGeneric()) {
GuiItem item = this.guiItemMap.get(enchantment);
if(item == null) {
updateValueForGeneric(enchantment, false, false);
}else {
addToPage(item);
}
}
if(shouldUpdate) update();
}else{
super.updateValueForGeneric(generic, shouldUpdate);
}
}
// Unused methods // Unused methods
@Override @Override

View file

@ -118,8 +118,8 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu
this.pages.clear(); this.pages.clear();
this.pages.add(this.firstPage); this.pages.add(this.firstPage);
for (T conflict : getEveryDisplayableInstanceOfGeneric()) { for (T generic : getEveryDisplayableInstanceOfGeneric()) {
updateValueForGeneric(conflict, false); updateValueForGeneric(generic, false);
} }
update(); update();

View file

@ -65,9 +65,9 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui<CAEnchantme
protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() { protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() {
Stream<CAEnchantment> toDisplayStream; Stream<CAEnchantment> toDisplayStream;
if(this.displayUnselected){ if(this.displayUnselected){
toDisplayStream = CAEnchantmentRegistry.getInstance().values().stream(); toDisplayStream = CAEnchantmentRegistry.getInstance().getNameSortedEnchantments().stream();
}else{ }else{
toDisplayStream = this.selectedEnchant.stream(); toDisplayStream = this.selectedEnchant.stream().sorted(Comparator.comparing(CAEnchantment::getName));
} }
Set<CAEnchantment> illegalEnchantments = this.enchantContainer.illegalEnchantments(); Set<CAEnchantment> illegalEnchantments = this.enchantContainer.illegalEnchantments();

View file

@ -7,20 +7,13 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui; import xyz.alexcrea.cuanvil.gui.config.MainConfigGui;
import java.util.*; import java.util.*;
public class GuiSharedConstant { public class GuiSharedConstant {
public static final List<CAEnchantment> SORTED_ENCHANTMENT_LIST; private GuiSharedConstant(){}
static {
SORTED_ENCHANTMENT_LIST = new ArrayList<>(CAEnchantmentRegistry.getInstance().values());
SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
}
public static final Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE; public static final Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE;
public static final GuiItem SECONDARY_BACKGROUND_ITEM = GuiGlobalItems.backgroundItem(GuiSharedConstant.SECONDARY_BACKGROUND_MATERIAL); public static final GuiItem SECONDARY_BACKGROUND_ITEM = GuiGlobalItems.backgroundItem(GuiSharedConstant.SECONDARY_BACKGROUND_MATERIAL);

View file

@ -51,8 +51,8 @@ class ReloadExecutor : CommandExecutor {
// temporary: handle 1.21 update // temporary: handle 1.21 update
Update_1_21.handleUpdate() Update_1_21.handleUpdate()
// Register enchantment of compatible plugin and load configuration change. // Handle dependency reload
DependencyManager.handleCompatibilityConfig() DependencyManager.handleConfigReload()
// Call event // Call event
val configReadyEvent = CAConfigReadyEvent() val configReadyEvent = CAConfigReadyEvent()

View file

@ -44,4 +44,13 @@ object DependencyManager {
} }
fun handleConfigReload(){
// Register enchantment of compatible plugin and load configuration change.
handleCompatibilityConfig()
// Then handle plugin reload
ecoEnchantCompatibility?.handleConfigReload()
}
} }

View file

@ -1,5 +1,6 @@
package xyz.alexcrea.cuanvil.dependency package xyz.alexcrea.cuanvil.dependency
import com.willfp.ecoenchants.enchant.EcoEnchant
import com.willfp.ecoenchants.enchant.EcoEnchants import com.willfp.ecoenchants.enchant.EcoEnchants
import io.delilaheve.CustomAnvil import io.delilaheve.CustomAnvil
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
@ -17,15 +18,39 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
PrepareAnvilEvent.getHandlerList().unregister(this.ecoEnchantPlugin) PrepareAnvilEvent.getHandlerList().unregister(this.ecoEnchantPlugin)
} }
private var ecoEnchantOldEnchantments: MutableSet<EcoEnchant>? = null
fun registerEnchantments() { fun registerEnchantments() {
CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...") CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...")
for (ecoEnchant in EcoEnchants.values()) { val enchantments = EcoEnchants.values()
for (ecoEnchant in enchantments) {
EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant. EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant.
EnchantmentApi.registerEnchantment(CAEcoEnchant(ecoEnchant)) EnchantmentApi.registerEnchantment(CAEcoEnchant(ecoEnchant))
} }
ecoEnchantOldEnchantments = HashSet(enchantments)
CustomAnvil.instance.logger.info("Eco Enchant should now work as expected !") CustomAnvil.instance.logger.info("Eco Enchant should now work as expected !")
} }
fun handleConfigReload() {
// Should not happen in known case.
if(this.ecoEnchantOldEnchantments == null) return
val newEnchantments = EcoEnchants.values()
// Add new enchantments
for (ecoEnchant in newEnchantments)
if(!this.ecoEnchantOldEnchantments!!.contains(ecoEnchant))
EnchantmentApi.registerEnchantment(CAEcoEnchant(ecoEnchant))
// Remove old enchantments that not now currently used
this.ecoEnchantOldEnchantments!!.removeAll(newEnchantments)
for (oldEnchantment in this.ecoEnchantOldEnchantments!!) {
EnchantmentApi.unregisterEnchantment(oldEnchantment.enchantment)
}
this.ecoEnchantOldEnchantments = HashSet(newEnchantments)
}
} }