diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java index 35f0ebe..0c6b8d7 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java @@ -1,5 +1,6 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterManager; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockBreak; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockPlace; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnPrepareItemCraftEvent; @@ -22,6 +23,8 @@ public class Main extends JavaPlugin { public static Logger log; + public static LinkedBeaconTeleporterManager lbtManager; + public void onEnable() { log = getLogger(); @@ -34,6 +37,8 @@ public class Main extends JavaPlugin { loadConfigs(); + lbtManager = new LinkedBeaconTeleporterManager(); + log.info("Plugin activated"); } diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java new file mode 100644 index 0000000..3f31a55 --- /dev/null +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java @@ -0,0 +1,69 @@ +package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter; + +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class LinkedBeaconTeleporterManager { + // Stores all placed Beacon-Teleporters for fast access + // TODO: Evaluate necessity of 2 HashMaps with main reason of performance + HashMap> linkedBeaconTeleporterById = new HashMap(); + HashMap linkedBeaconTeleporterByLoc = new HashMap(); + + public LinkedBeaconTeleporterManager(){ + Main.log.info("Loading LinkedBeaconTeleporter from config.."); + + // Load teleporters + for(String teleporterId : Main.data.getKeys(false)){ + ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId); + + // New list for this id + ArrayList lbtBlocks = new ArrayList(); + + for(String blockId : teleportersData.getKeys(false)) { + ConfigurationSection teleporterData = teleportersData.getConfigurationSection(blockId); + + // Load location + String serializedLoc = teleporterData.getString("loc"); + Location loc; + try { + loc = Function.deserializeBlockLocation(teleporterData.getString("loc")); + } catch (InvalidPropertiesFormatException e) { + Main.log.severe("Could not load location='"+ serializedLoc +"' from "+ teleporterId +" > "+ blockId); + continue; + } + + // Check if block is of type Beacon (in case the world was changed without the plugin running) + Block block = loc.getBlock(); + if(block.getType() != Material.BEACON){ + // Data out of sync! Skipping + Main.log.warning(teleporterId +" > "+ blockId +" at location "+ serializedLoc +" changed and is no longer LinkedBlockTeleporter! (Changed without the plugin running?)"); + continue; + } + + // Construct block + LinkedBeaconTeleporterBlock lbtBlock = new LinkedBeaconTeleporterBlock(teleporterId, block); + + // Add block to id-list + lbtBlocks.add(lbtBlock); + + // Save to loc-list + linkedBeaconTeleporterByLoc.put(serializedLoc, lbtBlock); + } + + // Save list to id-list + linkedBeaconTeleporterById.put(teleporterId, lbtBlocks); + } + + Main.log.info("All done!"); + } +}