Refactor the RegionEvent into two subclasses for future operations

This commit is contained in:
Torrie Fischer 2012-04-25 16:22:26 -04:00
parent 830fbd31f2
commit 21af2c946a
5 changed files with 91 additions and 20 deletions

View File

@ -0,0 +1,38 @@
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.Event;
import org.bukkit.event.HandlerList;
public class RegionCreateEvent extends RegionEvent {
public static final HandlerList s_handlers = new HandlerList();
public RegionCreateEvent(Region region) {
super(region);
}
public HandlerList getHandlers() {
return s_handlers;
}
public static HandlerList getHandlerList() {
return s_handlers;
}
}

View File

@ -21,19 +21,12 @@ package us.camin.regions;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
public class RegionEvent extends Event { public abstract class RegionEvent extends Event {
private static final HandlerList s_handlers = new HandlerList(); private static final HandlerList s_handlers = new HandlerList();
public final EventType type;
public final Region region; public final Region region;
public enum EventType { public RegionEvent(Region region) {
Added,
Removed
}
public RegionEvent(Region region, EventType type) {
this.region = region; this.region = region;
this.type = type;
} }
@Override @Override

View File

@ -36,14 +36,15 @@ public class RegionEventHandler implements Listener {
} }
@EventHandler @EventHandler
public void onRegionEvent(RegionEvent event) { public void onRegionEvent(RegionRemoveEvent 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 marker = m_set.findMarkerByLabel(event.region.name());
marker.deleteMarker(); marker.deleteMarker();
} }
@EventHandler
public void onRegionEvent(RegionCreateEvent event) {
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);
} }
} }

View File

@ -73,7 +73,7 @@ public class RegionManager {
if (!m_regions.containsKey(worldName)) if (!m_regions.containsKey(worldName))
m_regions.put(worldName, new ArrayList<Region>()); m_regions.put(worldName, new ArrayList<Region>());
if (m_regions.get(worldName).add(r)) { if (m_regions.get(worldName).add(r)) {
m_pm.callEvent(new RegionEvent(r, RegionEvent.EventType.Added)); m_pm.callEvent(new RegionCreateEvent(r));
} }
return false; return false;
} }
@ -83,7 +83,7 @@ public class RegionManager {
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)) {
if (m_regions.get(worldName).remove(r)) { if (m_regions.get(worldName).remove(r)) {
m_pm.callEvent(new RegionEvent(r, RegionEvent.EventType.Removed)); m_pm.callEvent(new RegionRemoveEvent(r));
} }
return true; return true;
} }

View File

@ -0,0 +1,39 @@
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.Event;
import org.bukkit.event.HandlerList;
public class RegionRemoveEvent extends RegionEvent {
public static final HandlerList s_handlers = new HandlerList();
public RegionRemoveEvent(Region region) {
super(region);
}
public HandlerList getHandlers() {
return s_handlers;
}
public static HandlerList getHandlerList() {
return s_handlers;
}
}