mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2026-01-03 22:26:15 +00:00
further improve
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -61,6 +61,7 @@ public class BlockManagerImpl implements BlockManager, Listener {
|
||||
|
||||
private final CustomFishingPlugin plugin;
|
||||
private final HashMap<String, BlockLibrary> blockLibraryMap;
|
||||
private BlockLibrary[] blockDetectionArray;
|
||||
private final HashMap<String, BlockConfig> blockConfigMap;
|
||||
private final HashMap<String, BlockDataModifierBuilder> dataBuilderMap;
|
||||
private final HashMap<String, BlockStateModifierBuilder> 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<String, BlockConfig> tempMap = new HashMap<>(this.blockConfigMap);
|
||||
this.blockConfigMap.clear();
|
||||
for (Map.Entry<String, BlockConfig> entry : tempMap.entrySet()) {
|
||||
if (entry.getValue().isPersist()) {
|
||||
tempMap.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
this.blockLibraryMap.clear();
|
||||
}
|
||||
|
||||
private void resetBlockDetectionOrder() {
|
||||
ArrayList<BlockLibrary> 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<String, BlockConfig> tempMap = new HashMap<>(this.blockConfigMap);
|
||||
this.blockConfigMap.clear();
|
||||
for (Map.Entry<String, BlockConfig> 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
|
||||
|
||||
@@ -85,14 +85,13 @@ public class ItemManagerImpl implements ItemManager, Listener {
|
||||
private final CustomFishingPlugin plugin;
|
||||
private final HashMap<Key, BuildableItem> buildableItemMap;
|
||||
private final HashMap<String, ItemLibrary> itemLibraryMap;
|
||||
private final List<ItemLibrary> 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<Key, BuildableItem> tempMap = new HashMap<>(this.buildableItemMap);
|
||||
this.buildableItemMap.clear();
|
||||
this.itemDetectionList.clear();
|
||||
for (Map.Entry<Key, BuildableItem> 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<ItemLibrary> 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<String> 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
|
||||
|
||||
Reference in New Issue
Block a user