Use the new Region event to create dynmap markers
This commit is contained in:
		@@ -24,6 +24,8 @@ import org.bukkit.plugin.PluginManager;
 | 
				
			|||||||
import org.bukkit.World;
 | 
					import org.bukkit.World;
 | 
				
			||||||
import org.bukkit.command.CommandExecutor;
 | 
					import org.bukkit.command.CommandExecutor;
 | 
				
			||||||
import org.bukkit.configuration.ConfigurationSection;
 | 
					import org.bukkit.configuration.ConfigurationSection;
 | 
				
			||||||
 | 
					import org.dynmap.markers.MarkerAPI;
 | 
				
			||||||
 | 
					import org.dynmap.DynmapCommonAPI;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.logging.Logger;
 | 
					import java.util.logging.Logger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -40,8 +42,7 @@ public class Plugin extends JavaPlugin {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public void onEnable() {
 | 
					    public void onEnable() {
 | 
				
			||||||
        log.info("[Regions] Enabling Regions");
 | 
					        log.info("[Regions] Enabling Regions");
 | 
				
			||||||
        m_regions = new RegionManager();
 | 
					        m_regions = new RegionManager(getServer().getPluginManager());
 | 
				
			||||||
        loadRegions();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        m_playerWatcher = new PlayerWatcher(this);
 | 
					        m_playerWatcher = new PlayerWatcher(this);
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
@@ -49,6 +50,18 @@ public class Plugin extends JavaPlugin {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        CommandExecutor regionCommand = new RegionCommand(this);
 | 
					        CommandExecutor regionCommand = new RegionCommand(this);
 | 
				
			||||||
        getCommand("region").setExecutor(regionCommand);
 | 
					        getCommand("region").setExecutor(regionCommand);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        org.bukkit.plugin.Plugin mapPlugin = getServer().getPluginManager().getPlugin("dynmap");
 | 
				
			||||||
 | 
					        if (mapPlugin instanceof DynmapCommonAPI) {
 | 
				
			||||||
 | 
					            DynmapCommonAPI mapAPI = (DynmapCommonAPI)mapPlugin;
 | 
				
			||||||
 | 
					            MarkerAPI markerAPI = mapAPI.getMarkerAPI();
 | 
				
			||||||
 | 
					            RegionEventHandler regionHandler = new RegionEventHandler(markerAPI);
 | 
				
			||||||
 | 
					            getServer().getPluginManager().registerEvents(regionHandler, this);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            log.info("[Regions] Dynmap not found. Disabling map support.");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        loadRegions();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void loadTestRegions() {
 | 
					    private void loadTestRegions() {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										49
									
								
								src/main/java/us/camin/regions/RegionEventHandler.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/main/java/us/camin/regions/RegionEventHandler.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
				
			|||||||
 | 
					package us.camin.regions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * This file is part of Regions
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Regions is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					 * the Free Software Foundation, either version 3 of the License, or
 | 
				
			||||||
 | 
					 * (at your option) any later version.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Regions is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					 * GNU General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					 * along with Regions.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.bukkit.event.Listener;
 | 
				
			||||||
 | 
					import org.dynmap.markers.MarkerSet;
 | 
				
			||||||
 | 
					import org.dynmap.markers.MarkerIcon;
 | 
				
			||||||
 | 
					import org.dynmap.markers.MarkerAPI;
 | 
				
			||||||
 | 
					import org.dynmap.markers.Marker;
 | 
				
			||||||
 | 
					import org.bukkit.event.EventHandler;
 | 
				
			||||||
 | 
					import org.bukkit.Location;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class RegionEventHandler implements Listener {
 | 
				
			||||||
 | 
					    private MarkerSet m_set;
 | 
				
			||||||
 | 
					    private MarkerAPI m_api;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public RegionEventHandler(MarkerAPI markerAPI) {
 | 
				
			||||||
 | 
					        m_api = markerAPI;
 | 
				
			||||||
 | 
					        m_set = m_api.createMarkerSet("Regions", "Regions", null, false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @EventHandler
 | 
				
			||||||
 | 
					    public void onRegionEvent(RegionEvent event) {
 | 
				
			||||||
 | 
					        if (event.type == RegionEvent.EventType.Added) {
 | 
				
			||||||
 | 
					            Location loc = event.region.location();
 | 
				
			||||||
 | 
					            MarkerIcon icon = m_api.getMarkerIcon("default");
 | 
				
			||||||
 | 
					            m_set.createMarker(null, event.region.name(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), icon, false);
 | 
				
			||||||
 | 
					        } else if (event.type == RegionEvent.EventType.Removed) {
 | 
				
			||||||
 | 
					            Marker marker = m_set.findMarkerByLabel(event.region.name());
 | 
				
			||||||
 | 
					            marker.deleteMarker();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -24,6 +24,7 @@ import org.bukkit.configuration.ConfigurationSection;
 | 
				
			|||||||
import org.bukkit.Bukkit;
 | 
					import org.bukkit.Bukkit;
 | 
				
			||||||
import org.bukkit.Server;
 | 
					import org.bukkit.Server;
 | 
				
			||||||
import org.bukkit.entity.Player;
 | 
					import org.bukkit.entity.Player;
 | 
				
			||||||
 | 
					import org.bukkit.plugin.PluginManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.logging.Logger;
 | 
					import java.util.logging.Logger;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
@@ -37,8 +38,10 @@ import java.util.Set;
 | 
				
			|||||||
public class RegionManager {
 | 
					public class RegionManager {
 | 
				
			||||||
    Logger log = Logger.getLogger("Regions.RegionManager");
 | 
					    Logger log = Logger.getLogger("Regions.RegionManager");
 | 
				
			||||||
    private Map<String, Collection<Region>> m_regions;
 | 
					    private Map<String, Collection<Region>> m_regions;
 | 
				
			||||||
 | 
					    private PluginManager m_pm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public RegionManager() {
 | 
					    public RegionManager(PluginManager pm) {
 | 
				
			||||||
 | 
					        m_pm = pm;
 | 
				
			||||||
        m_regions = new HashMap<String, Collection<Region>>();
 | 
					        m_regions = new HashMap<String, Collection<Region>>();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -56,14 +59,20 @@ public class RegionManager {
 | 
				
			|||||||
        log.fine("Adding new region "+r.name()+" at "+r.location());
 | 
					        log.fine("Adding new region "+r.name()+" at "+r.location());
 | 
				
			||||||
        if (!m_regions.containsKey(worldName))
 | 
					        if (!m_regions.containsKey(worldName))
 | 
				
			||||||
            m_regions.put(worldName, new ArrayList<Region>());
 | 
					            m_regions.put(worldName, new ArrayList<Region>());
 | 
				
			||||||
        return m_regions.get(worldName).add(r);
 | 
					        if (m_regions.get(worldName).add(r)) {
 | 
				
			||||||
 | 
					            m_pm.callEvent(new RegionEvent(r, RegionEvent.EventType.Added));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public boolean removeRegion(Region r) {
 | 
					    public boolean removeRegion(Region r) {
 | 
				
			||||||
        String worldName = r.location().getWorld().getName();
 | 
					        String worldName = r.location().getWorld().getName();
 | 
				
			||||||
        log.fine("Removing region "+r.name()+" from "+r.location());
 | 
					        log.fine("Removing region "+r.name()+" from "+r.location());
 | 
				
			||||||
        if (m_regions.containsKey(worldName)) {
 | 
					        if (m_regions.containsKey(worldName)) {
 | 
				
			||||||
            return m_regions.get(worldName).remove(r);
 | 
					            if (m_regions.get(worldName).remove(r)) {
 | 
				
			||||||
 | 
					                m_pm.callEvent(new RegionEvent(r, RegionEvent.EventType.Removed));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user