<commit_before>package com.kylantraynor.civilizations.hook.dynmap;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Level;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.AreaMarker;
import org.dynmap.markers.Marker;
import org.dynmap.markers.MarkerAPI;
import org.dynmap.markers.MarkerIcon;
import org.dynmap.markers.MarkerSet;

import com.kylantraynor.civilizations.Civilizations;
import com.kylantraynor.civilizations.Economy;
import com.kylantraynor.civilizations.groups.Group;
import com.kylantraynor.civilizations.groups.NationMember;
import com.kylantraynor.civilizations.groups.settlements.Camp;
import com.kylantraynor.civilizations.groups.settlements.Settlement;
import com.kylantraynor.civilizations.groups.settlements.forts.Fort;
import com.kylantraynor.civilizations.groups.settlements.plots.market.MarketStall;
import com.kylantraynor.civilizations.territories.InfluenceMap;
import com.kylantraynor.civilizations.territories.InfluentSite;
import com.kylantraynor.civilizations.territories.Region;
import com.kylantraynor.civilizations.util.Util;
import com.kylantraynor.voronoi.VCell;

public class DynmapHook {
	private static Plugin plugin;
	private static DynmapAPI api;
	private static MarkerAPI markerAPI;
	private static boolean reload;
	private static MarkerSet campMarkerSet;
	private static MarkerSet stallsMarkerSet;
	private static MarkerSet regionsMarkerSet;
	private static HashMap<String, Marker> markerList = new HashMap<String, Marker>();
	private static MarkerSet settlementsMarkerSet;
	/**
	 * Tries to load the dynmap plugin. Returns true if successfully loaded, returns false otherwise.
	 * @param manager
	 * @return boolean
	 */
	public static boolean load(PluginManager manager) {
		if((plugin = manager.getPlugin("dynmap")) != null){
			api = (DynmapAPI)plugin;
			return true;
		}
		return false;
	}
	
	/**
	 * Gets the state of the Dynmap hook. Returns true if the plugin is loaded and enabled, returns false otherwise.
	 * @return boolean
	 */
	public static boolean isEnabled(){
		if(plugin != null){
			return plugin.isEnabled();
		} else {
			return false;
		}
	}
	
	/**
	 * Activates the Dynmap for Civilizations
	 */
	public static void activateDynmap() {
		try{
			markerAPI = api.getMarkerAPI();
			if (markerAPI == null) {
				Civilizations.log("SEVERE", "Error loading dynmap marker API!");
				return;
		    }
			if (reload){
				Civilizations.currentInstance.reloadConfig();
				if (campMarkerSet != null){
					campMarkerSet.deleteMarkerSet();
					campMarkerSet = null;
				}
				if(settlementsMarkerSet != null){
					settlementsMarkerSet.deleteMarkerSet();
					settlementsMarkerSet = null;
				}
				if (stallsMarkerSet != null){
					stallsMarkerSet.deleteMarkerSet();
					stallsMarkerSet = null;
				}
				if(regionsMarkerSet != null){
					regionsMarkerSet.deleteMarkerSet();
					regionsMarkerSet = null;
				}
		    } else {
		    	reload = true;
		    }
			loadCampMarkerSet();
			loadStallsMarkerSet();
			loadRegionsMarkerSet();
			loadSettlementsMarkerSet();
		} catch (Exception e) {
			Civilizations.log("SEVERE", "Something went wrong activating Dynmap for Civilizations. Is it up to date?");
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Loads the marker set for Regions.
	 */
	private static void loadRegionsMarkerSet() {
		if(!isEnabled()) return;
		regionsMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.regions");
		if(regionsMarkerSet == null)
			regionsMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.regions", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Regions.Name", "Regions"), null, false);
		else
			regionsMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Regions.Name", "Regions"));
		if(regionsMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating Regions MarkerSet.");
			return;
		}
		int minZoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Regions.MinZoom", 0);
		if(minZoom > 0){
			regionsMarkerSet.setMinZoom(minZoom);
		}
		regionsMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Regions.LayerPrio", 10));
		regionsMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Regions.HideByDefault", false));
	}
	
	/**
	 * Loads the marker set for Settlements.
	 */
	private static void loadSettlementsMarkerSet() {
		if(!isEnabled()) return;
		settlementsMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.settlements");
		if(settlementsMarkerSet == null)
			settlementsMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.settlements", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Settlements.Name", "Settlements"), null, false);
		else
			settlementsMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Settlements.Name", "Settlements"));
		if(settlementsMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating Settlements MarkerSet.");
			return;
		}
		int minZoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Settlements.MinZoom", 0);
		if(minZoom > 0){
			settlementsMarkerSet.setMinZoom(minZoom);
		}
		settlementsMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Settlements.LayerPrio", 20));
		settlementsMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Settlements.HideByDefault", false));
	}

	/**
	 * Loads the marker set for Camps
	 */
	private static void loadCampMarkerSet() {
		if(!isEnabled()) return;
		campMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.camps");
		if (campMarkerSet == null) {
			campMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.camps", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Camp.Name", "Camps"), null, false);
		} else {
			campMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Camp.Name", "Camps"));
		}
		if (campMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating marker set");
			return;
		}
		int minzoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Camp.MinZoom", 5);
		if (minzoom > 0) {
		   campMarkerSet.setMinZoom(minzoom);
		}
		campMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Camp.LayerPrio", 50));
	    campMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Camp.HideByDefault", false));
	}
	
	/**
	 * Loads the marker set for Stalls
	 */
	private static void loadStallsMarkerSet() {
		if(!isEnabled()) return;
		stallsMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.stalls");
		if (stallsMarkerSet == null) {
			stallsMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.stalls", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Name", "Market Stalls"), null, false);
		} else {
			stallsMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Stalls.Name", "Market Stalls"));
		}
		if (stallsMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating marker set");
			return;
		}
		int minzoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Stalls.MinZoom", 5);
		if (minzoom > 0) {
		   stallsMarkerSet.setMinZoom(minzoom);
		}
		stallsMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Stalls.LayerPrio", 50));
	    stallsMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Stalls.HideByDefault", false));
	}
	
	/**
	 * Update a settlement on the dynmap.
	 * @param settlement to update.<commit_msg>Switched priorities around on dynmap.<commit_after>package com.kylantraynor.civilizations.hook.dynmap;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Level;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.AreaMarker;
import org.dynmap.markers.Marker;
import org.dynmap.markers.MarkerAPI;
import org.dynmap.markers.MarkerIcon;
import org.dynmap.markers.MarkerSet;

import com.kylantraynor.civilizations.Civilizations;
import com.kylantraynor.civilizations.Economy;
import com.kylantraynor.civilizations.groups.Group;
import com.kylantraynor.civilizations.groups.NationMember;
import com.kylantraynor.civilizations.groups.settlements.Camp;
import com.kylantraynor.civilizations.groups.settlements.Settlement;
import com.kylantraynor.civilizations.groups.settlements.forts.Fort;
import com.kylantraynor.civilizations.groups.settlements.plots.market.MarketStall;
import com.kylantraynor.civilizations.territories.InfluenceMap;
import com.kylantraynor.civilizations.territories.InfluentSite;
import com.kylantraynor.civilizations.territories.Region;
import com.kylantraynor.civilizations.util.Util;
import com.kylantraynor.voronoi.VCell;

public class DynmapHook {
	private static Plugin plugin;
	private static DynmapAPI api;
	private static MarkerAPI markerAPI;
	private static boolean reload;
	private static MarkerSet campMarkerSet;
	private static MarkerSet stallsMarkerSet;
	private static MarkerSet regionsMarkerSet;
	private static HashMap<String, Marker> markerList = new HashMap<String, Marker>();
	private static MarkerSet settlementsMarkerSet;
	/**
	 * Tries to load the dynmap plugin. Returns true if successfully loaded, returns false otherwise.
	 * @param manager
	 * @return boolean
	 */
	public static boolean load(PluginManager manager) {
		if((plugin = manager.getPlugin("dynmap")) != null){
			api = (DynmapAPI)plugin;
			return true;
		}
		return false;
	}
	
	/**
	 * Gets the state of the Dynmap hook. Returns true if the plugin is loaded and enabled, returns false otherwise.
	 * @return boolean
	 */
	public static boolean isEnabled(){
		if(plugin != null){
			return plugin.isEnabled();
		} else {
			return false;
		}
	}
	
	/**
	 * Activates the Dynmap for Civilizations
	 */
	public static void activateDynmap() {
		try{
			markerAPI = api.getMarkerAPI();
			if (markerAPI == null) {
				Civilizations.log("SEVERE", "Error loading dynmap marker API!");
				return;
		    }
			if (reload){
				Civilizations.currentInstance.reloadConfig();
				if (campMarkerSet != null){
					campMarkerSet.deleteMarkerSet();
					campMarkerSet = null;
				}
				if(settlementsMarkerSet != null){
					settlementsMarkerSet.deleteMarkerSet();
					settlementsMarkerSet = null;
				}
				if (stallsMarkerSet != null){
					stallsMarkerSet.deleteMarkerSet();
					stallsMarkerSet = null;
				}
				if(regionsMarkerSet != null){
					regionsMarkerSet.deleteMarkerSet();
					regionsMarkerSet = null;
				}
		    } else {
		    	reload = true;
		    }
			loadCampMarkerSet();
			loadStallsMarkerSet();
			loadRegionsMarkerSet();
			loadSettlementsMarkerSet();
		} catch (Exception e) {
			Civilizations.log("SEVERE", "Something went wrong activating Dynmap for Civilizations. Is it up to date?");
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Loads the marker set for Regions.
	 */
	private static void loadRegionsMarkerSet() {
		if(!isEnabled()) return;
		regionsMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.regions");
		if(regionsMarkerSet == null)
			regionsMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.regions", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Regions.Name", "Regions"), null, false);
		else
			regionsMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Regions.Name", "Regions"));
		if(regionsMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating Regions MarkerSet.");
			return;
		}
		int minZoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Regions.MinZoom", 0);
		if(minZoom > 0){
			regionsMarkerSet.setMinZoom(minZoom);
		}
		regionsMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Regions.LayerPrio", 50));
		regionsMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Regions.HideByDefault", false));
	}
	
	/**
	 * Loads the marker set for Settlements.
	 */
	private static void loadSettlementsMarkerSet() {
		if(!isEnabled()) return;
		settlementsMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.settlements");
		if(settlementsMarkerSet == null)
			settlementsMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.settlements", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Settlements.Name", "Settlements"), null, false);
		else
			settlementsMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Settlements.Name", "Settlements"));
		if(settlementsMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating Settlements MarkerSet.");
			return;
		}
		int minZoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Settlements.MinZoom", 0);
		if(minZoom > 0){
			settlementsMarkerSet.setMinZoom(minZoom);
		}
		settlementsMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Settlements.LayerPrio", 9));
		settlementsMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Settlements.HideByDefault", false));
	}

	/**
	 * Loads the marker set for Camps
	 */
	private static void loadCampMarkerSet() {
		if(!isEnabled()) return;
		campMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.camps");
		if (campMarkerSet == null) {
			campMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.camps", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Camp.Name", "Camps"), null, false);
		} else {
			campMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Camp.Name", "Camps"));
		}
		if (campMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating marker set");
			return;
		}
		int minzoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Camp.MinZoom", 5);
		if (minzoom > 0) {
		   campMarkerSet.setMinZoom(minzoom);
		}
		campMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Camp.LayerPrio", 8));
	    campMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Camp.HideByDefault", false));
	}
	
	/**
	 * Loads the marker set for Stalls
	 */
	private static void loadStallsMarkerSet() {
		if(!isEnabled()) return;
		stallsMarkerSet = markerAPI.getMarkerSet("civilizations.markerset.stalls");
		if (stallsMarkerSet == null) {
			stallsMarkerSet = markerAPI.createMarkerSet("civilizations.markerset.stalls", Civilizations.getInstanceConfig().getString("Dynmap.Layer.Name", "Market Stalls"), null, false);
		} else {
			stallsMarkerSet.setMarkerSetLabel(Civilizations.getInstanceConfig().getString("Dynmap.Layer.Stalls.Name", "Market Stalls"));
		}
		if (stallsMarkerSet == null){
			Civilizations.log("SEVERE", "Error creating marker set");
			return;
		}
		int minzoom = Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Stalls.MinZoom", 5);
		if (minzoom > 0) {
		   stallsMarkerSet.setMinZoom(minzoom);
		}
		stallsMarkerSet.setLayerPriority(Civilizations.getInstanceConfig().getInt("Dynmap.Layer.Stalls.LayerPrio", 1));
	    stallsMarkerSet.setHideByDefault(Civilizations.getInstanceConfig().getBoolean("Dynmap.Layer.Stalls.HideByDefault", false));
	}
	
	/**
	 * Update a settlement on the dynmap.
	 * @param settlement to update.
{'fraction_non_alphanumeric': 0.11967286435371542, 'fraction_numerical': 0.0018529167465337678, 'mean_word_length': 5.296057924376508, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '144033', 'n_tokens_mistral': 5443, 'n_tokens_neox': 4749, 'n_words': 863}