diff --git a/plugin/src/main/java/net/momirealms/customfishing/libraries/dependencies/Dependency.java b/plugin/src/main/java/net/momirealms/customfishing/libraries/dependencies/Dependency.java index 0c20bf6c..d92226c1 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/libraries/dependencies/Dependency.java +++ b/plugin/src/main/java/net/momirealms/customfishing/libraries/dependencies/Dependency.java @@ -26,7 +26,6 @@ package net.momirealms.customfishing.libraries.dependencies; import com.google.common.collect.ImmutableList; -import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.libraries.dependencies.relocation.Relocation; import org.bukkit.Bukkit; import org.jetbrains.annotations.Nullable; diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java index 5ff3c80c..bfc61638 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/block/BlockManagerImpl.java @@ -61,6 +61,7 @@ public class BlockManagerImpl implements BlockManager, Listener { private final CustomFishingPlugin plugin; private final HashMap blockLibraryMap; + private BlockLibrary[] blockDetectionArray; private final HashMap blockConfigMap; private final HashMap dataBuilderMap; private final HashMap stateBuilderMap; @@ -75,6 +76,38 @@ public class BlockManagerImpl implements BlockManager, Listener { this.registerInbuiltProperties(); } + public void load() { + this.loadConfig(); + Bukkit.getPluginManager().registerEvents(this, plugin); + this.resetBlockDetectionOrder(); + } + + public void unload() { + HandlerList.unregisterAll(this); + HashMap tempMap = new HashMap<>(this.blockConfigMap); + this.blockConfigMap.clear(); + for (Map.Entry entry : tempMap.entrySet()) { + if (entry.getValue().isPersist()) { + tempMap.put(entry.getKey(), entry.getValue()); + } + } + } + + public void disable() { + this.blockLibraryMap.clear(); + } + + private void resetBlockDetectionOrder() { + ArrayList list = new ArrayList<>(); + for (String plugin : CFConfig.itemDetectOrder) { + BlockLibrary library = blockLibraryMap.get(plugin); + if (library != null) { + list.add(library); + } + } + this.blockDetectionArray = list.toArray(new BlockLibrary[0]); + } + /** * Event handler for the EntityChangeBlockEvent. * This method is triggered when an entity changes a block, typically when a block falls or lands. @@ -129,6 +162,7 @@ public class BlockManagerImpl implements BlockManager, Listener { public boolean registerBlockLibrary(BlockLibrary blockLibrary) { if (this.blockLibraryMap.containsKey(blockLibrary.identification())) return false; this.blockLibraryMap.put(blockLibrary.identification(), blockLibrary); + this.resetBlockDetectionOrder(); return true; } @@ -141,7 +175,10 @@ public class BlockManagerImpl implements BlockManager, Listener { */ @Override public boolean unregisterBlockLibrary(String identification) { - return blockLibraryMap.remove(identification) != null; + boolean success = blockLibraryMap.remove(identification) != null; + if (success) + this.resetBlockDetectionOrder(); + return success; } /** @@ -196,11 +233,6 @@ public class BlockManagerImpl implements BlockManager, Listener { return stateBuilderMap.remove(type) != null; } - public void load() { - this.loadConfig(); - Bukkit.getPluginManager().registerEvents(this, plugin); - } - private void registerInbuiltProperties() { this.registerDirectional(); this.registerStorage(); @@ -212,21 +244,6 @@ public class BlockManagerImpl implements BlockManager, Listener { this.registerAge(); } - public void unload() { - HandlerList.unregisterAll(this); - HashMap tempMap = new HashMap<>(this.blockConfigMap); - this.blockConfigMap.clear(); - for (Map.Entry entry : tempMap.entrySet()) { - if (entry.getValue().isPersist()) { - tempMap.put(entry.getKey(), entry.getValue()); - } - } - } - - public void disable() { - this.blockLibraryMap.clear(); - } - /** * Loads configuration files from the plugin's data folder and processes them. * Configuration files are organized by type (e.g., "block"). @@ -353,13 +370,10 @@ public class BlockManagerImpl implements BlockManager, Listener { @Override @NotNull public String getAnyPluginBlockID(Block block) { - for (String plugin : CFConfig.blockDetectOrder) { - BlockLibrary blockLibrary = blockLibraryMap.get(plugin); - if (blockLibrary != null) { - String id = blockLibrary.getBlockID(block); - if (id != null) { - return id; - } + for (BlockLibrary blockLibrary : blockDetectionArray) { + String id = blockLibrary.getBlockID(block); + if (id != null) { + return id; } } // Should not reach this because vanilla library would always work diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java index c6867a0d..3f06a6e0 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java @@ -85,14 +85,13 @@ public class ItemManagerImpl implements ItemManager, Listener { private final CustomFishingPlugin plugin; private final HashMap buildableItemMap; private final HashMap itemLibraryMap; - private final List itemDetectionList; + private ItemLibrary[] itemDetectionArray; public ItemManagerImpl(CustomFishingPlugin plugin) { instance = this; this.plugin = plugin; this.itemLibraryMap = new LinkedHashMap<>(); this.buildableItemMap = new HashMap<>(); - this.itemDetectionList = new ArrayList<>(); this.registerItemLibrary(new CustomFishingItemImpl()); this.registerItemLibrary(new VanillaItemImpl()); } @@ -100,19 +99,13 @@ public class ItemManagerImpl implements ItemManager, Listener { public void load() { this.loadItemsFromPluginFolder(); Bukkit.getPluginManager().registerEvents(this, plugin); - for (String plugin : CFConfig.itemDetectOrder) { - ItemLibrary library = itemLibraryMap.get(plugin); - if (library != null) { - itemDetectionList.add(library); - } - } + this.resetItemDetectionOrder(); } public void unload() { HandlerList.unregisterAll(this); HashMap tempMap = new HashMap<>(this.buildableItemMap); this.buildableItemMap.clear(); - this.itemDetectionList.clear(); for (Map.Entry entry : tempMap.entrySet()) { if (entry.getValue().persist()) { tempMap.put(entry.getKey(), entry.getValue()); @@ -120,6 +113,17 @@ public class ItemManagerImpl implements ItemManager, Listener { } } + private void resetItemDetectionOrder() { + ArrayList list = new ArrayList<>(); + for (String plugin : CFConfig.itemDetectOrder) { + ItemLibrary library = itemLibraryMap.get(plugin); + if (library != null) { + list.add(library); + } + } + this.itemDetectionArray = list.toArray(new ItemLibrary[0]); + } + public Collection getItemLibraries() { return itemLibraryMap.keySet(); } @@ -258,7 +262,7 @@ public class ItemManagerImpl implements ItemManager, Listener { public String getAnyPluginItemID(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) return "AIR"; - for (ItemLibrary library : itemDetectionList) { + for (ItemLibrary library : itemDetectionArray) { String id = library.getItemID(itemStack); if (id != null) { return id; @@ -419,6 +423,7 @@ public class ItemManagerImpl implements ItemManager, Listener { public boolean registerItemLibrary(ItemLibrary itemLibrary) { if (itemLibraryMap.containsKey(itemLibrary.identification())) return false; itemLibraryMap.put(itemLibrary.identification(), itemLibrary); + this.resetItemDetectionOrder(); return true; } @@ -430,7 +435,10 @@ public class ItemManagerImpl implements ItemManager, Listener { */ @Override public boolean unRegisterItemLibrary(String identification) { - return itemLibraryMap.remove(identification) != null; + boolean success = itemLibraryMap.remove(identification) != null; + if (success) + this.resetItemDetectionOrder(); + return success; } @Override