Added Manager for storing Data related to id and location

master
Ruakij 4 years ago
parent 16a46a6a35
commit 48f961fc9c

@ -1,5 +1,6 @@
package eu.ruekov.ruakij.LinkedBeaconTeleporters; 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.OnBlockBreak;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockPlace; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockPlace;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnPrepareItemCraftEvent; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnPrepareItemCraftEvent;
@ -22,6 +23,8 @@ public class Main extends JavaPlugin {
public static Logger log; public static Logger log;
public static LinkedBeaconTeleporterManager lbtManager;
public void onEnable() { public void onEnable() {
log = getLogger(); log = getLogger();
@ -34,6 +37,8 @@ public class Main extends JavaPlugin {
loadConfigs(); loadConfigs();
lbtManager = new LinkedBeaconTeleporterManager();
log.info("Plugin activated"); log.info("Plugin activated");
} }

@ -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<String, List<LinkedBeaconTeleporterBlock>> linkedBeaconTeleporterById = new HashMap();
HashMap<String, LinkedBeaconTeleporterBlock> 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<LinkedBeaconTeleporterBlock> 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!");
}
}
Loading…
Cancel
Save