mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 08:14:00 +02:00
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:
commit
914331e0ac
9 changed files with 103 additions and 21 deletions
|
|
@ -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/):
|
||||
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/):
|
||||
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.
|
||||
|
||||
If you like Custom Anvil to support a specific custom enchantment plugin.
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ public class CAEnchantmentRegistry {
|
|||
private final HashMap<NamespacedKey, CAEnchantment> byKeyMap;
|
||||
private final HashMap<String, CAEnchantment> byNameMap;
|
||||
|
||||
private final SortedSet<CAEnchantment> nameSortedEnchantments;
|
||||
|
||||
private final List<CAEnchantment> unoptimisedGetValues;
|
||||
private final List<CAEnchantment> unoptimisedCleanValues;
|
||||
|
||||
|
|
@ -34,6 +36,8 @@ public class CAEnchantmentRegistry {
|
|||
byKeyMap = new HashMap<>();
|
||||
byNameMap = new HashMap<>();
|
||||
|
||||
nameSortedEnchantments = new TreeSet<>(Comparator.comparing(CAEnchantment::getName));
|
||||
|
||||
unoptimisedGetValues = new ArrayList<>();
|
||||
unoptimisedCleanValues = new ArrayList<>();
|
||||
|
||||
|
|
@ -82,6 +86,7 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
byKeyMap.put(enchantment.getKey(), enchantment);
|
||||
byNameMap.put(enchantment.getName(), enchantment);
|
||||
nameSortedEnchantments.add(enchantment);
|
||||
|
||||
if(!enchantment.isGetOptimised()){
|
||||
unoptimisedGetValues.add(enchantment);
|
||||
|
|
@ -109,6 +114,8 @@ public class CAEnchantmentRegistry {
|
|||
byKeyMap.remove(enchantment.getKey());
|
||||
byNameMap.remove(enchantment.getName());
|
||||
|
||||
nameSortedEnchantments.remove(enchantment);
|
||||
|
||||
unoptimisedGetValues.remove(enchantment);
|
||||
unoptimisedCleanValues.remove(enchantment);
|
||||
return true;
|
||||
|
|
@ -145,10 +152,10 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* Gets a map of all the registered enchantments.
|
||||
* @return Map of enchantments.
|
||||
* @return Immutable map of enchantments.
|
||||
*/
|
||||
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.
|
||||
* @return Get mutable "clean enchantments optimised operation" list.
|
||||
* @return Mutable "clean enchantments optimised operation" list.
|
||||
*/
|
||||
public List<BulkCleanEnchantOperation> getOptimisedCleanOperators() {
|
||||
return optimisedCleanOperators;
|
||||
|
|
@ -179,10 +186,17 @@ public class CAEnchantmentRegistry {
|
|||
|
||||
/**
|
||||
* 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() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
|||
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
|
||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.SettingGui;
|
||||
|
|
@ -37,7 +38,7 @@ public abstract class AbstractEnchantConfigGui<T extends SettingGui.SettingGuiFa
|
|||
|
||||
@Override
|
||||
protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() {
|
||||
return GuiSharedConstant.SORTED_ENCHANTMENT_LIST;
|
||||
return CAEnchantmentRegistry.getInstance().getNameSortedEnchantments();
|
||||
}
|
||||
|
||||
@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
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -118,8 +118,8 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu
|
|||
this.pages.clear();
|
||||
this.pages.add(this.firstPage);
|
||||
|
||||
for (T conflict : getEveryDisplayableInstanceOfGeneric()) {
|
||||
updateValueForGeneric(conflict, false);
|
||||
for (T generic : getEveryDisplayableInstanceOfGeneric()) {
|
||||
updateValueForGeneric(generic, false);
|
||||
}
|
||||
|
||||
update();
|
||||
|
|
|
|||
|
|
@ -65,9 +65,9 @@ public class EnchantSelectSettingGui extends SettingGuiListConfigGui<CAEnchantme
|
|||
protected Collection<CAEnchantment> getEveryDisplayableInstanceOfGeneric() {
|
||||
Stream<CAEnchantment> toDisplayStream;
|
||||
if(this.displayUnselected){
|
||||
toDisplayStream = CAEnchantmentRegistry.getInstance().values().stream();
|
||||
toDisplayStream = CAEnchantmentRegistry.getInstance().getNameSortedEnchantments().stream();
|
||||
}else{
|
||||
toDisplayStream = this.selectedEnchant.stream();
|
||||
toDisplayStream = this.selectedEnchant.stream().sorted(Comparator.comparing(CAEnchantment::getName));
|
||||
}
|
||||
Set<CAEnchantment> illegalEnchantments = this.enchantContainer.illegalEnchantments();
|
||||
|
||||
|
|
|
|||
|
|
@ -7,20 +7,13 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
|||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
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 java.util.*;
|
||||
|
||||
public class GuiSharedConstant {
|
||||
|
||||
public static final List<CAEnchantment> SORTED_ENCHANTMENT_LIST;
|
||||
|
||||
static {
|
||||
SORTED_ENCHANTMENT_LIST = new ArrayList<>(CAEnchantmentRegistry.getInstance().values());
|
||||
SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey()));
|
||||
}
|
||||
private GuiSharedConstant(){}
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -51,8 +51,8 @@ class ReloadExecutor : CommandExecutor {
|
|||
// temporary: handle 1.21 update
|
||||
Update_1_21.handleUpdate()
|
||||
|
||||
// Register enchantment of compatible plugin and load configuration change.
|
||||
DependencyManager.handleCompatibilityConfig()
|
||||
// Handle dependency reload
|
||||
DependencyManager.handleConfigReload()
|
||||
|
||||
// Call event
|
||||
val configReadyEvent = CAConfigReadyEvent()
|
||||
|
|
|
|||
|
|
@ -44,4 +44,13 @@ object DependencyManager {
|
|||
|
||||
}
|
||||
|
||||
fun handleConfigReload(){
|
||||
// Register enchantment of compatible plugin and load configuration change.
|
||||
handleCompatibilityConfig()
|
||||
|
||||
// Then handle plugin reload
|
||||
ecoEnchantCompatibility?.handleConfigReload()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package xyz.alexcrea.cuanvil.dependency
|
||||
|
||||
import com.willfp.ecoenchants.enchant.EcoEnchant
|
||||
import com.willfp.ecoenchants.enchant.EcoEnchants
|
||||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
|
|
@ -17,15 +18,39 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
|
|||
PrepareAnvilEvent.getHandlerList().unregister(this.ecoEnchantPlugin)
|
||||
}
|
||||
|
||||
private var ecoEnchantOldEnchantments: MutableSet<EcoEnchant>? = null
|
||||
fun registerEnchantments() {
|
||||
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.registerEnchantment(CAEcoEnchant(ecoEnchant))
|
||||
}
|
||||
|
||||
ecoEnchantOldEnchantments = HashSet(enchantments)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue